UNPKG

ajsfw

Version:
177 lines (176 loc) 7.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Document_1 = require("./Document"); var OPEN_BRACKET = "<"; var OPEN_BRACKET_CC = "<".charCodeAt(0); var CLOSE_BRACKET = ">"; var CLOSE_BRACKET_CC = ">".charCodeAt(0); var MINUS_CC = "-".charCodeAt(0); var SLASH_CC = "/".charCodeAt(0); var EXCLAMATION_CC = "!".charCodeAt(0); var SINGLE_QUOTE_CC = "'".charCodeAt(0); var DOUBLE_QUOTE_CC = "\"".charCodeAt(0); var UNPAIR_NODE_NAMES = ["IMG", "BR", "INPUT", "META", "LINK"]; var NAME_SPACER = "\n\t>/= "; var DomParser = (function () { function DomParser(document) { this.__document = document || new Document_1.default(); } Object.defineProperty(DomParser.prototype, "document", { get: function () { return this.__document; }, enumerable: true, configurable: true }); DomParser.prototype.parse = function (stream) { this.__pos = 0; this.__stream = stream; var rootNodes = []; while (this.__pos !== -1) { if (this.__stream[this.__pos] === OPEN_BRACKET) { var node = this.__parseNode(); rootNodes.push(node); } else { this.__pos++; } if (this.__pos > this.__stream.length - 1) { this.__pos = -1; } } if (rootNodes.length > 1) { return rootNodes; } else { if (rootNodes.length === 1) { return rootNodes[0]; } else { return null; } } }; DomParser.prototype.__parseNode = function () { this.__pos++; var tagName = this.__parseName(); var node = this.document.createElement(tagName); while (this.__stream.charCodeAt(this.__pos) !== CLOSE_BRACKET_CC && this.__stream[this.__pos]) { var c = this.__stream.charCodeAt(this.__pos); if ((c > 64 && c < 91) || (c > 96 && c < 123)) { var name_1 = this.__parseName(); var code = this.__stream.charCodeAt(this.__pos); while (code && code !== SINGLE_QUOTE_CC && code !== DOUBLE_QUOTE_CC && !((code > 64 && code < 91) || (code > 96 && code < 123)) && code !== CLOSE_BRACKET_CC) { this.__pos++; code = this.__stream.charCodeAt(this.__pos); } var value = void 0; if (code === SINGLE_QUOTE_CC || code === DOUBLE_QUOTE_CC) { value = this.__parseString(); if (this.__pos === -1) { return node; } } else { value = null; this.__pos--; } node.setAttribute(name_1, value); } this.__pos++; } if (this.__stream.charCodeAt(this.__pos - 1) === SLASH_CC) { this.__pos++; return node; } var start; switch (node.nodeName) { case "SCRIPT": start = this.__pos + 1; this.__pos = this.__stream.indexOf("</" + "script>", this.__pos); node.appendChild(this.__document.createTextNode(this.__stream.slice(start, this.__pos - 1))); this.__pos += 8; break; case "STYLE": start = this.__pos + 1; this.__pos = this.__stream.indexOf("</" + "style>", this.__pos); node.appendChild(this.__document.createTextNode(this.__stream.slice(start, this.__pos - 1))); this.__pos += 7; break; default: if (UNPAIR_NODE_NAMES.indexOf(node.nodeName) === -1) { this.__pos++; this.__parseChildren(node); } break; } return node; }; DomParser.prototype.__parseChildren = function (parentNode) { while (this.__stream[this.__pos]) { if (this.__stream.charCodeAt(this.__pos + 1) === SLASH_CC) { this.__pos = this.__stream.indexOf(CLOSE_BRACKET, this.__pos); if (this.__pos + 1) { this.__pos += 1; } return; } if (this.__stream.charCodeAt(this.__pos) !== OPEN_BRACKET_CC) { var text = this.__parseText(); if (text.trim().length > 0) { parentNode.appendChild(this.__document.createTextNode(text)); } this.__pos++; continue; } if (this.__stream.charCodeAt(this.__pos + 1) === EXCLAMATION_CC) { if (this.__stream.charCodeAt(this.__pos + 2) === MINUS_CC) { while (this.__pos !== -1 && !(this.__stream.charCodeAt(this.__pos) === CLOSE_BRACKET_CC && this.__stream.charCodeAt(this.__pos - 1) === MINUS_CC && this.__stream.charCodeAt(this.__pos - 2) === MINUS_CC && this.__pos !== -1)) { this.__pos = this.__stream.indexOf(CLOSE_BRACKET, this.__pos + 1); } if (this.__pos === -1) { this.__pos = this.__stream.length; } } else { this.__pos += 2; while (this.__stream.charCodeAt(this.__pos) !== CLOSE_BRACKET_CC && this.__stream[this.__pos]) { this.__pos++; } } this.__pos++; continue; } var node = this.__parseNode(); parentNode.appendChild(node); } }; DomParser.prototype.__parseText = function () { var start = this.__pos; this.__pos = this.__stream.indexOf(OPEN_BRACKET, this.__pos) - 1; if (this.__pos === -2) { this.__pos = this.__stream.length; } return this.__stream.slice(start, this.__pos + 1); }; DomParser.prototype.__parseName = function () { var start = this.__pos; while (NAME_SPACER.indexOf(this.__stream[this.__pos]) === -1 && this.__stream[this.__pos]) { this.__pos++; } return this.__stream.slice(start, this.__pos); }; DomParser.prototype.__parseString = function () { var startChar = this.__stream[this.__pos]; var startpos = ++this.__pos; this.__pos = this.__stream.indexOf(startChar, startpos); return this.__stream.slice(startpos, this.__pos); }; return DomParser; }()); exports.default = DomParser;