UNPKG

very-small-parser

Version:

A very small Markdown, HTML, and CSS parser.

46 lines (45 loc) 1.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HtmlParser = void 0; const Parser_1 = require("../Parser"); const util_1 = require("../util"); const parsers_1 = require("./parsers"); class HtmlParser extends Parser_1.Parser { constructor(opts) { super(opts); this.first = (0, util_1.first)(this.parsers); } parse(src) { const children = []; const end = src.length; let remaining = src; let length = 0; while (length < end) { const tok = this.first(this, remaining); if (!tok) { const textToken = { type: 'text', value: remaining[0], len: 1, }; children.push(textToken); length += 1; remaining = remaining.slice(1); continue; } children.push(tok); length += tok.len || 0; remaining = remaining.slice(tok.len); } return children; } parsef(src) { const [children, len] = (0, util_1.loop0)(this, this.first, src); const root = { type: 'root', children, len }; return root; } el(src) { return (0, parsers_1.el)(this, src); } } exports.HtmlParser = HtmlParser;