@solidjs/html 2.0.0-experimental.9 → 2.0.0-rc.1
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/README.md +43 -5
- package/dist/html.cjs +7 -574
- package/dist/html.js +8 -575
- package/package.json +22 -13
- package/types/index.d.ts +3 -0
- package/types/tagged-jsx.d.ts +12567 -0
- package/types-cjs/index.d.cts +3 -0
- package/types-cjs/package.json +3 -0
- package/types-cjs/tagged-jsx.d.cts +12567 -0
package/dist/html.js
CHANGED
|
@@ -1,585 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RawTextElements, VoidElements, MathMLElements, SVGElements, claimElement, mergeProps, createComponent, spread, insert } from '@solidjs/web';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
const lookup = {
|
|
6
|
-
area: true,
|
|
7
|
-
base: true,
|
|
8
|
-
br: true,
|
|
9
|
-
col: true,
|
|
10
|
-
embed: true,
|
|
11
|
-
hr: true,
|
|
12
|
-
img: true,
|
|
13
|
-
input: true,
|
|
14
|
-
keygen: true,
|
|
15
|
-
link: true,
|
|
16
|
-
menuitem: true,
|
|
17
|
-
meta: true,
|
|
18
|
-
param: true,
|
|
19
|
-
source: true,
|
|
20
|
-
track: true,
|
|
21
|
-
wbr: true
|
|
22
|
-
};
|
|
23
|
-
function parseTag(tag) {
|
|
24
|
-
const res = {
|
|
25
|
-
type: 'tag',
|
|
26
|
-
name: '',
|
|
27
|
-
voidElement: false,
|
|
28
|
-
attrs: [],
|
|
29
|
-
children: []
|
|
30
|
-
};
|
|
31
|
-
const tagMatch = tag.match(/<\/?([^\s]+?)[/\s>]/);
|
|
32
|
-
if (tagMatch) {
|
|
33
|
-
res.name = tagMatch[1];
|
|
34
|
-
if (lookup[tagMatch[1].toLowerCase()] || tag.charAt(tag.length - 2) === '/') {
|
|
35
|
-
res.voidElement = true;
|
|
36
|
-
}
|
|
37
|
-
if (res.name.startsWith('!--')) {
|
|
38
|
-
const endIndex = tag.indexOf('-->');
|
|
39
|
-
return {
|
|
40
|
-
type: 'comment',
|
|
41
|
-
comment: endIndex !== -1 ? tag.slice(4, endIndex) : ''
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
const reg = new RegExp(attrRE);
|
|
46
|
-
for (const match of tag.matchAll(reg)) {
|
|
47
|
-
if ((match[1] || match[2]).startsWith('use:')) {
|
|
48
|
-
res.attrs.push({
|
|
49
|
-
type: 'directive',
|
|
50
|
-
name: match[1] || match[2],
|
|
51
|
-
value: match[4] || match[5] || ''
|
|
52
|
-
});
|
|
53
|
-
} else {
|
|
54
|
-
res.attrs.push({
|
|
55
|
-
type: 'attr',
|
|
56
|
-
name: match[1] || match[2],
|
|
57
|
-
value: match[4] || match[5] || ''
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return res;
|
|
62
|
-
}
|
|
63
|
-
function pushTextNode(list, html, start) {
|
|
64
|
-
const end = html.indexOf('<', start);
|
|
65
|
-
const content = html.slice(start, end === -1 ? undefined : end);
|
|
66
|
-
if (!/^\s*$/.test(content)) {
|
|
67
|
-
list.push({
|
|
68
|
-
type: 'text',
|
|
69
|
-
content: content
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
function pushCommentNode(list, tag) {
|
|
74
|
-
const content = tag.replace('<!--', '').replace('-->', '');
|
|
75
|
-
if (!/^\s*$/.test(content)) {
|
|
76
|
-
list.push({
|
|
77
|
-
type: 'comment',
|
|
78
|
-
content: content
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
function parse(html) {
|
|
83
|
-
const result = [];
|
|
84
|
-
let current = undefined;
|
|
85
|
-
let level = -1;
|
|
86
|
-
const arr = [];
|
|
87
|
-
const byTag = {};
|
|
88
|
-
html.replace(tagRE, (tag, index) => {
|
|
89
|
-
const isOpen = tag.charAt(1) !== '/';
|
|
90
|
-
const isComment = tag.slice(0, 4) === '<!--';
|
|
91
|
-
const start = index + tag.length;
|
|
92
|
-
const nextChar = html.charAt(start);
|
|
93
|
-
let parent = undefined;
|
|
94
|
-
if (isOpen && !isComment) {
|
|
95
|
-
level++;
|
|
96
|
-
current = parseTag(tag);
|
|
97
|
-
if (!current.voidElement && nextChar && nextChar !== '<') {
|
|
98
|
-
pushTextNode(current.children, html, start);
|
|
99
|
-
}
|
|
100
|
-
byTag[current.tagName] = current;
|
|
101
|
-
if (level === 0) {
|
|
102
|
-
result.push(current);
|
|
103
|
-
}
|
|
104
|
-
parent = arr[level - 1];
|
|
105
|
-
if (parent) {
|
|
106
|
-
parent.children.push(current);
|
|
107
|
-
}
|
|
108
|
-
arr[level] = current;
|
|
109
|
-
}
|
|
110
|
-
if (isComment) {
|
|
111
|
-
if (level < 0) {
|
|
112
|
-
pushCommentNode(result, tag);
|
|
113
|
-
} else {
|
|
114
|
-
pushCommentNode(arr[level].children, tag);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
if (isComment || !isOpen || current.voidElement) {
|
|
118
|
-
if (!isComment) {
|
|
119
|
-
level--;
|
|
120
|
-
}
|
|
121
|
-
if (nextChar !== '<' && nextChar) {
|
|
122
|
-
parent = level === -1 ? result : arr[level].children;
|
|
123
|
-
pushTextNode(parent, html, start);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
return result;
|
|
128
|
-
}
|
|
129
|
-
function attrString(attrs) {
|
|
130
|
-
const buff = [];
|
|
131
|
-
for (const attr of attrs) {
|
|
132
|
-
buff.push(attr.name + '="' + attr.value.replace(/"/g, '"') + '"');
|
|
133
|
-
}
|
|
134
|
-
if (!buff.length) {
|
|
135
|
-
return '';
|
|
136
|
-
}
|
|
137
|
-
return ' ' + buff.join(' ');
|
|
138
|
-
}
|
|
139
|
-
function stringifier(buff, doc) {
|
|
140
|
-
switch (doc.type) {
|
|
141
|
-
case 'text':
|
|
142
|
-
return buff + doc.content;
|
|
143
|
-
case 'tag':
|
|
144
|
-
buff += '<' + doc.name + (doc.attrs ? attrString(doc.attrs) : '') + (doc.voidElement ? '/>' : '>');
|
|
145
|
-
if (doc.voidElement) {
|
|
146
|
-
return buff;
|
|
147
|
-
}
|
|
148
|
-
return buff + doc.children.reduce(stringifier, '') + '</' + doc.name + '>';
|
|
149
|
-
case 'comment':
|
|
150
|
-
return buff += '<!--' + doc.content + '-->';
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
function stringify(doc) {
|
|
154
|
-
return doc.reduce(function (token, rootEl) {
|
|
155
|
-
return token + stringifier('', rootEl);
|
|
156
|
-
}, '');
|
|
157
|
-
}
|
|
158
|
-
const cache = new Map();
|
|
159
|
-
const VOID_ELEMENTS = /^(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)$/i;
|
|
160
|
-
const spaces = " \\f\\n\\r\\t";
|
|
161
|
-
const almostEverything = "[^" + spaces + "\\/>\"'=]+";
|
|
162
|
-
const attrName = "[ " + spaces + "]+(?:use:<!--#-->|" + almostEverything + ")";
|
|
163
|
-
const tagName = "<([A-Za-z$#]+[A-Za-z0-9:_-]*)((?:";
|
|
164
|
-
const attrPartials = "(?:\\s*=\\s*(?:'[^']*?'|\"[^\"]*?\"|\\([^)]*?\\)|<[^>]*?>|" + almostEverything + "))?)";
|
|
165
|
-
const attrSeeker = new RegExp(tagName + attrName + attrPartials + "+)([ " + spaces + "]*/?>)", "g");
|
|
166
|
-
const findAttributes = new RegExp("(" + attrName + "\\s*=\\s*)(<!--#-->|['\"(]([\\w\\s]*<!--#-->[\\w\\s]*)*['\")])", "gi");
|
|
167
|
-
const selfClosing = new RegExp(tagName + attrName + attrPartials + "*)([ " + spaces + "]*/>)", "g");
|
|
168
|
-
const marker = "<!--#-->";
|
|
169
|
-
const reservedNameSpaces = new Set(["class", "on", "style", "use", "prop"]);
|
|
170
|
-
function attrReplacer($0, $1, $2, $3) {
|
|
171
|
-
return "<" + $1 + $2.replace(findAttributes, replaceAttributes) + $3;
|
|
172
|
-
}
|
|
173
|
-
function replaceAttributes($0, $1, $2) {
|
|
174
|
-
return $1.replace(/<!--#-->/g, "###") + ($2[0] === '"' || $2[0] === "'" ? $2.replace(/<!--#-->/g, "###") : '"###"');
|
|
175
|
-
}
|
|
176
|
-
function fullClosing($0, $1, $2) {
|
|
177
|
-
return VOID_ELEMENTS.test($1) ? $0 : "<" + $1 + $2 + "></" + $1 + ">";
|
|
178
|
-
}
|
|
179
|
-
function parseDirective(name, value, tag, options) {
|
|
180
|
-
if (name === "use:###" && value === "###") {
|
|
181
|
-
const count = options.counter++;
|
|
182
|
-
options.exprs.push(`typeof exprs[${count}] === "function" ? r.use(exprs[${count}], ${tag}, exprs[${options.counter++}]) : (()=>{throw new Error("use:### must be a function")})()`);
|
|
183
|
-
} else {
|
|
184
|
-
throw new Error(`Not support syntax ${name} must be use:{function}`);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
function createHTML(r, {
|
|
188
|
-
delegateEvents = true,
|
|
189
|
-
functionBuilder = (...args) => new Function(...args)
|
|
190
|
-
} = {}) {
|
|
191
|
-
let uuid = 1;
|
|
192
|
-
r.wrapProps = props => {
|
|
193
|
-
const d = Object.getOwnPropertyDescriptors(props);
|
|
194
|
-
for (const k in d) {
|
|
195
|
-
if (typeof d[k].value === "function" && !d[k].value.length) r.dynamicProperty(props, k);
|
|
196
|
-
}
|
|
197
|
-
return props;
|
|
198
|
-
};
|
|
199
|
-
r.resolveFn = fn => typeof fn === "function" ? fn() : fn;
|
|
200
|
-
function createTemplate(statics, opt) {
|
|
201
|
-
let i = 0,
|
|
202
|
-
markup = "";
|
|
203
|
-
for (; i < statics.length - 1; i++) {
|
|
204
|
-
markup = markup + statics[i] + "<!--#-->";
|
|
205
|
-
}
|
|
206
|
-
markup = markup + statics[i];
|
|
207
|
-
const replaceList = [[selfClosing, fullClosing], [/<(<!--#-->)/g, "<###"], [/\.\.\.(<!--#-->)/g, "###"], [attrSeeker, attrReplacer], [/>\n+\s*/g, ">"], [/\n+\s*</g, "<"], [/\s+</g, " <"], [/>\s+/g, "> "]];
|
|
208
|
-
markup = replaceList.reduce((acc, x) => {
|
|
209
|
-
return acc.replace(x[0], x[1]);
|
|
210
|
-
}, markup);
|
|
211
|
-
const pars = parse(markup);
|
|
212
|
-
const [html, code] = parseTemplate(pars, opt.funcBuilder),
|
|
213
|
-
templates = [];
|
|
214
|
-
for (let i = 0; i < html.length; i++) {
|
|
215
|
-
templates.push(document.createElement("template"));
|
|
216
|
-
templates[i].innerHTML = html[i];
|
|
217
|
-
const nomarkers = templates[i].content.querySelectorAll("script,style");
|
|
218
|
-
for (let j = 0; j < nomarkers.length; j++) {
|
|
219
|
-
const d = nomarkers[j].firstChild?.data || "";
|
|
220
|
-
if (d.indexOf(marker) > -1) {
|
|
221
|
-
const parts = d.split(marker).reduce((memo, p, i) => {
|
|
222
|
-
i && memo.push("");
|
|
223
|
-
memo.push(p);
|
|
224
|
-
return memo;
|
|
225
|
-
}, []);
|
|
226
|
-
nomarkers[i].firstChild.replaceWith(...parts);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
templates[0].create = code;
|
|
231
|
-
cache.set(statics, templates);
|
|
232
|
-
return templates;
|
|
233
|
-
}
|
|
234
|
-
function parseKeyValue(node, tag, name, value, isSVG, options) {
|
|
235
|
-
let expr, parts, namespace;
|
|
236
|
-
if (value === "###") {
|
|
237
|
-
expr = `_$v`;
|
|
238
|
-
options.counter++;
|
|
239
|
-
} else {
|
|
240
|
-
const chunks = value.split("###");
|
|
241
|
-
options.counter = chunks.length - 1 + options.counter;
|
|
242
|
-
expr = chunks.map((v, i) => i ? ` + _$v[${i - 1}] + "${v}"` : `"${v}"`).join("");
|
|
243
|
-
}
|
|
244
|
-
if ((parts = name.split(":")) && parts[1] && reservedNameSpaces.has(parts[0])) {
|
|
245
|
-
name = parts[1];
|
|
246
|
-
namespace = parts[0];
|
|
247
|
-
}
|
|
248
|
-
const isChildProp = r.ChildProperties.has(name);
|
|
249
|
-
const isProp = r.Properties.has(name);
|
|
250
|
-
if (name === "style") {
|
|
251
|
-
options.exprs.push(`r.style(${tag},${expr},_$p)`);
|
|
252
|
-
} else if (name === "class") {
|
|
253
|
-
options.exprs.push(`r.className(${tag},${expr},${isSVG},_$p)`);
|
|
254
|
-
} else if (isChildProp || !isSVG && isProp || namespace === "prop") {
|
|
255
|
-
options.exprs.push(`${tag}.${name} = ${expr}`);
|
|
256
|
-
} else {
|
|
257
|
-
const ns = isSVG && name.indexOf(":") > -1 && r.SVGNamespace[name.split(":")[0]];
|
|
258
|
-
if (ns) options.exprs.push(`r.setAttributeNS(${tag},"${ns}","${name}",${expr})`);else options.exprs.push(`r.setAttribute(${tag},"${name}",${expr})`);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
function parseAttribute(node, tag, name, value, isSVG, options) {
|
|
262
|
-
if (name.slice(0, 2) === "on") {
|
|
263
|
-
if (!name.includes(":")) {
|
|
264
|
-
const lc = name.slice(2).toLowerCase();
|
|
265
|
-
const delegate = delegateEvents && r.DelegatedEvents.has(lc);
|
|
266
|
-
options.exprs.push(`r.addEventListener(${tag},"${lc}",exprs[${options.counter++}],${delegate})`);
|
|
267
|
-
delegate && options.delegatedEvents.add(lc);
|
|
268
|
-
} else {
|
|
269
|
-
options.exprs.push(`${tag}.addEventListener("${name.slice(3)}",exprs[${options.counter++}])`);
|
|
270
|
-
}
|
|
271
|
-
} else if (name === "ref") {
|
|
272
|
-
options.exprs.push(`exprs[${options.counter++}](${tag})`);
|
|
273
|
-
} else {
|
|
274
|
-
const childOptions = Object.assign({}, options, {
|
|
275
|
-
exprs: []
|
|
276
|
-
}),
|
|
277
|
-
count = options.counter;
|
|
278
|
-
parseKeyValue(node, tag, name, value, isSVG, childOptions);
|
|
279
|
-
options.decl.push(`_fn${count} = (_$v, _$p) => {\n${childOptions.exprs.join(";\n")};\n}`);
|
|
280
|
-
if (value === "###") {
|
|
281
|
-
options.exprs.push(`typeof exprs[${count}] === "function" ? r.effect(() => exprs[${count}](), _fn${count}) : _fn${count}(exprs[${count}])`);
|
|
282
|
-
} else {
|
|
283
|
-
let check = "";
|
|
284
|
-
let list = "";
|
|
285
|
-
let reactiveList = "";
|
|
286
|
-
for (let i = count; i < childOptions.counter; i++) {
|
|
287
|
-
if (i !== count) {
|
|
288
|
-
check += " || ";
|
|
289
|
-
list += ",";
|
|
290
|
-
reactiveList += ",";
|
|
291
|
-
}
|
|
292
|
-
check += `typeof exprs[${i}] === "function"`;
|
|
293
|
-
list += `exprs[${i}]`;
|
|
294
|
-
reactiveList += `r.resolveFn(exprs[${i}])`;
|
|
295
|
-
}
|
|
296
|
-
options.exprs.push(check + ` ? r.effect(() => [${reactiveList}], _fn${count}) : _fn${count}([${list}])`);
|
|
297
|
-
}
|
|
298
|
-
options.counter = childOptions.counter;
|
|
299
|
-
options.wrap = false;
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
function processChildren(node, options) {
|
|
303
|
-
const childOptions = Object.assign({}, options, {
|
|
304
|
-
first: true,
|
|
305
|
-
multi: false,
|
|
306
|
-
parent: options.path
|
|
307
|
-
});
|
|
308
|
-
if (node.children.length > 1) {
|
|
309
|
-
for (let i = 0; i < node.children.length; i++) {
|
|
310
|
-
const child = node.children[i];
|
|
311
|
-
if (child.type === "comment" && child.content === "#" || child.type === "tag" && child.name === "###") {
|
|
312
|
-
childOptions.multi = true;
|
|
313
|
-
break;
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
let i = 0;
|
|
318
|
-
while (i < node.children.length) {
|
|
319
|
-
const child = node.children[i];
|
|
320
|
-
if (child.name === "###") {
|
|
321
|
-
if (childOptions.multi) {
|
|
322
|
-
node.children[i] = {
|
|
323
|
-
type: "comment",
|
|
324
|
-
content: "#"
|
|
325
|
-
};
|
|
326
|
-
i++;
|
|
327
|
-
} else node.children.splice(i, 1);
|
|
328
|
-
processComponent(child, childOptions);
|
|
329
|
-
continue;
|
|
330
|
-
}
|
|
331
|
-
parseNode(child, childOptions);
|
|
332
|
-
if (!childOptions.multi && child.type === "comment" && child.content === "#") node.children.splice(i, 1);else i++;
|
|
333
|
-
}
|
|
334
|
-
options.counter = childOptions.counter;
|
|
335
|
-
options.templateId = childOptions.templateId;
|
|
336
|
-
options.hasCustomElement = options.hasCustomElement || childOptions.hasCustomElement;
|
|
337
|
-
options.isImportNode = options.isImportNode || childOptions.isImportNode;
|
|
338
|
-
}
|
|
339
|
-
function processComponentProps(propGroups) {
|
|
340
|
-
let result = [];
|
|
341
|
-
for (const props of propGroups) {
|
|
342
|
-
if (Array.isArray(props)) {
|
|
343
|
-
if (!props.length) continue;
|
|
344
|
-
result.push(`r.wrapProps({${props.join(",") || ""}})`);
|
|
345
|
-
} else result.push(props);
|
|
346
|
-
}
|
|
347
|
-
return result.length > 1 ? `r.mergeProps(${result.join(",")})` : result[0];
|
|
348
|
-
}
|
|
349
|
-
function processComponent(node, options) {
|
|
350
|
-
let props = [];
|
|
351
|
-
const keys = Object.keys(node.attrs),
|
|
352
|
-
propGroups = [props],
|
|
353
|
-
componentIdentifier = options.counter++;
|
|
354
|
-
for (let i = 0; i < keys.length; i++) {
|
|
355
|
-
const {
|
|
356
|
-
type,
|
|
357
|
-
name,
|
|
358
|
-
value
|
|
359
|
-
} = node.attrs[i];
|
|
360
|
-
if (type === "attr") {
|
|
361
|
-
if (name === "###") {
|
|
362
|
-
propGroups.push(`exprs[${options.counter++}]`);
|
|
363
|
-
propGroups.push(props = []);
|
|
364
|
-
} else if (value === "###") {
|
|
365
|
-
props.push(`"${name}": exprs[${options.counter++}]`);
|
|
366
|
-
} else props.push(`"${name}": "${value}"`);
|
|
367
|
-
} else if (type === 'directive') {
|
|
368
|
-
const tag = `_$el${uuid++}`;
|
|
369
|
-
const topDecl = !options.decl.length;
|
|
370
|
-
options.decl.push(topDecl ? "" : `${tag} = ${options.path}.${options.first ? "firstChild" : "nextSibling"}`);
|
|
371
|
-
parseDirective(name, value, tag, options);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
if (node.children.length === 1 && node.children[0].type === "comment" && node.children[0].content === "#") {
|
|
375
|
-
props.push(`children: () => exprs[${options.counter++}]`);
|
|
376
|
-
} else if (node.children.length) {
|
|
377
|
-
const children = {
|
|
378
|
-
type: "fragment",
|
|
379
|
-
children: node.children
|
|
380
|
-
},
|
|
381
|
-
childOptions = Object.assign({}, options, {
|
|
382
|
-
first: true,
|
|
383
|
-
decl: [],
|
|
384
|
-
exprs: [],
|
|
385
|
-
parent: false
|
|
386
|
-
});
|
|
387
|
-
parseNode(children, childOptions);
|
|
388
|
-
props.push(`children: () => { ${childOptions.exprs.join(";\n")}}`);
|
|
389
|
-
options.templateId = childOptions.templateId;
|
|
390
|
-
options.counter = childOptions.counter;
|
|
391
|
-
}
|
|
392
|
-
let tag;
|
|
393
|
-
if (options.multi) {
|
|
394
|
-
tag = `_$el${uuid++}`;
|
|
395
|
-
options.decl.push(`${tag} = ${options.path}.${options.first ? "firstChild" : "nextSibling"}`);
|
|
396
|
-
}
|
|
397
|
-
if (options.parent) options.exprs.push(`r.insert(${options.parent}, r.createComponent(exprs[${componentIdentifier}],${processComponentProps(propGroups)})${tag ? `, ${tag}` : ""})`);else options.exprs.push(`${options.fragment ? "" : "return "}r.createComponent(exprs[${componentIdentifier}],${processComponentProps(propGroups)})`);
|
|
398
|
-
options.path = tag;
|
|
399
|
-
options.first = false;
|
|
400
|
-
}
|
|
401
|
-
function parseNode(node, options) {
|
|
402
|
-
if (node.type === "fragment") {
|
|
403
|
-
const parts = [];
|
|
404
|
-
node.children.forEach(child => {
|
|
405
|
-
if (child.type === "tag") {
|
|
406
|
-
if (child.name === "###") {
|
|
407
|
-
const childOptions = Object.assign({}, options, {
|
|
408
|
-
first: true,
|
|
409
|
-
fragment: true,
|
|
410
|
-
decl: [],
|
|
411
|
-
exprs: []
|
|
412
|
-
});
|
|
413
|
-
processComponent(child, childOptions);
|
|
414
|
-
parts.push(childOptions.exprs[0]);
|
|
415
|
-
options.counter = childOptions.counter;
|
|
416
|
-
options.templateId = childOptions.templateId;
|
|
417
|
-
return;
|
|
418
|
-
}
|
|
419
|
-
options.templateId++;
|
|
420
|
-
const id = uuid;
|
|
421
|
-
const childOptions = Object.assign({}, options, {
|
|
422
|
-
first: true,
|
|
423
|
-
decl: [],
|
|
424
|
-
exprs: []
|
|
425
|
-
});
|
|
426
|
-
options.templateNodes.push([child]);
|
|
427
|
-
parseNode(child, childOptions);
|
|
428
|
-
parts.push(`function() { ${childOptions.decl.join(",\n") + ";\n" + childOptions.exprs.join(";\n") + `;\nreturn _$el${id};\n`}}()`);
|
|
429
|
-
options.counter = childOptions.counter;
|
|
430
|
-
options.templateId = childOptions.templateId;
|
|
431
|
-
} else if (child.type === "text") {
|
|
432
|
-
parts.push(`"${child.content}"`);
|
|
433
|
-
} else if (child.type === "comment") {
|
|
434
|
-
if (child.content === "#") parts.push(`exprs[${options.counter++}]`);else if (child.content) {
|
|
435
|
-
for (let i = 0; i < child.content.split("###").length - 1; i++) {
|
|
436
|
-
parts.push(`exprs[${options.counter++}]`);
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
});
|
|
441
|
-
options.exprs.push(`return [${parts.join(", \n")}]`);
|
|
442
|
-
} else if (node.type === "tag") {
|
|
443
|
-
const tag = `_$el${uuid++}`;
|
|
444
|
-
const topDecl = !options.decl.length;
|
|
445
|
-
const templateId = options.templateId;
|
|
446
|
-
options.decl.push(topDecl ? "" : `${tag} = ${options.path}.${options.first ? "firstChild" : "nextSibling"}`);
|
|
447
|
-
const isSVG = r.SVGElements.has(node.name);
|
|
448
|
-
options.hasCustomElement = node.name.includes("-") || node.attrs.some(e => e.name === "is");
|
|
449
|
-
options.isImportNode = (node.name === "img" || node.name === "iframe") && node.attrs.some(e => e.name === "loading" && e.value === "lazy");
|
|
450
|
-
if (node.attrs.some(e => e.name === "###")) {
|
|
451
|
-
const spreadArgs = [];
|
|
452
|
-
let current = "";
|
|
453
|
-
const newAttrs = [];
|
|
454
|
-
for (let i = 0; i < node.attrs.length; i++) {
|
|
455
|
-
const {
|
|
456
|
-
type,
|
|
457
|
-
name,
|
|
458
|
-
value
|
|
459
|
-
} = node.attrs[i];
|
|
460
|
-
if (type === "attr") {
|
|
461
|
-
if (value.includes("###")) {
|
|
462
|
-
let count = options.counter++;
|
|
463
|
-
current += `${name}: ${name !== "ref" ? `typeof exprs[${count}] === "function" ? exprs[${count}]() : ` : ""}exprs[${count}],`;
|
|
464
|
-
} else if (name === "###") {
|
|
465
|
-
if (current.length) {
|
|
466
|
-
spreadArgs.push(`()=>({${current}})`);
|
|
467
|
-
current = "";
|
|
468
|
-
}
|
|
469
|
-
spreadArgs.push(`exprs[${options.counter++}]`);
|
|
470
|
-
} else {
|
|
471
|
-
newAttrs.push(node.attrs[i]);
|
|
472
|
-
}
|
|
473
|
-
} else if (type === "directive") {
|
|
474
|
-
parseDirective(name, value, tag, options);
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
node.attrs = newAttrs;
|
|
478
|
-
if (current.length) {
|
|
479
|
-
spreadArgs.push(`()=>({${current}})`);
|
|
480
|
-
}
|
|
481
|
-
options.exprs.push(`r.spread(${tag},${spreadArgs.length === 1 ? `typeof ${spreadArgs[0]} === "function" ? r.mergeProps(${spreadArgs[0]}) : ${spreadArgs[0]}` : `r.mergeProps(${spreadArgs.join(",")})`},${isSVG},${!!node.children.length})`);
|
|
482
|
-
} else {
|
|
483
|
-
for (let i = 0; i < node.attrs.length; i++) {
|
|
484
|
-
const {
|
|
485
|
-
type,
|
|
486
|
-
name,
|
|
487
|
-
value
|
|
488
|
-
} = node.attrs[i];
|
|
489
|
-
if (type === "directive") {
|
|
490
|
-
parseDirective(name, value, tag, options);
|
|
491
|
-
node.attrs.splice(i, 1);
|
|
492
|
-
i--;
|
|
493
|
-
} else if (type === "attr") {
|
|
494
|
-
if (value.includes("###")) {
|
|
495
|
-
node.attrs.splice(i, 1);
|
|
496
|
-
i--;
|
|
497
|
-
parseAttribute(node, tag, name, value, isSVG, options);
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
options.path = tag;
|
|
503
|
-
options.first = false;
|
|
504
|
-
processChildren(node, options);
|
|
505
|
-
if (topDecl) {
|
|
506
|
-
options.decl[0] = options.hasCustomElement || options.isImportNode ? `const ${tag} = r.untrack(() => document.importNode(tmpls[${templateId}].content.firstChild, true))` : `const ${tag} = tmpls[${templateId}].content.firstChild.cloneNode(true)`;
|
|
507
|
-
}
|
|
508
|
-
} else if (node.type === "text") {
|
|
509
|
-
const tag = `_$el${uuid++}`;
|
|
510
|
-
options.decl.push(`${tag} = ${options.path}.${options.first ? "firstChild" : "nextSibling"}`);
|
|
511
|
-
options.path = tag;
|
|
512
|
-
options.first = false;
|
|
513
|
-
} else if (node.type === "comment") {
|
|
514
|
-
const tag = `_$el${uuid++}`;
|
|
515
|
-
options.decl.push(`${tag} = ${options.path}.${options.first ? "firstChild" : "nextSibling"}`);
|
|
516
|
-
if (node.content === "#") {
|
|
517
|
-
if (options.multi) {
|
|
518
|
-
options.exprs.push(`r.insert(${options.parent}, exprs[${options.counter++}], ${tag})`);
|
|
519
|
-
} else options.exprs.push(`r.insert(${options.parent}, exprs[${options.counter++}])`);
|
|
520
|
-
}
|
|
521
|
-
options.path = tag;
|
|
522
|
-
options.first = false;
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
function parseTemplate(nodes, funcBuilder) {
|
|
526
|
-
const options = {
|
|
527
|
-
path: "",
|
|
528
|
-
decl: [],
|
|
529
|
-
exprs: [],
|
|
530
|
-
delegatedEvents: new Set(),
|
|
531
|
-
counter: 0,
|
|
532
|
-
first: true,
|
|
533
|
-
multi: false,
|
|
534
|
-
templateId: 0,
|
|
535
|
-
templateNodes: []
|
|
536
|
-
},
|
|
537
|
-
id = uuid,
|
|
538
|
-
origNodes = nodes;
|
|
539
|
-
let toplevel;
|
|
540
|
-
if (nodes.length > 1) {
|
|
541
|
-
nodes = [{
|
|
542
|
-
type: "fragment",
|
|
543
|
-
children: nodes
|
|
544
|
-
}];
|
|
545
|
-
}
|
|
546
|
-
if (nodes[0].name === "###") {
|
|
547
|
-
toplevel = true;
|
|
548
|
-
processComponent(nodes[0], options);
|
|
549
|
-
} else parseNode(nodes[0], options);
|
|
550
|
-
r.delegateEvents(Array.from(options.delegatedEvents));
|
|
551
|
-
const templateNodes = [origNodes].concat(options.templateNodes);
|
|
552
|
-
return [templateNodes.map(t => stringify(t)), funcBuilder("tmpls", "exprs", "r", options.decl.join(",\n") + ";\n" + options.exprs.join(";\n") + (toplevel ? "" : `;\nreturn _$el${id};\n`))];
|
|
553
|
-
}
|
|
554
|
-
function html(statics, ...args) {
|
|
555
|
-
const templates = cache.get(statics) || createTemplate(statics, {
|
|
556
|
-
funcBuilder: functionBuilder
|
|
557
|
-
});
|
|
558
|
-
return templates[0].create(templates, args, r);
|
|
559
|
-
}
|
|
560
|
-
return html;
|
|
561
|
-
}
|
|
3
|
+
const e=e=>t(e)||e>=48&&e<=58||e===46||e===45,t=e=>e>=65&&e<=90||e>=97&&e<=122||e===95||e===36,n=e=>e>=9&&e<=13||e===32,r=(r,i)=>{let a=[],o=0,s=``,c=0;for(let l=0;l<r.length;l++){let u=r[l],d=u.length;for(c=0;c<d;)switch(o){case 0:{s=``;let e=u.indexOf(`<`,c);e===-1?(c<d&&a.push({type:6,value:u.slice(c)}),c=d):(e>c&&a.push({type:6,value:u.slice(c,e)}),u[e+1]===`!`&&u[e+2]===`-`&&u[e+3]===`-`?(o=3,c=e+4):(a.push({type:0}),o=1,c=e+1));break}case 1:{let r=u.charCodeAt(c);if(n(r))c++;else if(r===62)i.has(s)&&a[a.length-2]?.type!==2?o=2:(o=0,s=``),a.push({type:1}),c++;else if(r===61)a.push({type:4}),c++;else if(r===47){let e=u.charCodeAt(c+1),t=u.slice(c+2).search(/\S/),n=e===47&&a[a.length-1]?.type===0&&t!==-1&&u[c+2+t]===`>`;e===47&&!n?o=4:e===42?o=5:(a.push({type:2}),c++);}else if(r===34||r===39){let e=u[c],t=u.indexOf(e,c+1);if(t===-1)throw Error(`Unterminated string at ${l}:${c}`);a.push({type:5,value:u.slice(c+1,t),quote:e}),c=t+1;}else if(t(r)){let t=c;for(;c<d&&e(u.charCodeAt(c));)c++;let n=u.slice(t,c);s===``&&(s=n),a.push({type:3,value:n});}else if(r===46&&u[c+1]===`.`&&u[c+2]===`.`)a.push({type:8}),c+=3;else throw Error(`Unexpected Character: ${u[c]} at ${l}:${c}`);break}case 2:{let e=RegExp(`<\\s*/\\s*${s}\\s*>`,`g`);e.lastIndex=c;let t=e.exec(u);if(t){let e=t.index;e>c&&a.push({type:6,value:u.slice(c,e)}),o=0,c=e,s=``;}else a.push({type:6,value:u.slice(c)}),c=d;break}case 3:case 4:case 5:{let e=o===4?`
|
|
4
|
+
`:o===5?`*/`:`-->`,t=u.indexOf(e,c);t===-1?c=d:(o=o===3?0:1,c=t+e.length);break}}l<r.length-1&&(o===0||o===1||o===2)&&a.push({type:7,value:l});}return a},i=e=>{let t=e.charCodeAt(0);return t>=65&&t<=90},a=(e,t)=>{let n={type:0,children:[]},r=[n],a=0,o=e.length;for(;a<o;){let n=e[a],s=r[r.length-1];switch(n.type){case 6:{let t=n.value;if(t.trim()===``){let t=e[a-1]?.type,n=e[a+1]?.type;if(t===1||n===0){a++;continue}}s.children.push({type:3,value:t}),a++;continue}case 7:s.children.push({type:4,value:n.value}),a++;continue;case 0:{let n=e[++a];if(n.type===2){let n=e[++a],i=e[++a],o=r[r.length-1];if(r.length>1&&i.type===1&&(n?.type===3&&o.name===n.value||(n?.type===7||n.type===2)&&typeof o.name==`number`)){let e=r.pop();e?.type===1&&t.has(e.name)&&(e.children=[]),a++;continue}throw Error(`Mismatched closing tag for <${o.name}>`)}if(n.type===3||n.type===7){let t=n.value,c={type:typeof t==`number`||i(t)?2:1,name:t,props:[],children:[]};for(s.children.push(c),a++;a<o;){let t=e[a];if(t.type===1||t.type===2)break;if(t.type===8){let t=e[a+1];if(t?.type===7)c.props.push({type:3,value:t.value}),a+=2;else throw Error(`Spread operator in <${c.name}> must be followed by an expression`)}else if(t.type===3){let n=t.value;if(e[a+1]?.type===4){a+=2;let t=e[a];if(t.type===7)c.props.push({name:n,type:2,value:t.value}),a++;else if(t.type===5){let e=t.quote;c.props.push({name:n,value:t.value,quote:e,type:1}),a++;}else throw Error(`Attribute value for "${n}" in <${c.name}> must be an expression or a string`)}else c.props.push({type:0,name:n,value:true}),a++;}else throw Error(`Invalid attribute in <${c.name}>`)}let l=e[a];l.type===2?a+=2:l.type===1&&(a++,r.push(c));continue}}default:throw Error(`Unexpected token: ${JSON.stringify(n)} after <${r[r.length-1].name}>`)}}if(r.length>1)throw Error(`Unclosed tag for <${r[r.length-1].name}>`);return n},o=e=>e.length===1?e[0]:e;function s(e){let t=new WeakMap,n=new Set(e.RawTextElements);n.delete(`template`);let i=document.createTreeWalker(document,129),s=t=>e.SVGElements.has(t)?document.createElementNS(`http://www.w3.org/2000/svg`,t):e.MathMLElements.has(t)?document.createElementNS(`http://www.w3.org/1998/Math/MathML`,t):document.createElement(t),c=e=>{let t=(t,...n)=>m(l(t),n,e);return t.components=e,t.jsx=t,t.define=t=>c({...e,...t}),t},l=i=>{let o=t.get(i);return o||(o=a(r(i,n),e.VoidElements),u(o,false),t.set(i,o)),o},u=(e,t)=>{if(e.type===1){if(!t){let n=document.createElement(`template`);n.content.appendChild(f(e)),e.template=n,t=true;}e.children.forEach(e=>u(e,t));}else e.type===2||e.type===0?e.children.forEach(e=>u(e,false)):e.type===3&&!t&&(d.innerHTML=e.value,e.value=d.content.textContent??``);},d=document.createElement(`template`),f=e=>{switch(e.type){case 3:return d.innerHTML=e.value,document.createTextNode(d.content.textContent??``);case 4:return document.createComment(`+`);case 2:return document.createComment(e.name);case 1:let t=false,n=s(e.name),r=e.name===`a`?`href`:e.name===`form`?`action`:void 0;return e.props=e.props.filter(i=>i.type===1?i.name.startsWith(`prop:`)?true:(n.setAttribute(i.name,i.value),!t&&i.name===r&&(e.claim=true),t):i.type===0?(n.setAttribute(i.name,``),!t&&i.name===r&&(e.claim=true),t):i.type===3?(t=true,t):true),(e.name===`template`?n.content:n).append(...e.children.map(f)),n}},p=(t,n,r)=>{switch(t.type){case 3:return t.value;case 4:return n[t.value];case 2:let i=typeof t.name==`string`?r[t.name]:n[t.name];if(i&&typeof i==`function`)return e.createComponent(i,h(t,n,r));throw Error(`Component "${t.name}" not found in registry`);case 1:let a=m(t,n,r),o=h(t,n,r);return e.spread(a,o,true),t.claim&&e.claimElement(a),a}},m=(t,n,r)=>{if(t.type!==1||!t.template)return o(t.children.map(e=>p(e,n,r)));let a=t.template.content.firstChild.cloneNode(true);i.currentNode=a;let s=(t,i)=>{for(let a of t)if(a.type===1||a.type===4||a.type===2){let t=i.nextNode();if(a.type===4||a.type===2)e.insert(t.parentNode,p(a,n,r),t),i.currentNode=t;else {if(a.props.length){let i=h(a,n,r);e.spread(t,i,true);}a.claim&&e.claimElement(t),s(a.children,a.name===`template`?document.createTreeWalker(t.content,129):i);}}};return s(t.children,t.name===`template`?document.createTreeWalker(a.content,129):i),a},h=(t,n,r,i={})=>{for(let r of t.props)switch(r.type){case 0:i[r.name]=true;break;case 1:i[r.name]=r.value;break;case 2:g(i,r.name,n[r.value]);break;case 3:let t=n[r.value];if(!t||typeof t!=`object`)throw Error(`Can only spread objects`);i=e.mergeProps(i,t);break}return t.type===2&&t.children.length&&Object.defineProperty(i,`children`,{get(){return m(t,n,r)}}),i},g=(e,t,n)=>{typeof n==`function`&&n.length===0&&t!==`ref`&&!t.startsWith(`on`)?Object.defineProperty(e,t,{get(){return n()},enumerable:true}):e[t]=n;};return c({})}
|
|
562
5
|
|
|
563
|
-
const html =
|
|
564
|
-
effect,
|
|
565
|
-
style,
|
|
6
|
+
const html = s({
|
|
566
7
|
insert,
|
|
567
|
-
untrack,
|
|
568
8
|
spread,
|
|
569
9
|
createComponent,
|
|
570
|
-
delegateEvents,
|
|
571
|
-
className,
|
|
572
10
|
mergeProps,
|
|
573
|
-
|
|
574
|
-
setAttribute,
|
|
575
|
-
setAttributeNS,
|
|
576
|
-
addEventListener,
|
|
577
|
-
getPropAlias,
|
|
578
|
-
Properties,
|
|
579
|
-
ChildProperties,
|
|
580
|
-
DelegatedEvents,
|
|
11
|
+
claimElement,
|
|
581
12
|
SVGElements,
|
|
582
|
-
|
|
13
|
+
MathMLElements,
|
|
14
|
+
VoidElements,
|
|
15
|
+
RawTextElements
|
|
583
16
|
});
|
|
584
17
|
|
|
585
18
|
export { html as default };
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solidjs/html",
|
|
3
|
-
"description": "
|
|
4
|
-
"version": "2.0.0-
|
|
3
|
+
"description": "Tagged-template-literal templating for Solid — write components with no build step.",
|
|
4
|
+
"version": "2.0.0-rc.1",
|
|
5
5
|
"author": "Ryan Carniato",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://solidjs.com",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
|
-
"url": "https://github.com/solidjs/solid
|
|
10
|
+
"url": "git+https://github.com/solidjs/solid.git",
|
|
11
|
+
"directory": "packages/solid-html"
|
|
11
12
|
},
|
|
12
13
|
"publishConfig": {
|
|
13
14
|
"access": "public"
|
|
@@ -20,28 +21,36 @@
|
|
|
20
21
|
"files": [
|
|
21
22
|
"dist",
|
|
22
23
|
"types",
|
|
24
|
+
"types-cjs",
|
|
23
25
|
"package.json"
|
|
24
26
|
],
|
|
25
27
|
"exports": {
|
|
26
28
|
".": {
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
"import": {
|
|
30
|
+
"types": "./types/index.d.ts",
|
|
31
|
+
"default": "./dist/html.js"
|
|
32
|
+
},
|
|
33
|
+
"require": {
|
|
34
|
+
"types": "./types-cjs/index.d.cts",
|
|
35
|
+
"default": "./dist/html.cjs"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
32
38
|
},
|
|
33
39
|
"peerDependencies": {
|
|
34
|
-
"@solidjs/web": "^2.0.0-
|
|
40
|
+
"@solidjs/web": "^2.0.0-rc.1"
|
|
35
41
|
},
|
|
36
42
|
"devDependencies": {
|
|
37
|
-
"@solidjs/web": "2.0.0-
|
|
43
|
+
"@solidjs/web": "2.0.0-rc.1",
|
|
44
|
+
"solid-js": "2.0.0-rc.1"
|
|
38
45
|
},
|
|
39
46
|
"scripts": {
|
|
40
47
|
"build": "npm-run-all -nl build:*",
|
|
41
48
|
"build:clean": "rimraf dist/ coverage/",
|
|
42
49
|
"build:js": "rollup -c",
|
|
43
|
-
"types": "npm-run-all -nl types
|
|
44
|
-
"types:clean": "rimraf types/",
|
|
45
|
-
"types:html": "tsc --project ./tsconfig.json && ncp ../../node_modules
|
|
50
|
+
"types": "npm-run-all -nl types:clean types:html types:cjs",
|
|
51
|
+
"types:clean": "rimraf types/ types-cjs/",
|
|
52
|
+
"types:html": "tsc --project ./tsconfig.json && ncp ../../node_modules/@dom-expressions/tagged-jsx/dist/index.d.mts ./types/tagged-jsx.d.ts",
|
|
53
|
+
"types:cjs": "node ../../scripts/sync-dual-types.mjs ./types ./types-cjs",
|
|
54
|
+
"test": "vitest run"
|
|
46
55
|
}
|
|
47
56
|
}
|
package/types/index.d.ts
ADDED