-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathparser.js
More file actions
162 lines (139 loc) · 3.44 KB
/
parser.js
File metadata and controls
162 lines (139 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import {
Comment,
DocumentType,
Text,
Fragment,
Element,
Component,
fromJSON,
} from '../src/dom/ish.js';
import parser from '../src/parser/index.js';
// import { update } from '../src/dom/update.js';
const parse = parser({
Comment,
DocumentType,
Text,
Fragment,
Element,
Component,
//update,
});
const parseHTML = template => parse(template, false);
const assert = (ok, message) => {
if (!ok) throw new Error(message);
};
const html = (template, ...values) => {
const [node, updates] = parseHTML(template);
// for (let i = 0; i < updates.length; i++) {
// const [path, update, type] = updates[i];
// update(path.reduce(tree, node), values[i]);
// }
const stringified = JSON.stringify(node);
assert(JSON.stringify(fromJSON(JSON.parse(stringified))) === stringified, [
'',
stringified,
JSON.stringify(fromJSON(JSON.parse(stringified))),
].join('\n'));
return node.toString();
};
console.log(html`<div><template><p test=${'ok'} />${'value'}</></div>`);
console.log(html`<table>${[]}</table>`);
console.log(html`
<div hidden>
<!--/* comment */-->
<p class=${'test'}>Hello</p>
<hr />
<svg>
<!--! important comment !-->
<rect hidden />
</svg>
${'World'}
<rect />
<!--/* comment */-->
<!--// comment -->
<!--# comment -->
<!--; comment ;-->
</div>
`);
console.log(html`
<${Object} a="1" b=${2} ?c=${false} ?d=${true}>
<p>Component</p>
</>
`);
console.log(html`
<!doctype html>
<texatea prop="value" />
<style> test </style>
<script> ${'test'} </script>
<p />
`);
console.log(html`
<!-- comment -->
<p />
<table><td /></>
<table><tr class=${'test'} /></>
<p />
`);
console.log(html`<hr><br>`);
console.log(html`<a ...${'props'} /> b`);
// EXPECTED ERRORS
try {
html`\x00`;
}
catch ({ message }) {
assert(message === 'Invalid content: NUL char \\x00 found in template: \\x00', message);
}
try {
html`<p test="a ${'b'} c" />`;
}
catch ({ message }) {
assert(message.startsWith('Invalid test attribute in template definition'), message);
}
try {
html`<p `;
}
catch ({ message }) {
assert(message === 'Unclosed element <p> found in template <p ', message);
}
try {
html`<!-- `;
}
catch ({ message }) {
assert(message === 'Invalid content "<!" found in template: <!-- ', message);
}
try {
html`<!--->`;
}
catch ({ message }) {
assert(message === 'Invalid comment: no closing --> found in template <!--->', message);
}
try {
html`<!loltype gotcha>`;
}
catch ({ message }) {
assert(message === 'Invalid doctype: loltype gotcha found in template <!loltype gotcha>', message);
}
try {
html`<p></`;
}
catch ({ message }) {
assert(message === 'Invalid closing tag: </... found in template: <p></', message);
}
try {
html`<div></div></div>`;
}
catch ({ message }) {
assert(message === 'Too many closing tags found in template <div></div></div>', message);
}
try {
html`<textarea></>`;
}
catch ({ message }) {
assert(message === 'The text only <textarea> element requires explicit </textarea> closing tag in template <textarea></>', message);
}
try {
html`<textarea>a ${'b'} c</textarea>`;
}
catch ({ message }) {
assert(message === 'Mixed text and interpolations found in text only <textarea> element "a \\u0000 c" in template <textarea>a , c</textarea>', message);
}