@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,399 @@
|
|
|
1
|
+
import type { CustomSyntaxChecker } from '../../types';
|
|
2
|
+
|
|
3
|
+
import { log } from '../../debug';
|
|
4
|
+
import { matched, unmatched } from '../../match-result';
|
|
5
|
+
import { TokenCollection } from '../../token';
|
|
6
|
+
|
|
7
|
+
import { datetimeTokenCheck } from './datetime-tokens';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @see https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#durations
|
|
11
|
+
*/
|
|
12
|
+
export const checkDurationISO8601LikeString: CustomSyntaxChecker = () =>
|
|
13
|
+
function checkDurationISO8601LikeString(value) {
|
|
14
|
+
log('CHECK: duration-string (ISO8601 like)');
|
|
15
|
+
|
|
16
|
+
// PnDTnHnMnS
|
|
17
|
+
const tokens = TokenCollection.fromPatterns(value, [
|
|
18
|
+
// Start sign P
|
|
19
|
+
/[^0-9T]?/,
|
|
20
|
+
// date part
|
|
21
|
+
/[^T]*/,
|
|
22
|
+
// Time sign T
|
|
23
|
+
/[^0-9]?/,
|
|
24
|
+
// Time part
|
|
25
|
+
/.*/,
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
log('Duration ISO8601: "%s" => %O', tokens.value, tokens);
|
|
29
|
+
|
|
30
|
+
const res = tokens.eachCheck(
|
|
31
|
+
p => {
|
|
32
|
+
if (!p || !p.value) {
|
|
33
|
+
return unmatched('', 'missing-token', {
|
|
34
|
+
expects: [{ type: 'const', value: 'P' }],
|
|
35
|
+
partName: 'duration',
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!p.match('P', false)) {
|
|
40
|
+
return p.unmatched({
|
|
41
|
+
reason: 'unexpected-token',
|
|
42
|
+
expects: [{ type: 'const', value: 'P' }],
|
|
43
|
+
partName: 'duration',
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
d => {
|
|
48
|
+
if (!d || !d.value) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const [num, sign, extra] = TokenCollection.fromPatterns(d, [/[0-9]+/, /./]);
|
|
53
|
+
|
|
54
|
+
log('Date part: "%s" => %O', d.value, { num, sign });
|
|
55
|
+
|
|
56
|
+
if (!num.match(/^[0-9]+$/)) {
|
|
57
|
+
return num.unmatched({
|
|
58
|
+
reason: 'unexpected-token',
|
|
59
|
+
expects: [],
|
|
60
|
+
partName: 'date',
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (!sign || !sign.value) {
|
|
65
|
+
return unmatched('', 'missing-token', {
|
|
66
|
+
expects: [{ type: 'const', value: 'D' }],
|
|
67
|
+
partName: 'date',
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!sign.match('D', false)) {
|
|
72
|
+
return sign.unmatched({
|
|
73
|
+
reason: 'unexpected-token',
|
|
74
|
+
expects: [{ type: 'const', value: 'D' }],
|
|
75
|
+
partName: 'date',
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (extra && extra.value) {
|
|
80
|
+
return extra.unmatched({
|
|
81
|
+
reason: 'unexpected-token',
|
|
82
|
+
expects: [{ type: 'const', value: 'T' }],
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
t => {
|
|
87
|
+
if (!t || !t.value) {
|
|
88
|
+
return unmatched('', 'missing-token', {
|
|
89
|
+
expects: [{ type: 'const', value: 'T' }],
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (!t.match('T', false)) {
|
|
94
|
+
return t.unmatched({
|
|
95
|
+
reason: 'unexpected-token',
|
|
96
|
+
expects: [{ type: 'const', value: 'T' }],
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
time => {
|
|
101
|
+
if (!time) {
|
|
102
|
+
return unmatched('', 'missing-token', {
|
|
103
|
+
expects: [
|
|
104
|
+
{ type: 'common', value: 'hour' },
|
|
105
|
+
{ type: 'common', value: 'minute' },
|
|
106
|
+
{ type: 'common', value: 'second' },
|
|
107
|
+
],
|
|
108
|
+
partName: 'time',
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const timeTokens = TokenCollection.fromPatterns(time, [
|
|
113
|
+
// Hour part
|
|
114
|
+
/[0-9]+(\.[0-9]*)?[^0-9]?/,
|
|
115
|
+
// Minute part
|
|
116
|
+
/[0-9]+(\.[0-9]*)?[^0-9]?/,
|
|
117
|
+
// Second part
|
|
118
|
+
/[0-9]+(\.[0-9]*)?[^0-9]?/,
|
|
119
|
+
]);
|
|
120
|
+
|
|
121
|
+
log('Time part: "%s" => %O', timeTokens.value, timeTokens);
|
|
122
|
+
|
|
123
|
+
const [h, m, s] = timeTokens;
|
|
124
|
+
|
|
125
|
+
if ((h?.value || '') + (m?.value || '') + (s?.value || '') === '') {
|
|
126
|
+
return unmatched('', 'missing-token', {
|
|
127
|
+
expects: [
|
|
128
|
+
{ type: 'common', value: 'hour' },
|
|
129
|
+
{ type: 'common', value: 'minute' },
|
|
130
|
+
{ type: 'common', value: 'second' },
|
|
131
|
+
],
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const specified = new Set<'H' | 'M' | 'S'>();
|
|
136
|
+
|
|
137
|
+
for (const t of timeTokens) {
|
|
138
|
+
if (!t || !t.value) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (specified.has('S')) {
|
|
143
|
+
return t.unmatched({
|
|
144
|
+
reason: 'extra-token',
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const [num, dpfp, sign] = TokenCollection.fromPatterns(t, [/[0-9]+/, /(\.[0-9]*)?/, /[^0-9]+/]);
|
|
149
|
+
|
|
150
|
+
log('Time part (h|m|s): "%s" => %O', t.value, { num, dpfp, sign });
|
|
151
|
+
|
|
152
|
+
if (!num.match(/^[0-9]+$/)) {
|
|
153
|
+
return num.unmatched({
|
|
154
|
+
reason: 'unexpected-token',
|
|
155
|
+
expects: [{ type: 'common', value: 'number' }],
|
|
156
|
+
partName: 'time',
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (dpfp && dpfp.value) {
|
|
161
|
+
const [dp, fp] = TokenCollection.fromPatterns(dpfp, [/\./, /[0-9]+/]);
|
|
162
|
+
|
|
163
|
+
log('Second fractional part (h|m|s): "%s" => %O', dpfp.value, { dp, fp });
|
|
164
|
+
|
|
165
|
+
if (!dp.match('.')) {
|
|
166
|
+
return dp.unmatched({
|
|
167
|
+
reason: 'unexpected-token',
|
|
168
|
+
expects: [],
|
|
169
|
+
partName: 'decimal point',
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (!fp || !fp.value) {
|
|
174
|
+
return unmatched('', 'missing-token', {
|
|
175
|
+
expects: [{ type: 'common', value: 'fractional part' }],
|
|
176
|
+
partName: 'second',
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (!fp.match(/^[0-9]+$/)) {
|
|
181
|
+
return fp.unmatched({
|
|
182
|
+
reason: 'unexpected-token',
|
|
183
|
+
expects: [],
|
|
184
|
+
partName: 'fractional part',
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (!(1 <= fp.length && fp.length <= 3)) {
|
|
189
|
+
return fp.unmatched({
|
|
190
|
+
reason: { type: 'out-of-range-length-digit', gte: 1, lte: 3 },
|
|
191
|
+
expects: [],
|
|
192
|
+
partName: 'fractional part',
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const expects = [
|
|
198
|
+
{ type: 'const', value: 'H' },
|
|
199
|
+
{ type: 'const', value: 'M' },
|
|
200
|
+
{ type: 'const', value: 'S' },
|
|
201
|
+
] as const;
|
|
202
|
+
|
|
203
|
+
if (!sign || !sign.value) {
|
|
204
|
+
return unmatched('', 'missing-token', {
|
|
205
|
+
expects: expects.filter(e => !specified.has(e.value)),
|
|
206
|
+
partName: 'time',
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (specified.has('M') && sign.match('H', false)) {
|
|
211
|
+
return sign.unmatched({
|
|
212
|
+
reason: 'unexpected-token',
|
|
213
|
+
expects: [{ type: 'const', value: 'S' }],
|
|
214
|
+
partName: 'time',
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (
|
|
219
|
+
!sign.match(['H', 'M', 'S'], false) ||
|
|
220
|
+
(sign.match('H', false) && specified.has('H')) ||
|
|
221
|
+
(sign.match('M', false) && specified.has('M'))
|
|
222
|
+
) {
|
|
223
|
+
return sign.unmatched({
|
|
224
|
+
reason: 'unexpected-token',
|
|
225
|
+
expects: expects.filter(e => !specified.has(e.value)),
|
|
226
|
+
partName: 'time',
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
specified.add(sign.value as 'H' | 'M' | 'S');
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
datetimeTokenCheck.extra,
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
if (!res.matched) {
|
|
237
|
+
log('Failed: %O', res);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return res;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
export const checkDurationComponentListString: CustomSyntaxChecker = () =>
|
|
244
|
+
function checkDurationComponentListString(value) {
|
|
245
|
+
log('CHECK: duration-string (duration component list)');
|
|
246
|
+
|
|
247
|
+
if (!value) {
|
|
248
|
+
return unmatched('', 'empty-token', {
|
|
249
|
+
expects: [{ type: 'common', value: 'time' }],
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const patterns = [
|
|
254
|
+
// 1. Zero or more ASCII whitespace.
|
|
255
|
+
/\s*/,
|
|
256
|
+
// 2. One or more ASCII digits, representing a number of time units,
|
|
257
|
+
// scaled by the duration time component scale specified (see below)
|
|
258
|
+
// to represent a number of seconds.
|
|
259
|
+
/[0-9]+/,
|
|
260
|
+
// 3. If the duration time component scale specified is 1 (i.e. the units are seconds),
|
|
261
|
+
// then, optionally, a U+002E FULL STOP character (.) followed by
|
|
262
|
+
// one, two, or three ASCII digits, representing a fraction of a second.
|
|
263
|
+
/(\.[0-9]*)?/,
|
|
264
|
+
// 4. Zero or more ASCII whitespace.
|
|
265
|
+
/\s*/,
|
|
266
|
+
// 5. One of the following characters, representing the duration time component scale
|
|
267
|
+
// of the time unit used in the numeric part of the duration time component:
|
|
268
|
+
/[^0-9]/,
|
|
269
|
+
// 6. Zero or more ASCII whitespace.
|
|
270
|
+
/\s*/,
|
|
271
|
+
];
|
|
272
|
+
|
|
273
|
+
const tokens = TokenCollection.fromPatterns(value, patterns, { repeat: true });
|
|
274
|
+
const components = tokens.chunk(patterns.length);
|
|
275
|
+
|
|
276
|
+
const specified = new Set<'w' | 'd' | 'h' | 'm' | 's'>();
|
|
277
|
+
|
|
278
|
+
for (const component of components) {
|
|
279
|
+
log('Duration Component: "%s" => %O', component.value, component);
|
|
280
|
+
|
|
281
|
+
const [, , dpfp, , unit] = component;
|
|
282
|
+
|
|
283
|
+
const res = component.eachCheck(
|
|
284
|
+
ws => {},
|
|
285
|
+
num => {
|
|
286
|
+
if (!num || !num.value) {
|
|
287
|
+
const reason = dpfp?.value || unit?.value ? 'unexpected-token' : 'missing-token';
|
|
288
|
+
return unmatched('', reason, {
|
|
289
|
+
expects: [{ type: 'common', value: 'time' }],
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
},
|
|
293
|
+
dpfp => {
|
|
294
|
+
if (!dpfp || !dpfp.value) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const unitExpects = (
|
|
299
|
+
[
|
|
300
|
+
{ type: 'const', value: 'w' },
|
|
301
|
+
{ type: 'const', value: 'd' },
|
|
302
|
+
{ type: 'const', value: 'h' },
|
|
303
|
+
{ type: 'const', value: 'm' },
|
|
304
|
+
{ type: 'const', value: 's' },
|
|
305
|
+
] as const
|
|
306
|
+
).filter(e => !specified.has(e.value));
|
|
307
|
+
|
|
308
|
+
if (specified.has('s')) {
|
|
309
|
+
return dpfp.unmatched({
|
|
310
|
+
reason: 'unexpected-token',
|
|
311
|
+
expects: unitExpects,
|
|
312
|
+
partName: 'unit',
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const [, fp] = TokenCollection.fromPatterns(dpfp, [/\./, /[0-9]+/]);
|
|
317
|
+
|
|
318
|
+
if (!fp || !fp.value) {
|
|
319
|
+
return unmatched('', 'missing-token', {
|
|
320
|
+
expects: [{ type: 'common', value: 'fractional part' }],
|
|
321
|
+
partName: 'fractional part',
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if (!fp.match(/[0-9]+/)) {
|
|
326
|
+
return fp.unmatched({
|
|
327
|
+
reason: 'unexpected-token',
|
|
328
|
+
expects: [{ type: 'common', value: 'fractional part' }],
|
|
329
|
+
partName: 'fractional part',
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (!(1 <= fp.length && fp.length <= 3)) {
|
|
334
|
+
return fp.unmatched({
|
|
335
|
+
reason: { type: 'out-of-range-length-digit', gte: 1, lte: 3 },
|
|
336
|
+
expects: [],
|
|
337
|
+
partName: 'fractional part',
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
},
|
|
341
|
+
ws => {},
|
|
342
|
+
unit => {
|
|
343
|
+
const expects = (
|
|
344
|
+
[
|
|
345
|
+
{ type: 'const', value: 'w' },
|
|
346
|
+
{ type: 'const', value: 'd' },
|
|
347
|
+
{ type: 'const', value: 'h' },
|
|
348
|
+
{ type: 'const', value: 'm' },
|
|
349
|
+
{ type: 'const', value: 's' },
|
|
350
|
+
] as const
|
|
351
|
+
).filter(e => !specified.has(e.value));
|
|
352
|
+
|
|
353
|
+
if (!unit || !unit.value) {
|
|
354
|
+
return unmatched('', 'missing-token', {
|
|
355
|
+
expects,
|
|
356
|
+
partName: 'unit',
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const unitVal = unit.value.toLowerCase() as 'w' | 'd' | 'h' | 'm' | 's';
|
|
361
|
+
|
|
362
|
+
if (specified.has(unitVal)) {
|
|
363
|
+
return unit.unmatched({
|
|
364
|
+
reason: 'duplicated',
|
|
365
|
+
expects,
|
|
366
|
+
partName: 'unit',
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if (dpfp && dpfp.value && unit.match(['w', 'd', 'h', 'm'])) {
|
|
371
|
+
return unit.unmatched({
|
|
372
|
+
reason: 'unexpected-token',
|
|
373
|
+
expects: [{ type: 'const', value: 's' }],
|
|
374
|
+
partName: 'unit',
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (!unit.match(['w', 'd', 'h', 'm', 's'])) {
|
|
379
|
+
return unit.unmatched({
|
|
380
|
+
reason: 'unexpected-token',
|
|
381
|
+
expects,
|
|
382
|
+
partName: 'unit',
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
specified.add(unitVal);
|
|
387
|
+
},
|
|
388
|
+
ws => {},
|
|
389
|
+
);
|
|
390
|
+
|
|
391
|
+
if (!res.matched) {
|
|
392
|
+
log('Failed: %O', res);
|
|
393
|
+
|
|
394
|
+
return res;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return matched();
|
|
399
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { CustomSyntaxChecker } from '../../types';
|
|
2
|
+
|
|
3
|
+
import { log } from '../../debug';
|
|
4
|
+
import { unmatched } from '../../match-result';
|
|
5
|
+
import { TokenCollection } from '../../token';
|
|
6
|
+
|
|
7
|
+
import { datetimeTokenCheck } from './datetime-tokens';
|
|
8
|
+
import { parseTimeZone } from './time-zone-offset-string';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @see https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#global-dates-and-times
|
|
12
|
+
*/
|
|
13
|
+
export const checkGlobalDateAndTimeString: CustomSyntaxChecker = () =>
|
|
14
|
+
function checkGlobalDateAndTimeString(value) {
|
|
15
|
+
log('CHECK: global-date-and-time-string');
|
|
16
|
+
|
|
17
|
+
const tokens = TokenCollection.fromPatterns(value, [
|
|
18
|
+
// YYYY
|
|
19
|
+
/[^-]*/,
|
|
20
|
+
// -
|
|
21
|
+
/[^0-9]?/,
|
|
22
|
+
// MM
|
|
23
|
+
/[^-]*/,
|
|
24
|
+
// -
|
|
25
|
+
/[^0-9]?/,
|
|
26
|
+
// DD
|
|
27
|
+
/[^T\s]*/,
|
|
28
|
+
// T \s
|
|
29
|
+
/[^0-9]?/,
|
|
30
|
+
// hh
|
|
31
|
+
/[^:]*/,
|
|
32
|
+
// :
|
|
33
|
+
/[^0-9]?/,
|
|
34
|
+
// mm
|
|
35
|
+
/[^:Z+-]*/,
|
|
36
|
+
// :ss.sss
|
|
37
|
+
/(:[^Z+-]*)?/,
|
|
38
|
+
// time-zone
|
|
39
|
+
/.*/,
|
|
40
|
+
]);
|
|
41
|
+
|
|
42
|
+
log('Global Date and Time "%s" => %O', tokens.value, tokens);
|
|
43
|
+
|
|
44
|
+
const res = tokens.eachCheck(
|
|
45
|
+
datetimeTokenCheck.year,
|
|
46
|
+
datetimeTokenCheck.hyphen,
|
|
47
|
+
datetimeTokenCheck.month,
|
|
48
|
+
datetimeTokenCheck.hyphen,
|
|
49
|
+
datetimeTokenCheck.date,
|
|
50
|
+
datetimeTokenCheck.localDateTimeSeparator,
|
|
51
|
+
datetimeTokenCheck.hour,
|
|
52
|
+
datetimeTokenCheck.coron,
|
|
53
|
+
datetimeTokenCheck.minute,
|
|
54
|
+
second => {
|
|
55
|
+
if (!second || !second.value) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const secondTokens = TokenCollection.fromPatterns(second, [/:?/, /[0-9]*/, /\.?/, /[0-9]*/]);
|
|
60
|
+
|
|
61
|
+
log('Scond Part: "%s" => %O', secondTokens.value, secondTokens);
|
|
62
|
+
|
|
63
|
+
const res = secondTokens.eachCheck(
|
|
64
|
+
datetimeTokenCheck.coron,
|
|
65
|
+
datetimeTokenCheck.second,
|
|
66
|
+
datetimeTokenCheck.decimalPointOrEnd,
|
|
67
|
+
datetimeTokenCheck.secondFractionalPart,
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
if (!res.matched) {
|
|
71
|
+
return res;
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
zone => {
|
|
75
|
+
if (!zone || !zone.value) {
|
|
76
|
+
return unmatched(value, 'missing-token', {
|
|
77
|
+
expects: [{ type: 'common', value: 'time-zone' }],
|
|
78
|
+
partName: 'time-zone',
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const res = parseTimeZone(zone);
|
|
83
|
+
|
|
84
|
+
if (!res.matched) {
|
|
85
|
+
return res;
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
datetimeTokenCheck.extra,
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
if (!res.matched) {
|
|
92
|
+
log('Failed: %O', res);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return res;
|
|
96
|
+
};
|