-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathgenerate-webref-sets.mjs
More file actions
213 lines (166 loc) · 5.53 KB
/
generate-webref-sets.mjs
File metadata and controls
213 lines (166 loc) · 5.53 KB
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
import fs from 'node:fs/promises';
import path from 'node:path';
import css from '@webref/css';
import { fork } from 'css-tree';
import { generate_atrule_set, generate_set } from './generate-set.mjs';
import { trim_at } from './trim-at.mjs';
function assign_new_definition(set, name, definition) {
const exists_in_set = set[name];
if (exists_in_set && exists_in_set !== definition) {
// eslint-disable-next-line no-console
console.error(`duplicate definition ${name}`);
}
set[name] = definition;
}
function assign_new_atrule_descriptor_definition(set, atrule, name, definition) {
const exists_in_set = set[atrule]?.descriptors?.[name];
if (exists_in_set) {
// eslint-disable-next-line no-console
console.error(`duplicate atrule descriptor @${atrule} ${name}`);
}
set[atrule] ??= {
descriptors: Object(null),
};
set[atrule].descriptors[name] = definition;
}
export async function generate_webref_sets() {
const atrules = Object(null);
const properties = Object(null);
const types = Object(null);
// Set missing definitions
{
types['attr-unit'] = JSON.parse(await fs.readFile(path.join('raw-data', 'units.json'))).join(' | ');
types['size-keyword'] = JSON.parse(await fs.readFile(path.join('raw-data', 'size-keywords.json'))).join(' | ');
// https://github.com/w3c/csswg-drafts/issues/11127
types['scroll-state-feature'] = '<ident> : <ident>';
// https://drafts.csswg.org/css-ui-4/#cursor
types['url-set-option'] = '[ <url> | <string> ] [ <resolution> || type( <string> ) ]?';
types['url-set()'] = 'url-set( <url-set-option># )';
types['font-src-list'] = '[ <url> [ format(<font-format>)]? [ tech( <font-tech>#)]? | local(<family-name>) ]#';
// https://drafts.csswg.org/pointer-animations-1/#animation-range-centering
types['timeline-range-center-subject'] = '<ident>';
// https://drafts.csswg.org/css-conditional-5/#typedef-supports-condition-name
types['supports-condition-name'] = '<custom-ident>';
// https://github.com/csstree/csstree/issues/300
types['whole-value'] = '<declaration-value>';
// https://github.com/w3c/csswg-drafts/issues/13092
types['anchored-feature'] = 'fallback: <\'position-try-fallbacks\'>';
// https://drafts.csswg.org/css-ui-4/
types['url-set'] = 'image-set( [ [ <url> | <string> ] [ <resolution> || type(<string>) ]? ]# )';
// https://drafts.csswg.org/css-conditional-5/
types['style-feature-value'] = '<declaration-value>';
// https://drafts.csswg.org/css-animations-2/#typedef-animation-action
types['animation-action'] = 'none | play | pause | play-forwards | play-backwards | pause | reset | replay';
}
const data = await css.listAll();
for (const atrule of data.atrules) {
if (!atrule.descriptors?.length) {
continue;
}
for (const descriptor of atrule.descriptors) {
if (descriptor.name.startsWith('-webkit-')) {
continue;
}
if (!descriptor.syntax) {
continue;
}
if (descriptor.type === 'discrete' || descriptor.type === 'range') {
// definitions for features in `@container` and `@media`
continue;
}
assign_new_atrule_descriptor_definition(
atrules,
trim_at(atrule.name),
descriptor.name,
descriptor.syntax,
);
}
}
for (const property of data.properties) {
if (property.name.startsWith('-webkit-')) {
continue;
}
if (property.name === 'clip') {
property.syntax = 'rect( <top> , <right> , <bottom> , <left> ) | rect( <top> <right> <bottom> <left> ) | auto';
}
// https://github.com/csstree/csstree/pull/354
if ((
property.name === 'overflow-x' ||
property.name === 'overflow-y' ||
property.name === 'overflow-inline' ||
property.name === 'overflow-block'
) && property.syntax === 'visible | hidden | clip | scroll | auto') {
property.syntax = '| <-non-standard-overflow>';
}
if (!property.syntax) {
continue;
}
assign_new_definition(
properties,
property.name,
property.syntax,
);
}
for (const t of data.types) {
if (!t.syntax) {
continue;
}
if (t.name === 'content-list' && t.syntax === '[ <string> | <counter()> | <counters()> | <content()> | <attr()> ]+') {
continue;
}
assign_new_definition(
types,
t.name,
t.syntax,
);
}
for (const f of data.functions) {
if (f.name === 'type()' && f.for.length === 1 && f.for[0] === 'image-set()') {
continue;
}
if (f.name === 'rect()' && f.for.length === 1 && f.for[0] === 'clip') {
continue;
}
if (f.name === 'fit-content()' && f.syntax === 'fit-content(<length-percentage [0,∞]>)') {
continue;
}
if (f.name === 'scale()' && f.syntax === 'scale( <number> , <number>? )') {
continue;
}
if (f.name === 'scaleX()' && f.syntax === 'scaleX( <number> )') {
continue;
}
if (f.name === 'scaleY()' && f.syntax === 'scaleY( <number> )') {
continue;
}
if (!f.syntax) {
continue;
}
assign_new_definition(
types,
f.name,
f.syntax,
);
}
const forkedLexer = fork({
atrules: atrules,
properties: properties,
types: types,
}).lexer;
const atrules_set = generate_atrule_set(forkedLexer.atrules);
const properties_set = generate_set(forkedLexer.properties);
const types_set = generate_set(forkedLexer.types);
// https://drafts.csswg.org/css-conditional-5/
types_set['style-feature-name'] = {
syntax: '<dashed-ident> | ' + Object.keys(properties_set).sort().filter((x) => x !== '--*').join(' | '),
};
// https://github.com/csstree/csstree/issues/292
types_set['ray()'] = {
syntax: 'ray( [ <angle> && <ray-size>? && contain? && [ at <position> ]? ] )',
};
return {
atrules: atrules_set,
properties: properties_set,
types: types_set,
};
}