@markuplint/types 1.0.0-dev.20211213.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 +21 -0
- package/README.md +117 -0
- package/extends.json +12 -0
- package/gen/specific-schema.json +98 -0
- package/gen/types.ts +66 -0
- package/lib/check-multi-types.d.ts +2 -0
- package/lib/check-multi-types.js +29 -0
- package/lib/check.d.ts +8 -0
- package/lib/check.js +49 -0
- package/lib/check.spec.d.ts +1 -0
- package/lib/check.spec.js +57 -0
- package/lib/css-syntax.d.ts +2 -0
- package/lib/css-syntax.js +141 -0
- package/lib/css-syntax.spec.d.ts +1 -0
- package/lib/css-syntax.spec.js +157 -0
- package/lib/debug.d.ts +2 -0
- package/lib/debug.js +6 -0
- package/lib/defs.d.ts +3 -0
- package/lib/defs.js +752 -0
- package/lib/enum.d.ts +3 -0
- package/lib/enum.js +26 -0
- package/lib/get-candicate.d.ts +3 -0
- package/lib/get-candicate.js +26 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +6 -0
- package/lib/keyword-type.d.ts +3 -0
- package/lib/keyword-type.js +46 -0
- package/lib/list.d.ts +3 -0
- package/lib/list.js +29 -0
- package/lib/list.spec.d.ts +1 -0
- package/lib/list.spec.js +129 -0
- package/lib/match-result.d.ts +9 -0
- package/lib/match-result.js +33 -0
- package/lib/number.d.ts +3 -0
- package/lib/number.js +41 -0
- package/lib/primitive/index.d.ts +7 -0
- package/lib/primitive/index.js +17 -0
- package/lib/primitive/index.spec.d.ts +1 -0
- package/lib/primitive/index.spec.js +62 -0
- package/lib/primitive/is-float.d.ts +6 -0
- package/lib/primitive/is-float.js +12 -0
- package/lib/primitive/is-int.d.ts +6 -0
- package/lib/primitive/is-int.js +12 -0
- package/lib/primitive/is-non-zero-uint.d.ts +6 -0
- package/lib/primitive/is-non-zero-uint.js +12 -0
- package/lib/primitive/is-quantity.d.ts +7 -0
- package/lib/primitive/is-quantity.js +41 -0
- package/lib/primitive/is-uint.d.ts +8 -0
- package/lib/primitive/is-uint.js +19 -0
- package/lib/primitive/range.d.ts +8 -0
- package/lib/primitive/range.js +18 -0
- package/lib/primitive/split-unit.d.ts +9 -0
- package/lib/primitive/split-unit.js +24 -0
- package/lib/rfc/is-bcp-47.d.ts +5 -0
- package/lib/rfc/is-bcp-47.js +14 -0
- package/lib/rfc/is-bcp-47.spec.d.ts +1 -0
- package/lib/rfc/is-bcp-47.spec.js +19 -0
- package/lib/token/index.d.ts +2 -0
- package/lib/token/index.js +7 -0
- package/lib/token/token-collection.d.ts +62 -0
- package/lib/token/token-collection.js +383 -0
- package/lib/token/token-collection.spec.d.ts +1 -0
- package/lib/token/token-collection.spec.js +144 -0
- package/lib/token/token.d.ts +55 -0
- package/lib/token/token.js +128 -0
- package/lib/types.d.ts +78 -0
- package/lib/types.js +2 -0
- package/lib/types.schema.d.ts +54 -0
- package/lib/types.schema.js +8 -0
- package/lib/w3c/check-serialized-permissions-policy.d.ts +15 -0
- package/lib/w3c/check-serialized-permissions-policy.js +251 -0
- package/lib/w3c/check-serialized-permissions-policy.spec.d.ts +1 -0
- package/lib/w3c/check-serialized-permissions-policy.spec.js +26 -0
- package/lib/whatwg/check-autocomplete.d.ts +5 -0
- package/lib/whatwg/check-autocomplete.js +282 -0
- package/lib/whatwg/check-autocomplete.spec.d.ts +1 -0
- package/lib/whatwg/check-autocomplete.spec.js +370 -0
- package/lib/whatwg/check-datetime/date-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/date-string.js +31 -0
- package/lib/whatwg/check-datetime/datetime-tokens.d.ts +3 -0
- package/lib/whatwg/check-datetime/datetime-tokens.js +509 -0
- package/lib/whatwg/check-datetime/duration-string.d.ts +6 -0
- package/lib/whatwg/check-datetime/duration-string.js +322 -0
- package/lib/whatwg/check-datetime/global-date-and-time-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/global-date-and-time-string.js +66 -0
- package/lib/whatwg/check-datetime/index.d.ts +5 -0
- package/lib/whatwg/check-datetime/index.js +35 -0
- package/lib/whatwg/check-datetime/index.spec.d.ts +1 -0
- package/lib/whatwg/check-datetime/index.spec.js +318 -0
- package/lib/whatwg/check-datetime/local-date-and-time-string.d.ts +9 -0
- package/lib/whatwg/check-datetime/local-date-and-time-string.js +116 -0
- package/lib/whatwg/check-datetime/month-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/month-string.js +20 -0
- package/lib/whatwg/check-datetime/time-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/time-string.js +35 -0
- package/lib/whatwg/check-datetime/time-zone-offset-string.d.ts +7 -0
- package/lib/whatwg/check-datetime/time-zone-offset-string.js +75 -0
- package/lib/whatwg/check-datetime/week-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/week-string.js +29 -0
- package/lib/whatwg/check-datetime/year-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/year-string.js +20 -0
- package/lib/whatwg/check-datetime/yearless-date-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/yearless-date-string.js +27 -0
- package/lib/whatwg/check-mime-type.d.ts +11 -0
- package/lib/whatwg/check-mime-type.js +42 -0
- package/lib/whatwg/check-mime-type.spec.d.ts +1 -0
- package/lib/whatwg/check-mime-type.spec.js +58 -0
- package/lib/whatwg/is-abs-url.d.ts +15 -0
- package/lib/whatwg/is-abs-url.js +34 -0
- package/lib/whatwg/is-abs-url.spec.d.ts +1 -0
- package/lib/whatwg/is-abs-url.spec.js +8 -0
- package/lib/whatwg/is-browser-context-name.d.ts +11 -0
- package/lib/whatwg/is-browser-context-name.js +18 -0
- package/lib/whatwg/is-custom-element-name.d.ts +22 -0
- package/lib/whatwg/is-custom-element-name.js +77 -0
- package/lib/whatwg/is-itemprop-name.d.ts +12 -0
- package/lib/whatwg/is-itemprop-name.js +19 -0
- package/package.json +33 -0
- package/src/check-multi-types.ts +35 -0
- package/src/check.spec.ts +62 -0
- package/src/check.ts +48 -0
- package/src/css-syntax.spec.ts +167 -0
- package/src/css-syntax.ts +177 -0
- package/src/debug.ts +3 -0
- package/src/defs.ts +810 -0
- package/src/enum.ts +25 -0
- package/src/get-candicate.ts +24 -0
- package/src/index.ts +3 -0
- package/src/keyword-type.ts +52 -0
- package/src/list.spec.ts +133 -0
- package/src/list.ts +34 -0
- package/src/match-result.ts +49 -0
- package/src/number.ts +46 -0
- package/src/primitive/index.spec.ts +65 -0
- package/src/primitive/index.ts +7 -0
- package/src/primitive/is-float.ts +8 -0
- package/src/primitive/is-int.ts +8 -0
- package/src/primitive/is-non-zero-uint.ts +8 -0
- package/src/primitive/is-quantity.ts +38 -0
- package/src/primitive/is-uint.ts +15 -0
- package/src/primitive/range.ts +14 -0
- package/src/primitive/split-unit.ts +20 -0
- package/src/rfc/is-bcp-47.spec.ts +23 -0
- package/src/rfc/is-bcp-47.ts +13 -0
- package/src/token/index.ts +2 -0
- package/src/token/token-collection.spec.ts +147 -0
- package/src/token/token-collection.ts +444 -0
- package/src/token/token.ts +144 -0
- package/src/types.schema.ts +1059 -0
- package/src/types.ts +118 -0
- package/src/w3c/check-serialized-permissions-policy.spec.ts +27 -0
- package/src/w3c/check-serialized-permissions-policy.ts +287 -0
- package/src/whatwg/check-autocomplete.spec.ts +378 -0
- package/src/whatwg/check-autocomplete.ts +324 -0
- package/src/whatwg/check-datetime/date-string.ts +44 -0
- package/src/whatwg/check-datetime/datetime-tokens.ts +600 -0
- package/src/whatwg/check-datetime/duration-string.ts +399 -0
- package/src/whatwg/check-datetime/global-date-and-time-string.ts +96 -0
- package/src/whatwg/check-datetime/index.spec.ts +333 -0
- package/src/whatwg/check-datetime/index.ts +36 -0
- package/src/whatwg/check-datetime/local-date-and-time-string.ts +163 -0
- package/src/whatwg/check-datetime/month-string.ts +31 -0
- package/src/whatwg/check-datetime/time-string.ts +49 -0
- package/src/whatwg/check-datetime/time-zone-offset-string.ts +93 -0
- package/src/whatwg/check-datetime/week-string.ts +41 -0
- package/src/whatwg/check-datetime/year-string.ts +26 -0
- package/src/whatwg/check-datetime/yearless-date-string.ts +38 -0
- package/src/whatwg/check-mime-type.spec.ts +59 -0
- package/src/whatwg/check-mime-type.ts +46 -0
- package/src/whatwg/is-abs-url.spec.ts +8 -0
- package/src/whatwg/is-abs-url.ts +31 -0
- package/src/whatwg/is-browser-context-name.ts +16 -0
- package/src/whatwg/is-custom-element-name.ts +76 -0
- package/src/whatwg/is-itemprop-name.ts +17 -0
- package/tsconfig.test.json +3 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/types.schema.json +1092 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import type { CustomCssSyntax } from './types';
|
|
2
|
+
|
|
3
|
+
import { cssSyntaxMatch } from './css-syntax';
|
|
4
|
+
|
|
5
|
+
test('CSS Standard', () => {
|
|
6
|
+
expect(cssSyntaxMatch('10px', '<length>').matched).toBe(true);
|
|
7
|
+
expect(cssSyntaxMatch('one-pixel', '<length>').matched).toBe(false);
|
|
8
|
+
expect(cssSyntaxMatch('(prefers-color-scheme: dark)', '<media-query-list>').matched).toBe(true);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
test('Extends', () => {
|
|
12
|
+
const sourceSizeList: CustomCssSyntax = {
|
|
13
|
+
ref: 'https://html.spec.whatwg.org/multipage/images.html#sizes-attributes',
|
|
14
|
+
syntax: {
|
|
15
|
+
apply: '<source-size-list>',
|
|
16
|
+
def: {
|
|
17
|
+
'source-size-list': '[ <source-size># , ]? <source-size-value>',
|
|
18
|
+
'source-size': '<media-condition> <source-size-value>',
|
|
19
|
+
'source-size-value': '<length>',
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
expect(cssSyntaxMatch('50vw', sourceSizeList).matched).toBe(true);
|
|
24
|
+
expect(cssSyntaxMatch('(max-width: 600px) 200px, 50vw', sourceSizeList).matched).toBe(true);
|
|
25
|
+
expect(cssSyntaxMatch('print 300vw', sourceSizeList).matched).toBe(false);
|
|
26
|
+
expect(cssSyntaxMatch('(max-width: 600px) 200px', sourceSizeList).matched).toBe(false);
|
|
27
|
+
expect(
|
|
28
|
+
cssSyntaxMatch('(max-width: 600px) 200px', {
|
|
29
|
+
...sourceSizeList,
|
|
30
|
+
syntax: {
|
|
31
|
+
...sourceSizeList.syntax,
|
|
32
|
+
apply: '<source-size>',
|
|
33
|
+
},
|
|
34
|
+
}).matched,
|
|
35
|
+
).toBe(true);
|
|
36
|
+
|
|
37
|
+
const keyTimes: CustomCssSyntax = {
|
|
38
|
+
ref: 'https://svgwg.org/specs/animations/#KeyTimesAttribute',
|
|
39
|
+
syntax: {
|
|
40
|
+
apply: '<key-times>',
|
|
41
|
+
def: {
|
|
42
|
+
'key-times': '<number> [; <number>]* [;]?',
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
expect(cssSyntaxMatch('20; 20; 30', keyTimes).matched).toBe(true);
|
|
47
|
+
expect(cssSyntaxMatch('20; 20; 30;', keyTimes).matched).toBe(true);
|
|
48
|
+
expect(cssSyntaxMatch('20; 20; abc;', keyTimes).matched).toBe(false);
|
|
49
|
+
expect(cssSyntaxMatch('20; 20; ;', keyTimes).matched).toBe(false);
|
|
50
|
+
|
|
51
|
+
const SVGViewBox: CustomCssSyntax = {
|
|
52
|
+
ref: 'https://svgwg.org/svg2-draft/coords.html#ViewBoxAttribute',
|
|
53
|
+
syntax: {
|
|
54
|
+
apply: '<view-box>',
|
|
55
|
+
def: {
|
|
56
|
+
'view-box': '<min-x> [,]? <min-y> [,]? <width> [,]? <height>',
|
|
57
|
+
'min-x': '<number>',
|
|
58
|
+
'min-y': '<number>',
|
|
59
|
+
width: '<number>',
|
|
60
|
+
height: '<number>',
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
expect(cssSyntaxMatch('0 0 30 10', SVGViewBox).matched).toBe(true);
|
|
65
|
+
expect(cssSyntaxMatch('0 0, 30, 10', SVGViewBox).matched).toBe(true);
|
|
66
|
+
expect(cssSyntaxMatch('0 0, 30', SVGViewBox).matched).toBe(false);
|
|
67
|
+
|
|
68
|
+
const percentageList: CustomCssSyntax = {
|
|
69
|
+
ref: 'https://developer.mozilla.org/en-US/docs/Web/SVG/Content_type#percentage',
|
|
70
|
+
syntax: {
|
|
71
|
+
apply: '<percentage-list>',
|
|
72
|
+
def: {
|
|
73
|
+
'percentage-list': '[ <number> [,]? ]* <number>',
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
expect(cssSyntaxMatch('0 0 30 10', percentageList).matched).toBe(true);
|
|
78
|
+
expect(cssSyntaxMatch('0, 0, 30 10', percentageList).matched).toBe(true);
|
|
79
|
+
expect(cssSyntaxMatch('0', percentageList).matched).toBe(true);
|
|
80
|
+
expect(cssSyntaxMatch('', percentageList).matched).toBe(false);
|
|
81
|
+
|
|
82
|
+
const custom: CustomCssSyntax = {
|
|
83
|
+
ref: 'n/a',
|
|
84
|
+
syntax: {
|
|
85
|
+
apply: '<custom>',
|
|
86
|
+
def: {
|
|
87
|
+
custom: '0 | 1 | .',
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
expect(cssSyntaxMatch('0', custom).matched).toBe(true);
|
|
92
|
+
expect(cssSyntaxMatch('1', custom).matched).toBe(true);
|
|
93
|
+
expect(cssSyntaxMatch('.', custom).matched).toBe(true);
|
|
94
|
+
expect(cssSyntaxMatch('2', custom).matched).toBe(false);
|
|
95
|
+
expect(cssSyntaxMatch(':', custom).matched).toBe(false);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test('custom-type-checking', () => {
|
|
99
|
+
const custom: CustomCssSyntax = {
|
|
100
|
+
ref: 'n/a',
|
|
101
|
+
caseSensitive: true,
|
|
102
|
+
syntax: {
|
|
103
|
+
apply: '<custom>',
|
|
104
|
+
def: {
|
|
105
|
+
custom: '<custom-type>',
|
|
106
|
+
'custom-type': token => {
|
|
107
|
+
if (!token) {
|
|
108
|
+
return 0;
|
|
109
|
+
}
|
|
110
|
+
return token.value === '1' ? 1 : 0;
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
expect(cssSyntaxMatch('1', custom).matched).toBe(true);
|
|
116
|
+
expect(cssSyntaxMatch('0', custom).matched).toBe(false);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('case-sensivite', () => {
|
|
120
|
+
const custom: CustomCssSyntax = {
|
|
121
|
+
ref: 'n/a',
|
|
122
|
+
caseSensitive: true,
|
|
123
|
+
syntax: {
|
|
124
|
+
apply: '<custom>',
|
|
125
|
+
def: {
|
|
126
|
+
custom: 'CaSeS [a | B | aBc]',
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
expect(cssSyntaxMatch('CaSeS a', custom).matched).toBe(true);
|
|
131
|
+
expect(cssSyntaxMatch('CaSeS B', custom).matched).toBe(true);
|
|
132
|
+
expect(cssSyntaxMatch('CaSeS aBc', custom).matched).toBe(true);
|
|
133
|
+
expect(cssSyntaxMatch('CaSeS A', custom)).toStrictEqual({
|
|
134
|
+
column: 7,
|
|
135
|
+
line: 1,
|
|
136
|
+
matched: false,
|
|
137
|
+
length: 1,
|
|
138
|
+
offset: 6,
|
|
139
|
+
partName: 'value',
|
|
140
|
+
raw: 'A',
|
|
141
|
+
reason: 'syntax-error',
|
|
142
|
+
ref: 'n/a',
|
|
143
|
+
expects: [
|
|
144
|
+
{
|
|
145
|
+
type: 'syntax',
|
|
146
|
+
value: 'CaSeS [ a | B | aBc ]',
|
|
147
|
+
},
|
|
148
|
+
],
|
|
149
|
+
});
|
|
150
|
+
expect(cssSyntaxMatch('CaSeS abc', custom)).toStrictEqual({
|
|
151
|
+
column: 7,
|
|
152
|
+
line: 1,
|
|
153
|
+
matched: false,
|
|
154
|
+
length: 3,
|
|
155
|
+
offset: 6,
|
|
156
|
+
partName: 'value',
|
|
157
|
+
raw: 'abc',
|
|
158
|
+
reason: 'syntax-error',
|
|
159
|
+
ref: 'n/a',
|
|
160
|
+
expects: [
|
|
161
|
+
{
|
|
162
|
+
type: 'syntax',
|
|
163
|
+
value: 'CaSeS [ a | B | aBc ]',
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
});
|
|
167
|
+
});
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import type { CustomCssSyntax, CssSyntaxTokenizer, CSSSyntaxToken, GetNextToken, Result, CssSyntax } from './types';
|
|
2
|
+
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
import csstree from 'css-tree';
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
import { SyntaxMatchError } from 'css-tree/lib/lexer/error.js';
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
import { matchAsTree } from 'css-tree/lib/lexer/match.js';
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
import prepareTokens from 'css-tree/lib/lexer/prepare-tokens.js';
|
|
11
|
+
|
|
12
|
+
import { log } from './debug';
|
|
13
|
+
import { tokenizers } from './defs';
|
|
14
|
+
import { matched } from './match-result';
|
|
15
|
+
|
|
16
|
+
// eslint-disable-next-line no-redeclare
|
|
17
|
+
interface SyntaxMatchError {
|
|
18
|
+
message: string;
|
|
19
|
+
rawMessage: 'Mismatch';
|
|
20
|
+
syntax: string;
|
|
21
|
+
css: string;
|
|
22
|
+
mismatchOffset: number;
|
|
23
|
+
mismatchLength: number;
|
|
24
|
+
offset: number;
|
|
25
|
+
line: number;
|
|
26
|
+
column: number;
|
|
27
|
+
loc: {
|
|
28
|
+
source: '<unknown>';
|
|
29
|
+
start: { offset: number; line: number; column: number };
|
|
30
|
+
end: { offset: number; line: number; column: number };
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function cssSyntaxMatch(value: string, type: CssSyntax | CustomCssSyntax): Result {
|
|
35
|
+
const origin = value;
|
|
36
|
+
let defName: `<${string}>`;
|
|
37
|
+
const typesExtended: Record<string, string> = {};
|
|
38
|
+
const typesCheckers: Record<string, CssSyntaxTokenizer> = {};
|
|
39
|
+
let propsExtended: Record<string, string>;
|
|
40
|
+
let ref: string | undefined = undefined;
|
|
41
|
+
let caseSensitive = false;
|
|
42
|
+
let ebnf: Record<string, string | string[]> | null = null;
|
|
43
|
+
|
|
44
|
+
if (typeof type === 'string') {
|
|
45
|
+
defName = type;
|
|
46
|
+
propsExtended = {};
|
|
47
|
+
} else {
|
|
48
|
+
defName = type.syntax.apply;
|
|
49
|
+
ebnf = type.syntax.ebnf || null;
|
|
50
|
+
|
|
51
|
+
if (ebnf) {
|
|
52
|
+
// Work in progress
|
|
53
|
+
return matched();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const types = {
|
|
57
|
+
...tokenizers,
|
|
58
|
+
...type.syntax.def,
|
|
59
|
+
};
|
|
60
|
+
Object.keys(types).forEach(key => {
|
|
61
|
+
const valueOrChecker = types[key];
|
|
62
|
+
if (typeof valueOrChecker === 'string') {
|
|
63
|
+
typesExtended[key] = valueOrChecker;
|
|
64
|
+
} else {
|
|
65
|
+
typesCheckers[key] = valueOrChecker;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
propsExtended = { ...type.syntax.properties };
|
|
69
|
+
if (type.caseSensitive) {
|
|
70
|
+
caseSensitive = true;
|
|
71
|
+
Object.keys(typesExtended).forEach(key => eachMimicCases(key, typesExtended));
|
|
72
|
+
Object.keys(propsExtended).forEach(key => eachMimicCases(key, propsExtended));
|
|
73
|
+
value = mimicCases(value);
|
|
74
|
+
}
|
|
75
|
+
ref = type.ref;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// @ts-ignore
|
|
79
|
+
const lexer = csstree.fork({
|
|
80
|
+
types: typesExtended,
|
|
81
|
+
properties: propsExtended,
|
|
82
|
+
}).lexer;
|
|
83
|
+
|
|
84
|
+
Object.keys(typesCheckers).forEach(key => {
|
|
85
|
+
const checker = typesCheckers[key];
|
|
86
|
+
lexer.addType_(key, (token: CSSSyntaxToken, getNextToken: GetNextToken) =>
|
|
87
|
+
checker(token, getNextToken, cssSyntaxMatch),
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const { isProp, name, matcher } = defToMatcher(lexer, defName);
|
|
92
|
+
const refParam = isProp ? 'Property' : 'Type';
|
|
93
|
+
ref = ref || `https://csstree.github.io/docs/syntax/#${refParam}:${name}`;
|
|
94
|
+
|
|
95
|
+
// eslint-disable-next-line no-console
|
|
96
|
+
const _w = console.warn;
|
|
97
|
+
// eslint-disable-next-line no-console
|
|
98
|
+
console.warn = warn => log('WARNING: %s (by %s => %s)', warn, value, type);
|
|
99
|
+
|
|
100
|
+
const tokens = prepareTokens(value, lexer.syntax);
|
|
101
|
+
const result = matchAsTree(tokens, matcher.match, lexer);
|
|
102
|
+
|
|
103
|
+
if (caseSensitive) {
|
|
104
|
+
if (result.tokens && Array.isArray(result.tokens)) {
|
|
105
|
+
let reducer = 0;
|
|
106
|
+
result.tokens = result.tokens.map((token: CSSSyntaxToken) => {
|
|
107
|
+
const value = token.value;
|
|
108
|
+
const originValue = deMimicCases(value || '');
|
|
109
|
+
const isMimiced = value !== originValue;
|
|
110
|
+
reducer += isMimiced ? 'mimiccases------mimiccases'.length : 0;
|
|
111
|
+
token.value = originValue;
|
|
112
|
+
token.balance = token.balance - reducer;
|
|
113
|
+
return token;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
log('css-tree/result: %O', result);
|
|
119
|
+
// eslint-disable-next-line no-console
|
|
120
|
+
console.warn = _w;
|
|
121
|
+
|
|
122
|
+
if (result.match) {
|
|
123
|
+
log('css-tree/result.match: %s', JSON.stringify(result.match, null, 2));
|
|
124
|
+
return matched();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const error: SyntaxMatchError = new SyntaxMatchError(result.reason, matcher.syntax, value, result);
|
|
128
|
+
|
|
129
|
+
if (caseSensitive) {
|
|
130
|
+
error.message = deMimicCases(error.message);
|
|
131
|
+
error.syntax = deMimicCases(error.syntax);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
log('css-tree/SyntaxMatchError: %O', error);
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
matched: false,
|
|
138
|
+
ref,
|
|
139
|
+
raw: origin.slice(error.mismatchOffset, error.mismatchOffset + error.mismatchLength),
|
|
140
|
+
offset: error.mismatchOffset,
|
|
141
|
+
length: error.mismatchLength,
|
|
142
|
+
line: error.line,
|
|
143
|
+
column: error.column,
|
|
144
|
+
expects: [
|
|
145
|
+
{
|
|
146
|
+
type: 'syntax',
|
|
147
|
+
value: error.syntax,
|
|
148
|
+
},
|
|
149
|
+
],
|
|
150
|
+
partName: error.mismatchOffset === 0 ? undefined : 'value',
|
|
151
|
+
reason: 'syntax-error',
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function defToMatcher(lexer: any, def: `<${string}>`) {
|
|
156
|
+
const isProp = def.search("<'") === 0;
|
|
157
|
+
const name = def.replace(/^<'?|'?>$/g, '');
|
|
158
|
+
const matcher = isProp ? lexer.properties[name] : lexer.types[name];
|
|
159
|
+
return {
|
|
160
|
+
isProp,
|
|
161
|
+
name,
|
|
162
|
+
matcher,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function eachMimicCases(key: string, obj: Record<string, string>) {
|
|
167
|
+
const value = obj[key];
|
|
168
|
+
obj[key] = mimicCases(value);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function mimicCases(value: string) {
|
|
172
|
+
return value.replace(/[A-Z]/g, $0 => `mimiccases---${$0}---mimiccases`);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function deMimicCases(value: string) {
|
|
176
|
+
return value.replace(/mimiccases---([A-Z])---mimiccases/g, (_, $1) => $1);
|
|
177
|
+
}
|
package/src/debug.ts
ADDED