/
Spec.ts
1120 lines (979 loc) · 33.8 KB
/
Spec.ts
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import type { MarkupData } from 'parse5';
import type { EcmarkupError, Options } from './ecmarkup';
import type { Context } from './Context';
import type { BiblioData, StepBiblioEntry } from './Biblio';
import type { BuilderInterface } from './Builder';
import * as path from 'path';
import * as fs from 'fs';
import * as yaml from 'js-yaml';
import * as utils from './utils';
import * as hljs from 'highlight.js';
// Builders
import Import, { EmuImportElement } from './Import';
import H1 from './H1';
import Clause from './Clause';
import ClauseNumbers from './clauseNums';
import Algorithm from './Algorithm';
import Dfn from './Dfn';
import Example from './Example';
import Figure from './Figure';
import Note from './Note';
import Toc from './Toc';
import Menu from './Menu';
import Production from './Production';
import NonTerminal from './NonTerminal';
import ProdRef from './ProdRef';
import Grammar from './Grammar';
import Xref from './Xref';
import Eqn from './Eqn';
import Biblio from './Biblio';
import { autolink, replacerForNamespace, NO_CLAUSE_AUTOLINK } from './autolinker';
import { lint } from './lint/lint';
import { CancellationToken } from 'prex';
import type { JSDOM } from 'jsdom';
const DRAFT_DATE_FORMAT = { year: 'numeric', month: 'long', day: 'numeric', timeZone: 'UTC' };
const STANDARD_DATE_FORMAT = { year: 'numeric', month: 'long', timeZone: 'UTC' };
const NO_EMD = new Set(['PRE', 'CODE', 'EMU-PRODUCTION', 'EMU-ALG', 'EMU-GRAMMAR', 'EMU-EQN']);
interface VisitorMap {
[k: string]: BuilderInterface;
}
interface TextNodeContext {
clause: Spec | Clause;
node: Node;
inAlg: boolean;
currentId: string | null;
}
let builders: BuilderInterface[] = [
Clause,
H1,
Algorithm,
Xref,
Dfn,
Eqn,
Grammar,
Production,
Example,
Figure,
NonTerminal,
ProdRef,
Note,
];
const visitorMap = builders.reduce((map, T) => {
T.elements.forEach(e => (map[e] = T));
return map;
}, {} as VisitorMap);
// NOTE: This is the public API for spec as used in the types. Do not remove.
export default interface Spec {
spec: this;
opts: Options;
rootPath: string;
rootDir: string;
namespace: string;
toHTML(): string;
exportBiblio(): any;
}
export type Warning =
| {
type: 'global';
ruleId: string;
message: string;
}
| {
type: 'node';
node: Text | Element;
ruleId: string;
message: string;
}
| {
type: 'attr';
node: Element;
attr: string;
ruleId: string;
message: string;
}
| {
type: 'contents';
node: Text | Element;
ruleId: string;
message: string;
nodeRelativeLine: number;
nodeRelativeColumn: number;
}
| {
type: 'raw';
ruleId: string;
message: string;
line: number;
column: number;
file?: string;
source?: string;
};
function wrapWarn(source: string, spec: Spec, warn: (err: EcmarkupError) => void) {
return (e: Warning) => {
let { message, ruleId } = e;
let line: number | undefined;
let column: number | undefined;
let nodeType: string;
let file: string | undefined = undefined;
switch (e.type) {
case 'global':
line = undefined;
column = undefined;
nodeType = 'html';
break;
case 'raw':
({ line, column } = e);
nodeType = 'html';
if (e.file != null) {
file = e.file;
source = e.source!;
}
break;
case 'node':
if (e.node.nodeType === 3 /* Node.TEXT_NODE */) {
let loc = spec.locate(e.node);
if (loc) {
file = loc.file;
if (loc.source != null) {
source = loc.source;
}
({ line, col: column } = loc);
}
nodeType = 'text';
} else {
let loc = spec.locate(e.node as Element);
if (loc) {
file = loc.file;
if (loc.source != null) {
source = loc.source;
}
({ line, col: column } = loc.startTag);
}
nodeType = (e.node as Element).tagName.toLowerCase();
}
break;
case 'attr': {
let loc = spec.locate(e.node);
if (loc) {
file = loc.file;
if (loc.source != null) {
source = loc.source;
}
({ line, column } = utils.attrValueLocation(source, loc, e.attr));
}
nodeType = e.node.tagName.toLowerCase();
break;
}
case 'contents': {
let { nodeRelativeLine, nodeRelativeColumn } = e;
if (e.node.nodeType === 3 /* Node.TEXT_NODE */) {
// i.e. a text node, which does not have a tag
let loc = spec.locate(e.node);
if (loc) {
file = loc.file;
if (loc.source != null) {
source = loc.source;
}
line = loc.line + nodeRelativeLine - 1;
column = nodeRelativeLine === 1 ? loc.col + nodeRelativeColumn - 1 : nodeRelativeColumn;
}
nodeType = 'text';
} else {
let loc = spec.locate(e.node);
if (loc) {
file = loc.file;
if (loc.source != null) {
source = loc.source;
}
// We have to adjust both for start of tag -> end of tag and end of tag -> passed position
// TODO switch to using loc.startTag.end{Line,Col} once parse5 can be upgraded
let tagSrc = source.slice(loc.startTag.startOffset, loc.startTag.endOffset);
let tagEnd = utils.offsetToLineAndColumn(
tagSrc,
loc.startTag.endOffset - loc.startOffset
);
line = loc.startTag.line + tagEnd.line + nodeRelativeLine - 2;
if (nodeRelativeLine === 1) {
if (tagEnd.line === 1) {
column = loc.startTag.col + tagEnd.column + nodeRelativeColumn - 2;
} else {
column = tagEnd.column + nodeRelativeColumn - 1;
}
} else {
column = nodeRelativeColumn;
}
}
nodeType = (e.node as Element).tagName.toLowerCase();
}
break;
}
}
warn({
message,
ruleId,
// we omit source for global errors so that we don't get a codeframe
source: e.type === 'global' ? undefined : source,
file,
nodeType,
line,
column,
});
};
}
function isEmuImportElement(node: Node): node is EmuImportElement {
return node.nodeType === 1 && node.nodeName === 'EMU-IMPORT';
}
/*@internal*/
export default class Spec {
spec: this;
opts: Options;
rootPath: string;
rootDir: string;
sourceText: string | undefined;
namespace: string;
biblio: Biblio;
dom: any; /* JSDOM types are not updated, this is the jsdom DOM object */
doc: Document;
imports: Import[];
node: HTMLElement;
nodeIds: Set<string>;
subclauses: Clause[];
replacementAlgorithmToContainedLabeledStepEntries: Map<Element, StepBiblioEntry[]>; // map from re to its labeled nodes
labeledStepsToBeRectified: Set<string>;
replacementAlgorithms: { element: Element; target: string }[];
cancellationToken: CancellationToken;
log: (msg: string) => void;
warn: (err: Warning) => void | undefined;
_figureCounts: { [type: string]: number };
_xrefs: Xref[];
_ntRefs: NonTerminal[];
_prodRefs: ProdRef[];
_textNodes: { [s: string]: [TextNodeContext] };
private _fetch: (file: string, token: CancellationToken) => PromiseLike<string>;
constructor(
rootPath: string,
fetch: (file: string, token: CancellationToken) => PromiseLike<string>,
dom: any,
opts?: Options,
sourceText?: string, // TODO we need not support this being undefined
token = CancellationToken.none
) {
opts = opts || {};
this.spec = this;
this.opts = {};
this.rootPath = rootPath;
this.rootDir = path.dirname(this.rootPath);
this.sourceText = sourceText;
this.doc = dom.window.document;
this.dom = dom;
this._fetch = fetch;
this.subclauses = [];
this.imports = [];
this.node = this.doc.body;
this.nodeIds = new Set();
this.replacementAlgorithmToContainedLabeledStepEntries = new Map();
this.labeledStepsToBeRectified = new Set();
this.replacementAlgorithms = [];
this.cancellationToken = token;
this.log = opts.log ?? (() => {});
this.warn = opts.warn ? wrapWarn(sourceText!, this, opts.warn) : () => {};
this._figureCounts = {
table: 0,
figure: 0,
};
this._xrefs = [];
this._ntRefs = [];
this._prodRefs = [];
this._textNodes = {};
this.processMetadata();
Object.assign(this.opts, opts);
if (typeof this.opts.status === 'undefined') {
this.opts.status = 'proposal';
}
if (typeof this.opts.toc === 'undefined') {
this.opts.toc = true;
}
if (typeof this.opts.copyright === 'undefined') {
this.opts.copyright = true;
}
if (typeof this.opts.ecma262Biblio === 'undefined') {
this.opts.ecma262Biblio = true;
}
if (!this.opts.date) {
this.opts.date = new Date();
}
if (this.opts.stage != undefined) {
this.opts.stage = String(this.opts.stage);
}
if (!this.opts.location) {
this.opts.location = '<no location>';
}
this.namespace = this.opts.location;
this.biblio = new Biblio(this.opts.location);
}
public fetch(file: string) {
return this._fetch(file, this.cancellationToken);
}
public async build() {
/*
The Ecmarkup build process proceeds as follows:
1. Load biblios, making xrefs and auto-linking from external specs work
2. Load imports by recursively inlining the import files' content into the emu-import element
3. Generate boilerplate text
4. Do a walk of the DOM visting elements and text nodes. Text nodes are replaced by text and HTML nodes depending
on content. Elements are built by delegating to builders. Builders work by modifying the DOM ahead of them so
the new elements they make are visited during the walk. Elements added behind the current iteration must be
handled specially (eg. see static exit method of clause). Xref, nt, and prodref's are collected for linking
in the next step.
5. Linking. After the DOM walk we have a complete picture of all the symbols in the document so we proceed to link
the various xrefs to the proper place.
6. Adding charset, highlighting code, etc.
7. Add CSS & JS dependencies.
*/
this.log('Loading biblios...');
if (this.opts.ecma262Biblio) {
await this.loadECMA262Biblio();
}
await this.loadBiblios();
this.log('Loading imports...');
await this.loadImports();
this.log('Building boilerplate...');
this.buildBoilerplate();
this.log('Walking document, building various elements...');
const context: Context = {
spec: this,
node: this.doc.body,
importStack: [],
clauseStack: [],
tagStack: [],
clauseNumberer: ClauseNumbers(),
inNoAutolink: false,
inAlg: false,
inNoEmd: false,
startEmd: null,
currentId: null,
};
const document = this.doc;
if (this.opts.lintSpec) {
this.log('Linting...');
const source = this.sourceText;
if (source === undefined) {
throw new Error('Cannot lint when source text is not available');
}
lint(this.warn, source, this, document);
}
const walker = document.createTreeWalker(document.body, 1 | 4 /* elements and text nodes */);
walk(walker, context);
this.setReplacementAlgorithmOffsets();
this.autolink();
this.log('Linking xrefs...');
this._xrefs.forEach(xref => xref.build());
this.log('Linking non-terminal references...');
this._ntRefs.forEach(nt => nt.build());
this.log('Linking production references...');
this._prodRefs.forEach(prod => prod.build());
this.log('Building reference graph...');
this.buildReferenceGraph();
this.highlightCode();
this.setCharset();
this.setFirstH1Class();
this.buildSpecWrapper();
if (this.opts.toc) {
this.log('Building table of contents...');
let toc: Toc | Menu;
if (this.opts.oldToc) {
toc = new Toc(this);
} else {
toc = new Menu(this);
}
toc.build();
}
await this.buildAssets();
return this;
}
public toHTML() {
let htmlEle = this.doc.documentElement;
return '<!doctype html>\n' + (htmlEle.hasAttributes() ? htmlEle.outerHTML : htmlEle.innerHTML);
}
public locate(
node: Element | Node
): ({ file?: string; source?: string } & MarkupData.ElementLocation) | undefined {
let pointer: Element | Node | null = node;
while (pointer != null) {
if (isEmuImportElement(pointer)) {
break;
}
pointer = pointer.parentElement;
}
if (pointer == null) {
let loc = (this.dom as JSDOM).nodeLocation(node);
if (loc) {
// we can't just spread `loc` because not all properties are own/enumerable
return {
startTag: loc.startTag,
endTag: loc.endTag,
startOffset: loc.startOffset,
endOffset: loc.endOffset,
attrs: loc.attrs,
line: loc.line,
col: loc.col,
};
}
} else if (pointer.dom) {
let loc = pointer.dom.nodeLocation(node);
if (loc) {
return {
file: pointer.importPath!,
source: pointer.source!,
startTag: loc.startTag,
endTag: loc.endTag,
startOffset: loc.startOffset,
endOffset: loc.endOffset,
attrs: loc.attrs,
line: loc.line,
col: loc.col,
};
}
}
}
private buildReferenceGraph() {
let counter = 0;
this._xrefs.forEach(xref => {
let entry = xref.entry;
if (!entry || entry.namespace === 'global') return;
if (!entry.id && entry.refId) {
entry = this.spec.biblio.byId(entry.refId);
}
if (!xref.id) {
const id = `_ref_${counter++}`;
xref.node.setAttribute('id', id);
xref.id = id;
}
entry.referencingIds.push(xref.id);
});
this._ntRefs.forEach(prod => {
const entry = prod.entry;
if (!entry || entry.namespace === 'global') return;
// if this is the defining nt of an emu-production, don't create a ref
if (prod.node.parentNode!.nodeName === 'EMU-PRODUCTION') return;
const id = `_ref_${counter++}`;
prod.node.setAttribute('id', id);
entry.referencingIds.push(id);
});
}
private async buildAssets() {
const jsContents = await concatJs();
const cssContents = await utils.readFile(path.join(__dirname, '../css/elements.css'));
if (this.opts.jsOut) {
this.log(`Writing js file to ${this.opts.jsOut}...`);
await utils.writeFile(this.opts.jsOut, jsContents);
}
if (this.opts.cssOut) {
this.log(`Writing css file to ${this.opts.cssOut}...`);
await utils.writeFile(this.opts.cssOut, cssContents);
}
if (this.opts.assets === 'none') return;
// back-compat: if we already have a link to this script or css, bail out
let skipJs = false,
skipCss = false,
outDir: string;
if (this.opts.outfile) {
outDir = path.dirname(this.opts.outfile);
} else {
outDir = process.cwd();
}
if (this.opts.jsOut) {
const scripts = this.doc.querySelectorAll('script');
for (let i = 0; i < scripts.length; i++) {
const script = scripts[i];
const src = script.getAttribute('src');
if (src && path.normalize(path.join(outDir, src)) === path.normalize(this.opts.jsOut)) {
this.log(`Found existing js link to ${src}, skipping inlining...`);
skipJs = true;
}
}
}
if (this.opts.cssOut) {
const links = this.doc.querySelectorAll('link[rel=stylesheet]');
for (let i = 0; i < links.length; i++) {
const link = links[i];
const href = link.getAttribute('href');
if (href && path.normalize(path.join(outDir, href)) === path.normalize(this.opts.cssOut)) {
this.log(`Found existing css link to ${href}, skipping inlining...`);
skipCss = true;
}
}
}
if (!skipJs) {
this.log('Inlining JavaScript assets...');
const script = this.doc.createElement('script');
script.textContent = jsContents;
this.doc.head.appendChild(script);
}
if (!skipCss) {
this.log('Inlining CSS assets...');
const style = this.doc.createElement('style');
style.textContent = cssContents;
this.doc.head.appendChild(style);
}
}
private buildSpecWrapper() {
const elements = this.doc.body.childNodes;
const wrapper = this.doc.createElement('div');
wrapper.id = 'spec-container';
while (elements.length > 0) {
wrapper.appendChild(elements[0]);
}
this.doc.body.appendChild(wrapper);
}
private processMetadata() {
const block = this.doc.querySelector('pre.metadata');
if (!block || !block.parentNode) {
return;
}
let data: any;
try {
data = yaml.safeLoad(block.textContent!);
} catch (e) {
if (typeof e?.mark.line === 'number' && typeof e?.mark.column === 'number') {
this.warn({
type: 'contents',
ruleId: 'invalid-metadata',
message: `metadata block failed to parse: ${e.reason}`,
node: block,
nodeRelativeLine: e.mark.line + 1,
nodeRelativeColumn: e.mark.column + 1,
});
} else {
this.warn({
type: 'node',
ruleId: 'invalid-metadata',
message: 'metadata block failed to parse',
node: block,
});
}
return;
} finally {
block.parentNode.removeChild(block);
}
Object.assign(this.opts, data);
}
private async loadECMA262Biblio() {
this.cancellationToken.throwIfCancellationRequested();
await this.loadBiblio(path.join(__dirname, '../ecma262biblio.json'));
}
private async loadBiblios() {
this.cancellationToken.throwIfCancellationRequested();
await Promise.all(
Array.from(this.doc.querySelectorAll('emu-biblio')).map(biblio =>
this.loadBiblio(path.join(this.rootDir, biblio.getAttribute('href')!))
)
);
}
private async loadBiblio(file: string) {
const contents = await this.fetch(file);
this.biblio.addExternalBiblio(JSON.parse(contents));
}
private async loadImports() {
await loadImports(this, this.spec.doc.body, this.rootDir);
}
public exportBiblio(): any {
if (!this.opts.location) {
this.warn({
type: 'global',
ruleId: 'no-location',
message:
"no spec location specified; biblio not generated. try --location or setting the location in the document's metadata block",
});
return {};
}
const biblio: BiblioData = {};
biblio[this.opts.location] = this.biblio.toJSON();
return biblio;
}
private highlightCode() {
this.log('Highlighting syntax...');
const codes = this.doc.querySelectorAll('pre code');
for (let i = 0; i < codes.length; i++) {
const classAttr = codes[i].getAttribute('class');
if (!classAttr) continue;
const lang = classAttr.replace(/lang(uage)?-/, '');
let input = codes[i].textContent!;
// remove leading and trailing blank lines
input = input.replace(/^(\s*[\r\n])+|([\r\n]\s*)+$/g, '');
// remove base inden based on indent of first non-blank line
const baseIndent = input.match(/^\s*/) || '';
const baseIndentRe = new RegExp('^' + baseIndent, 'gm');
input = input.replace(baseIndentRe, '');
const result = hljs.highlight(lang, input);
codes[i].innerHTML = result.value;
codes[i].setAttribute('class', classAttr + ' hljs');
}
}
// if there is no text between body start and the first H1, the first H1 is given a class of "first"
private setFirstH1Class() {
const document = this.spec.doc;
const walker = document.createTreeWalker(document.body, 1 | 4 /* elements and text nodes */);
let node = walker.currentNode;
while (node) {
if (node.nodeType === 3 && node.textContent!.trim().length > 0) {
return;
}
if (walker.currentNode.nodeType === 1 && walker.currentNode.nodeName === 'H1') {
(node as HTMLElement).classList.add('first');
return;
}
node = walker.nextNode()!;
}
}
private buildBoilerplate() {
this.cancellationToken.throwIfCancellationRequested();
const status = this.opts.status!;
const version = this.opts.version;
const title = this.opts.title;
const shortname = this.opts.shortname;
const location = this.opts.location;
const stage = this.opts.stage;
if (this.opts.copyright) {
if (status !== 'draft' && status !== 'standard' && !this.opts.contributors) {
this.warn({
type: 'global',
ruleId: 'no-contributors',
message:
'contributors not specified, skipping copyright boilerplate. specify contributors in your frontmatter metadata',
});
} else {
this.buildCopyrightBoilerplate();
}
}
// no title boilerplate generated if title not specified
if (!title) return;
// title
if (title && !this._updateBySelector('title', title)) {
const titleElem = this.doc.createElement('title');
titleElem.innerHTML = title;
this.doc.head.appendChild(titleElem);
const h1 = this.doc.createElement('h1');
h1.setAttribute('class', 'title');
h1.innerHTML = title;
this.doc.body.insertBefore(h1, this.doc.body.firstChild);
}
// version string, ala 6th Edition July 2016 or Draft 10 / September 26, 2015
let versionText = '';
let omitShortname = false;
if (version) {
versionText += version + ' / ';
} else if (status === 'proposal' && stage) {
versionText += 'Stage ' + stage + ' Draft / ';
} else if (shortname && status === 'draft') {
versionText += 'Draft ' + shortname + ' / ';
omitShortname = true;
} else {
return;
}
const defaultDateFormat = status === 'standard' ? STANDARD_DATE_FORMAT : DRAFT_DATE_FORMAT;
const date = new Intl.DateTimeFormat('en-US', defaultDateFormat).format(this.opts.date);
versionText += date;
if (!this._updateBySelector('h1.version', versionText)) {
const h1 = this.doc.createElement('h1');
h1.setAttribute('class', 'version');
h1.innerHTML = versionText;
this.doc.body.insertBefore(h1, this.doc.body.firstChild);
}
// shortname and status, ala 'Draft ECMA-262
if (shortname && !omitShortname) {
// for proposals, link shortname to location
const shortnameLinkHtml =
status === 'proposal' && location ? `<a href="${location}">${shortname}</a>` : shortname;
const shortnameHtml =
status.charAt(0).toUpperCase() + status.slice(1) + ' ' + shortnameLinkHtml;
if (!this._updateBySelector('h1.shortname', shortnameHtml)) {
const h1 = this.doc.createElement('h1');
h1.setAttribute('class', 'shortname');
h1.innerHTML = shortnameHtml;
this.doc.body.insertBefore(h1, this.doc.body.firstChild);
}
}
}
private buildCopyrightBoilerplate() {
let address: string;
let copyright: string;
let license: string;
let addressFile: string | undefined;
let copyrightFile: string | undefined;
let licenseFile: string | undefined;
if (this.opts.boilerplate) {
if (this.opts.boilerplate.address) {
addressFile = path.join(process.cwd(), this.opts.boilerplate.address);
}
if (this.opts.boilerplate.copyright) {
copyrightFile = path.join(process.cwd(), this.opts.boilerplate.copyright);
}
if (this.opts.boilerplate.license) {
licenseFile = path.join(process.cwd(), this.opts.boilerplate.license);
}
}
// Get content from files
address = getBoilerplate(addressFile || 'address');
copyright = getBoilerplate(copyrightFile || `${this.opts.status}-copyright`);
license = getBoilerplate(licenseFile || 'software-license');
if (this.opts.status === 'proposal') {
address = '';
}
// Operate on content
copyright = copyright.replace(/!YEAR!/g, '' + this.opts.date!.getFullYear());
if (this.opts.contributors) {
copyright = copyright.replace(/!CONTRIBUTORS!/g, this.opts.contributors);
}
let copyrightClause = this.doc.querySelector('.copyright-and-software-license');
if (!copyrightClause) {
let last: HTMLElement | undefined;
utils.domWalkBackward(this.doc.body, node => {
if (last) return false;
if (node.nodeName === 'EMU-CLAUSE' || node.nodeName === 'EMU-ANNEX') {
last = node as HTMLElement;
return false;
}
});
copyrightClause = this.doc.createElement('emu-annex');
copyrightClause.setAttribute('id', 'sec-copyright-and-software-license');
if (last && last.parentNode) {
last.parentNode.insertBefore(copyrightClause, last.nextSibling);
} else {
this.doc.body.appendChild(copyrightClause);
}
}
copyrightClause.innerHTML = `
<h1>Copyright & Software License</h1>
${address}
<h2>Copyright Notice</h2>
${copyright.replace('!YEAR!', '' + this.opts.date!.getFullYear())}
<h2>Software License</h2>
${license}
`;
}
private setReplacementAlgorithmOffsets() {
this.log('Finding offsets for replacement algorithm steps...');
let pending: Map<string, Element[]> = new Map();
let setReplacementAlgorithmStart = (element: Element, stepNumbers: number[]) => {
let rootList = element.firstElementChild! as HTMLOListElement;
rootList.start = stepNumbers[stepNumbers.length - 1];
if (stepNumbers.length > 1) {
// Note the off-by-one here: a length of 1 indicates we are replacing a top-level step, which means we do not need to adjust the styling.
if (stepNumbers.length === 2) {
rootList.classList.add('nested-once');
} else if (stepNumbers.length === 3) {
rootList.classList.add('nested-twice');
} else if (stepNumbers.length === 4) {
rootList.classList.add('nested-thrice');
} else if (stepNumbers.length === 5) {
rootList.classList.add('nested-four-times');
} else {
// At the sixth level and deeper (so once we have nested five times) we use a consistent line numbering style, so no further cases are necessary
rootList.classList.add('nested-lots');
}
}
// Fix up the biblio entries for any labeled steps in the algorithm
for (let entry of this.replacementAlgorithmToContainedLabeledStepEntries.get(element)!) {
entry.stepNumbers = [...stepNumbers, ...entry.stepNumbers.slice(1)];
this.labeledStepsToBeRectified.delete(entry.id);
// Now that we've figured out where the step is, we can deal with algorithms replacing it
if (pending.has(entry.id)) {
let todo = pending.get(entry.id)!;
pending.delete(entry.id);
for (let replacementAlgorithm of todo) {
setReplacementAlgorithmStart(replacementAlgorithm, entry.stepNumbers);
}
}
}
};
for (let { element, target } of this.replacementAlgorithms) {
if (this.labeledStepsToBeRectified.has(target)) {
if (!pending.has(target)) {
pending.set(target, []);
}
pending.get(target)!.push(element);
} else {
// When the target is not itself within a replacement, or is within a replacement which we have already rectified, we can just use its step number directly
let targetEntry = this.biblio.byId(target);
if (targetEntry == null) {
this.warn({
type: 'attr',
attr: 'replaces-step',
ruleId: 'invalid-replacement',
message: `could not find step ${JSON.stringify(target)}`,
node: element,
});
} else if (targetEntry.type !== 'step') {
this.warn({
type: 'attr',
attr: 'replaces-step',
ruleId: 'invalid-replacement',
message: `expected algorithm to replace a step, not a ${targetEntry.type}`,
node: element,
});
} else {
setReplacementAlgorithmStart(element, targetEntry.stepNumbers);
}
}
}
if (pending.size > 0) {
// todo consider line/column missing cases
this.warn({
type: 'global',
ruleId: 'invalid-replacement',
message:
'could not unambiguously determine replacement algorithm offsets - do you have a cycle in your replacement algorithms?',
});
}
}
public autolink() {
this.log('Autolinking terms and abstract ops...');
let namespaces = Object.keys(this._textNodes);
for (let i = 0; i < namespaces.length; i++) {
let namespace = namespaces[i];
const [replacer, autolinkmap] = replacerForNamespace(namespace, this.biblio);
let nodes = this._textNodes[namespace];
for (let j = 0; j < nodes.length; j++) {
const { node, clause, inAlg, currentId } = nodes[j];
autolink(node, replacer, autolinkmap, clause, currentId, inAlg);
}
}
}
public setCharset() {
let current = this.spec.doc.querySelector('meta[charset]');
if (!current) {
current = this.spec.doc.createElement('meta');
this.spec.doc.head.insertBefore(current, this.spec.doc.head.firstChild);
}
current.setAttribute('charset', 'utf-8');
}
private _updateBySelector(selector: string, contents: string) {
const elem = this.doc.querySelector(selector);
if (elem && elem.textContent!.trim().length > 0) {
return true;
}
if (elem) {
elem.innerHTML = contents;
return true;
}
return false;
}
}
function getBoilerplate(file: string) {
let boilerplateFile: string = file;
try {
if (fs.lstatSync(file).isFile()) {
boilerplateFile = file;
}
} catch (error) {
boilerplateFile = path.join(__dirname, '../boilerplate', `${file}.html`);
}
return fs.readFileSync(boilerplateFile, 'utf8');
}
async function loadImports(spec: Spec, rootElement: HTMLElement, rootPath: string) {
let imports = rootElement.querySelectorAll('EMU-IMPORT');
for (let i = 0; i < imports.length; i++) {