forked from microsoft/TypeScript-DOM-lib-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch-idl.ts
More file actions
265 lines (245 loc) · 6.8 KB
/
Copy pathfetch-idl.ts
File metadata and controls
265 lines (245 loc) · 6.8 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
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
import * as fs from "fs";
import * as path from "path";
import fetch from "node-fetch";
import { JSDOM } from "jsdom";
import innerText from "styleless-innertext";
fetchIDLs(process.argv.slice(2));
interface IDLSource {
url: string;
title: string;
deprecated?: boolean;
}
const idlSelector = [
"pre.idl:not(.extract):not(.example)", // bikeshed and ReSpec
"pre.code code.idl-code", // Web Cryptography
"pre:not(.extract) code.idl", // HTML
"#permission-registry + pre.highlight", // Permissions
].join(",");
const cssPropSelector = [
".propdef dfn", // CSS Fonts, SVG
".propdef-title", // SVG Paint Servers
"dfn.css[data-dfn-type=property]",
].join(",");
async function fetchIDLs(filter: string[]) {
const idlSources = (
require("../inputfiles/idlSources.json") as IDLSource[]
).filter((source) => !filter.length || filter.includes(source.title));
await Promise.all(
idlSources.map(async (source) => {
const { idl, comments } = await fetchIDL(source);
fs.writeFileSync(
path.join(__dirname, `../inputfiles/idl/${source.title}.widl`),
idl + "\n"
);
if (comments) {
fs.writeFileSync(
path.join(
__dirname,
`../inputfiles/idl/${source.title}.commentmap.json`
),
comments + "\n"
);
}
})
);
}
async function fetchIDL(source: IDLSource) {
const response = await fetch(source.url);
if (source.url.endsWith(".idl")) {
return { idl: await response.text() };
}
const dom = JSDOM.fragment(await response.text());
let idl = extractIDL(dom);
const css = extractCSSDefinitions(dom);
if (css) {
idl = idl ? idl + `\n\n${css}` : css;
}
if (!idl) {
throw new Error(`Found no IDL or CSS from ${source.url}`);
}
const comments = processComments(dom);
return { idl, comments };
}
function extractIDL(dom: DocumentFragment) {
const elements = Array.from(dom.querySelectorAll(idlSelector)).filter(
(el) => {
if (el.parentElement && el.parentElement.classList.contains("example")) {
return false;
}
const previous = el.previousElementSibling;
if (!previous) {
return true;
}
return (
!previous.classList.contains("atrisk") &&
!previous.textContent!.includes("IDL Index")
);
}
);
elements.forEach((el) => {
el.querySelector("span.idlHeader")?.remove();
});
return elements
.map((element) => trimCommonIndentation(element.textContent!).trim())
.join("\n\n");
}
function extractCSSDefinitions(dom: DocumentFragment) {
const properties = Array.from(dom.querySelectorAll(cssPropSelector)).map(
(element) => element.textContent!.trim()
);
if (!properties.length) {
return "";
}
return `partial interface CSSStyleDeclaration {${properties
.map(
(property) =>
`\n [CEReactions] attribute [LegacyNullToEmptyString] CSSOMString ${hyphenToCamelCase(
property
)};`
)
.join("")}\n};`;
}
function hyphenToCamelCase(name: string) {
const camel = name
.replace(/^-(\w)/, (_, c) => c)
.replace(/-(\w)/g, (_, c) => c.toUpperCase());
return camel === "float" ? "_float" : camel;
}
function processComments(dom: DocumentFragment) {
const elements = [...dom.querySelectorAll("dl.domintro")];
if (!elements.length) {
return undefined;
}
const result: Record<string, string> = {};
for (const element of elements) {
for (const { dt, dd } of generateDescriptionPairs(element)) {
elements.push(...importNestedList(dd));
const comment = dd
.map((desc) => {
desc.normalize();
convertChildPre(desc);
return innerText(desc).replace(/’/g, "'");
})
.filter((text) => text)
.join("\n\n");
for (const key of dt.map((term) => getKey(term.innerHTML))) {
if (!key) {
continue;
}
const retargeted = retargetCommentKey(key, dom);
// prefer the first description
if (!result[retargeted]) {
result[retargeted] = comment;
}
}
}
}
if (!Object.keys(result).length) {
return undefined;
}
return JSON.stringify(result, undefined, 4);
}
function convertChildPre(e: Element) {
for (const pre of e.querySelectorAll("pre")) {
const code = pre.querySelector(":scope > code") as HTMLElement;
if (!code) {
continue;
}
const text = innerText(code, {
getComputedStyle() {
return { whiteSpace: "pre" } as CSSStyleDeclaration;
},
});
pre.textContent = "```\n" + text + "\n```";
}
}
function getKey(s: string) {
const keyRegexp = /#dom-([a-zA-Z0-9-_]+)/i;
const match = s.match(keyRegexp);
if (match) {
return match[1];
}
return undefined;
}
function* generateDescriptionPairs(domIntro: Element) {
const dt: HTMLElement[] = [];
const dd: HTMLElement[] = [];
let element = domIntro.firstElementChild;
while (element) {
switch (element.localName) {
case "dt":
if (dd.length) {
yield { dt: [...dt], dd: [...dd] };
dt.length = dd.length = 0;
}
dt.push(element as HTMLElement);
break;
case "dd":
dd.push(element as HTMLElement);
break;
}
element = element.nextElementSibling;
}
if (dd.length) {
yield { dt: [...dt], dd: [...dd] };
}
}
function* importNestedList(elements: Element[]) {
for (const element of elements) {
for (const dl of element.getElementsByTagName("dl")) {
dl.remove();
yield dl;
}
}
}
/**
* Specifications tends to keep existing keys even after a member relocation
* so that external links can be stable and won't broken.
*/
function retargetCommentKey(key: string, dom: DocumentFragment) {
const [parent, member] = key.split(/-/g);
if (!member) {
return parent;
}
const dfn = dom.getElementById(`dom-${key}`);
if (!dfn || !dfn.dataset.dfnFor) {
// The optional third word is for overloads and can be safely ignored.
return `${parent}-${member}`;
}
return `${dfn.dataset.dfnFor.toLowerCase()}-${member}`;
}
/**
* Remove common indentation:
* <pre>
* typedef Type = "type";
*
* dictionary Dictionary {
* "member"
* };
* </pre>
* Here the textContent has 6 common preceding whitespaces that can be unindented.
*/
function trimCommonIndentation(text: string) {
const lines = text.split("\n");
if (!lines[0].trim()) {
lines.shift();
}
if (!lines[lines.length - 1].trim()) {
lines.pop();
}
const commonIndentation = Math.min(
...lines.filter((line) => line.trim()).map(getIndentation)
);
return lines.map((line) => line.slice(commonIndentation)).join("\n");
}
/** Count preceding whitespaces */
function getIndentation(line: string) {
let count = 0;
for (const ch of line) {
if (ch !== " ") {
break;
}
count++;
}
return count;
}