UNPKG

siphon-cli

Version:

Simple bundler for web applications. 📦🔧🧡

191 lines (190 loc) • 8.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var fs = require("fs"); var errors_1 = require("../../errors"); var Structures = require("../../structures"); var getNodeAttributes_1 = require("./getNodeAttributes"); var utils_1 = require("../../../utils"); function getDOMNodes(source) { var srcText = fs.readFileSync(source).toString(); var tagStack = new Structures.Stack(); var start = 0; var node = { type: "", parent: null, }; var nodes = []; var textStore = ""; var textIsCounting = false; function handleForeignTags(startTag, textSlice, i, attrs, k) { var j = 1; var content = ""; while (textSlice[j]) { if (utils_1.stringMarkers.includes(textSlice[j])) { var marker = textSlice[j++]; content += marker; while (textSlice[j] && textSlice[j] !== marker) content += textSlice[j++]; content += textSlice[j++]; } else if (textSlice.slice(j, j + startTag.length + 3) === "</" + startTag + ">") { break; } else content += textSlice[j++]; } node = { type: "element", parent: tagStack.top(), attributes: attrs ? (0, getNodeAttributes_1.default)(attrs) : undefined, attributeList: attrs ? attrs : undefined, tagName: startTag, identifier: k ? ++k : 0, start: i, stop: j, content: content === "" ? undefined : content, }; nodes.push(node); return i + j - 1; } for (var i = 0, k = 0; srcText[i]; i++) { if (srcText.slice(i, i + 4) === "<!--") { i += 4; while (srcText[i] && srcText.slice(i, i + 3) !== "-->") i++; srcText[(i += 3)] ? "" : errors_1.default.enc("COMMENT_UNCLOSED", source, i); } if (srcText[i] == "<") { if (textIsCounting) { node = { type: "text", parent: tagStack.top(), identifier: ++k, content: textStore.replace(/([\n\r]*)/g, "").replace(/\s[\s]*/g, " "), }; nodes.push(node); textStore = ""; } textIsCounting = false; do i++; while ((0, utils_1.isSpaceCharac)(srcText[i])); (0, utils_1.checkForEnd)(srcText[i], source); if (srcText[i] === "/") { do i++; while ((0, utils_1.isSpaceCharac)(srcText[i])); (0, utils_1.checkForEnd)(srcText[i], source); var endofTag = ""; while (srcText[i] && srcText[i] !== " " && srcText[i] !== ">") endofTag += srcText[i++]; if (i > srcText.length) errors_1.default.enc("ABRUPT", source); while ((0, utils_1.isSpaceCharac)(srcText[i])) i++; (0, utils_1.checkForEnd)(srcText[i], source); if (endofTag.replace(/\n|\r/g, "") !== tagStack.top()[0]) errors_1.default.enc("UNEXPECTED_CLOSE", source, i); tagStack.pop(); } else { start = i; if (srcText[i] === ">") errors_1.default.enc("HTML_FRAGMENT", source, i); var startofTag = ""; while (srcText[i] && srcText[i] !== " " && srcText[i] !== ">") startofTag += srcText[i++]; (0, utils_1.checkForEnd)(srcText[i], source); startofTag = startofTag.replace(/\n|\r/g, ""); if (srcText[i] === " ") { do i++; while ((0, utils_1.isSpaceCharac)(srcText[i])); (0, utils_1.checkForEnd)(srcText[i], source); var attributeList = ""; while (srcText[i] && srcText[i] !== "/" && srcText[i] !== ">") { if (utils_1.stringMarkers.includes(srcText[i])) { var marker = srcText[i]; attributeList += srcText[i++]; while (srcText[i] && srcText[i] !== marker) attributeList += srcText[i++]; } attributeList += srcText[i++]; } (0, utils_1.checkForEnd)(srcText[i], source); if (srcText[i] === ">") { if ((0, utils_1.isForeignTag)(startofTag)) { i = handleForeignTags(startofTag, srcText.slice(i), i, attributeList, k); } else { node = { type: startofTag.toLowerCase() === "!doctype" ? "definition" : "element", parent: tagStack.top(), isVoid: (0, utils_1.isVoid)(startofTag) ? true : undefined, start: start, stop: i, tagName: startofTag, identifier: ++k, attributes: (0, getNodeAttributes_1.default)(attributeList), attributeList: attributeList, }; nodes.push(node); } if (!(0, utils_1.isVoid)(startofTag)) tagStack.push([startofTag.replace(/\n|\r/g, ""), k]); } else if (srcText[i] === "/") { do i++; while ((0, utils_1.isSpaceCharac)(srcText[i])); (0, utils_1.checkForEnd)(srcText[i], source); node = { type: "element", tagName: startofTag, parent: tagStack.top(), identifier: ++k, isVoid: (0, utils_1.isVoid)(startofTag) ? true : undefined, attributes: (0, getNodeAttributes_1.default)(attributeList), attributeList: attributeList, start: start, stop: i, }; nodes.push(node); if (!(0, utils_1.isVoid)(startofTag)) errors_1.default.enc("INVALID_VOID_TAG", source, i, { name: startofTag }); } } else { if ((0, utils_1.isForeignTag)(startofTag)) { i = handleForeignTags(startofTag, srcText.slice(i), i); } else { node = { type: "element", tagName: startofTag, identifier: ++k, parent: tagStack.top(), start: start, stop: i, }; nodes.push(node); } if (!(0, utils_1.isVoid)(startofTag)) tagStack.push([startofTag, k]); } } } else if (textIsCounting) { textStore += srcText[i]; } else if (!(0, utils_1.isSpaceCharac)(srcText[i])) { textStore += srcText[i]; textIsCounting = true; } } return nodes; } exports.default = getDOMNodes;