UNPKG

ecmarkup

Version:

Custom element definitions and core utilities for markup that specifies ECMAScript and related technologies.

84 lines (83 loc) 3.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Builder_1 = require("./Builder"); /*@internal*/ class RHS extends Builder_1.default { constructor(spec, prod, node) { super(spec, node); this.production = prod; this.node = node; this.constraints = node.getAttribute('constraints'); this.alternativeId = node.getAttribute('a'); } build() { if (this.node.textContent === '') { this.node.textContent = '[empty]'; return; } if (this.constraints) { const cs = this.spec.doc.createElement('emu-constraints'); cs.textContent = '[' + this.constraints + ']'; this.node.insertBefore(cs, this.node.childNodes[0]); } this.terminalify(this.node); } terminalify(parentNode) { var _a; // we store effects to perform later so the iteration doesn't get messed up const pairs = []; for (let i = 0; i < parentNode.childNodes.length; i++) { const node = parentNode.childNodes[i]; if (node.nodeType === 3) { pairs.push({ parent: parentNode, child: node }); } else if (node.nodeType === 1 && (node.nodeName === 'INS' || node.nodeName === 'DEL' || node.nodeName === 'MARK')) { for (let i = 0; i < node.childNodes.length; i++) { const child = node.childNodes[i]; if (child.nodeType === 3) { pairs.push({ parent: node, child: child }); } } } } let first = true; for (const { parent, child } of pairs) { if (!first && !/^\s+$/.test((_a = child.textContent) !== null && _a !== void 0 ? _a : '')) { if (parent === parentNode) { parentNode.insertBefore(this.spec.doc.createTextNode(' '), child); } else { // put the space outside of `<ins>` (etc) tags parentNode.insertBefore(this.spec.doc.createTextNode(' '), parent); } } first = false; this.wrapTerminal(parent, child); } } wrapTerminal(parentNode, node) { const textContent = node.textContent; const text = textContent.trim(); if (text === '' && textContent.length > 0) { // preserve intermediate whitespace return; } const pieces = text.split(/\s/); let first = true; pieces.forEach(p => { if (p.length === 0) { return; } const est = this.spec.doc.createElement('emu-t'); est.textContent = p; parentNode.insertBefore(est, node); if (!first) { parentNode.insertBefore(this.spec.doc.createTextNode(' '), est); } first = false; }); parentNode.removeChild(node); } } exports.default = RHS;