ecmarkup
Version:
Custom element definitions and core utilities for markup that specifies ECMAScript and related technologies.
48 lines (47 loc) • 1.71 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Builder_1 = require("./Builder");
class Example extends Builder_1.default {
constructor(spec, node, clause) {
super(spec, node);
this.clause = clause;
this.caption = this.node.getAttribute('caption');
if (this.node.hasAttribute('id')) {
this.id = this.node.getAttribute('id');
}
}
static async enter({ spec, node, clauseStack }) {
const clause = clauseStack[clauseStack.length - 1];
if (!clause)
return; // don't process examples outside of clauses
clause.examples.push(new Example(spec, node, clause));
}
build(number) {
if (this.id) {
// biblio is added during the build step as we don't know
// the number at build time. Could probably be fixed.
this.spec.biblio.add({
type: 'example',
id: this.id,
number: number || 1,
clauseId: this.clause.id,
});
}
const ele = this.spec.doc.createElement('figure');
ele.append(...this.node.childNodes);
this.node.append(ele);
let caption = 'Example';
if (number) {
caption += ' ' + number;
}
caption += ' (Informative)';
if (this.caption) {
caption += ': ' + this.caption;
}
const captionElem = this.spec.doc.createElement('figcaption');
captionElem.textContent = caption;
this.node.childNodes[0].insertBefore(captionElem, this.node.childNodes[0].firstChild);
}
}
Example.elements = ['EMU-EXAMPLE'];
exports.default = Example;