This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
forked from speced/respec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webidl-index.js
64 lines (63 loc) · 2.03 KB
/
webidl-index.js
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
/**
* Module: core/webidl-index
* constructs a summary of WebIDL in the document by
* cloning all the generated contiguous WebIDL nodes and
* appending them to pre element.
*
* Usage
* Add a <section id="idl-index"> to the document.
* It also supports title elements to generate a header.
* Or if a header element is an immediate child, then
* that is preferred.
*/
export const name = "core/webidl-index";
export function run(conf, doc, cb) {
const idlIndexSec = doc.querySelector("section#idl-index");
if (!idlIndexSec) {
return cb();
}
// Query for decedents headings, e.g., "h2:first-child, etc.."
const query = [2, 3, 4, 5, 6]
.map(level => `h${level}:first-child`)
.join(",");
if (!idlIndexSec.querySelector(query)) {
const header = document.createElement("h2");
if (idlIndexSec.title) {
header.innerHTML = idlIndexSec.title;
idlIndexSec.removeAttribute("title");
} else {
header.innerHTML = "IDL Index";
}
idlIndexSec.insertAdjacentElement("afterbegin", header);
}
if (!document.querySelector("pre.idl")) {
const text = "This specification doesn't declare any Web IDL.";
const noIDLFound = document.createTextNode(text);
idlIndexSec.appendChild(noIDLFound);
return cb();
}
const virtualSummary = document.createDocumentFragment();
const pre = document.createElement("pre");
pre.classList.add("idl", "def");
Array
.from(document.querySelectorAll("pre.def.idl"))
.map(elem => {
const div = document.createElement("div");
const clone = elem.cloneNode(true).firstElementChild;
div.appendChild(clone);
div.appendChild(document.createTextNode("\n"))
div.classList.add("respec-idl-separator");
return div;
})
.reduce((collector, elem) => {
collector.appendChild(elem);
return collector;
}, pre);
// Remove duplicate IDs
Array
.from(pre.querySelectorAll("*[id]"))
.forEach(elem => elem.removeAttribute("id"));
virtualSummary.appendChild(pre);
idlIndexSec.appendChild(virtualSummary);
cb();
}