@solidjs/html 2.0.0-experimental.0 → 2.0.0-experimental.10

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2016-2023 Ryan Carniato
3
+ Copyright (c) 2016-2025 Ryan Carniato
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/html.cjs CHANGED
@@ -64,7 +64,7 @@ function parseTag(tag) {
64
64
  }
65
65
  function pushTextNode(list, html, start) {
66
66
  const end = html.indexOf('<', start);
67
- const content = html.slice(start, end === -1 ? void 0 : end);
67
+ const content = html.slice(start, end === -1 ? undefined : end);
68
68
  if (!/^\s*$/.test(content)) {
69
69
  list.push({
70
70
  type: 'text',
@@ -83,7 +83,7 @@ function pushCommentNode(list, tag) {
83
83
  }
84
84
  function parse(html) {
85
85
  const result = [];
86
- let current = void 0;
86
+ let current = undefined;
87
87
  let level = -1;
88
88
  const arr = [];
89
89
  const byTag = {};
@@ -92,7 +92,7 @@ function parse(html) {
92
92
  const isComment = tag.slice(0, 4) === '<!--';
93
93
  const start = index + tag.length;
94
94
  const nextChar = html.charAt(start);
95
- let parent = void 0;
95
+ let parent = undefined;
96
96
  if (isOpen && !isComment) {
97
97
  level++;
98
98
  current = parseTag(tag);
@@ -168,7 +168,7 @@ const attrSeeker = new RegExp(tagName + attrName + attrPartials + "+)([ " + spac
168
168
  const findAttributes = new RegExp("(" + attrName + "\\s*=\\s*)(<!--#-->|['\"(]([\\w\\s]*<!--#-->[\\w\\s]*)*['\")])", "gi");
169
169
  const selfClosing = new RegExp(tagName + attrName + attrPartials + "*)([ " + spaces + "]*/>)", "g");
170
170
  const marker = "<!--#-->";
171
- const reservedNameSpaces = new Set(["class", "on", "style", "use", "prop", "attr"]);
171
+ const reservedNameSpaces = new Set(["class", "on", "style", "use", "prop"]);
172
172
  function attrReplacer($0, $1, $2, $3) {
173
173
  return "<" + $1 + $2.replace(findAttributes, replaceAttributes) + $3;
174
174
  }
@@ -253,8 +253,8 @@ function createHTML(r, {
253
253
  options.exprs.push(`r.style(${tag},${expr},_$p)`);
254
254
  } else if (name === "class") {
255
255
  options.exprs.push(`r.className(${tag},${expr},${isSVG},_$p)`);
256
- } else if (namespace !== "attr" && (isChildProp || !isSVG && (r.getPropAlias(name, node.name.toUpperCase()) || isProp) || namespace === "prop")) {
257
- options.exprs.push(`${tag}.${r.getPropAlias(name, node.name.toUpperCase()) || name} = ${expr}`);
256
+ } else if (isChildProp || !isSVG && isProp || namespace === "prop") {
257
+ options.exprs.push(`${tag}.${name} = ${expr}`);
258
258
  } else {
259
259
  const ns = isSVG && name.indexOf(":") > -1 && r.SVGNamespace[name.split(":")[0]];
260
260
  if (ns) options.exprs.push(`r.setAttributeNS(${tag},"${ns}","${name}",${expr})`);else options.exprs.push(`r.setAttribute(${tag},"${name}",${expr})`);
@@ -364,9 +364,9 @@ function createHTML(r, {
364
364
  propGroups.push(`exprs[${options.counter++}]`);
365
365
  propGroups.push(props = []);
366
366
  } else if (value === "###") {
367
- props.push(`${name}: exprs[${options.counter++}]`);
368
- } else props.push(`${name}: "${value}"`);
369
- } else if (type === "directive") {
367
+ props.push(`"${name}": exprs[${options.counter++}]`);
368
+ } else props.push(`"${name}": "${value}"`);
369
+ } else if (type === 'directive') {
370
370
  const tag = `_$el${uuid++}`;
371
371
  const topDecl = !options.decl.length;
372
372
  options.decl.push(topDecl ? "" : `${tag} = ${options.path}.${options.first ? "firstChild" : "nextSibling"}`);
package/dist/html.js CHANGED
@@ -1,28 +1,7 @@
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";
1
+ import { effect, style, insert, untrack, spread, createComponent, delegateEvents, className, mergeProps, dynamicProperty, setAttribute, setAttributeNS, addEventListener, getPropAlias, Properties, ChildProperties, DelegatedEvents, SVGElements, SVGNamespace } from '@solidjs/web';
22
2
 
23
3
  const tagRE = /(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g;
24
- const attrRE =
25
- /(?:\s(?<boolean>[^/\s><=]+?)(?=[\s/>]))|(?:(?<name>\S+?)(?:\s*=\s*(?:(['"])(?<quotedValue>[\s\S]*?)\3|(?<unquotedValue>[^\s>]+))))/g;
4
+ const attrRE = /(?:\s(?<boolean>[^/\s><=]+?)(?=[\s/>]))|(?:(?<name>\S+?)(?:\s*=\s*(?:(['"])(?<quotedValue>[\s\S]*?)\3|(?<unquotedValue>[^\s>]+))))/g;
26
5
  const lookup = {
27
6
  area: true,
28
7
  base: true,
@@ -43,8 +22,8 @@ const lookup = {
43
22
  };
44
23
  function parseTag(tag) {
45
24
  const res = {
46
- type: "tag",
47
- name: "",
25
+ type: 'tag',
26
+ name: '',
48
27
  voidElement: false,
49
28
  attrs: [],
50
29
  children: []
@@ -52,70 +31,70 @@ function parseTag(tag) {
52
31
  const tagMatch = tag.match(/<\/?([^\s]+?)[/\s>]/);
53
32
  if (tagMatch) {
54
33
  res.name = tagMatch[1];
55
- if (lookup[tagMatch[1].toLowerCase()] || tag.charAt(tag.length - 2) === "/") {
34
+ if (lookup[tagMatch[1].toLowerCase()] || tag.charAt(tag.length - 2) === '/') {
56
35
  res.voidElement = true;
57
36
  }
58
- if (res.name.startsWith("!--")) {
59
- const endIndex = tag.indexOf("-->");
37
+ if (res.name.startsWith('!--')) {
38
+ const endIndex = tag.indexOf('-->');
60
39
  return {
61
- type: "comment",
62
- comment: endIndex !== -1 ? tag.slice(4, endIndex) : ""
40
+ type: 'comment',
41
+ comment: endIndex !== -1 ? tag.slice(4, endIndex) : ''
63
42
  };
64
43
  }
65
44
  }
66
45
  const reg = new RegExp(attrRE);
67
46
  for (const match of tag.matchAll(reg)) {
68
- if ((match[1] || match[2]).startsWith("use:")) {
47
+ if ((match[1] || match[2]).startsWith('use:')) {
69
48
  res.attrs.push({
70
- type: "directive",
49
+ type: 'directive',
71
50
  name: match[1] || match[2],
72
- value: match[4] || match[5] || ""
51
+ value: match[4] || match[5] || ''
73
52
  });
74
53
  } else {
75
54
  res.attrs.push({
76
- type: "attr",
55
+ type: 'attr',
77
56
  name: match[1] || match[2],
78
- value: match[4] || match[5] || ""
57
+ value: match[4] || match[5] || ''
79
58
  });
80
59
  }
81
60
  }
82
61
  return res;
83
62
  }
84
63
  function pushTextNode(list, html, start) {
85
- const end = html.indexOf("<", start);
86
- const content = html.slice(start, end === -1 ? void 0 : end);
64
+ const end = html.indexOf('<', start);
65
+ const content = html.slice(start, end === -1 ? undefined : end);
87
66
  if (!/^\s*$/.test(content)) {
88
67
  list.push({
89
- type: "text",
68
+ type: 'text',
90
69
  content: content
91
70
  });
92
71
  }
93
72
  }
94
73
  function pushCommentNode(list, tag) {
95
- const content = tag.replace("<!--", "").replace("-->", "");
74
+ const content = tag.replace('<!--', '').replace('-->', '');
96
75
  if (!/^\s*$/.test(content)) {
97
76
  list.push({
98
- type: "comment",
77
+ type: 'comment',
99
78
  content: content
100
79
  });
101
80
  }
102
81
  }
103
82
  function parse(html) {
104
83
  const result = [];
105
- let current = void 0;
84
+ let current = undefined;
106
85
  let level = -1;
107
86
  const arr = [];
108
87
  const byTag = {};
109
88
  html.replace(tagRE, (tag, index) => {
110
- const isOpen = tag.charAt(1) !== "/";
111
- const isComment = tag.slice(0, 4) === "<!--";
89
+ const isOpen = tag.charAt(1) !== '/';
90
+ const isComment = tag.slice(0, 4) === '<!--';
112
91
  const start = index + tag.length;
113
92
  const nextChar = html.charAt(start);
114
- let parent = void 0;
93
+ let parent = undefined;
115
94
  if (isOpen && !isComment) {
116
95
  level++;
117
96
  current = parseTag(tag);
118
- if (!current.voidElement && nextChar && nextChar !== "<") {
97
+ if (!current.voidElement && nextChar && nextChar !== '<') {
119
98
  pushTextNode(current.children, html, start);
120
99
  }
121
100
  byTag[current.tagName] = current;
@@ -139,7 +118,7 @@ function parse(html) {
139
118
  if (!isComment) {
140
119
  level--;
141
120
  }
142
- if (nextChar !== "<" && nextChar) {
121
+ if (nextChar !== '<' && nextChar) {
143
122
  parent = level === -1 ? result : arr[level].children;
144
123
  pushTextNode(parent, html, start);
145
124
  }
@@ -150,58 +129,49 @@ function parse(html) {
150
129
  function attrString(attrs) {
151
130
  const buff = [];
152
131
  for (const attr of attrs) {
153
- buff.push(attr.name + '="' + attr.value.replace(/"/g, "&quot;") + '"');
132
+ buff.push(attr.name + '="' + attr.value.replace(/"/g, '&quot;') + '"');
154
133
  }
155
134
  if (!buff.length) {
156
- return "";
135
+ return '';
157
136
  }
158
- return " " + buff.join(" ");
137
+ return ' ' + buff.join(' ');
159
138
  }
160
139
  function stringifier(buff, doc) {
161
140
  switch (doc.type) {
162
- case "text":
141
+ case 'text':
163
142
  return buff + doc.content;
164
- case "tag":
165
- buff +=
166
- "<" + doc.name + (doc.attrs ? attrString(doc.attrs) : "") + (doc.voidElement ? "/>" : ">");
143
+ case 'tag':
144
+ buff += '<' + doc.name + (doc.attrs ? attrString(doc.attrs) : '') + (doc.voidElement ? '/>' : '>');
167
145
  if (doc.voidElement) {
168
146
  return buff;
169
147
  }
170
- return buff + doc.children.reduce(stringifier, "") + "</" + doc.name + ">";
171
- case "comment":
172
- return (buff += "<!--" + doc.content + "-->");
148
+ return buff + doc.children.reduce(stringifier, '') + '</' + doc.name + '>';
149
+ case 'comment':
150
+ return buff += '<!--' + doc.content + '-->';
173
151
  }
174
152
  }
175
153
  function stringify(doc) {
176
154
  return doc.reduce(function (token, rootEl) {
177
- return token + stringifier("", rootEl);
178
- }, "");
155
+ return token + stringifier('', rootEl);
156
+ }, '');
179
157
  }
180
158
  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;
159
+ const VOID_ELEMENTS = /^(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)$/i;
183
160
  const spaces = " \\f\\n\\r\\t";
184
161
  const almostEverything = "[^" + spaces + "\\/>\"'=]+";
185
162
  const attrName = "[ " + spaces + "]+(?:use:<!--#-->|" + almostEverything + ")";
186
163
  const tagName = "<([A-Za-z$#]+[A-Za-z0-9:_-]*)((?:";
187
- const attrPartials =
188
- "(?:\\s*=\\s*(?:'[^']*?'|\"[^\"]*?\"|\\([^)]*?\\)|<[^>]*?>|" + almostEverything + "))?)";
164
+ const attrPartials = "(?:\\s*=\\s*(?:'[^']*?'|\"[^\"]*?\"|\\([^)]*?\\)|<[^>]*?>|" + almostEverything + "))?)";
189
165
  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
- );
166
+ const findAttributes = new RegExp("(" + attrName + "\\s*=\\s*)(<!--#-->|['\"(]([\\w\\s]*<!--#-->[\\w\\s]*)*['\")])", "gi");
194
167
  const selfClosing = new RegExp(tagName + attrName + attrPartials + "*)([ " + spaces + "]*/>)", "g");
195
168
  const marker = "<!--#-->";
196
- const reservedNameSpaces = new Set(["class", "on", "style", "use", "prop", "attr"]);
169
+ const reservedNameSpaces = new Set(["class", "on", "style", "use", "prop"]);
197
170
  function attrReplacer($0, $1, $2, $3) {
198
171
  return "<" + $1 + $2.replace(findAttributes, replaceAttributes) + $3;
199
172
  }
200
173
  function replaceAttributes($0, $1, $2) {
201
- return (
202
- $1.replace(/<!--#-->/g, "###") +
203
- ($2[0] === '"' || $2[0] === "'" ? $2.replace(/<!--#-->/g, "###") : '"###"')
204
- );
174
+ return $1.replace(/<!--#-->/g, "###") + ($2[0] === '"' || $2[0] === "'" ? $2.replace(/<!--#-->/g, "###") : '"###"');
205
175
  }
206
176
  function fullClosing($0, $1, $2) {
207
177
  return VOID_ELEMENTS.test($1) ? $0 : "<" + $1 + $2 + "></" + $1 + ">";
@@ -209,17 +179,15 @@ function fullClosing($0, $1, $2) {
209
179
  function parseDirective(name, value, tag, options) {
210
180
  if (name === "use:###" && value === "###") {
211
181
  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
- );
182
+ options.exprs.push(`typeof exprs[${count}] === "function" ? r.use(exprs[${count}], ${tag}, exprs[${options.counter++}]) : (()=>{throw new Error("use:### must be a function")})()`);
215
183
  } else {
216
184
  throw new Error(`Not support syntax ${name} must be use:{function}`);
217
185
  }
218
186
  }
219
- function createHTML(
220
- r,
221
- { delegateEvents = true, functionBuilder = (...args) => new Function(...args) } = {}
222
- ) {
187
+ function createHTML(r, {
188
+ delegateEvents = true,
189
+ functionBuilder = (...args) => new Function(...args)
190
+ } = {}) {
223
191
  let uuid = 1;
224
192
  r.wrapProps = props => {
225
193
  const d = Object.getOwnPropertyDescriptors(props);
@@ -228,7 +196,7 @@ function createHTML(
228
196
  }
229
197
  return props;
230
198
  };
231
- r.resolveFn = fn => (typeof fn === "function" ? fn() : fn);
199
+ r.resolveFn = fn => typeof fn === "function" ? fn() : fn;
232
200
  function createTemplate(statics, opt) {
233
201
  let i = 0,
234
202
  markup = "";
@@ -236,16 +204,7 @@ function createHTML(
236
204
  markup = markup + statics[i] + "<!--#-->";
237
205
  }
238
206
  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
- ];
207
+ const replaceList = [[selfClosing, fullClosing], [/<(<!--#-->)/g, "<###"], [/\.\.\.(<!--#-->)/g, "###"], [attrSeeker, attrReplacer], [/>\n+\s*/g, ">"], [/\n+\s*</g, "<"], [/\s+</g, " <"], [/>\s+/g, "> "]];
249
208
  markup = replaceList.reduce((acc, x) => {
250
209
  return acc.replace(x[0], x[1]);
251
210
  }, markup);
@@ -280,7 +239,7 @@ function createHTML(
280
239
  } else {
281
240
  const chunks = value.split("###");
282
241
  options.counter = chunks.length - 1 + options.counter;
283
- expr = chunks.map((v, i) => (i ? ` + _$v[${i - 1}] + "${v}"` : `"${v}"`)).join("");
242
+ expr = chunks.map((v, i) => i ? ` + _$v[${i - 1}] + "${v}"` : `"${v}"`).join("");
284
243
  }
285
244
  if ((parts = name.split(":")) && parts[1] && reservedNameSpaces.has(parts[0])) {
286
245
  name = parts[1];
@@ -292,19 +251,11 @@ function createHTML(
292
251
  options.exprs.push(`r.style(${tag},${expr},_$p)`);
293
252
  } else if (name === "class") {
294
253
  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
- );
254
+ } else if (isChildProp || !isSVG && isProp || namespace === "prop") {
255
+ options.exprs.push(`${tag}.${name} = ${expr}`);
304
256
  } else {
305
257
  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})`);
258
+ if (ns) options.exprs.push(`r.setAttributeNS(${tag},"${ns}","${name}",${expr})`);else options.exprs.push(`r.setAttribute(${tag},"${name}",${expr})`);
308
259
  }
309
260
  }
310
261
  function parseAttribute(node, tag, name, value, isSVG, options) {
@@ -312,14 +263,10 @@ function createHTML(
312
263
  if (!name.includes(":")) {
313
264
  const lc = name.slice(2).toLowerCase();
314
265
  const delegate = delegateEvents && r.DelegatedEvents.has(lc);
315
- options.exprs.push(
316
- `r.addEventListener(${tag},"${lc}",exprs[${options.counter++}],${delegate})`
317
- );
266
+ options.exprs.push(`r.addEventListener(${tag},"${lc}",exprs[${options.counter++}],${delegate})`);
318
267
  delegate && options.delegatedEvents.add(lc);
319
268
  } else {
320
- options.exprs.push(
321
- `${tag}.addEventListener("${name.slice(3)}",exprs[${options.counter++}])`
322
- );
269
+ options.exprs.push(`${tag}.addEventListener("${name.slice(3)}",exprs[${options.counter++}])`);
323
270
  }
324
271
  } else if (name === "ref") {
325
272
  options.exprs.push(`exprs[${options.counter++}](${tag})`);
@@ -331,9 +278,7 @@ function createHTML(
331
278
  parseKeyValue(node, tag, name, value, isSVG, childOptions);
332
279
  options.decl.push(`_fn${count} = (_$v, _$p) => {\n${childOptions.exprs.join(";\n")};\n}`);
333
280
  if (value === "###") {
334
- options.exprs.push(
335
- `typeof exprs[${count}] === "function" ? r.effect(() => exprs[${count}](), _fn${count}) : _fn${count}(exprs[${count}])`
336
- );
281
+ options.exprs.push(`typeof exprs[${count}] === "function" ? r.effect(() => exprs[${count}](), _fn${count}) : _fn${count}(exprs[${count}])`);
337
282
  } else {
338
283
  let check = "";
339
284
  let list = "";
@@ -348,9 +293,7 @@ function createHTML(
348
293
  list += `exprs[${i}]`;
349
294
  reactiveList += `r.resolveFn(exprs[${i}])`;
350
295
  }
351
- options.exprs.push(
352
- check + ` ? r.effect(() => [${reactiveList}], _fn${count}) : _fn${count}([${list}])`
353
- );
296
+ options.exprs.push(check + ` ? r.effect(() => [${reactiveList}], _fn${count}) : _fn${count}([${list}])`);
354
297
  }
355
298
  options.counter = childOptions.counter;
356
299
  options.wrap = false;
@@ -365,10 +308,7 @@ function createHTML(
365
308
  if (node.children.length > 1) {
366
309
  for (let i = 0; i < node.children.length; i++) {
367
310
  const child = node.children[i];
368
- if (
369
- (child.type === "comment" && child.content === "#") ||
370
- (child.type === "tag" && child.name === "###")
371
- ) {
311
+ if (child.type === "comment" && child.content === "#" || child.type === "tag" && child.name === "###") {
372
312
  childOptions.multi = true;
373
313
  break;
374
314
  }
@@ -389,9 +329,7 @@ function createHTML(
389
329
  continue;
390
330
  }
391
331
  parseNode(child, childOptions);
392
- if (!childOptions.multi && child.type === "comment" && child.content === "#")
393
- node.children.splice(i, 1);
394
- else i++;
332
+ if (!childOptions.multi && child.type === "comment" && child.content === "#") node.children.splice(i, 1);else i++;
395
333
  }
396
334
  options.counter = childOptions.counter;
397
335
  options.templateId = childOptions.templateId;
@@ -414,28 +352,26 @@ function createHTML(
414
352
  propGroups = [props],
415
353
  componentIdentifier = options.counter++;
416
354
  for (let i = 0; i < keys.length; i++) {
417
- const { type, name, value } = node.attrs[i];
355
+ const {
356
+ type,
357
+ name,
358
+ value
359
+ } = node.attrs[i];
418
360
  if (type === "attr") {
419
361
  if (name === "###") {
420
362
  propGroups.push(`exprs[${options.counter++}]`);
421
- propGroups.push((props = []));
363
+ propGroups.push(props = []);
422
364
  } else if (value === "###") {
423
- props.push(`${name}: exprs[${options.counter++}]`);
424
- } else props.push(`${name}: "${value}"`);
425
- } else if (type === "directive") {
365
+ props.push(`"${name}": exprs[${options.counter++}]`);
366
+ } else props.push(`"${name}": "${value}"`);
367
+ } else if (type === 'directive') {
426
368
  const tag = `_$el${uuid++}`;
427
369
  const topDecl = !options.decl.length;
428
- options.decl.push(
429
- topDecl ? "" : `${tag} = ${options.path}.${options.first ? "firstChild" : "nextSibling"}`
430
- );
370
+ options.decl.push(topDecl ? "" : `${tag} = ${options.path}.${options.first ? "firstChild" : "nextSibling"}`);
431
371
  parseDirective(name, value, tag, options);
432
372
  }
433
373
  }
434
- if (
435
- node.children.length === 1 &&
436
- node.children[0].type === "comment" &&
437
- node.children[0].content === "#"
438
- ) {
374
+ if (node.children.length === 1 && node.children[0].type === "comment" && node.children[0].content === "#") {
439
375
  props.push(`children: () => exprs[${options.counter++}]`);
440
376
  } else if (node.children.length) {
441
377
  const children = {
@@ -458,20 +394,7 @@ function createHTML(
458
394
  tag = `_$el${uuid++}`;
459
395
  options.decl.push(`${tag} = ${options.path}.${options.first ? "firstChild" : "nextSibling"}`);
460
396
  }
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
- );
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)})`);
475
398
  options.path = tag;
476
399
  options.first = false;
477
400
  }
@@ -502,21 +425,13 @@ function createHTML(
502
425
  });
503
426
  options.templateNodes.push([child]);
504
427
  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
- );
428
+ parts.push(`function() { ${childOptions.decl.join(",\n") + ";\n" + childOptions.exprs.join(";\n") + `;\nreturn _$el${id};\n`}}()`);
513
429
  options.counter = childOptions.counter;
514
430
  options.templateId = childOptions.templateId;
515
431
  } else if (child.type === "text") {
516
432
  parts.push(`"${child.content}"`);
517
433
  } else if (child.type === "comment") {
518
- if (child.content === "#") parts.push(`exprs[${options.counter++}]`);
519
- else if (child.content) {
434
+ if (child.content === "#") parts.push(`exprs[${options.counter++}]`);else if (child.content) {
520
435
  for (let i = 0; i < child.content.split("###").length - 1; i++) {
521
436
  parts.push(`exprs[${options.counter++}]`);
522
437
  }
@@ -528,26 +443,24 @@ function createHTML(
528
443
  const tag = `_$el${uuid++}`;
529
444
  const topDecl = !options.decl.length;
530
445
  const templateId = options.templateId;
531
- options.decl.push(
532
- topDecl ? "" : `${tag} = ${options.path}.${options.first ? "firstChild" : "nextSibling"}`
533
- );
446
+ options.decl.push(topDecl ? "" : `${tag} = ${options.path}.${options.first ? "firstChild" : "nextSibling"}`);
534
447
  const isSVG = r.SVGElements.has(node.name);
535
448
  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");
449
+ options.isImportNode = (node.name === "img" || node.name === "iframe") && node.attrs.some(e => e.name === "loading" && e.value === "lazy");
539
450
  if (node.attrs.some(e => e.name === "###")) {
540
451
  const spreadArgs = [];
541
452
  let current = "";
542
453
  const newAttrs = [];
543
454
  for (let i = 0; i < node.attrs.length; i++) {
544
- const { type, name, value } = node.attrs[i];
455
+ const {
456
+ type,
457
+ name,
458
+ value
459
+ } = node.attrs[i];
545
460
  if (type === "attr") {
546
461
  if (value.includes("###")) {
547
462
  let count = options.counter++;
548
- current += `${name}: ${
549
- name !== "ref" ? `typeof exprs[${count}] === "function" ? exprs[${count}]() : ` : ""
550
- }exprs[${count}],`;
463
+ current += `${name}: ${name !== "ref" ? `typeof exprs[${count}] === "function" ? exprs[${count}]() : ` : ""}exprs[${count}],`;
551
464
  } else if (name === "###") {
552
465
  if (current.length) {
553
466
  spreadArgs.push(`()=>({${current}})`);
@@ -565,16 +478,14 @@ function createHTML(
565
478
  if (current.length) {
566
479
  spreadArgs.push(`()=>({${current}})`);
567
480
  }
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
- );
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})`);
575
482
  } else {
576
483
  for (let i = 0; i < node.attrs.length; i++) {
577
- const { type, name, value } = node.attrs[i];
484
+ const {
485
+ type,
486
+ name,
487
+ value
488
+ } = node.attrs[i];
578
489
  if (type === "directive") {
579
490
  parseDirective(name, value, tag, options);
580
491
  node.attrs.splice(i, 1);
@@ -592,10 +503,7 @@ function createHTML(
592
503
  options.first = false;
593
504
  processChildren(node, options);
594
505
  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)`;
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)`;
599
507
  }
600
508
  } else if (node.type === "text") {
601
509
  const tag = `_$el${uuid++}`;
@@ -630,12 +538,10 @@ function createHTML(
630
538
  origNodes = nodes;
631
539
  let toplevel;
632
540
  if (nodes.length > 1) {
633
- nodes = [
634
- {
635
- type: "fragment",
636
- children: nodes
637
- }
638
- ];
541
+ nodes = [{
542
+ type: "fragment",
543
+ children: nodes
544
+ }];
639
545
  }
640
546
  if (nodes[0].name === "###") {
641
547
  toplevel = true;
@@ -643,25 +549,12 @@ function createHTML(
643
549
  } else parseNode(nodes[0], options);
644
550
  r.delegateEvents(Array.from(options.delegatedEvents));
645
551
  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
- ];
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`))];
658
553
  }
659
554
  function html(statics, ...args) {
660
- const templates =
661
- cache.get(statics) ||
662
- createTemplate(statics, {
663
- funcBuilder: functionBuilder
664
- });
555
+ const templates = cache.get(statics) || createTemplate(statics, {
556
+ funcBuilder: functionBuilder
557
+ });
665
558
  return templates[0].create(templates, args, r);
666
559
  }
667
560
  return html;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@solidjs/html",
3
3
  "description": "Build-less Tagged-Template-Literal Templating for Solid",
4
- "version": "2.0.0-experimental.0",
4
+ "version": "2.0.0-experimental.10",
5
5
  "author": "Ryan Carniato",
6
6
  "license": "MIT",
7
7
  "homepage": "https://solidjs.com",
@@ -31,10 +31,10 @@
31
31
  "./dist/*": "./dist/*"
32
32
  },
33
33
  "peerDependencies": {
34
- "@solidjs/web": "^2.0.0-experimental.0"
34
+ "@solidjs/web": "^2.0.0-experimental.10"
35
35
  },
36
36
  "devDependencies": {
37
- "@solidjs/web": "2.0.0-experimental.0"
37
+ "@solidjs/web": "2.0.0-experimental.10"
38
38
  },
39
39
  "scripts": {
40
40
  "build": "npm-run-all -nl build:*",
package/types/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import type { HTMLTag } from "./lit.js";
2
- declare const html: HTMLTag;
3
- export default html;
package/types/lit.d.ts DELETED
@@ -1,57 +0,0 @@
1
- type MountableElement = Element | Document | ShadowRoot | DocumentFragment | Node;
2
- type ClassList =
3
- | Record<string, boolean>
4
- | Array<string | number | boolean | null | undefined | Record<string, boolean>>;
5
- interface Runtime {
6
- effect<T>(fn: (prev?: T) => T, effect: (value: T, prev?: T) => void, init?: T): void;
7
- untrack<T>(fn: () => T): T;
8
- insert(parent: MountableElement, accessor: any, marker?: Node | null, init?: any): any;
9
- spread<T>(node: Element, accessor: (() => T) | T, isSVG?: Boolean, skipChildren?: Boolean): void;
10
- createComponent(Comp: (props: any) => any, props: any): any;
11
- addEventListener(
12
- node: Element,
13
- name: string,
14
- handler: EventListener | EventListenerObject | (EventListenerObject & AddEventListenerOptions),
15
- delegate: boolean
16
- ): void;
17
- delegateEvents(eventNames: string[]): void;
18
- className(
19
- node: Element,
20
- value: string | ClassList,
21
- isSVG?: boolean,
22
- prev?: string | ClassList
23
- ): void;
24
- style(
25
- node: Element,
26
- value: {
27
- [k: string]: string;
28
- },
29
- prev?: {
30
- [k: string]: string;
31
- }
32
- ): void;
33
- mergeProps(...sources: unknown[]): unknown;
34
- dynamicProperty(props: any, key: string): any;
35
- setAttribute(node: Element, name: string, value: any): void;
36
- setAttributeNS(node: Element, namespace: string, name: string, value: any): void;
37
- getPropAlias(prop: string, tagName: string): string | undefined;
38
- Properties: Set<string>;
39
- ChildProperties: Set<string>;
40
- DelegatedEvents: Set<string>;
41
- SVGElements: Set<string>;
42
- SVGNamespace: Record<string, string>;
43
- }
44
- export type HTMLTag = {
45
- (statics: TemplateStringsArray, ...args: unknown[]): Node | Node[];
46
- };
47
- export declare function createHTML(
48
- r: Runtime,
49
- {
50
- delegateEvents,
51
- functionBuilder
52
- }?: {
53
- delegateEvents?: boolean;
54
- functionBuilder?: (...args: string[]) => Function;
55
- }
56
- ): HTMLTag;
57
- export {};