@solidjs/html 2.0.0-rc.8 → 2.0.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.
Files changed (3) hide show
  1. package/README.md +16 -4
  2. package/dist/html.js +27 -13
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -92,14 +92,17 @@ html`<div id=${() => props.id}>${() => firstName() + lastName()}</div>`
92
92
 
93
93
  2. Events on components require explicit event in the arguments
94
94
 
95
- Solid's Tagged Template Literals automatically wraps functions passed to props of components with no arguments in getters so you need to provide one to prevent this. The same applies to render props like in the `<For>` component.
95
+ Solid's Tagged Template Literals automatically wrap functions with no arguments passed to props of components in getters, so you need to declare an argument to prevent this. That covers every prop, `onXxx` handlers and `ref` included; the same applies to render props like in the `<For>` component.
96
96
 
97
97
  ```js
98
- // good
99
- html`<${Button} onClick=${(e) => console.log("Hi")} />`;
98
+ // good: passed as a function
99
+ html`<${Button} onClick=${e => console.log("Hi")} />`;
100
100
 
101
- // bad
101
+ // bad: a getter, so `props.onClick` runs `console.log` and reads its result
102
102
  html`<${Button} onClick=${() => console.log("Hi")} />`;
103
+
104
+ // getter: `props.on` reads `key()`
105
+ html`<${Loading} on=${() => key()}>...<//>`;
103
106
  ```
104
107
 
105
108
  4. All refs are callback form
@@ -119,4 +122,13 @@ html`
119
122
  <div>1</div>
120
123
  <div>2</div>
121
124
  `
125
+ ```
126
+
127
+ 6. Only `onXxx` names are event handlers
128
+
129
+ On elements, camelCase `onXxx` names (`on` followed by an uppercase letter) are event handlers and are passed through as functions, as is `ref`. A lowercase `onclick` is a plain attribute, and a zero-argument function passed to it becomes a getter like any other attribute.
130
+
131
+ ```js
132
+ html`<button onClick=${() => save()} />`; // event handler
133
+ html`<button onclick=${() => code()} />`; // reactive `onclick` attribute
122
134
  ```
package/dist/html.js CHANGED
@@ -1,4 +1,4 @@
1
- import { RawTextElements, spread, claimElement, createComponent, insert, mergeProps, SVGElements, MathMLElements, VoidElements } from '@solidjs/web';
1
+ import { RawTextElements, trustedHTML, spread, claimElement, createComponent, insert, mergeProps, SVGElements, MathMLElements, VoidElements } from '@solidjs/web';
2
2
 
3
3
  const OPEN_TAG_TOKEN = 0;
4
4
  const CLOSE_TAG_TOKEN = 1;
@@ -86,7 +86,7 @@ const tokenize = (strings, rawTextElements) => {
86
86
  if (isWhitespace(code)) {
87
87
  cursor++;
88
88
  } else if (code === 62) {
89
- if (rawTextElements.has(lastTagName) && tokens[tokens.length - 2]?.type !== SLASH_TOKEN) {
89
+ if (rawTextElements.has(lastTagName) && tokens[tokens.length - 1]?.type !== SLASH_TOKEN && tokens[tokens.length - 2]?.type !== SLASH_TOKEN) {
90
90
  state = STATE_RAW_TEXT;
91
91
  } else {
92
92
  state = STATE_TEXT;
@@ -131,7 +131,7 @@ const tokenize = (strings, rawTextElements) => {
131
131
  const start = cursor;
132
132
  while (cursor < len && isIdentifierChar(str.charCodeAt(cursor))) cursor++;
133
133
  const value = str.slice(start, cursor);
134
- if (lastTagName === "") {
134
+ if (tokens[tokens.length - 1]?.type === OPEN_TAG_TOKEN) {
135
135
  lastTagName = value;
136
136
  }
137
137
  tokens.push({
@@ -400,17 +400,25 @@ function createHtml() {
400
400
  }
401
401
  node.children.forEach(child => buildTemplate(child, insideTemplate));
402
402
  } else if (node.type === COMPONENT_NODE || node.type === ROOT_NODE) {
403
+ if (node.type === COMPONENT_NODE) decodeStaticProps(node);
403
404
  node.children.forEach(child => buildTemplate(child, false));
404
405
  } else if (node.type === TEXT_NODE && !insideTemplate) {
405
- textTemplate.innerHTML = node.value;
406
+ textTemplate.innerHTML = trustedHTML(node.value);
406
407
  node.value = textTemplate.content.textContent ?? "";
407
408
  }
408
409
  };
409
410
  const textTemplate = document.createElement("template");
411
+ const decodeStaticProps = node => {
412
+ for (const prop of node.props) {
413
+ if (prop.type !== STATIC_PROP || !prop.value.includes("&")) continue;
414
+ textTemplate.innerHTML = trustedHTML(`<i a="${prop.value.replace(/"/g, "&quot;")}"></i>`);
415
+ prop.value = textTemplate.content.firstChild.getAttribute("a");
416
+ }
417
+ };
410
418
  const buildNodes = node => {
411
419
  switch (node.type) {
412
420
  case TEXT_NODE:
413
- textTemplate.innerHTML = node.value;
421
+ textTemplate.innerHTML = trustedHTML(node.value);
414
422
  return document.createTextNode(textTemplate.content.textContent ?? "");
415
423
  case EXPRESSION_NODE:
416
424
  return document.createComment("+");
@@ -420,6 +428,7 @@ function createHtml() {
420
428
  let hasSpread = false;
421
429
  const elem = createElement(node.name);
422
430
  const claimAttr = node.name === "a" ? "href" : node.name === "form" ? "action" : undefined;
431
+ decodeStaticProps(node);
423
432
  node.props = node.props.filter(prop => {
424
433
  if (prop.type === STATIC_PROP) {
425
434
  if (prop.name.startsWith("prop:")) return true;
@@ -490,35 +499,40 @@ function createHtml() {
490
499
  return element;
491
500
  };
492
501
  const gatherProps = (node, values, components, props = {}) => {
502
+ const sources = [];
503
+ let own = props;
493
504
  for (const prop of node.props) {
494
505
  switch (prop.type) {
495
506
  case BOOLEAN_PROP:
496
- props[prop.name] = true;
507
+ own[prop.name] = true;
497
508
  break;
498
509
  case STATIC_PROP:
499
- props[prop.name] = prop.value;
510
+ own[prop.name] = prop.value;
500
511
  break;
501
512
  case EXPRESSION_PROP:
502
- applyGetter(props, prop.name, values[prop.value]);
513
+ applyGetter(own, prop.name, values[prop.value], node.type === COMPONENT_NODE);
503
514
  break;
504
515
  case SPREAD_PROP:
505
516
  const spreadValue = values[prop.value];
506
517
  if (!spreadValue || typeof spreadValue !== "object") throw new Error("Can only spread objects");
507
- props = mergeProps(props, spreadValue);
518
+ sources.push(own, spreadValue);
519
+ own = {};
508
520
  break;
509
521
  }
510
522
  }
511
523
  if (node.type === COMPONENT_NODE && node.children.length) {
512
- Object.defineProperty(props, "children", {
524
+ Object.defineProperty(own, "children", {
513
525
  get() {
514
526
  return renderChildren(node, values, components);
515
527
  }
516
528
  });
517
529
  }
518
- return props;
530
+ if (sources.length === 0) return own;
531
+ sources.push(own);
532
+ return mergeProps(...sources);
519
533
  };
520
- const applyGetter = (props, name, value) => {
521
- if (typeof value === "function" && value.length === 0 && name !== "ref" && !name.startsWith("on")) {
534
+ const applyGetter = (props, name, value, component) => {
535
+ if (typeof value === "function" && value.length === 0 && (component || name !== "ref" && !/^on[A-Z]/.test(name))) {
522
536
  Object.defineProperty(props, name, {
523
537
  get() {
524
538
  return value();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@solidjs/html",
3
3
  "description": "Tagged-template-literal templating for Solid — write components with no build step.",
4
- "version": "2.0.0-rc.8",
4
+ "version": "2.0.0",
5
5
  "author": "Ryan Carniato",
6
6
  "license": "MIT",
7
7
  "homepage": "https://solidjs.com",
@@ -42,7 +42,7 @@
42
42
  "test": "vitest run"
43
43
  },
44
44
  "peerDependencies": {
45
- "@solidjs/web": "^2.0.0-rc.8"
45
+ "@solidjs/web": "^2.0.0"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@solidjs/web": "workspace:*",