UNPKG

1.79 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/*@internal*/
4class Toc {
5 constructor(spec) {
6 this.spec = spec;
7 }
8 build() {
9 if (this.spec.subclauses.length === 0) {
10 return;
11 }
12 const html = Toc.build(this.spec);
13 const tocContainer = this.spec.doc.createElement('div');
14 tocContainer.innerHTML = '<h2>Table of Contents</h2>' + html;
15 const intro = this.spec.doc.querySelector('emu-intro, emu-clause, emu-annex');
16 if (intro && intro.parentNode) {
17 intro.parentNode.insertBefore(tocContainer, intro);
18 }
19 const bodyClass = this.spec.doc.body.getAttribute('class') || '';
20 this.spec.doc.body.setAttribute('class', bodyClass + ' oldtoc');
21 }
22 static build(level, expandy) {
23 let html = '<ol class="toc">';
24 level.subclauses.forEach(sub => {
25 html += '<li>';
26 if (expandy) {
27 if (sub.subclauses.length > 0) {
28 html += '<span class="item-toggle">◢</span>';
29 }
30 else {
31 html += '<span class="item-toggle-none"></span>';
32 }
33 }
34 html += '<a href="#' + sub.id + '" title="' + sub.title + '">';
35 if (sub.number) {
36 html += '<span class="secnum">' + sub.number + '</span> ';
37 }
38 html += shorten(sub.titleHTML) + '</a>';
39 if (sub.subclauses.length > 0)
40 html += Toc.build(sub, expandy);
41 html += '</li>';
42 });
43 html += '</ol>';
44 return html;
45 }
46}
47exports.default = Toc;
48function shorten(title) {
49 return title.replace('Static Semantics:', 'SS:').replace('Runtime Semantics:', 'RS:');
50}