UNPKG

3.52 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const Builder_1 = require("./Builder");
13const utils_1 = require("./utils");
14/*@internal*/
15class ProdRef extends Builder_1.default {
16 constructor(spec, node, namespace) {
17 super(spec, node);
18 this.spec = spec;
19 this.node = node;
20 this.namespace = namespace;
21 this.name = node.getAttribute('name');
22 }
23 static enter({ node, spec, clauseStack }) {
24 return __awaiter(this, void 0, void 0, function* () {
25 const clause = clauseStack[clauseStack.length - 1];
26 const namespace = clause ? clause.namespace : spec.namespace;
27 const prodref = new ProdRef(spec, node, namespace);
28 spec._prodRefs.push(prodref);
29 });
30 }
31 build() {
32 const entry = this.spec.biblio.byProductionName(this.name, this.namespace);
33 const prod = entry ? entry._instance : null;
34 let copy;
35 if (!prod) {
36 console.error('Could not find production named ' + this.node.getAttribute('name'));
37 return;
38 }
39 if (utils_1.shouldInline(this.node)) {
40 const cls = this.node.getAttribute('class') || '';
41 if (cls.indexOf('inline') === -1) {
42 this.node.setAttribute('class', cls + ' inline');
43 }
44 }
45 if (this.node.hasAttribute('a')) {
46 this.node.setAttribute('collapsed', '');
47 if (!prod.rhsesById[this.node.getAttribute('a')]) {
48 console.error('Could not find alternative ' +
49 this.node.getAttribute('a') +
50 ' of production ' +
51 prod.name);
52 return;
53 }
54 copy = prod.node.cloneNode(false);
55 // copy nodes until the first RHS. This captures the production name and any annotations.
56 for (let j = 0; j < prod.node.childNodes.length; j++) {
57 if (prod.node.childNodes[j].nodeName === 'EMU-RHS')
58 break;
59 copy.appendChild(prod.node.childNodes[j].cloneNode(true));
60 }
61 copy.appendChild(prod.rhsesById[this.node.getAttribute('a')].node.cloneNode(true));
62 }
63 else {
64 copy = prod.node.cloneNode(true);
65 }
66 copy.removeAttribute('id');
67 if (this.node.parentNode) {
68 this.node.parentNode.replaceChild(copy, this.node);
69 }
70 // copy attributes over (especially important for 'class').
71 for (let j = 0; j < this.node.attributes.length; j++) {
72 const attr = this.node.attributes[j];
73 if (!copy.hasAttribute(attr.name)) {
74 copy.setAttribute(attr.name, attr.value);
75 }
76 }
77 }
78}
79exports.default = ProdRef;
80ProdRef.elements = ['EMU-PRODREF'];