UNPKG

@web-atoms/core

Version:
190 lines (189 loc) • 5.94 kB
System.register(["../../core/XNode", "../../style/StyleRule", "../controls/AtomControl", "./AtomUI", "./Encoder"], function (_export, _context) { "use strict"; var XNode, AtomStyleRules, ElementValueSetters, descendentElementIterator, Encoder, HtmlNode, encoder, fromHyphenToCamel, setters; function mergeStyles(a) { const r = {}; for (const key in a) { if (a.hasOwnProperty(key)) { const element = a[key]; if (element === undefined || element === null) { continue; } if (/^style\-/i.test(key)) { const style = r.style || (r.style = ""); const newStyle = `${key.substring(6)}: ${encoder.htmlEncode(element, false)};`; r.style = style ? `${style} ${newStyle}` : newStyle; continue; } if (key === "style") { if (r.style) { r.style = `${r.style} ${element}`; continue; } } r[key] = element; } } return r; } function convertToText(node) { if (typeof node.name === "function") { node = node.name(Object.assign(Object.assign({}, node.attributes), { children: node.children })); return convertToText(node); } if (node.name === "br") { return "<br/>"; } let attrs = ""; const attributes = mergeStyles(node.attributes); const name = node.name; const children = node.children; let textContent = ""; for (const key in attributes) { if (attributes.hasOwnProperty(key)) { const element = attributes[key]; if (element === null || element === undefined) { continue; } if (key === "text") { textContent = element.toString(); continue; } if (key === "style" && typeof element === "object") { if (element instanceof AtomStyleRules) { attrs += ` ${key}="${encoder.htmlEncode(element.toStyleSheet(), false)}"`; continue; } attrs += ` ${key}="${encoder.htmlEncode(new AtomStyleRules(element).toStyleSheet(), false)}"`; continue; } attrs += ` ${key}="${encoder.htmlEncode(element, false)}"`; } } const content = renderChildren(node, children); return `<${name}${attrs}>${textContent} \t${content.map(s => s.toString().split("\n").join("\n\t")).join("\r\n")} </${name}>`; } function renderChildren(node, children) { if (!children) { return []; } const content = []; if (children) { for (const iterator of children) { if (!iterator) { continue; } if (Array.isArray(iterator)) { for (const child of renderChildren(node, iterator)) { if (child === undefined || child === null) { continue; } content.push(child); } } else { if (typeof iterator !== "string") { content.push(convertToText(iterator)); continue; } content.push(iterator); } } } return content; } function render(node, root) { const a = node.attributes; if (a) { for (const key in a) { if (Object.prototype.hasOwnProperty.call(a, key)) { let element = a[key]; const setter = setters[key]; if (setter !== void 0) { setter(null, root, element); continue; } if (key.length > 5 && /^style/.test(key)) { if (/^style\-/.test(key)) { root.style.setProperty(key.substring(6), element); } else { root.style[fromHyphenToCamel(key.substring(6))] = element; } continue; } if (/^data-/i.test(key)) { if (typeof element === "object") { element = JSON.stringify(element); } root.dataset[fromHyphenToCamel(key.substring(5))] = element; continue; } root[key] = element; } } } const children = node.children; if (!children) { return; } for (const iterator of children) { if (!iterator) { continue; } if (typeof iterator === "string") { root.appendChild(document.createTextNode(iterator)); continue; } const name = iterator.name; const child = document.createElement(name); render(iterator, child); root.appendChild(child); } } _export({ mergeStyles: mergeStyles, convertToText: convertToText }); return { setters: [function (_coreXNode) { XNode = _coreXNode.default; }, function (_styleStyleRule) { AtomStyleRules = _styleStyleRule.AtomStyleRules; }, function (_controlsAtomControl) { ElementValueSetters = _controlsAtomControl.ElementValueSetters; }, function (_AtomUI) { descendentElementIterator = _AtomUI.descendentElementIterator; }, function (_Encoder) { Encoder = _Encoder.default; }], execute: function () { _export("encoder", encoder = Encoder("entity")); fromHyphenToCamel = input => input.replace(/-([a-z])/g, g => g[1].toUpperCase()); setters = ElementValueSetters; HtmlNode = class HtmlNode { static convert(node) { return convertToText(node); } static toElement(node, sanitize = true) { const div = document.createElement("div"); render(XNode.create("div", null, node), div); if (sanitize) { for (const e of descendentElementIterator(div)) { if (/^script$/i.test(e.nodeName)) { e.remove(); continue; } } } return div.firstElementChild; } }; HtmlNode.render = render; _export("default", HtmlNode); } }; }); //# sourceMappingURL=HtmlNode.js.map