1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const Builder_1 = require("./Builder");
|
4 |
|
5 | class RHS extends Builder_1.default {
|
6 | constructor(spec, prod, node) {
|
7 | super(spec, node);
|
8 | this.production = prod;
|
9 | this.node = node;
|
10 | this.constraints = node.getAttribute('constraints');
|
11 | this.alternativeId = node.getAttribute('a');
|
12 | }
|
13 | build() {
|
14 | if (this.node.textContent === '') {
|
15 | this.node.textContent = '[empty]';
|
16 | return;
|
17 | }
|
18 | if (this.constraints) {
|
19 | const cs = this.spec.doc.createElement('emu-constraints');
|
20 | cs.textContent = '[' + this.constraints + ']';
|
21 | this.node.insertBefore(cs, this.node.childNodes[0]);
|
22 | }
|
23 | this.terminalify(this.node);
|
24 | }
|
25 | terminalify(parentNode) {
|
26 | var _a;
|
27 |
|
28 | const pairs = [];
|
29 | for (let i = 0; i < parentNode.childNodes.length; i++) {
|
30 | const node = parentNode.childNodes[i];
|
31 | if (node.nodeType === 3) {
|
32 | pairs.push({ parent: parentNode, child: node });
|
33 | }
|
34 | else if (node.nodeType === 1 &&
|
35 | (node.nodeName === 'INS' || node.nodeName === 'DEL' || node.nodeName === 'MARK')) {
|
36 | for (let i = 0; i < node.childNodes.length; i++) {
|
37 | const child = node.childNodes[i];
|
38 | if (child.nodeType === 3) {
|
39 | pairs.push({ parent: node, child: child });
|
40 | }
|
41 | }
|
42 | }
|
43 | }
|
44 | let first = true;
|
45 | for (const { parent, child } of pairs) {
|
46 | if (!first && !/^\s+$/.test((_a = child.textContent) !== null && _a !== void 0 ? _a : '')) {
|
47 | if (parent === parentNode) {
|
48 | parentNode.insertBefore(this.spec.doc.createTextNode(' '), child);
|
49 | }
|
50 | else {
|
51 |
|
52 | parentNode.insertBefore(this.spec.doc.createTextNode(' '), parent);
|
53 | }
|
54 | }
|
55 | first = false;
|
56 | this.wrapTerminal(parent, child);
|
57 | }
|
58 | }
|
59 | wrapTerminal(parentNode, node) {
|
60 | const textContent = node.textContent;
|
61 | const text = textContent.trim();
|
62 | if (text === '' && textContent.length > 0) {
|
63 |
|
64 | return;
|
65 | }
|
66 | const pieces = text.split(/\s/);
|
67 | let first = true;
|
68 | pieces.forEach(p => {
|
69 | if (p.length === 0) {
|
70 | return;
|
71 | }
|
72 | const est = this.spec.doc.createElement('emu-t');
|
73 | est.textContent = p;
|
74 | parentNode.insertBefore(est, node);
|
75 | if (!first) {
|
76 | parentNode.insertBefore(this.spec.doc.createTextNode(' '), est);
|
77 | }
|
78 | first = false;
|
79 | });
|
80 | parentNode.removeChild(node);
|
81 | }
|
82 | }
|
83 | exports.default = RHS;
|