-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathwattsi2bikeshed.js
More file actions
533 lines (471 loc) · 17.7 KB
/
wattsi2bikeshed.js
File metadata and controls
533 lines (471 loc) · 17.7 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
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
import { JSDOM } from "jsdom";
import { readFileSync, writeFileSync } from "node:fs";
const boilerplate = `<pre class=metadata>
Group: WHATWG
H1: HTML
Shortname: html
Text Macro: TWITTER htmlstandard
Text Macro: LATESTRD 2025-01
Abstract: HTML is Bikeshed.
Indent: 1
Markup Shorthands: css off, markdown-block off
Complain About: accidental-2119 off, missing-example-ids off
Include MDN Panels: false
</pre>`;
const kCrossRefAttribute = 'data-x';
function replaceWithChildren(elem) {
while (elem.firstChild) {
elem.parentNode.insertBefore(elem.firstChild, elem);
}
elem.remove();
}
function isElement(node) {
return node?.nodeType === 1;
}
function isText(node) {
return node?.nodeType === 3;
}
const markup = /[\[\]{}<>&]/g;
function hasMarkup(text) {
return markup.test(text);
}
// Get the "topic" for cross-references like Wattsi:
// https://github.com/whatwg/wattsi/blob/b9c28036a2a174f7f87315164f001120596a95f1/src/wattsi.pas#L882-L894
function getTopic(elem) {
let result;
while (true) {
if (elem.hasAttribute(kCrossRefAttribute)) {
result = elem.getAttribute(kCrossRefAttribute);
break;
} else if (isElement(elem.firstChild) && elem.firstChild === elem.lastChild) {
elem = elem.firstChild;
continue;
} else {
result = elem.textContent;
break;
}
}
// This matches Wattsi's MungeStringToTopic in spirit,
// but perhaps not in every detail:
return result
.replaceAll('#', '')
.replaceAll(/\s+/g, ' ')
.toLowerCase()
.trim();
}
// Convert a topic to an ID like Wattsi:
// https://github.com/whatwg/wattsi/blob/b9c28036a2a174f7f87315164f001120596a95f1/src/wattsi.pas#L786-L832
function getId(topic) {
// Note: no toLowerCase() because this is already done in getTopic().
return topic
.replaceAll(/["?`]/g, '')
.replaceAll(/[\s<>\[\\\]^{|}%]+/g, ' ').trim()
.replaceAll(' ', '-');
}
// Get the linking text like Bikeshed:
// https://github.com/speced/bikeshed/blob/50d0ec772915adcd5cec0c2989a27fa761d70e71/bikeshed/h/dom.py#L174-L201
//
// Also approximate the additional munging Bikeshed does here:
// https://github.com/speced/bikeshed/blob/f3fd50cc3a67ecbffb562b16252237aeaa2b4eae/bikeshed/refs/manager.py#L291-L297
function getBikeshedLinkTextSet(elem) {
const texts = new Set();
const dataLt = elem.getAttribute('data-lt');
if (dataLt === '') {
return texts;
}
function add(lt) {
lt = lt.trim().replaceAll(/\s+/g, ' ');
// These are the extra bits from addLocalDfns in Bikeshed:
lt = lt.replaceAll("’", "'");
// TODO: line-ending em dashes or -- (if they exist in HTML)
// TODO: only lowercase dfn types that Bikeshed would if lowercasing
// everything results in collisions that Bikeshed doesn't have.
lt = lt.toLowerCase();
texts.add(lt);
}
if (dataLt) {
// TODO: what's the `rawText in ["|", "||", "|||"]` condition for?
dataLt.split('|').map(add);
} else {
switch (elem.localName) {
case 'dfn':
case 'a':
add(elem.textContent);
break;
case 'h2':
case 'h3':
case 'h4':
case 'h5':
case 'h6':
add((elem.querySelector('.content') ?? elem).textContent);
break;
}
}
const dataLocalLt = elem.getAttribute('data-local-lt');
if (dataLocalLt) {
if (dataLocalLt.includes('|')) {
console.warn('Ignoring data-local-lt value containing |:', dataLocalLt);
} else {
add(dataLocalLt);
}
}
return texts;
}
// Get the *first* linking text like Bikeshed:
// https://github.com/speced/bikeshed/blob/50d0ec772915adcd5cec0c2989a27fa761d70e71/bikeshed/h/dom.py#L215-L220
function getBikeshedLinkText(elem) {
for (const text of getBikeshedLinkTextSet(elem)) {
return text;
}
return null;
}
// Add for and lt to ensure that Bikeshed will link the <a> to the right <dfn>.
function ensureLink(a, dfn, dfnLtCounts) {
if (dfn.hasAttribute('for')) {
a.setAttribute('for', dfn.getAttribute('for'));
// TODO: don't add when it's already unambiguous.
}
const dfnLts = getBikeshedLinkTextSet(dfn);
if (dfnLts.size === 0) {
console.warn('No linking text for', dfn.outerHTML);
return;
}
const aLt = getBikeshedLinkText(a);
if (!aLt) {
console.warn('No linking text for', a.outerHTML);
return;
}
if (a.hasAttribute('for')) {
// TODO: look in dfnLts when that tracks <dfn for>
return;
}
for (const lt of dfnLts) {
if (dfnLtCounts.get(lt) === 1) {
// This is a unique linking text.
// Note: data-lt is rewritten to lt later. It would also work to remove
// any data-lt attribute here and just add lt.
a.setAttribute('data-lt', lt);
return;
}
}
if (!dfn.hasAttribute('data-local-lt')) {
if (!dfn.id) {
console.warn('No id for dfn', dfn.outerHTML);
return;
}
// Use a prefix to make the linking text unique. The prefix is "xxx-""
// because class="XXX" is used as a FIXME/TODO in HTML, and these
// local-lt attributes should be removed over time.
dfn.setAttribute('data-local-lt', `xxx-${dfn.id}`);
}
a.setAttribute('data-lt', dfn.getAttribute('data-local-lt'));
}
function convert(infile, outfile) {
const source = readFileSync(infile, 'utf-8');
const dom = new JSDOM(source);
const document = dom.window.document;
document.body.prepend(JSDOM.fragment(boilerplate));
for (const dt of document.querySelectorAll('#ref-list dt')) {
const node = dt.firstChild;
if (isText(node) && node.data.startsWith('[')) {
node.data = '\\' + node.data;
}
}
// Convert w-nodev and similar attributes to include-if/exclude-if using the
// appropriate status for each. Wattsi handling is here:
// https://github.com/whatwg/wattsi/blob/b9c28036a2a174f7f87315164f001120596a95f1/src/wattsi.pas#L735-L759
// TODO: w-nosplit, which doesn't correspond to a status.
const conditions = [
['html', 'LS'],
['dev', 'LS-DEV'],
['snap', 'LS-COMMIT'],
['review', 'whatwg/RD'],
];
for (const elem of document.querySelectorAll("*")) {
const includeIf = [];
const excludeIf = [];
for (const [cond, status] of conditions) {
if (elem.hasAttribute(`w-${cond}`)) {
includeIf.push(status);
elem.removeAttribute(`w-${cond}`);
}
if (elem.hasAttribute(`w-no${cond}`)) {
excludeIf.push(status);
elem.removeAttribute(`w-no${cond}`);
}
}
if (includeIf.length) {
elem.setAttribute('include-if', includeIf.join(', '));
}
if (excludeIf.length) {
elem.setAttribute('exclude-if', excludeIf.join(', '));
}
}
// Scan all definitions
const crossRefs = new Map(); // map from Wattsi topic to <dfn>
const dfnLtCounts = new Map(); // map from Bikeshed link text to number of uses in <dfn>
for (const dfn of document.querySelectorAll('dfn')) {
const topic = getTopic(dfn);
if (topic === '') {
// This isn't a linkable definition and Wattsi outputs a plain <dfn>
// with no attributes. The closest thing in Bikeshed is a definition
// with no linking text that is not exported.
dfn.setAttribute('data-lt', '');
dfn.setAttribute('noexport', '');
continue;
}
if (crossRefs.has(topic)) {
console.warn('Duplicate <dfn> topic:', topic);
}
crossRefs.set(topic, dfn);
if (!dfn.hasAttribute('id')) {
// TODO: avoid if Bikeshed would generate the same ID
dfn.setAttribute('id', getId(topic));
}
const lts = getBikeshedLinkTextSet(dfn);
// Remove "new" from the linking text of constructors.
if (dfn.hasAttribute('constructor') && !dfn.hasAttribute('data-lt')) {
for (const lt of lts) {
if (lt.startsWith('new ')) {
dfn.setAttribute('data-lt', lt.substring(4));
break;
}
}
}
// Remove leading "document." from linking text of document.write/writeln.
if (dfn.hasAttribute('method') && dfn.getAttribute('for') === 'Document' &&
!dfn.hasAttribute('data-lt')) {
for (const lt of lts) {
if (lt.startsWith('document.')) {
dfn.setAttribute('data-lt', lt.substring(9));
break;
}
}
}
// Count uses of each Bikeshed linking text
if (dfn.hasAttribute('for')) {
// TODO: track <dfn for> as well
continue;
}
for (const lt of lts) {
const count = (dfnLtCounts.get(lt) ?? 0) + 1
dfnLtCounts.set(lt, count);
}
}
// Track used <dfn>s in order to identify the unused ones.
const usedDfns = new Set();
// Replace <span> with the inner <code> or a new <a>.
// TODO: align more closely with Wattsi:
// https://github.com/whatwg/wattsi/blob/b9c28036a2a174f7f87315164f001120596a95f1/src/wattsi.pas#L1454-L1487
const spans = document.querySelectorAll('span');
for (const [i, span] of Object.entries(spans)) {
// Don't touch any span with a descendent span.
if (span.contains(spans[+i + 1])) {
// TODO: vet for weird cases that need fixing
continue;
}
// Leave dev/nodev alone here.
if (span.hasAttribute('w-dev') || span.hasAttribute('w-nodev')) {
continue;
}
// Leave <span> in SVG alone.
if (span.hasAttribute('xmlns')) {
continue;
}
// Empty data-x="" means it's not a link.
if (span.getAttribute(kCrossRefAttribute) === '') {
continue;
}
// An empty span with an ID is used to preserve old IDs.
// TODO: hoist to oldids attribute for Bikeshed
if (span.hasAttribute('id') && span.firstChild === null) {
continue;
}
const topic = getTopic(span);
const dfn = crossRefs.get(topic);
if (!dfn) {
// TODO: vet these cases for any that should actually be linked
// console.log(span.outerHTML);
continue;
}
if (span.hasAttribute('subdfn')) {
// TODO: generate an ID based on the linked term, like Wattsi:
// https://github.com/whatwg/wattsi/blob/b9c28036a2a174f7f87315164f001120596a95f1/src/wattsi.pas#L943-L961
span.removeAttribute('subdfn');
}
// For <span><code>foo</code></span> and <span>"<code>SyntaxError</code>"</span>,
// drop the outer <span> and depend on the <code> linking logic. Note that this
// excludes the surrounding quotes from the link text, which is a minor change.
// The <code> element is further transformed in a following step.
function isQuote(node) {
return false; // <- hack to disable unwrapping
return isText(node) && node.data === '"';
}
const code = span.querySelector('code');
if (code && (
// <code> is the single child
(span.childNodes.length === 1 && span.firstChild === code) ||
// <code> has surrounding " text nodes
(span.childNodes.length === 3 &&
isQuote(span.firstChild) && isQuote(span.lastChild) &&
span.firstChild.nextSibling === code))) {
if (span.hasAttributes()) {
console.warn('Discarding <span> attributes:', span.outerHTML);
}
replaceWithChildren(span);
continue;
}
// Output a <a> instead of <span>.
const a = document.createElement('a');
for (const name of span.getAttributeNames()) {
const value = span.getAttribute(name);
switch (name) {
case 'data-x':
case 'exclude-if':
case 'include-if':
break;
case 'data-lt':
case 'id':
// Copy over.
a.setAttribute(name, value);
break;
default:
console.warn('Unhandled <span> attribute:', name);
}
}
// Move the <span> children over to <a>.
while (span.firstChild) {
a.appendChild(span.firstChild);
}
span.replaceWith(a);
ensureLink(a, dfn, dfnLtCounts);
usedDfns.add(dfn);
}
// Wrap <i data-x="..."> with <a>. Wattsi handling is here:
// https://github.com/whatwg/wattsi/blob/b9c28036a2a174f7f87315164f001120596a95f1/src/wattsi.pas#L1454-L1487
for (const i of document.querySelectorAll('i[data-x]')) {
if (i.closest('dfn')) {
continue;
}
const topic = getTopic(i);
const dfn = crossRefs.get(topic);
if (!dfn) {
continue;
// TODO: vet these cases for any that should actually be linked
// console.log(i.outerHTML);
}
const a = document.createElement('a');
i.parentNode.insertBefore(a, i);
a.appendChild(i);
ensureLink(a, dfn, dfnLtCounts);
usedDfns.add(dfn);
}
for (const code of document.querySelectorAll('pre > code')) {
const pre = code.parentNode;
if (pre.firstChild !== code || pre.lastChild !== code) {
console.warn('Skipping a <pre><code> with sibling nodes: ' + pre.outerHTML);
continue;
}
if (code.hasAttribute('class')) {
switch (code.className) {
case 'idl':
pre.className = 'idl';
break;
case 'js':
pre.className = 'lang-javascript';
break;
case 'abnf':
case 'css':
case 'html':
case 'json':
pre.className = `lang-${code.className}`;
break;
default:
console.warn('Unhandled <pre><code> class:', code.className);
}
}
// Strip any markup in the code.
// TODO: Indent as much as the <pre> element is indented. This is the
// style in other WHATWG specs using Bikeshed.
pre.textContent = '\n' + pre.textContent.trim() + '\n';
}
// Link <code> to the right thing.
for (const code of document.querySelectorAll('code')) {
// <code undefined> shouldn't be linked.
if (code.hasAttribute('undefined')) {
code.removeAttribute('undefined');
continue;
}
// <code> inside <a> or <dfn> should be left untouched.
if (code.closest('a, dfn')) {
continue;
}
const topic = getTopic(code);
if (topic === '') {
continue;
}
const dfn = crossRefs.get(topic);
if (!dfn) {
console.warn('No <dfn> found for topic:', topic);
continue;
}
if (code.hasAttribute('subdfn')) {
// TODO: generate an ID based on the linked term, like Wattsi:
// https://github.com/whatwg/wattsi/blob/b9c28036a2a174f7f87315164f001120596a95f1/src/wattsi.pas#L943-L961
code.removeAttribute('subdfn');
}
const a = document.createElement('a');
for (const name of code.getAttributeNames()) {
a.setAttribute(name, code.getAttribute(name));
code.removeAttribute(name);
}
code.replaceWith(a);
a.appendChild(code);
ensureLink(a, dfn, dfnLtCounts);
usedDfns.add(dfn);
}
// Rewrite data-lt to lt and data-local-lt to local-lt.
for (const elem of document.querySelectorAll('[data-lt]')) {
elem.setAttribute('lt', elem.getAttribute('data-lt'));
elem.removeAttribute('data-lt');
}
for (const elem of document.querySelectorAll('[data-local-lt]')) {
elem.setAttribute('local-lt', elem.getAttribute('data-local-lt'));
elem.removeAttribute('data-local-lt');
}
for (const elem of document.querySelectorAll('[data-x]')) {
elem.removeAttribute(kCrossRefAttribute);
}
for (const elem of document.querySelectorAll('[data-x-href]')) {
// TODO
elem.removeAttribute('data-x-href');
}
// Add noexport to unused <dfn>s to silence Bikeshed warnings about them.
// TODO: vet for cases that are accidentally unused.
for (const dfn of crossRefs.values()) {
if (usedDfns.has(dfn)) {
continue;
}
// This <dfn> is unused by Wattsi rules.
if (dfn.hasAttribute('data-export') || dfn.hasAttribute('export')) {
continue;
}
dfn.setAttribute('noexport', '');
}
// Simplify <a> to Bikeshed autolinks.
for (const a of document.querySelectorAll('a')) {
break;
const hasSingleTextNode = isText(a.firstChild) && a.firstChild === a.lastChild;
if (hasSingleTextNode && !a.hasAttributes()) {
const text = a.firstChild.data;
if (!hasMarkup(text)) {
a.replaceWith(`[=${text}=]`);
}
}
// TODO: handle <a for>.
}
const output = document.body.innerHTML
.replaceAll('[[', '\\[[');
writeFileSync(outfile, output, 'utf-8');
}
convert(process.argv[2], process.argv[3]);