ecmarkup
Version:
Custom element definitions and core utilities for markup that specifies ECMAScript and related technologies.
69 lines (68 loc) • 2.75 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Builder_1 = require("./Builder");
const utils_1 = require("./utils");
class ProdRef extends Builder_1.default {
constructor(spec, node, namespace) {
super(spec, node);
this.spec = spec;
this.node = node;
this.namespace = namespace;
this.name = node.getAttribute('name');
}
static async enter({ node, spec, clauseStack }) {
const clause = clauseStack[clauseStack.length - 1];
const namespace = clause ? clause.namespace : spec.namespace;
const prodref = new ProdRef(spec, node, namespace);
spec._prodRefs.push(prodref);
}
build() {
const entry = this.spec.biblio.byProductionName(this.name, this.namespace);
const prod = entry ? entry._instance : null;
let copy;
if (!prod) {
console.error('Could not find production named ' + this.node.getAttribute('name'));
return;
}
if ((0, utils_1.shouldInline)(this.node)) {
const cls = this.node.getAttribute('class') || '';
if (cls.indexOf('inline') === -1) {
this.node.setAttribute('class', cls + ' inline');
}
}
if (this.node.hasAttribute('a')) {
this.node.setAttribute('collapsed', '');
if (!prod.rhsesById[this.node.getAttribute('a')]) {
console.error('Could not find alternative ' +
this.node.getAttribute('a') +
' of production ' +
prod.name);
return;
}
copy = prod.node.cloneNode(false);
// copy nodes until the first RHS. This captures the production name and any annotations.
for (let j = 0; j < prod.node.childNodes.length; j++) {
if (prod.node.childNodes[j].nodeName === 'EMU-RHS')
break;
copy.appendChild(prod.node.childNodes[j].cloneNode(true));
}
copy.appendChild(prod.rhsesById[this.node.getAttribute('a')].node.cloneNode(true));
}
else {
copy = prod.node.cloneNode(true);
}
copy.removeAttribute('id');
if (this.node.parentNode) {
this.node.parentNode.replaceChild(copy, this.node);
}
// copy attributes over (especially important for 'class').
for (let j = 0; j < this.node.attributes.length; j++) {
const attr = this.node.attributes[j];
if (!copy.hasAttribute(attr.name)) {
copy.setAttribute(attr.name, attr.value);
}
}
}
}
ProdRef.elements = ['EMU-PRODREF'];
exports.default = ProdRef;