-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathextract-cssdfn.mjs
747 lines (690 loc) · 25.1 KB
/
extract-cssdfn.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
import informativeSelector from './informative-selector.mjs';
/**
* Extract the list of CSS definitions in the current spec
*
* @function
* @public
* @return {Promise} The promise to get an extract of the CSS definitions, or
* an empty CSS description object if the spec does not contain any CSS
* definition. The return object will have properties named "properties",
* "descriptors", "selectors" and "values".
*/
export default function () {
// List of inconsistencies and errors found in the spec while trying to make
// sense of the CSS definitions and production rules it contains
const warnings = [];
const res = {
// Properties are always defined in dedicated tables in modern CSS specs
properties: extractDfns({
selector: 'table.propdef:not(.attrdef)',
extractor: extractTableDfn,
duplicates: 'merge',
mayReturnMultipleDfns: true,
warnings
}),
// At-rules, selectors, functions and types are defined through dfns with
// the right "data-dfn-type" attribute
// Note some selectors are re-defined locally in HTML and Fullscreen. We
// won't import them.
atrules: extractDfns({
selector: 'dfn[data-dfn-type=at-rule]',
extractor: extractTypedDfn,
duplicates: 'reject',
warnings
}),
selectors: extractDfns({
selector: ['dfn[data-dfn-type=selector][data-export]:not([data-dfn-for])',
'dfn[data-dfn-type=selector][data-export][data-dfn-for=""]'
].join(','),
extractor: extractTypedDfn,
duplicates: 'reject',
warnings
}),
values: extractDfns({
selector: ['dfn[data-dfn-type=function]:not([data-dfn-for])',
'dfn[data-dfn-type=function][data-dfn-for=""]',
'dfn[data-dfn-type=type]:not([data-dfn-for])',
'dfn[data-dfn-type=type][data-dfn-for=""]'
].join(','),
extractor: extractTypedDfn,
duplicates: 'reject',
keepDfnType: true,
warnings
})
};
// At-rules have descriptors, defined in dedicated tables in modern CSS specs
// Note some of the descriptors are defined with a "type" property set to
// "range". Not sure what the type of a descriptor is supposed to mean, but
// let's keep that information around. One such example is the
// "-webkit-device-pixel-ratio" descriptor for "@media" at-rule in compat:
// https://compat.spec.whatwg.org/#css-media-queries-webkit-device-pixel-ratio
let descriptors = extractDfns({
selector: 'table.descdef:not(.attrdef)',
extractor: extractTableDfn,
duplicates: 'push',
mayReturnMultipleDfns: true,
keepDfnType: true,
warnings
});
// Older specs may follow older recipes, let's give them a try if we couldn't
// extract properties or descriptors
if (res.properties.length === 0 && descriptors.length === 0) {
res.properties = extractDfns({
selector: 'div.propdef dl',
extractor: extractDlDfn,
duplicates: 'merge',
mayReturnMultipleDfns: true,
warnings
});
descriptors = extractDfns({
selector: 'div.descdef dl',
extractor: extractDlDfn,
duplicates: 'push',
mayReturnMultipleDfns: true,
warnings
});
}
// Move descriptors to at-rules structure
for (const desclist of descriptors) {
for (const desc of desclist) {
let rule = res.atrules.find(r => r.name === desc.for);
if (rule) {
if (!rule.descriptors) {
rule.descriptors = [];
}
}
else {
rule = { name: desc.for, descriptors: [] };
res.atrules.push(rule);
}
rule.descriptors.push(desc);
}
}
for (const rule of res.atrules) {
if (!rule.descriptors) {
rule.descriptors = [];
}
}
// Keep an index of "root" (non-namespaced + descriptors) dfns
const rootDfns = Object.values(res).flat();
for (const desclist of descriptors) {
for (const desc of desclist) {
rootDfns.push(desc);
}
}
// Extract value dfns.
// Note some of the values can be namespaced "function" or "type" dfns, such
// as "<content-replacement>" in css-content-3:
// https://drafts.csswg.org/css-content-3/#typedef-content-content-replacement
const values = extractDfns({
selector: ['dfn[data-dfn-type=value][data-dfn-for]:not([data-dfn-for=""])',
'dfn[data-dfn-type=function][data-dfn-for]:not([data-dfn-for=""])',
'dfn[data-dfn-type=type][data-dfn-for]:not([data-dfn-for=""])',
'dfn[data-dfn-type=selector][data-dfn-for]:not([data-dfn-for=""])'
].join(','),
extractor: extractTypedDfn,
duplicates: 'push',
keepDfnType: true,
warnings
}).flat();
const matchName = (name, { approx = false } = {}) => dfn => {
let res = dfn.name === name;
if (!res && name.match(/^@.+\/.+$/)) {
// Value reference might be for an at-rule descriptor:
// https://tabatkins.github.io/bikeshed/#dfn-for
const parts = name.split('/');
res = dfn.name === parts[1] && dfn.for === parts[0];
}
if (!res && approx) {
res = `<${dfn.name}>` === name;
}
return res;
};
// Extract production rules from pre.prod contructs
// and complete result structure accordingly
const rules = extractProductionRules(document);
for (const rule of rules) {
const dfn = rootDfns.find(matchName(rule.name)) ??
rootDfns.find(matchName(rule.name, { approx: true }));
if (dfn) {
dfn.value = rule.value;
}
else {
let matchingValues = values.filter(matchName(rule.name));
if (matchingValues.length === 0) {
matchingValues = values.filter(matchName(rule.name, { approx: true }));
}
for (const matchingValue of matchingValues) {
matchingValue.value = rule.value;
}
if (matchingValues.length === 0) {
// Dangling production rule. That should never happen for properties,
// at-rules, descriptors and functions, since they should always be
// defined somewhere. That happens from time to time for types that are
// described in prose and that don't have a dfn. One could perhaps argue
// that these constructs ought to have a dfn too.
if (!res.warnings) {
res.warnings = []
}
const warning = Object.assign({ msg: 'Missing definition' }, rule);
warnings.push(warning);
rootDfns.push(warning);
}
}
}
// We now need to associate values with dfns. CSS specs tend to list in
// "data-dfn-for" attributes of values the construct to which the value
// applies directly but also the constructs to which the value indirectly
// applies. For instance, "open-quote" in css-content-3 directly applies to
// "<quote>" and indirectly applies to "<content-list>" (which has "<quote>"
// in its value syntax) and to "content" (which has "<content-list>" in its
// value syntax), and all 3 appear in the "data-dfn-for" attribute:
// https://drafts.csswg.org/css-content-3/#valdef-content-open-quote
//
// To make it easier to make sense of the extracted data and avoid duplicates
// in the resulting structure, the goal is to only keep the constructs to
// which the value applies directly. In the previous example, the goal is to
// list "open-quote" under "<quote>" but not under "<content-list>" and
// "<content>".
// Start by looking at values that are "for" something. Their list of parents
// appears in the "data-dfn-for" attribute of their definition.
const parents = {};
for (const value of values) {
if (!parents[value.name]) {
parents[value.name] = [];
}
parents[value.name].push(...value.for.split(',').map(ref => ref.trim()));
}
// Then look at non-namespaced types and functions. Their list of parents
// are all the definitions whose values reference them (for instance,
// "<quote>" has "<content-list>" as parent because "<content-list>" has
// "<quote>" in its value syntax).
for (const type of res.values) {
if (!parents[type.name]) {
parents[type.name] = [];
}
for (const value of values) {
if (value.value?.includes(type.name)) {
parents[type.name].push(value.name);
}
}
for (const dfn of rootDfns) {
if (dfn.value?.includes(type.name)) {
parents[type.name].push(dfn.name);
}
}
}
// Helper functions to reason on the parents index we just created.
// Note there may be cycles. For instance, in CSS Images 4, <image> references
// <image-set()>, which references <image-set-option>, which references
// <image> again:
// https://drafts.csswg.org/css-images-4/#typedef-image
const isAncestorOf = (ancestor, child) => {
const checkChild = (c, depth) =>
(depth++ < 10) &&
(c === ancestor || parents[c]?.find(p => checkChild(p, depth)));
return checkChild(child, 0);
};
const isDeepestConstruct = (name, list) => {
return list.every(p => p === name || !isAncestorOf(name, p));
}
// We may now associate values with dfns
for (const value of values) {
value.for.split(',')
.map(ref => ref.trim())
.filter((ref, _, arr) => isDeepestConstruct(ref, arr))
.forEach(ref => {
// Look for the referenced definition in root dfns
const dfn = rootDfns.find(matchName(ref)) ??
rootDfns.find(matchName(ref, { approx: true }));
if (dfn) {
if (!dfn.values) {
dfn.values = [];
}
dfn.values.push(value);
}
else {
// If the referenced definition is not in root dfns, look in
// namespaced dfns as functions/types are sometimes namespaced to a
// property, and values may reference these functions/types.
let referencedValues = values.filter(matchName(ref));
if (referencedValues.length === 0) {
referencedValues = values.filter(matchName(ref, { approx: true }));
}
for (const referencedValue of referencedValues) {
if (!referencedValue.values) {
referencedValue.values = [];
}
referencedValue.values.push(value);
}
if (referencedValues.length === 0) {
warnings.push(Object.assign(
{ msg: 'Dangling value' },
value,
{ for: ref }
));
}
}
});
}
// Don't keep the info on whether value comes from a pure syntax section
for (const dfn of rootDfns) {
delete dfn.pureSyntax;
}
for (const value of values) {
delete value.for;
delete value.pureSyntax;
}
// Report warnings
if (warnings.length > 0) {
res.warnings = warnings;
}
return res;
}
/**
* Normalize value definitions extracted from specs
*
* In particular, replace minus characters that may appear in values:
* https://github.com/tabatkins/bikeshed/issues/2308
*
* @param {String} value Value to normalize
* @return {String} Normalized value
*/
const normalize = value => value.trim().replace(/\s+/g, ' ').replace(/−/g, '-');
/**
* Converts a definition label as it appears in a CSS spec to a lower camel
* case property name.
*
* @param {String} label Definition label
* @return {String} lower camel case property name for the label
*/
const dfnLabel2Property = label => label.trim()
.replace(/:/, '')
.split(' ')
.map((str, idx) => (idx === 0) ?
str.toLowerCase() :
str.charAt(0).toUpperCase() + str.slice(1))
.join('')
// Spec may use singular when there is only one new value
// (e.g. new value of "text-transform" in MathML Core)
.replace(/^newValue$/, 'newValues');
/**
* Selector to use to exclude inner blocks that list tests, references and/or
* link to implementation statuses, which would provide too much detailed info
* in prose content.
*/
const asideSelector = 'aside, .mdn-anno, .wpt-tests-block';
/**
* Extract a CSS definition from a table
*
* All recent CSS specs should follow that pattern
*/
const extractTableDfn = table => {
let res = {};
const lines = [...table.querySelectorAll('tr')]
.map(line => {
const cleanedLine = line.cloneNode(true);
const annotations = cleanedLine.querySelectorAll(asideSelector);
annotations.forEach(n => n.remove());
return {
name: dfnLabel2Property(cleanedLine.querySelector(':first-child').textContent),
value: normalize(cleanedLine.querySelector('td:last-child').textContent)
};
});
for (let prop of lines) {
res[prop.name] = prop.value;
}
return res;
};
/**
* Extract a CSS definition from a dl list
*
* Used in "old" CSS specs
*/
const extractDlDfn = dl => {
let res = {};
res.name = dl.querySelector('dt').textContent.replace(/'/g, '').trim();
const lines = [...dl.querySelectorAll('dd table tr')]
.map(line => Object.assign({
name: dfnLabel2Property(line.querySelector(':first-child').textContent),
value: normalize(line.querySelector('td:last-child').textContent)
}));
for (let prop of lines) {
res[prop.name] = prop.value;
}
return res;
};
/**
* Merges CSS definitions for the same property into one
*
* The function runs a few simple sanity checks on the definitions. More checks
* would be needed to fully validate that merging is fine.
*
* The function returns null if CSS definitions cannot be merged.
*/
const mergeDfns = (dfn1, dfn2) => {
// Definitions should be about the same property
if (dfn1.name !== dfn2.name) {
return null;
}
// There should never be two base definitions for the same CSS property
if (dfn1.value && dfn2.value) {
return null;
}
const [baseDfn, partialDfn] = dfn2.value ? [dfn2, dfn1] : [dfn1, dfn2];
if ((!baseDfn.value && !baseDfn.newValues) ||
!partialDfn.newValues ||
(partialDfn.initial && partialDfn.initial !== baseDfn.initial)) {
return null;
}
const merged = Object.assign(baseDfn);
if (merged.value) {
merged.value += ` | ${normalize(partialDfn.newValues)}`;
}
else {
merged.newValues += ` | ${normalize(partialDfn.newValues)}`;
}
return merged;
};
/**
* Extract CSS definitions in a spec using the given CSS selector and extractor
*
* The "duplicates" option controls the behavior of the function when it
* encounters a duplicate (or similar) definition for the same thing. Values
* may be "reject" to report a warning, "merge" to merge the definitions, and
* "push" to keep both definitions separated.
*
* Merge issues and unexpected duplicates get reported as warnings in the
* "warnings" array passed as parameter.
*/
const extractDfns = ({ root = document,
selector,
extractor,
duplicates = 'reject',
mayReturnMultipleDfns = false,
keepDfnType = false,
warnings = [] }) => {
const res = [];
[...root.querySelectorAll(selector)]
.filter(el => !el.closest(informativeSelector))
.filter(el => !el.querySelector('ins, del'))
.map(extractor)
.filter(dfn => !!dfn?.name)
.map(dfn => !mayReturnMultipleDfns ? [dfn] :
dfn.name.split(',').map(name => Object.assign({}, dfn, { name: name.trim() })))
.reduce((acc, val) => acc.concat(val), [])
.forEach(dfn => {
if (dfn.type && !keepDfnType) {
delete dfn.type;
}
const idx = res.findIndex(e => e.name === dfn.name);
if (idx >= 0) {
switch (duplicates) {
case 'merge':
const merged = mergeDfns(res[idx], dfn);
if (merged) {
res[idx] = merged;
}
else {
warnings.push(Object.assign(
{ msg: 'Unmergeable definition' },
dfn
));
}
break;
case 'push':
res[idx].push(dfn);
default:
warnings.push(Object.assign(
{ msg: 'Duplicate definition' },
dfn
));
}
}
else {
res.push(duplicates !== 'push' ? dfn : [dfn]);
}
});
return res;
};
/**
* Regular expression used to split production rules:
* Split on the space that precedes a term immediately before an equal sign
* that is not wrapped in quotes (an equal sign wrapped in quotes is part of
* actual value syntax)
*/
const reSplitRules = /\s(?=[^\s]+?\s*?=[^'])/;
/**
* Helper function to parse a production rule. The "pureSyntax" parameter
* should be set to indicate that the rule comes from a pure syntactic block
* and should have precedence over another value definition that may be
* extracted from the prose. For instance, this makes it possible to extract
* `<abs()> = abs( <calc-sum> )` from the syntax part in CSS Values instead
* of `<abs()> = abs(A)` which is how the function is defined in prose.
*/
const parseProductionRule = (rule, { res = [], pureSyntax = false }) => {
const nameAndValue = rule
.replace(/\/\*[^]*?\*\//gm, '') // Drop comments
.split(/\s?=\s/)
.map(s => s.trim().replace(/\s+/g, ' '));
const name = nameAndValue[0];
const value = nameAndValue[1];
const normalizedValue = normalize(value);
let entry = res.find(e => e.name === name);
if (!entry) {
entry = { name };
res.push(entry);
}
if (!entry.value || (pureSyntax && !entry.pureSyntax)) {
entry.value = normalizedValue;
entry.pureSyntax = pureSyntax;
}
else if (entry.value !== normalizedValue) {
// Second definition found. Typically happens for the statement and
// block @layer definitions in css-cascade-5. We'll combine the values
// as alternative.
entry.value += ` | ${normalizedValue}`;
}
return entry;
};
/**
* Extract the definition name. If multiple linking texts are possible, the
* function will select the one that is a "syntax" definition. For instance, it
* will pick up "<identifier>" for "identifiers|<identifier>" in CSS 2:
* https://drafts.csswg.org/css2/#value-def-identifier
*
* The function throws if it cannot extract an obvious name from the definition.
* We may want to be smarter about that in the future, but this should really
* never happen in practice (the above "identifiers" example is the only one
* CSS value definition so far to have multiple linking texts)
*/
const getDfnName = dfn => {
if (dfn.getAttribute('data-lt')) {
const names = dfn.getAttribute('data-lt').split('|').map(normalize);
let name = names.find(n =>
n.startsWith('<') || // Looks like a "type"
n.startsWith('@') || // Looks like an "at-rule"
n.startsWith(':') || // Looks like a "descriptor"
n.endsWith('()')); // Looks like a "function"
if (!name) {
// No specific type found in the list, look for the actual term
name = names.find(n => n === dfn.textContent.trim());
}
if (!name) {
if (names.length > 1) {
throw new Error(`Found multiple linking texts for dfn without any obvious one: ${names.join(', ')}`);
}
name = names[0];
}
return name;
}
else {
return dfn.textContent.trim();
}
};
/**
* Extract the given dfn
*/
const extractTypedDfn = dfn => {
let res = {};
const arr = [];
const dfnType = dfn.getAttribute('data-dfn-type');
const dfnFor = dfn.getAttribute('data-dfn-for');
const parent = dfn.parentNode.cloneNode(true);
const fnRegExp = /^([:a-zA-Z_][:a-zA-Z0-9_\-]+)\([^\)]+\)$/;
// Remove note references as in:
// https://drafts.csswg.org/css-syntax-3/#the-anb-type
// and remove MDN annotations as well
[...parent.querySelectorAll('sup')]
.map(sup => sup.parentNode.removeChild(sup));
[...parent.querySelectorAll(asideSelector)]
.map(annotation => annotation.parentNode.removeChild(annotation));
const text = parent.textContent.trim();
if (text.match(/\s?=\s/)) {
// Definition appears in a "prod = foo" text, that's all good
// ... except in css-easing-2 draft where text also contains another
// production rule as a child of the first one:
// https://drafts.csswg.org/css-easing-2/#typedef-step-easing-function
const prod = text.split(reSplitRules)
.find(p => p.trim().startsWith(dfn.textContent.trim()));
if (dfn.closest('pre')) {
// Don't attempt to parse pre tags at this stage, they are tricky to
// split, we'll parse them as text and map them to the right definitions
// afterwards.
// That said, we may be looking at a function definition on the right hand
// side of a production rule, as in the definition of "linear()" in
// css-easing-2: https://drafts.csswg.org/css-easing-2/#funcdef-linear
// In such a case, we still want to extract the function parameters
if (dfn.textContent.trim().match(fnRegExp)) {
const fn = dfn.textContent.trim().match(fnRegExp)[1];
res = parseProductionRule(`${fn}() = ${dfn.textContent.trim()}`, { pureSyntax: false });
}
else {
res = { name: getDfnName(dfn) };
}
}
else if (prod) {
res = parseProductionRule(prod, { pureSyntax: true });
}
else {
// "=" may appear in another formula in the body of the text, as in:
// https://drafts.csswg.org/css-speech-1/#typedef-voice-volume-decibel
// It may be worth checking but not an error per se.
console.warn('[reffy]', `Found "=" next to definition of ${dfn.textContent.trim()} but no production rule. Did I miss something?`);
res = { name: getDfnName(dfn), prose: text.replace(/\s+/g, ' ') };
}
}
else if (dfn.textContent.trim().match(fnRegExp)) {
// Definition is "prod(foo bar)", create a "prod() = prod(foo bar)" entry
const fn = dfn.textContent.trim().match(fnRegExp)[1];
res = parseProductionRule(`${fn}() = ${dfn.textContent.trim()}`, { pureSyntax: false });
}
else if (parent.nodeName === 'DT') {
// Definition is in a <dt>, look for value in following <dd>
let dd = dfn.parentNode;
while (dd && (dd.nodeName !== 'DD')) {
dd = dd.nextSibling;
}
if (!dd) {
return;
}
const code = dd.querySelector('code.prod, pre.prod');
if (code && !code.closest(informativeSelector)) {
if (code.textContent.startsWith(`${text} = `) ||
code.textContent.startsWith(`<${text}> = `)) {
res = parseProductionRule(code.textContent, { pureSyntax: true });
}
else {
res = parseProductionRule(`${text} = ${code.textContent}`, { pureSyntax: false });
}
}
else {
// Remove notes, details sections that link to tests, and subsections
// that go too much into details
dd = dd.cloneNode(true);
[...dd.children].forEach(c => {
if (c.tagName === 'DETAILS' ||
c.tagName === 'DL' ||
c.classList.contains('note')) {
c.remove();
}
});
[...dd.querySelectorAll('sup')]
.map(sup => sup.parentNode.removeChild(sup));
[...dd.querySelectorAll(asideSelector)]
.map(annotation => annotation.parentNode.removeChild(annotation));
res = {
name: getDfnName(dfn),
prose: dd.textContent.trim().replace(/\s+/g, ' ')
};
}
}
else if (parent.nodeName === 'P') {
// Definition is in regular prose, extract value from prose.
res = {
name: getDfnName(dfn),
prose: parent.textContent.trim().replace(/\s+/g, ' ')
};
}
else {
// Definition is in a heading or a more complex structure, just list the
// name for now.
res = { name: getDfnName(dfn) };
}
res.type = dfnType;
if (dfnType === 'value') {
res.value = normalize(res.name);
}
if (dfnFor) {
res.for = dfnFor;
}
return res;
};
/**
* Extract production rules defined in the specification in "<pre>" tags and
* complete the result structure received as parameter accordingly.
*/
const extractProductionRules = root => {
// For <pre> tags that don't have a "prod" class (e.g. in HTML and
// css-namespaces), make sure they contain a <dfn> with a valid CSS
// data-dfn-type attribute to avoid parsing things that are not production
// rules. In all cases, make sure we're not in a changelog with details as in:
// https://drafts.csswg.org/css-backgrounds-3/#changes-2017-10
const rules = [];
[...root.querySelectorAll('pre.prod:not(:has(del)):not(:has(ins))')]
.concat([...root.querySelectorAll('pre:not(.idl):not(:has(.idl)):not(:has(del)):not(:has(ins))')]
.filter(el => el.querySelector([
'dfn[data-dfn-type=at-rule]',
'dfn[data-dfn-type=selector]',
'dfn[data-dfn-type=value]',
'dfn[data-dfn-type=function]',
'dfn[data-dfn-type=type]'
].join(','))))
.filter(el => !el.closest(informativeSelector))
.map(el => el.cloneNode(true))
.map(el => {
[...el.querySelectorAll(asideSelector)]
.map(aside => aside.parentNode.removeChild(aside));
return el;
})
.map(el => {
[...el.querySelectorAll('sup')]
.map(sup => sup.parentNode.removeChild(sup));
return el;
})
.map(el => el.textContent)
.map(val => val.replace(/\/\*[^]*?\*\//gm, '')) // Drop comments
.map(val => val.split(reSplitRules)) // Separate definitions
.flat()
.map(text => text.trim())
.forEach(text => {
if (text.match(/\s?=\s/)) {
parseProductionRule(text, { res: rules, pureSyntax: true });
}
else if (text.startsWith('@')) {
const name = text.split(' ')[0];
parseProductionRule(`${name} = ${text}`, { res: rules, pureSyntax: true });
}
});
return rules;
}