@solidjs/html 2.0.0-experimental.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +84 -0
- package/dist/html.cjs +587 -0
- package/dist/html.js +692 -0
- package/package.json +47 -0
- package/types/index.d.ts +3 -0
- package/types/lit.d.ts +57 -0
package/dist/html.js
ADDED
|
@@ -0,0 +1,692 @@
|
|
|
1
|
+
import {
|
|
2
|
+
effect,
|
|
3
|
+
style,
|
|
4
|
+
insert,
|
|
5
|
+
untrack,
|
|
6
|
+
spread,
|
|
7
|
+
createComponent,
|
|
8
|
+
delegateEvents,
|
|
9
|
+
className,
|
|
10
|
+
mergeProps,
|
|
11
|
+
dynamicProperty,
|
|
12
|
+
setAttribute,
|
|
13
|
+
setAttributeNS,
|
|
14
|
+
addEventListener,
|
|
15
|
+
getPropAlias,
|
|
16
|
+
Properties,
|
|
17
|
+
ChildProperties,
|
|
18
|
+
DelegatedEvents,
|
|
19
|
+
SVGElements,
|
|
20
|
+
SVGNamespace
|
|
21
|
+
} from "@solidjs/web";
|
|
22
|
+
|
|
23
|
+
const tagRE = /(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g;
|
|
24
|
+
const attrRE =
|
|
25
|
+
/(?:\s(?<boolean>[^/\s><=]+?)(?=[\s/>]))|(?:(?<name>\S+?)(?:\s*=\s*(?:(['"])(?<quotedValue>[\s\S]*?)\3|(?<unquotedValue>[^\s>]+))))/g;
|
|
26
|
+
const lookup = {
|
|
27
|
+
area: true,
|
|
28
|
+
base: true,
|
|
29
|
+
br: true,
|
|
30
|
+
col: true,
|
|
31
|
+
embed: true,
|
|
32
|
+
hr: true,
|
|
33
|
+
img: true,
|
|
34
|
+
input: true,
|
|
35
|
+
keygen: true,
|
|
36
|
+
link: true,
|
|
37
|
+
menuitem: true,
|
|
38
|
+
meta: true,
|
|
39
|
+
param: true,
|
|
40
|
+
source: true,
|
|
41
|
+
track: true,
|
|
42
|
+
wbr: true
|
|
43
|
+
};
|
|
44
|
+
function parseTag(tag) {
|
|
45
|
+
const res = {
|
|
46
|
+
type: "tag",
|
|
47
|
+
name: "",
|
|
48
|
+
voidElement: false,
|
|
49
|
+
attrs: [],
|
|
50
|
+
children: []
|
|
51
|
+
};
|
|
52
|
+
const tagMatch = tag.match(/<\/?([^\s]+?)[/\s>]/);
|
|
53
|
+
if (tagMatch) {
|
|
54
|
+
res.name = tagMatch[1];
|
|
55
|
+
if (lookup[tagMatch[1].toLowerCase()] || tag.charAt(tag.length - 2) === "/") {
|
|
56
|
+
res.voidElement = true;
|
|
57
|
+
}
|
|
58
|
+
if (res.name.startsWith("!--")) {
|
|
59
|
+
const endIndex = tag.indexOf("-->");
|
|
60
|
+
return {
|
|
61
|
+
type: "comment",
|
|
62
|
+
comment: endIndex !== -1 ? tag.slice(4, endIndex) : ""
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const reg = new RegExp(attrRE);
|
|
67
|
+
for (const match of tag.matchAll(reg)) {
|
|
68
|
+
if ((match[1] || match[2]).startsWith("use:")) {
|
|
69
|
+
res.attrs.push({
|
|
70
|
+
type: "directive",
|
|
71
|
+
name: match[1] || match[2],
|
|
72
|
+
value: match[4] || match[5] || ""
|
|
73
|
+
});
|
|
74
|
+
} else {
|
|
75
|
+
res.attrs.push({
|
|
76
|
+
type: "attr",
|
|
77
|
+
name: match[1] || match[2],
|
|
78
|
+
value: match[4] || match[5] || ""
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return res;
|
|
83
|
+
}
|
|
84
|
+
function pushTextNode(list, html, start) {
|
|
85
|
+
const end = html.indexOf("<", start);
|
|
86
|
+
const content = html.slice(start, end === -1 ? void 0 : end);
|
|
87
|
+
if (!/^\s*$/.test(content)) {
|
|
88
|
+
list.push({
|
|
89
|
+
type: "text",
|
|
90
|
+
content: content
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function pushCommentNode(list, tag) {
|
|
95
|
+
const content = tag.replace("<!--", "").replace("-->", "");
|
|
96
|
+
if (!/^\s*$/.test(content)) {
|
|
97
|
+
list.push({
|
|
98
|
+
type: "comment",
|
|
99
|
+
content: content
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function parse(html) {
|
|
104
|
+
const result = [];
|
|
105
|
+
let current = void 0;
|
|
106
|
+
let level = -1;
|
|
107
|
+
const arr = [];
|
|
108
|
+
const byTag = {};
|
|
109
|
+
html.replace(tagRE, (tag, index) => {
|
|
110
|
+
const isOpen = tag.charAt(1) !== "/";
|
|
111
|
+
const isComment = tag.slice(0, 4) === "<!--";
|
|
112
|
+
const start = index + tag.length;
|
|
113
|
+
const nextChar = html.charAt(start);
|
|
114
|
+
let parent = void 0;
|
|
115
|
+
if (isOpen && !isComment) {
|
|
116
|
+
level++;
|
|
117
|
+
current = parseTag(tag);
|
|
118
|
+
if (!current.voidElement && nextChar && nextChar !== "<") {
|
|
119
|
+
pushTextNode(current.children, html, start);
|
|
120
|
+
}
|
|
121
|
+
byTag[current.tagName] = current;
|
|
122
|
+
if (level === 0) {
|
|
123
|
+
result.push(current);
|
|
124
|
+
}
|
|
125
|
+
parent = arr[level - 1];
|
|
126
|
+
if (parent) {
|
|
127
|
+
parent.children.push(current);
|
|
128
|
+
}
|
|
129
|
+
arr[level] = current;
|
|
130
|
+
}
|
|
131
|
+
if (isComment) {
|
|
132
|
+
if (level < 0) {
|
|
133
|
+
pushCommentNode(result, tag);
|
|
134
|
+
} else {
|
|
135
|
+
pushCommentNode(arr[level].children, tag);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (isComment || !isOpen || current.voidElement) {
|
|
139
|
+
if (!isComment) {
|
|
140
|
+
level--;
|
|
141
|
+
}
|
|
142
|
+
if (nextChar !== "<" && nextChar) {
|
|
143
|
+
parent = level === -1 ? result : arr[level].children;
|
|
144
|
+
pushTextNode(parent, html, start);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
function attrString(attrs) {
|
|
151
|
+
const buff = [];
|
|
152
|
+
for (const attr of attrs) {
|
|
153
|
+
buff.push(attr.name + '="' + attr.value.replace(/"/g, """) + '"');
|
|
154
|
+
}
|
|
155
|
+
if (!buff.length) {
|
|
156
|
+
return "";
|
|
157
|
+
}
|
|
158
|
+
return " " + buff.join(" ");
|
|
159
|
+
}
|
|
160
|
+
function stringifier(buff, doc) {
|
|
161
|
+
switch (doc.type) {
|
|
162
|
+
case "text":
|
|
163
|
+
return buff + doc.content;
|
|
164
|
+
case "tag":
|
|
165
|
+
buff +=
|
|
166
|
+
"<" + doc.name + (doc.attrs ? attrString(doc.attrs) : "") + (doc.voidElement ? "/>" : ">");
|
|
167
|
+
if (doc.voidElement) {
|
|
168
|
+
return buff;
|
|
169
|
+
}
|
|
170
|
+
return buff + doc.children.reduce(stringifier, "") + "</" + doc.name + ">";
|
|
171
|
+
case "comment":
|
|
172
|
+
return (buff += "<!--" + doc.content + "-->");
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
function stringify(doc) {
|
|
176
|
+
return doc.reduce(function (token, rootEl) {
|
|
177
|
+
return token + stringifier("", rootEl);
|
|
178
|
+
}, "");
|
|
179
|
+
}
|
|
180
|
+
const cache = new Map();
|
|
181
|
+
const VOID_ELEMENTS =
|
|
182
|
+
/^(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)$/i;
|
|
183
|
+
const spaces = " \\f\\n\\r\\t";
|
|
184
|
+
const almostEverything = "[^" + spaces + "\\/>\"'=]+";
|
|
185
|
+
const attrName = "[ " + spaces + "]+(?:use:<!--#-->|" + almostEverything + ")";
|
|
186
|
+
const tagName = "<([A-Za-z$#]+[A-Za-z0-9:_-]*)((?:";
|
|
187
|
+
const attrPartials =
|
|
188
|
+
"(?:\\s*=\\s*(?:'[^']*?'|\"[^\"]*?\"|\\([^)]*?\\)|<[^>]*?>|" + almostEverything + "))?)";
|
|
189
|
+
const attrSeeker = new RegExp(tagName + attrName + attrPartials + "+)([ " + spaces + "]*/?>)", "g");
|
|
190
|
+
const findAttributes = new RegExp(
|
|
191
|
+
"(" + attrName + "\\s*=\\s*)(<!--#-->|['\"(]([\\w\\s]*<!--#-->[\\w\\s]*)*['\")])",
|
|
192
|
+
"gi"
|
|
193
|
+
);
|
|
194
|
+
const selfClosing = new RegExp(tagName + attrName + attrPartials + "*)([ " + spaces + "]*/>)", "g");
|
|
195
|
+
const marker = "<!--#-->";
|
|
196
|
+
const reservedNameSpaces = new Set(["class", "on", "style", "use", "prop", "attr"]);
|
|
197
|
+
function attrReplacer($0, $1, $2, $3) {
|
|
198
|
+
return "<" + $1 + $2.replace(findAttributes, replaceAttributes) + $3;
|
|
199
|
+
}
|
|
200
|
+
function replaceAttributes($0, $1, $2) {
|
|
201
|
+
return (
|
|
202
|
+
$1.replace(/<!--#-->/g, "###") +
|
|
203
|
+
($2[0] === '"' || $2[0] === "'" ? $2.replace(/<!--#-->/g, "###") : '"###"')
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
function fullClosing($0, $1, $2) {
|
|
207
|
+
return VOID_ELEMENTS.test($1) ? $0 : "<" + $1 + $2 + "></" + $1 + ">";
|
|
208
|
+
}
|
|
209
|
+
function parseDirective(name, value, tag, options) {
|
|
210
|
+
if (name === "use:###" && value === "###") {
|
|
211
|
+
const count = options.counter++;
|
|
212
|
+
options.exprs.push(
|
|
213
|
+
`typeof exprs[${count}] === "function" ? r.use(exprs[${count}], ${tag}, exprs[${options.counter++}]) : (()=>{throw new Error("use:### must be a function")})()`
|
|
214
|
+
);
|
|
215
|
+
} else {
|
|
216
|
+
throw new Error(`Not support syntax ${name} must be use:{function}`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
function createHTML(
|
|
220
|
+
r,
|
|
221
|
+
{ delegateEvents = true, functionBuilder = (...args) => new Function(...args) } = {}
|
|
222
|
+
) {
|
|
223
|
+
let uuid = 1;
|
|
224
|
+
r.wrapProps = props => {
|
|
225
|
+
const d = Object.getOwnPropertyDescriptors(props);
|
|
226
|
+
for (const k in d) {
|
|
227
|
+
if (typeof d[k].value === "function" && !d[k].value.length) r.dynamicProperty(props, k);
|
|
228
|
+
}
|
|
229
|
+
return props;
|
|
230
|
+
};
|
|
231
|
+
r.resolveFn = fn => (typeof fn === "function" ? fn() : fn);
|
|
232
|
+
function createTemplate(statics, opt) {
|
|
233
|
+
let i = 0,
|
|
234
|
+
markup = "";
|
|
235
|
+
for (; i < statics.length - 1; i++) {
|
|
236
|
+
markup = markup + statics[i] + "<!--#-->";
|
|
237
|
+
}
|
|
238
|
+
markup = markup + statics[i];
|
|
239
|
+
const replaceList = [
|
|
240
|
+
[selfClosing, fullClosing],
|
|
241
|
+
[/<(<!--#-->)/g, "<###"],
|
|
242
|
+
[/\.\.\.(<!--#-->)/g, "###"],
|
|
243
|
+
[attrSeeker, attrReplacer],
|
|
244
|
+
[/>\n+\s*/g, ">"],
|
|
245
|
+
[/\n+\s*</g, "<"],
|
|
246
|
+
[/\s+</g, " <"],
|
|
247
|
+
[/>\s+/g, "> "]
|
|
248
|
+
];
|
|
249
|
+
markup = replaceList.reduce((acc, x) => {
|
|
250
|
+
return acc.replace(x[0], x[1]);
|
|
251
|
+
}, markup);
|
|
252
|
+
const pars = parse(markup);
|
|
253
|
+
const [html, code] = parseTemplate(pars, opt.funcBuilder),
|
|
254
|
+
templates = [];
|
|
255
|
+
for (let i = 0; i < html.length; i++) {
|
|
256
|
+
templates.push(document.createElement("template"));
|
|
257
|
+
templates[i].innerHTML = html[i];
|
|
258
|
+
const nomarkers = templates[i].content.querySelectorAll("script,style");
|
|
259
|
+
for (let j = 0; j < nomarkers.length; j++) {
|
|
260
|
+
const d = nomarkers[j].firstChild?.data || "";
|
|
261
|
+
if (d.indexOf(marker) > -1) {
|
|
262
|
+
const parts = d.split(marker).reduce((memo, p, i) => {
|
|
263
|
+
i && memo.push("");
|
|
264
|
+
memo.push(p);
|
|
265
|
+
return memo;
|
|
266
|
+
}, []);
|
|
267
|
+
nomarkers[i].firstChild.replaceWith(...parts);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
templates[0].create = code;
|
|
272
|
+
cache.set(statics, templates);
|
|
273
|
+
return templates;
|
|
274
|
+
}
|
|
275
|
+
function parseKeyValue(node, tag, name, value, isSVG, options) {
|
|
276
|
+
let expr, parts, namespace;
|
|
277
|
+
if (value === "###") {
|
|
278
|
+
expr = `_$v`;
|
|
279
|
+
options.counter++;
|
|
280
|
+
} else {
|
|
281
|
+
const chunks = value.split("###");
|
|
282
|
+
options.counter = chunks.length - 1 + options.counter;
|
|
283
|
+
expr = chunks.map((v, i) => (i ? ` + _$v[${i - 1}] + "${v}"` : `"${v}"`)).join("");
|
|
284
|
+
}
|
|
285
|
+
if ((parts = name.split(":")) && parts[1] && reservedNameSpaces.has(parts[0])) {
|
|
286
|
+
name = parts[1];
|
|
287
|
+
namespace = parts[0];
|
|
288
|
+
}
|
|
289
|
+
const isChildProp = r.ChildProperties.has(name);
|
|
290
|
+
const isProp = r.Properties.has(name);
|
|
291
|
+
if (name === "style") {
|
|
292
|
+
options.exprs.push(`r.style(${tag},${expr},_$p)`);
|
|
293
|
+
} else if (name === "class") {
|
|
294
|
+
options.exprs.push(`r.className(${tag},${expr},${isSVG},_$p)`);
|
|
295
|
+
} else if (
|
|
296
|
+
namespace !== "attr" &&
|
|
297
|
+
(isChildProp ||
|
|
298
|
+
(!isSVG && (r.getPropAlias(name, node.name.toUpperCase()) || isProp)) ||
|
|
299
|
+
namespace === "prop")
|
|
300
|
+
) {
|
|
301
|
+
options.exprs.push(
|
|
302
|
+
`${tag}.${r.getPropAlias(name, node.name.toUpperCase()) || name} = ${expr}`
|
|
303
|
+
);
|
|
304
|
+
} else {
|
|
305
|
+
const ns = isSVG && name.indexOf(":") > -1 && r.SVGNamespace[name.split(":")[0]];
|
|
306
|
+
if (ns) options.exprs.push(`r.setAttributeNS(${tag},"${ns}","${name}",${expr})`);
|
|
307
|
+
else options.exprs.push(`r.setAttribute(${tag},"${name}",${expr})`);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
function parseAttribute(node, tag, name, value, isSVG, options) {
|
|
311
|
+
if (name.slice(0, 2) === "on") {
|
|
312
|
+
if (!name.includes(":")) {
|
|
313
|
+
const lc = name.slice(2).toLowerCase();
|
|
314
|
+
const delegate = delegateEvents && r.DelegatedEvents.has(lc);
|
|
315
|
+
options.exprs.push(
|
|
316
|
+
`r.addEventListener(${tag},"${lc}",exprs[${options.counter++}],${delegate})`
|
|
317
|
+
);
|
|
318
|
+
delegate && options.delegatedEvents.add(lc);
|
|
319
|
+
} else {
|
|
320
|
+
options.exprs.push(
|
|
321
|
+
`${tag}.addEventListener("${name.slice(3)}",exprs[${options.counter++}])`
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
} else if (name === "ref") {
|
|
325
|
+
options.exprs.push(`exprs[${options.counter++}](${tag})`);
|
|
326
|
+
} else {
|
|
327
|
+
const childOptions = Object.assign({}, options, {
|
|
328
|
+
exprs: []
|
|
329
|
+
}),
|
|
330
|
+
count = options.counter;
|
|
331
|
+
parseKeyValue(node, tag, name, value, isSVG, childOptions);
|
|
332
|
+
options.decl.push(`_fn${count} = (_$v, _$p) => {\n${childOptions.exprs.join(";\n")};\n}`);
|
|
333
|
+
if (value === "###") {
|
|
334
|
+
options.exprs.push(
|
|
335
|
+
`typeof exprs[${count}] === "function" ? r.effect(() => exprs[${count}](), _fn${count}) : _fn${count}(exprs[${count}])`
|
|
336
|
+
);
|
|
337
|
+
} else {
|
|
338
|
+
let check = "";
|
|
339
|
+
let list = "";
|
|
340
|
+
let reactiveList = "";
|
|
341
|
+
for (let i = count; i < childOptions.counter; i++) {
|
|
342
|
+
if (i !== count) {
|
|
343
|
+
check += " || ";
|
|
344
|
+
list += ",";
|
|
345
|
+
reactiveList += ",";
|
|
346
|
+
}
|
|
347
|
+
check += `typeof exprs[${i}] === "function"`;
|
|
348
|
+
list += `exprs[${i}]`;
|
|
349
|
+
reactiveList += `r.resolveFn(exprs[${i}])`;
|
|
350
|
+
}
|
|
351
|
+
options.exprs.push(
|
|
352
|
+
check + ` ? r.effect(() => [${reactiveList}], _fn${count}) : _fn${count}([${list}])`
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
options.counter = childOptions.counter;
|
|
356
|
+
options.wrap = false;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
function processChildren(node, options) {
|
|
360
|
+
const childOptions = Object.assign({}, options, {
|
|
361
|
+
first: true,
|
|
362
|
+
multi: false,
|
|
363
|
+
parent: options.path
|
|
364
|
+
});
|
|
365
|
+
if (node.children.length > 1) {
|
|
366
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
367
|
+
const child = node.children[i];
|
|
368
|
+
if (
|
|
369
|
+
(child.type === "comment" && child.content === "#") ||
|
|
370
|
+
(child.type === "tag" && child.name === "###")
|
|
371
|
+
) {
|
|
372
|
+
childOptions.multi = true;
|
|
373
|
+
break;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
let i = 0;
|
|
378
|
+
while (i < node.children.length) {
|
|
379
|
+
const child = node.children[i];
|
|
380
|
+
if (child.name === "###") {
|
|
381
|
+
if (childOptions.multi) {
|
|
382
|
+
node.children[i] = {
|
|
383
|
+
type: "comment",
|
|
384
|
+
content: "#"
|
|
385
|
+
};
|
|
386
|
+
i++;
|
|
387
|
+
} else node.children.splice(i, 1);
|
|
388
|
+
processComponent(child, childOptions);
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
parseNode(child, childOptions);
|
|
392
|
+
if (!childOptions.multi && child.type === "comment" && child.content === "#")
|
|
393
|
+
node.children.splice(i, 1);
|
|
394
|
+
else i++;
|
|
395
|
+
}
|
|
396
|
+
options.counter = childOptions.counter;
|
|
397
|
+
options.templateId = childOptions.templateId;
|
|
398
|
+
options.hasCustomElement = options.hasCustomElement || childOptions.hasCustomElement;
|
|
399
|
+
options.isImportNode = options.isImportNode || childOptions.isImportNode;
|
|
400
|
+
}
|
|
401
|
+
function processComponentProps(propGroups) {
|
|
402
|
+
let result = [];
|
|
403
|
+
for (const props of propGroups) {
|
|
404
|
+
if (Array.isArray(props)) {
|
|
405
|
+
if (!props.length) continue;
|
|
406
|
+
result.push(`r.wrapProps({${props.join(",") || ""}})`);
|
|
407
|
+
} else result.push(props);
|
|
408
|
+
}
|
|
409
|
+
return result.length > 1 ? `r.mergeProps(${result.join(",")})` : result[0];
|
|
410
|
+
}
|
|
411
|
+
function processComponent(node, options) {
|
|
412
|
+
let props = [];
|
|
413
|
+
const keys = Object.keys(node.attrs),
|
|
414
|
+
propGroups = [props],
|
|
415
|
+
componentIdentifier = options.counter++;
|
|
416
|
+
for (let i = 0; i < keys.length; i++) {
|
|
417
|
+
const { type, name, value } = node.attrs[i];
|
|
418
|
+
if (type === "attr") {
|
|
419
|
+
if (name === "###") {
|
|
420
|
+
propGroups.push(`exprs[${options.counter++}]`);
|
|
421
|
+
propGroups.push((props = []));
|
|
422
|
+
} else if (value === "###") {
|
|
423
|
+
props.push(`${name}: exprs[${options.counter++}]`);
|
|
424
|
+
} else props.push(`${name}: "${value}"`);
|
|
425
|
+
} else if (type === "directive") {
|
|
426
|
+
const tag = `_$el${uuid++}`;
|
|
427
|
+
const topDecl = !options.decl.length;
|
|
428
|
+
options.decl.push(
|
|
429
|
+
topDecl ? "" : `${tag} = ${options.path}.${options.first ? "firstChild" : "nextSibling"}`
|
|
430
|
+
);
|
|
431
|
+
parseDirective(name, value, tag, options);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
if (
|
|
435
|
+
node.children.length === 1 &&
|
|
436
|
+
node.children[0].type === "comment" &&
|
|
437
|
+
node.children[0].content === "#"
|
|
438
|
+
) {
|
|
439
|
+
props.push(`children: () => exprs[${options.counter++}]`);
|
|
440
|
+
} else if (node.children.length) {
|
|
441
|
+
const children = {
|
|
442
|
+
type: "fragment",
|
|
443
|
+
children: node.children
|
|
444
|
+
},
|
|
445
|
+
childOptions = Object.assign({}, options, {
|
|
446
|
+
first: true,
|
|
447
|
+
decl: [],
|
|
448
|
+
exprs: [],
|
|
449
|
+
parent: false
|
|
450
|
+
});
|
|
451
|
+
parseNode(children, childOptions);
|
|
452
|
+
props.push(`children: () => { ${childOptions.exprs.join(";\n")}}`);
|
|
453
|
+
options.templateId = childOptions.templateId;
|
|
454
|
+
options.counter = childOptions.counter;
|
|
455
|
+
}
|
|
456
|
+
let tag;
|
|
457
|
+
if (options.multi) {
|
|
458
|
+
tag = `_$el${uuid++}`;
|
|
459
|
+
options.decl.push(`${tag} = ${options.path}.${options.first ? "firstChild" : "nextSibling"}`);
|
|
460
|
+
}
|
|
461
|
+
if (options.parent)
|
|
462
|
+
options.exprs.push(
|
|
463
|
+
`r.insert(${
|
|
464
|
+
options.parent
|
|
465
|
+
}, r.createComponent(exprs[${componentIdentifier}],${processComponentProps(propGroups)})${
|
|
466
|
+
tag ? `, ${tag}` : ""
|
|
467
|
+
})`
|
|
468
|
+
);
|
|
469
|
+
else
|
|
470
|
+
options.exprs.push(
|
|
471
|
+
`${
|
|
472
|
+
options.fragment ? "" : "return "
|
|
473
|
+
}r.createComponent(exprs[${componentIdentifier}],${processComponentProps(propGroups)})`
|
|
474
|
+
);
|
|
475
|
+
options.path = tag;
|
|
476
|
+
options.first = false;
|
|
477
|
+
}
|
|
478
|
+
function parseNode(node, options) {
|
|
479
|
+
if (node.type === "fragment") {
|
|
480
|
+
const parts = [];
|
|
481
|
+
node.children.forEach(child => {
|
|
482
|
+
if (child.type === "tag") {
|
|
483
|
+
if (child.name === "###") {
|
|
484
|
+
const childOptions = Object.assign({}, options, {
|
|
485
|
+
first: true,
|
|
486
|
+
fragment: true,
|
|
487
|
+
decl: [],
|
|
488
|
+
exprs: []
|
|
489
|
+
});
|
|
490
|
+
processComponent(child, childOptions);
|
|
491
|
+
parts.push(childOptions.exprs[0]);
|
|
492
|
+
options.counter = childOptions.counter;
|
|
493
|
+
options.templateId = childOptions.templateId;
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
options.templateId++;
|
|
497
|
+
const id = uuid;
|
|
498
|
+
const childOptions = Object.assign({}, options, {
|
|
499
|
+
first: true,
|
|
500
|
+
decl: [],
|
|
501
|
+
exprs: []
|
|
502
|
+
});
|
|
503
|
+
options.templateNodes.push([child]);
|
|
504
|
+
parseNode(child, childOptions);
|
|
505
|
+
parts.push(
|
|
506
|
+
`function() { ${
|
|
507
|
+
childOptions.decl.join(",\n") +
|
|
508
|
+
";\n" +
|
|
509
|
+
childOptions.exprs.join(";\n") +
|
|
510
|
+
`;\nreturn _$el${id};\n`
|
|
511
|
+
}}()`
|
|
512
|
+
);
|
|
513
|
+
options.counter = childOptions.counter;
|
|
514
|
+
options.templateId = childOptions.templateId;
|
|
515
|
+
} else if (child.type === "text") {
|
|
516
|
+
parts.push(`"${child.content}"`);
|
|
517
|
+
} else if (child.type === "comment") {
|
|
518
|
+
if (child.content === "#") parts.push(`exprs[${options.counter++}]`);
|
|
519
|
+
else if (child.content) {
|
|
520
|
+
for (let i = 0; i < child.content.split("###").length - 1; i++) {
|
|
521
|
+
parts.push(`exprs[${options.counter++}]`);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
});
|
|
526
|
+
options.exprs.push(`return [${parts.join(", \n")}]`);
|
|
527
|
+
} else if (node.type === "tag") {
|
|
528
|
+
const tag = `_$el${uuid++}`;
|
|
529
|
+
const topDecl = !options.decl.length;
|
|
530
|
+
const templateId = options.templateId;
|
|
531
|
+
options.decl.push(
|
|
532
|
+
topDecl ? "" : `${tag} = ${options.path}.${options.first ? "firstChild" : "nextSibling"}`
|
|
533
|
+
);
|
|
534
|
+
const isSVG = r.SVGElements.has(node.name);
|
|
535
|
+
options.hasCustomElement = node.name.includes("-") || node.attrs.some(e => e.name === "is");
|
|
536
|
+
options.isImportNode =
|
|
537
|
+
(node.name === "img" || node.name === "iframe") &&
|
|
538
|
+
node.attrs.some(e => e.name === "loading" && e.value === "lazy");
|
|
539
|
+
if (node.attrs.some(e => e.name === "###")) {
|
|
540
|
+
const spreadArgs = [];
|
|
541
|
+
let current = "";
|
|
542
|
+
const newAttrs = [];
|
|
543
|
+
for (let i = 0; i < node.attrs.length; i++) {
|
|
544
|
+
const { type, name, value } = node.attrs[i];
|
|
545
|
+
if (type === "attr") {
|
|
546
|
+
if (value.includes("###")) {
|
|
547
|
+
let count = options.counter++;
|
|
548
|
+
current += `${name}: ${
|
|
549
|
+
name !== "ref" ? `typeof exprs[${count}] === "function" ? exprs[${count}]() : ` : ""
|
|
550
|
+
}exprs[${count}],`;
|
|
551
|
+
} else if (name === "###") {
|
|
552
|
+
if (current.length) {
|
|
553
|
+
spreadArgs.push(`()=>({${current}})`);
|
|
554
|
+
current = "";
|
|
555
|
+
}
|
|
556
|
+
spreadArgs.push(`exprs[${options.counter++}]`);
|
|
557
|
+
} else {
|
|
558
|
+
newAttrs.push(node.attrs[i]);
|
|
559
|
+
}
|
|
560
|
+
} else if (type === "directive") {
|
|
561
|
+
parseDirective(name, value, tag, options);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
node.attrs = newAttrs;
|
|
565
|
+
if (current.length) {
|
|
566
|
+
spreadArgs.push(`()=>({${current}})`);
|
|
567
|
+
}
|
|
568
|
+
options.exprs.push(
|
|
569
|
+
`r.spread(${tag},${
|
|
570
|
+
spreadArgs.length === 1
|
|
571
|
+
? `typeof ${spreadArgs[0]} === "function" ? r.mergeProps(${spreadArgs[0]}) : ${spreadArgs[0]}`
|
|
572
|
+
: `r.mergeProps(${spreadArgs.join(",")})`
|
|
573
|
+
},${isSVG},${!!node.children.length})`
|
|
574
|
+
);
|
|
575
|
+
} else {
|
|
576
|
+
for (let i = 0; i < node.attrs.length; i++) {
|
|
577
|
+
const { type, name, value } = node.attrs[i];
|
|
578
|
+
if (type === "directive") {
|
|
579
|
+
parseDirective(name, value, tag, options);
|
|
580
|
+
node.attrs.splice(i, 1);
|
|
581
|
+
i--;
|
|
582
|
+
} else if (type === "attr") {
|
|
583
|
+
if (value.includes("###")) {
|
|
584
|
+
node.attrs.splice(i, 1);
|
|
585
|
+
i--;
|
|
586
|
+
parseAttribute(node, tag, name, value, isSVG, options);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
options.path = tag;
|
|
592
|
+
options.first = false;
|
|
593
|
+
processChildren(node, options);
|
|
594
|
+
if (topDecl) {
|
|
595
|
+
options.decl[0] =
|
|
596
|
+
options.hasCustomElement || options.isImportNode
|
|
597
|
+
? `const ${tag} = r.untrack(() => document.importNode(tmpls[${templateId}].content.firstChild, true))`
|
|
598
|
+
: `const ${tag} = tmpls[${templateId}].content.firstChild.cloneNode(true)`;
|
|
599
|
+
}
|
|
600
|
+
} else if (node.type === "text") {
|
|
601
|
+
const tag = `_$el${uuid++}`;
|
|
602
|
+
options.decl.push(`${tag} = ${options.path}.${options.first ? "firstChild" : "nextSibling"}`);
|
|
603
|
+
options.path = tag;
|
|
604
|
+
options.first = false;
|
|
605
|
+
} else if (node.type === "comment") {
|
|
606
|
+
const tag = `_$el${uuid++}`;
|
|
607
|
+
options.decl.push(`${tag} = ${options.path}.${options.first ? "firstChild" : "nextSibling"}`);
|
|
608
|
+
if (node.content === "#") {
|
|
609
|
+
if (options.multi) {
|
|
610
|
+
options.exprs.push(`r.insert(${options.parent}, exprs[${options.counter++}], ${tag})`);
|
|
611
|
+
} else options.exprs.push(`r.insert(${options.parent}, exprs[${options.counter++}])`);
|
|
612
|
+
}
|
|
613
|
+
options.path = tag;
|
|
614
|
+
options.first = false;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
function parseTemplate(nodes, funcBuilder) {
|
|
618
|
+
const options = {
|
|
619
|
+
path: "",
|
|
620
|
+
decl: [],
|
|
621
|
+
exprs: [],
|
|
622
|
+
delegatedEvents: new Set(),
|
|
623
|
+
counter: 0,
|
|
624
|
+
first: true,
|
|
625
|
+
multi: false,
|
|
626
|
+
templateId: 0,
|
|
627
|
+
templateNodes: []
|
|
628
|
+
},
|
|
629
|
+
id = uuid,
|
|
630
|
+
origNodes = nodes;
|
|
631
|
+
let toplevel;
|
|
632
|
+
if (nodes.length > 1) {
|
|
633
|
+
nodes = [
|
|
634
|
+
{
|
|
635
|
+
type: "fragment",
|
|
636
|
+
children: nodes
|
|
637
|
+
}
|
|
638
|
+
];
|
|
639
|
+
}
|
|
640
|
+
if (nodes[0].name === "###") {
|
|
641
|
+
toplevel = true;
|
|
642
|
+
processComponent(nodes[0], options);
|
|
643
|
+
} else parseNode(nodes[0], options);
|
|
644
|
+
r.delegateEvents(Array.from(options.delegatedEvents));
|
|
645
|
+
const templateNodes = [origNodes].concat(options.templateNodes);
|
|
646
|
+
return [
|
|
647
|
+
templateNodes.map(t => stringify(t)),
|
|
648
|
+
funcBuilder(
|
|
649
|
+
"tmpls",
|
|
650
|
+
"exprs",
|
|
651
|
+
"r",
|
|
652
|
+
options.decl.join(",\n") +
|
|
653
|
+
";\n" +
|
|
654
|
+
options.exprs.join(";\n") +
|
|
655
|
+
(toplevel ? "" : `;\nreturn _$el${id};\n`)
|
|
656
|
+
)
|
|
657
|
+
];
|
|
658
|
+
}
|
|
659
|
+
function html(statics, ...args) {
|
|
660
|
+
const templates =
|
|
661
|
+
cache.get(statics) ||
|
|
662
|
+
createTemplate(statics, {
|
|
663
|
+
funcBuilder: functionBuilder
|
|
664
|
+
});
|
|
665
|
+
return templates[0].create(templates, args, r);
|
|
666
|
+
}
|
|
667
|
+
return html;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
const html = createHTML({
|
|
671
|
+
effect,
|
|
672
|
+
style,
|
|
673
|
+
insert,
|
|
674
|
+
untrack,
|
|
675
|
+
spread,
|
|
676
|
+
createComponent,
|
|
677
|
+
delegateEvents,
|
|
678
|
+
className,
|
|
679
|
+
mergeProps,
|
|
680
|
+
dynamicProperty,
|
|
681
|
+
setAttribute,
|
|
682
|
+
setAttributeNS,
|
|
683
|
+
addEventListener,
|
|
684
|
+
getPropAlias,
|
|
685
|
+
Properties,
|
|
686
|
+
ChildProperties,
|
|
687
|
+
DelegatedEvents,
|
|
688
|
+
SVGElements,
|
|
689
|
+
SVGNamespace
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
export { html as default };
|