@markuplint/types 4.0.0-dev.28 → 4.0.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017-2019 Yusuke Hirao
3
+ Copyright (c) 2017-2024 Yusuke Hirao
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -1,2 +1,2 @@
1
1
  import type { CustomSyntaxCheck, UnmatchedResult } from './types.js';
2
- export declare function checkMultiTypes(value: string, checks: readonly CustomSyntaxCheck[]): import("./types.js").MatchedResult | UnmatchedResult;
2
+ export declare function checkMultiTypes(value: string, checks: readonly CustomSyntaxCheck[]): UnmatchedResult | import("./types.js").MatchedResult;
package/lib/css-syntax.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { fork } from 'css-tree';
2
2
  import { log } from './debug.js';
3
- import { tokenizers } from './defs.js';
3
+ import { tokenizers, overrides } from './defs.js';
4
4
  import { matched } from './match-result.js';
5
5
  const MIMIC_TAG_L = 'mimiccases---';
6
6
  const MIMIC_TAG_R = '---mimiccases';
@@ -52,7 +52,10 @@ export function cssSyntaxMatch(value, type) {
52
52
  }
53
53
  // @ts-ignore
54
54
  const lexer = fork({
55
- types: typesExtended,
55
+ types: {
56
+ ...overrides,
57
+ ...typesExtended,
58
+ },
56
59
  properties: propsExtended,
57
60
  }).lexer;
58
61
  for (const key of Object.keys(typesCheckers)) {
package/lib/defs.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import type { Defs, CssSyntaxTokenizer } from './types.js';
2
2
  export declare const types: Defs;
3
+ export declare const overrides: Record<string, string>;
3
4
  export declare const tokenizers: Record<string, CssSyntaxTokenizer>;
package/lib/defs.js CHANGED
@@ -88,12 +88,14 @@ export const types = {
88
88
  },
89
89
  ],
90
90
  is: value => {
91
- // NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
92
- const nameStartChar = /[:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u{10000}-\u{EFFFF}]/u;
93
- // NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
94
- const nameCharTail = /-|[\d.\u00B7]|[\u0300-\u036F\u203F\u2040]/;
95
- // Name ::= NameStartChar (NameChar)*
96
- const name = new RegExp(`(?:${nameStartChar.source})(?:${nameCharTail})*`, 'u');
91
+ /**
92
+ * NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
93
+ * NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
94
+ * Name ::= NameStartChar (NameChar)*
95
+ */
96
+ const name =
97
+ // eslint-disable-next-line no-misleading-character-class
98
+ /[:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u{10000}-\u{EFFFF}][\d.\u00B7\u0300-\u036F\u203F\u2040-]*/u;
97
99
  return name.test(value) ? matched() : unmatched(value, 'unexpected-token');
98
100
  },
99
101
  },
@@ -403,51 +405,81 @@ export const types = {
403
405
  },
404
406
  Srcset: {
405
407
  ref: 'https://html.spec.whatwg.org/multipage/images.html#srcset-attributes',
406
- syntax: {
407
- apply: '<srcset>',
408
- def: {
409
- srcset: '<image-candidate-strings> [, <image-candidate-strings>]*',
410
- 'image-candidate-strings': '<valid-non-empty-url> [ <width-descriptor> | <pixel-density-descriptor> ]?',
411
- 'valid-non-empty-url'(token, getNextToken) {
412
- if (!token) {
413
- return 0;
414
- }
415
- let willAdoptTokenLength = 0;
416
- do {
417
- if (token.type === 13) {
408
+ is(value) {
409
+ const images = value.split(',');
410
+ for (const image of images) {
411
+ // image candidate string
412
+ const [url, , descriptor, ...tail] = new TokenCollection(image.trim(), {
413
+ disallowToSurroundBySpaces: true,
414
+ separator: 'space',
415
+ });
416
+ if (!url) {
417
+ return unmatched(value, 'unexpected-token', {
418
+ expects: [
419
+ {
420
+ type: 'format',
421
+ value: 'valid non-empty URL',
422
+ },
423
+ ],
424
+ });
425
+ }
426
+ if (descriptor) {
427
+ const { num, unit } = splitUnit(descriptor.value);
428
+ switch (unit) {
429
+ case 'w': {
430
+ if (!isUint(num)) {
431
+ return unmatched(value, 'unexpected-token', {
432
+ expects: [
433
+ {
434
+ type: 'format',
435
+ value: 'width descriptor',
436
+ },
437
+ ],
438
+ });
439
+ }
418
440
  break;
419
441
  }
420
- willAdoptTokenLength++;
421
- } while ((token = getNextToken(willAdoptTokenLength)));
422
- return willAdoptTokenLength;
423
- },
424
- 'width-descriptor'(token) {
425
- if (!token) {
426
- return 0;
427
- }
428
- const { num, unit } = splitUnit(token.value);
429
- if (unit !== 'w') {
430
- return 0;
431
- }
432
- if (!isUint(num)) {
433
- return 0;
434
- }
435
- return 1;
436
- },
437
- 'pixel-density-descriptor'(token) {
438
- if (!token) {
439
- return 0;
440
- }
441
- const { num, unit } = splitUnit(token.value);
442
- if (unit !== 'x') {
443
- return 0;
444
- }
445
- if (!isFloat(num)) {
446
- return 0;
442
+ case 'x': {
443
+ if (!isFloat(num)) {
444
+ return unmatched(value, 'unexpected-token', {
445
+ expects: [
446
+ {
447
+ type: 'format',
448
+ value: 'pixel density descriptor',
449
+ },
450
+ ],
451
+ });
452
+ }
453
+ break;
454
+ }
455
+ default: {
456
+ return unmatched(value, 'unexpected-token', {
457
+ expects: [
458
+ {
459
+ type: 'format',
460
+ value: 'width descriptor',
461
+ },
462
+ {
463
+ type: 'format',
464
+ value: 'pixel density descriptor',
465
+ },
466
+ ],
467
+ });
468
+ }
447
469
  }
448
- return 1;
449
- },
450
- },
470
+ }
471
+ if (tail[0]) {
472
+ return unmatched(value, 'unexpected-token', {
473
+ expects: [
474
+ {
475
+ type: 'syntax',
476
+ value: 'image candidate string',
477
+ },
478
+ ],
479
+ });
480
+ }
481
+ }
482
+ return matched();
451
483
  },
452
484
  },
453
485
  SourceSizeList: {
@@ -750,7 +782,7 @@ export const types = {
750
782
  syntax: {
751
783
  apply: '<view-box>',
752
784
  def: {
753
- 'view-box': '<min-x> [,]? <min-y> [,]? <width> [,]? <height>',
785
+ 'view-box': '<min-x> [,]? <min-y> [,]? <width> [,]? <height>', // As '[<min-x>,? <min-y>,? <width>,? <height>]',
754
786
  'min-x': '<number>',
755
787
  'min-y': '<number>',
756
788
  width: '<number>',
@@ -813,6 +845,27 @@ export const types = {
813
845
  },
814
846
  },
815
847
  };
848
+ export const overrides = {
849
+ // Alias
850
+ 'legacy-length-percentage': '<length> | <percentage> | <svg-length>',
851
+ 'legacy-angle': '<angle> | <zero> | <number>',
852
+ /**
853
+ * @see https://www.w3.org/TR/css-transforms-1/#funcdef-transform-translate
854
+ */
855
+ 'translate()': 'translate( <legacy-length-percentage> , <legacy-length-percentage>? ) | translate( <legacy-length-percentage> <legacy-length-percentage>? )',
856
+ /**
857
+ * @see https://www.w3.org/TR/css-transforms-1/#funcdef-transform-scale
858
+ */
859
+ 'scale()': 'scale( [ <number> | <percentage> ]#{1,2} )',
860
+ /**
861
+ * @see https://www.w3.org/TR/css-transforms-1/#funcdef-transform-rotate
862
+ */
863
+ 'rotate()': 'rotate( <legacy-angle> )',
864
+ /**
865
+ * @see https://www.w3.org/TR/css-transforms-1/#funcdef-transform-skew
866
+ */
867
+ 'skew()': 'skew( <legacy-angle> , <legacy-angle>? ) | skew( <legacy-angle> <legacy-angle>? )',
868
+ };
816
869
  export const tokenizers = {
817
870
  // RFC
818
871
  // https://tools.ietf.org/rfc/bcp/bcp47.html
@@ -2,7 +2,7 @@ import type { FormattedPrimitiveTypeCheck, MatchedResult, UnmatchedResult, Unmat
2
2
  export declare function matches(checker: FormattedPrimitiveTypeCheck, options?: UnmatchedResultOptions & {
3
3
  readonly ref?: string;
4
4
  readonly reason?: UnmatchedResultReason;
5
- }): (value: string) => MatchedResult | UnmatchedResult;
5
+ }): (value: string) => UnmatchedResult | MatchedResult;
6
6
  export declare function matched(): MatchedResult;
7
7
  export declare function unmatched(value: string, reason?: UnmatchedResultReason, options?: UnmatchedResultOptions & {
8
8
  readonly ref?: string;
@@ -24,7 +24,7 @@ export declare class TokenCollection extends Array<Token> {
24
24
  expects?: Expect[];
25
25
  ref?: string;
26
26
  cache?: boolean;
27
- }): import("../types.js").MatchedResult | UnmatchedResult;
27
+ }): UnmatchedResult | import("../types.js").MatchedResult;
28
28
  chunk(split: number): TokenCollection[];
29
29
  compareTokens(callback: (prev: Readonly<Token>, current: Readonly<Token>) => Readonly<Token> | null | void): Readonly<Token> | null | undefined;
30
30
  divide(position: number): readonly [TokenCollection, TokenCollection];
@@ -1,10 +1,10 @@
1
1
  export class Token {
2
2
  static getCol(value, offset) {
3
- const lines = value.slice(0, offset).split(/\n/g);
3
+ const lines = value.slice(0, offset).split(/\n/);
4
4
  return (lines.at(-1) ?? '').length + 1;
5
5
  }
6
6
  static getLine(value, offset) {
7
- return value.slice(0, offset).split(/\n/g).length;
7
+ return value.slice(0, offset).split(/\n/).length;
8
8
  }
9
9
  static getType(value, separators) {
10
10
  if (Token.whitespace.includes(value[0] ?? '')) {
@@ -2,7 +2,14 @@ import type { FormattedPrimitiveTypeCreator } from '../types.js';
2
2
  /**
3
3
  * valid name of custom element
4
4
  *
5
- * @see https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
5
+ * > PotentialCustomElementName ::=
6
+ * > [a-z] (PCENChar)* '-' (PCENChar)*
7
+ * > PCENChar ::=
8
+ * > "-" | "." | [0-9] | "_" | [a-z] | #xB7 | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x37D] |
9
+ * > [#x37F-#x1FFF] | [#x200C-#x200D] | [#x203F-#x2040] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
10
+ * > This uses the EBNF notation from the XML specification. [XML]
11
+ *
12
+ * > They start with an ASCII lower alpha, ensuring that the HTML parser will treat them as tags instead of as text.
6
13
  *
7
14
  * > - name must match the [PotentialCustomElementName](https://html.spec.whatwg.org/multipage/custom-elements.html#prod-potentialcustomelementname) production
8
15
  * > - name must not be any of the following:
@@ -15,8 +22,6 @@ import type { FormattedPrimitiveTypeCreator } from '../types.js';
15
22
  * > - `<font-face-name>`
16
23
  * > - `<missing-glyph>`
17
24
  *
18
- * ASCII-case-insensitively.
19
- * Originally, it is not possible to define a name including ASCII upper alphas in the custom element, but it is not treated as illegal by the HTML parser.
20
- *
25
+ * @see https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
21
26
  */
22
27
  export declare const isCustomElementName: FormattedPrimitiveTypeCreator;
@@ -1,8 +1,18 @@
1
1
  /**
2
- * PotentialCustomElementName
2
+ * PCENChar
3
3
  *
4
+ * > PCENChar ::=
5
+ * > "-" | "." | [0-9] | "_" | [a-z] | #xB7 | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x37D] |
6
+ * > [#x37F-#x1FFF] | [#x200C-#x200D] | [#x203F-#x2040] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
4
7
  * @see https://html.spec.whatwg.org/multipage/custom-elements.html#prod-potentialcustomelementname
5
8
  *
9
+ */
10
+ const onlyPCENChar =
11
+ // eslint-disable-next-line no-misleading-character-class
12
+ /^[-.\d_a-z\u00B7\u00C0-\u00D6[\u00D8-\u00F6\u00F8-\u037D\u037F-\u1FFF\u200C\u200D\u203F\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u{10000}-\u{EFFFF}]*$/u;
13
+ /**
14
+ * valid name of custom element
15
+ *
6
16
  * > PotentialCustomElementName ::=
7
17
  * > [a-z] (PCENChar)* '-' (PCENChar)*
8
18
  * > PCENChar ::=
@@ -10,34 +20,7 @@
10
20
  * > [#x37F-#x1FFF] | [#x200C-#x200D] | [#x203F-#x2040] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
11
21
  * > This uses the EBNF notation from the XML specification. [XML]
12
22
  *
13
- * ASCII-case-insensitively.
14
- * Originally, it is not possible to define a name including ASCII upper alphas in the custom element, but it is not treated as illegal by the HTML parser.
15
- */
16
- const rePCENChar = [
17
- '\\-',
18
- '\\.',
19
- '[0-9]',
20
- '_',
21
- '[a-z]',
22
- '\u00B7',
23
- '[\u00C0-\u00D6]',
24
- '[\u00D8-\u00F6]',
25
- '[\u00F8-\u037D]',
26
- '[\u037F-\u1FFF]',
27
- '[\u200C-\u200D]',
28
- '[\u203F-\u2040]',
29
- '[\u2070-\u218F]',
30
- '[\u2C00-\u2FEF]',
31
- '[\u3001-\uD7FF]',
32
- '[\uF900-\uFDCF]',
33
- '[\uFDF0-\uFFFD]',
34
- '[\uD800-\uDBFF][\uDC00-\uDFFF]',
35
- ].join('|');
36
- const rePCEN = new RegExp(`^[a-z](?:${rePCENChar})*\\-(?:${rePCENChar})*$`, 'i');
37
- /**
38
- * valid name of custom element
39
- *
40
- * @see https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
23
+ * > They start with an ASCII lower alpha, ensuring that the HTML parser will treat them as tags instead of as text.
41
24
  *
42
25
  * > - name must match the [PotentialCustomElementName](https://html.spec.whatwg.org/multipage/custom-elements.html#prod-potentialcustomelementname) production
43
26
  * > - name must not be any of the following:
@@ -50,9 +33,7 @@ const rePCEN = new RegExp(`^[a-z](?:${rePCENChar})*\\-(?:${rePCENChar})*$`, 'i')
50
33
  * > - `<font-face-name>`
51
34
  * > - `<missing-glyph>`
52
35
  *
53
- * ASCII-case-insensitively.
54
- * Originally, it is not possible to define a name including ASCII upper alphas in the custom element, but it is not treated as illegal by the HTML parser.
55
- *
36
+ * @see https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
56
37
  */
57
38
  export const isCustomElementName = () => {
58
39
  return tagName => {
@@ -68,6 +49,13 @@ export const isCustomElementName = () => {
68
49
  return false;
69
50
  }
70
51
  }
71
- return rePCEN.test(tagName);
52
+ // First char must be ASCII lowercase alpha.
53
+ if (!/^[a-z]/.test(tagName)) {
54
+ return false;
55
+ }
56
+ if (!tagName.includes('-')) {
57
+ return false;
58
+ }
59
+ return onlyPCENChar.test(tagName);
72
60
  };
73
61
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markuplint/types",
3
- "version": "4.0.0-dev.28+0131de5e",
3
+ "version": "4.0.0-rc.0",
4
4
  "description": "Type declaration and value checker",
5
5
  "repository": "git@github.com:markuplint/markuplint.git",
6
6
  "author": "Yusuke Hirao <yusukehirao@me.com>",
@@ -22,18 +22,22 @@
22
22
  "scripts": {
23
23
  "build": "tsc",
24
24
  "clean": "tsc --build --clean",
25
- "schema": "ts-node './gen/types.ts'; json2ts './types.schema.json' > './src/types.schema.ts'; prettier './src/types.schema.ts' './types.schema.json' --write; eslint './src/types.schema.ts' --fix; tsc;"
25
+ "schema": "run-s schema:types schema:schema build schema:prettier schema:eslint schema:prettier",
26
+ "schema:types": "node --loader ts-node/esm \"./gen/types.ts\"",
27
+ "schema:schema": "json2ts \"./types.schema.json\" > \"./src/types.schema.ts\"",
28
+ "schema:eslint": "eslint --fix \"./src/types.schema.ts\"",
29
+ "schema:prettier": "prettier --write \"./src/types.schema.ts\" \"./types.schema.json\" --log-level warn"
26
30
  },
27
31
  "dependencies": {
28
- "@types/css-tree": "^2.3.3",
29
- "@types/debug": "^4.1.10",
30
- "@types/whatwg-mimetype": "3.0.1",
32
+ "@types/css-tree": "^2.3.4",
33
+ "@types/debug": "^4.1.12",
34
+ "@types/whatwg-mimetype": "3.0.2",
31
35
  "bcp-47": "^2.1.0",
32
36
  "css-tree": "^2.3.1",
33
37
  "debug": "^4.3.4",
34
38
  "leven": "^4.0.0",
35
- "type-fest": "^4.5.0",
36
- "whatwg-mimetype": "^3.0.0"
39
+ "type-fest": "^4.10.2",
40
+ "whatwg-mimetype": "^4.0.0"
37
41
  },
38
- "gitHead": "0131de5ea9dd6d3fd5472d7b414b66644c758881"
42
+ "gitHead": "3fdeb45cb69ed52b3a215a7520cea1181601443f"
39
43
  }