UNPKG

@antv/f6-ui

Version:

UI system for @antv/f6

223 lines (178 loc) 5.15 kB
import { parseAttr } from '../parser/attr-parser'; var Node = /** @class */ function () { function Node() { this.type = ''; this.tagName = ''; this.attrs = {}; this.children = []; this.text = ''; } return Node; }(); var Helper = /** @class */ function () { function Helper(input) { this.input = ''; this.index = 0; this.input = input; } Helper.prototype.stepNext = function () { this.index++; }; Helper.prototype.cur = function () { return this.input[this.index]; }; Helper.prototype.twoChar = function () { return this.input[this.index] + this.input[this.index + 1]; }; Helper.prototype.stepWhile = function (test) { var s = ''; while (this.index < this.input.length && test && test(this.input[this.index])) { s += this.input[this.index++]; } return s; }; Helper.prototype.skipWhiteSpace = function () { this.stepWhile(function (letter) { return /\s/.test(letter); }); }; Helper.prototype.getText = function () { return this.stepWhile(function (letter) { return /[a-zA-Z0-9_]/.test(letter); }); }; Helper.prototype.eof = function () { return this.index >= this.input.length; }; return Helper; }(); var HtmlParser = /** @class */ function () { function HtmlParser(input) { this.helper = new Helper(input); } HtmlParser.prototype.parse = function () { return this.parseNodes(); }; HtmlParser.prototype.parseNodes = function () { var nodes = []; while (true) { nodes.push(this.parseNode()); this.helper.skipWhiteSpace(); if (this.helper.eof() || this.helper.twoChar() === '</') { break; } } return nodes; }; HtmlParser.prototype.parseNode = function () { this.helper.skipWhiteSpace(); if (this.helper.cur() === '<') { return this.parseElement(); } else { return this.parseTextNode(); } }; HtmlParser.prototype.parseElement = function () { var _a; var node = new Node(); node.type = 'element'; this.helper.stepNext(); var startTag = this.helper.getText(); node.tagName = startTag; node.attrs = this.parseAttributes(); // 自闭 if (this.helper.twoChar() === '/>') { this.helper.stepNext(); this.helper.stepNext(); return node; } // 非自闭 if (this.helper.cur() !== '>') throw new Error('解析标签开始失败'); this.helper.stepNext(); this.helper.skipWhiteSpace(); // 排除空标签 if (this.helper.twoChar() !== '</') { (_a = node.children).push.apply(_a, this.parseNodes()); this.helper.skipWhiteSpace(); } if (this.helper.twoChar() !== '</') throw new Error('解析标签结束失败'); this.helper.stepNext(); this.helper.stepNext(); var endTag = this.helper.getText(); if (endTag !== startTag) throw new Error('解析标签结束失败'); if (this.helper.cur() !== '>') throw new Error('解析标签结束失败'); this.helper.stepNext(); return node; }; HtmlParser.prototype.parseTextNode = function () { var node = new Node(); var text = this.helper.stepWhile(function (s) { return /[^<]/.test(s); }); node.type = 'text'; node.tagName = 'text'; node.text = text; return node; }; HtmlParser.prototype.parseAttributes = function () { var attrs = {}; this.helper.skipWhiteSpace(); while (!this.helper.eof() && this.helper.cur() !== '>' && this.helper.twoChar() !== '/>') { var name_1 = this.helper.stepWhile(function (letter) { return /[^\s=]/.test(letter); }); this.helper.skipWhiteSpace(); this.helper.stepNext(); this.helper.skipWhiteSpace(); var value = ''; if (this.helper.cur() === '"') { this.helper.stepNext(); value = this.helper.stepWhile(function (letter) { return /[^"]/.test(letter); }); this.helper.stepNext(); } else if (this.helper.cur() === "'") { this.helper.stepNext(); value = this.helper.stepWhile(function (letter) { return /[^']/.test(letter); }); this.helper.stepNext(); } else { value = this.helper.stepWhile(function (letter) { return /[^\s</>/]/.test(letter); }); } attrs[name_1] = parseAttr(name_1, value); this.helper.skipWhiteSpace(); } return attrs; }; HtmlParser.prototype.removeQuote = function (value) { var result = value; if (/^["']/.test(result[0])) { result = result.slice(1); } if (/["']$/.test(result[result.length - 1])) { result = result.slice(0, -1); } return result; }; return HtmlParser; }(); export default function parser(html, isNeedRoot) { var _a; if (isNeedRoot === void 0) { isNeedRoot = true; } var nodes = new HtmlParser(html).parse(); var rootNode = nodes[0]; if (((_a = nodes[0]) === null || _a === void 0 ? void 0 : _a.tagName) !== 'root' && isNeedRoot) { rootNode = new Node(); rootNode.tagName = 'root'; rootNode.children = nodes; } return rootNode; }