|
1 | | -import sax from "sax"; |
| 1 | +import { Parser } from "htmlparser2"; |
2 | 2 | import { parse as parseMustaches, isHole } from "./hole.js"; |
3 | 3 | import { create as createVNode, VNode, Props } from "./vnode.js"; |
4 | 4 | import * as logger from "./logger.js"; |
5 | 5 |
|
6 | 6 | /** Parse a template into a simple JSON markup representation */ |
7 | 7 | export const parse = (markup: string): VNode => { |
8 | | - const strict = false; |
9 | | - const parser = sax.parser(strict, { |
10 | | - trim: true, |
11 | | - lowercase: true, |
12 | | - xmlns: false, |
13 | | - }); |
14 | | - |
15 | 8 | let root: VNode = createVNode("documentfragment"); |
16 | 9 | let stack: Array<VNode> = [root]; |
17 | 10 |
|
18 | | - parser.onerror = (error: Error) => { |
19 | | - throw error; |
20 | | - }; |
21 | | - |
22 | | - parser.onopentag = (node) => { |
23 | | - // We've turned off the namespace feature, so node attributes will |
24 | | - // contain only string values, not QualifiedAttribute objects. |
25 | | - const props = parseProps(node.attributes as { [key: string]: string }); |
26 | | - const next = createVNode(node.name, props); |
27 | | - const top = getTop(stack); |
28 | | - if (!top) { |
29 | | - throw new ParseError(`No parent tag for ${node.name}`); |
30 | | - } |
31 | | - top.children.push(next); |
32 | | - stack.push(next); |
33 | | - }; |
34 | | - |
35 | | - parser.onclosetag = (tagName) => { |
36 | | - const node = stack.pop(); |
37 | | - if (!node) { |
38 | | - throw new ParseError(`Unexpected closing tag ${tagName}`); |
39 | | - } |
40 | | - }; |
41 | | - |
42 | | - parser.ontext = (text) => { |
43 | | - const top = getTop(stack); |
44 | | - const parsed = parseMustaches(text); |
45 | | - top.children.push(...parsed); |
46 | | - }; |
47 | | - |
48 | | - parser.write(markup).close(); |
49 | | - |
50 | | - if (getTop(stack) !== root) { |
51 | | - throw new ParseError(`Unexpected root node ${root.tag}`); |
52 | | - } |
53 | | - |
54 | | - logger.debug("Parsed", root); |
| 11 | + const parser = new Parser( |
| 12 | + { |
| 13 | + onopentag(name, attrs) { |
| 14 | + logger.debug("Open", name, attrs); |
| 15 | + // We've turned off the namespace feature, so node attributes will |
| 16 | + // contain only string values, not QualifiedAttribute objects. |
| 17 | + const props = parseProps(attrs as { [key: string]: string }); |
| 18 | + const next = createVNode(name, props); |
| 19 | + const top = getTop(stack); |
| 20 | + if (!top) { |
| 21 | + throw new ParseError(`No parent tag for ${name}`); |
| 22 | + } |
| 23 | + top.children.push(next); |
| 24 | + stack.push(next); |
| 25 | + }, |
| 26 | + onclosetag(name) { |
| 27 | + const vnode = stack.pop(); |
| 28 | + if (!vnode) { |
| 29 | + throw new ParseError(`Unexpected closing tag ${name}`); |
| 30 | + } |
| 31 | + }, |
| 32 | + ontext(text) { |
| 33 | + const top = getTop(stack); |
| 34 | + const parsed = parseMustaches(text.trim()); |
| 35 | + top.children.push(...parsed); |
| 36 | + }, |
| 37 | + }, |
| 38 | + { |
| 39 | + lowerCaseTags: true, |
| 40 | + lowerCaseAttributeNames: true, |
| 41 | + xmlMode: false, |
| 42 | + }, |
| 43 | + ); |
55 | 44 |
|
| 45 | + parser.write(markup); |
| 46 | + parser.end(); |
56 | 47 | return root; |
57 | 48 | }; |
58 | 49 |
|
|
0 commit comments