UNPKG

uhtml

Version:
81 lines (74 loc) • 2.14 kB
'use strict'; const Document = (require('./document.js')); const Element = (require('./element.js')); const SVGElement = (require('./element.js')); const { Node, parse, setAttributes, setChild } = require('./string-parser.js'); const { documentElement, nodeName, ownerDocument } = require('./symbols.js'); const { isSVG } = require('./utils.js'); class Root extends Node { constructor(svg) { const document = new Document(svg ? '' : 'html'); const node = svg ? new SVGElement('svg', document) : document.body; if (svg) document[documentElement] = node; super(node, svg); } onprocessinginstruction(name, data) { const { D: document } = this; switch (name) { case '!doctype': case '!DOCTYPE': document.doctype[nodeName] = data.slice(name.length).trim(); break; } } onopentag(name, attributes) { const { D: document, n: node, s: svg } = this; let child; if (svg || isSVG(name)) { switch (name) { case 'svg': case 'SVG': child = document.documentElement; break; default: child = setChild(node, new SVGElement('svg', document)); break; } this.s = true; } else { switch (name) { case 'html': case 'HTML': child = document.documentElement; break; case 'head': case 'HEAD': child = document.head; break; case 'body': case 'BODY': child = document.body; break; default: child = setChild(node, new Element(name, document)); break; } } setAttributes(this.n = child, attributes); } } const parseDocument = (xmlMode, text) => { const handler = new Root(xmlMode); const { D: document } = handler; parse(handler, xmlMode, text); if (xmlMode) document[ownerDocument] = null; return document; }; module.exports = class DOMParser { parseFromString(text, mimeType = 'text/html') { const html = mimeType === 'text/html'; if (html && text === '...') text = ''; return parseDocument(!html, text); } }