Skip to content

Commit 4f67697

Browse files
ryansolidClaude via Cursorcursoragent
authored
fix!: lowercase on* names are attributes, not event handlers (#3753)
* fix!: lowercase on* names are attributes, not event handlers Only `on` followed by an uppercase letter (`/^on[A-Z]/`) is an event handler. Lowercase names such as `onclick` are plain attributes everywhere: both compilers (DOM, hydratable, dynamic and SSR), the web runtime's spread/assign and server spread walk, useHead, and the html/h element paths. A leftover `on:click` lowering (to a `:click` listener) is gone; the name is now an ordinary namespaced attribute. Dev builds warn once per name with LOWERCASE_EVENT_ATTRIBUTE when a function reaches setAttribute under an `on*` name. ssrAttribute now escapes function values instead of interpolating their source raw. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]> * fix(html)!: component props follow h's getter rule, no onXxx exemption On components every zero-argument function prop, `onXxx` handlers and `ref` included, becomes a getter, matching @solidjs/h and 1.x hyper-dom-expressions: a component handler declares its event argument. This reverts #3751's unreleased component exemption. Elements keep the `ref` / `onXxx` exemption; lowercase `on*` stays an attribute. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]> * perf(web): keep the per-key event check off the regex engine The server spread walk and the client's assignProp test every key of a spread for `onXxx`. A `/^on[A-Z]/` test there regressed the spread-static-tail renderToString benches by 5-7% against the `startsWith("on")` it replaced. Gate on `startsWith("on")` and compare one char code, so a key not starting with `on` costs what it did before. `ssrAttribute` takes strings first, ahead of the new function branch, and h tests a prop's type before its name. Behavior is unchanged. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]> --------- Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent a5d0e76 commit 4f67697

42 files changed

Lines changed: 828 additions & 110 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"@solidjs/babel-plugin": patch
3+
"@solidjs/compiler": patch
4+
"@solidjs/web": patch
5+
"@solidjs/html": patch
6+
"@solidjs/h": patch
7+
"@solidjs/signals": patch
8+
---
9+
10+
**Breaking:** only `on` followed by an uppercase letter (`onClick`, `onPointerDown`) is an event handler. Lowercase `on*` names (`onclick`, `onmouseover`) are plain attributes everywhere:
11+
12+
- Both compilers compile `onclick={expr}` like any other attribute (`setAttribute`, reactive when `expr` is dynamic) instead of binding a delegated or native event, and SSR renders it as an escaped attribute instead of dropping it. A leftover 1.x `on:click={fn}` is likewise a plain namespaced attribute (it previously compiled to `addEventListener(":click", fn)`).
13+
- `@solidjs/web` `spread`/`assign` set lowercase `on*` keys as attributes, the server spread walk renders them, and `useHead` applies lowercase `on*` attributes (camelCase handler names stay skipped). `ssrAttribute` escapes a function value instead of interpolating its source raw.
14+
- `@solidjs/html` and `@solidjs/h` elements wrap a function passed to a lowercase `on*` in a getter like any other attribute; only `onXxx` and `ref` are exempt.
15+
- `@solidjs/html` components follow `@solidjs/h`'s rule: every zero-argument function prop, `onXxx` handlers and `ref` included, is a getter, so a component handler must declare its event argument (`onClick=${e => …}`).
16+
- New dev-only check `LOWERCASE_EVENT_ATTRIBUTE` (added to the `DiagnosticCode` union): warns once per attribute name when a function is set on a lowercase `on*` (or `on:`) attribute, naming the camelCase handler to use.

‎documentation/solid-2.0/07-dom.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ DOM behavior in Solid 2.0 follows HTML standards by default: attributes over pro
1818

1919
- **Attributes over properties:** Prefer setting attributes rather than properties in almost all cases. Aligns with web components and SSR.
2020
- **Lowercasing:** Use HTML lowercase for built-in attribute names (no camelCase for attributes). Exceptions:
21-
- **Event handlers** remain camelCase (e.g. `onClick`) to keep the `on` modifier clear.
21+
- **Event handlers** remain camelCase (e.g. `onClick`) to keep the `on` modifier clear. Only `on` followed by an uppercase letter is an event handler. Lowercase `on*` names (`onclick`, `onmouseover`) are plain attributes like any other — `onclick="track()"` is the HTML inline-handler attribute, and `onclick={expr}` sets that attribute (reactively when `expr` is dynamic) instead of binding an event. The JSX types do not declare lowercase `on*` names, and in development passing a function to one warns (`LOWERCASE_EVENT_ATTRIBUTE`). The same rule applies to `spread`, `@solidjs/html` and `@solidjs/h` elements, and SSR, which renders lowercase `on*` attributes.
2222
- **Default to attributes** But attributes such as `input.value`, `input.defaultValue`, `input.checked`, `input.defaultChecked`, `select.value`, `option.value`, `option.selected`, `option.defaultSelected`, `textarea.value`, `textarea.defaultValue`, `video.muted`, `video.defaultMuted`, `audio.muted`, `audio.defaultMuted` continue to be handled as props where that avoids confusion. Unfortunately, this leads to all form fields be special cased. For example: `<input value={dynamicCurrentValue()} defaultValue={dynamicDefaultValue()}/>` either can be dynamic or static, and in the absense of `defaultValue`, then, `value` is SSRed.
2323
- **Namespaces:** `attr:`, `bool:`, and `on:` namespaces are removed; the single standard behavior makes the model consistent. The compiler also no longer gives special handling to previously tolerated `class:` / `style:` namespace syntax. Use `class` object/array values for class toggles, `style` object values for style properties, and standard attributes/properties for everything else.
2424
- **XML Namespaces:** `svg` and `math` work as expected, however when using XML partials, an `xmlns` attribute is required for the browser to create the elements with the correct namespace. Solid adds these automatically to the tags that can recognize as SVG/MathML. For example an `a` tag returned from a partial to be used in XML need `xmlns` added by the user.

‎documentation/solid-2.0/08-dev-diagnostics.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,12 @@ Check (`warn`, dev only; kind `render`; server render and client hydrate; once p
699699

700700
Unscoped allocation alone is not the finding: a function hole with nothing scoped after it in its template lands on the same ids on both sides and stays silent. (An `<Errored>` zero-arity `fallback={() => <Fallback />}` thunk used to be the common instance — handed back unresolved and built by the consuming hole; `<Errored>` now calls a function-valued fallback inside its own scope whatever its arity, so that shape never reaches a hole.) Each side reports the permutation it can see. The server records the counter's next id when the hole was registered (`data.registered`, argument evaluation — where the client builds it) and around its evaluation in the walk (`data.before` → `data.after`), and reports when the hole allocated at a shifted position; `data.hole` is the hole's position in its template. The client always builds in place, so it reports when the content built inside an unscoped function hole moved the counter **and** missed a server-rendered key (`Hydration key miss …` is the symptom; this is the cause). `data.name` is the function (when it has one). Scoped holes, memo and component accessors, `children()`, `<For>` rows and the runtime's own children inserts (`spread`, `Portal`) never raise it. Fix: call the function at the hole (`{renderHead()}` — a call hole is scoped on both sides) or pass the built value.
701701

702+
#### `LOWERCASE_EVENT_ATTRIBUTE`
703+
704+
**Message:** "[LOWERCASE_EVENT_ATTRIBUTE] `onclick` received a function, but `onclick` is an attribute in Solid 2.0, not an event handler: the function was set as the attribute's text. Use `onClick` for event handlers."
705+
706+
Check (`warn`, dev only; kind `render`; client, once per attribute name). A function reached an attribute whose name starts with `on` but is not an event handler. Only `on` followed by an uppercase letter (`onClick`) binds an event in 2.0; a lowercase `onclick` — or a leftover 1.x `on:click` — is a plain attribute, so the function is stringified into it and never runs as a handler. Raised from the runtime's attribute write, so it covers compiled attributes, `spread`/`assign`, and hydration alike. `data.name` is the attribute, `data.handler` the camelCase name to use, `data.tag` the element. The JSX types do not declare lowercase `on*` names, so this is reached from JavaScript, through a cast, or from untyped props. Fix: rename to the camelCase handler (`onclick={save}` → `onClick={save}`); keep the lowercase name only for a string inline-handler attribute.
707+
702708
#### `BINDING_SLOT_POSITION`
703709

704710
**Messages:**
@@ -980,6 +986,7 @@ The runtime derives a request's trace itself in every tier — the W3C `tracepar
980986
| `HEAD_TAG_INVALID` | warn | head | `useHead` registration the render could not honor; `data.reason` names the rule (dev) |
981987
| `UNRECOGNIZED_INSERT_VALUE` | warn | render | Value at an insert position the renderer cannot render; skipped (dev; server and client) |
982988
| `UNSCOPED_HOLE_ALLOCATED_IDS` | warn | render | Unscoped hole was handed a function whose content took ids at a position the other side does not share; keys permute (dev) |
989+
| `LOWERCASE_EVENT_ATTRIBUTE` | warn | render | A function was set on a lowercase `on*` (or `on:`) attribute, which is not an event handler in 2.0; use `onXxx` (dev; client) |
983990
| `BINDING_SLOT_POSITION` | warn/err | ssr/render | Binding-slot value where it cannot bind; `data.reason` names the position (spread is an error and throws), client reasons `orphan`/`fill-shape` |
984991

985992
## Run attribution — "why did this run"

‎documentation/solid-2.0/MIGRATION.md‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,18 @@ Solid 2.0 aims to be more “what you write is what the platform sees”:
587587
<some-element enabled="true" />
588588
```
589589

590-
`on:` and `oncapture:` are removed. Keep using camelCase event handlers like `onClick` for Solid-managed events. For native listener options, use a ref callback:
590+
Event handlers are camelCase only: `on` followed by an uppercase letter (`onClick`, `onPointerDown`). **Lowercase `on*` names are attributes in 2.0.** In 1.x `<button onclick={handler}>` bound an event like `onClick`; in 2.0 it sets the `onclick` attribute (stringifying a function into it), in the browser, in SSR output, and through spreads. Rename lowercase handlers to camelCase:
591+
592+
```jsx
593+
// 1.x
594+
<button onclick={increment} />
595+
// 2.0
596+
<button onClick={increment} />
597+
```
598+
599+
A string value is now exactly the HTML inline-handler attribute (`<link rel="preload" onload="this.rel='stylesheet'">`). In development a function passed to a lowercase `on*` attribute warns with `LOWERCASE_EVENT_ATTRIBUTE`.
600+
601+
`on:` and `oncapture:` are removed. Keep using camelCase event handlers like `onClick` for Solid-managed events. A leftover `on:click={fn}` is now a plain (namespaced) attribute and warns in development like a lowercase `on*` name. For native listener options, use a ref callback:
591602

592603
```jsx
593604
const on = (type, handler, options) => el => el.addEventListener(type, handler, options);

‎packages/babel-plugin/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ Inline style attributes in templates when the value is a string or `Record<strin
253253
- Type: `boolean`
254254
- Default: `false`
255255

256-
SSR-only: keep attribute-slot positions on intrinsic elements bindable — `ref` / `on*` compile to a guarded `ssrClaim` hole instead of dropping, and a dynamic `class` / `style` compiles to a whole-attribute `ssrElementAttribute` hole instead of a value inside template quotes.
256+
SSR-only: keep attribute-slot positions on intrinsic elements bindable — `ref` / `onXxx` handlers compile to a guarded `ssrClaim` hole instead of dropping, and a dynamic `class` / `style` compiles to a whole-attribute `ssrElementAttribute` hole instead of a value inside template quotes.
257257

258258
### hoistProps
259259

@@ -329,7 +329,7 @@ const Parent = () => {
329329

330330
### on(eventName)
331331

332-
CamelCase attributes such as `onClick` are event handlers. The compiler delegates events that bubble or compose, otherwise it uses Level 1 `on_____` properties.
332+
CamelCase attributes such as `onClick` are event handlers. The compiler delegates events that bubble or compose, otherwise it uses Level 1 `on_____` properties. Only `on` followed by an uppercase letter is an event: lowercase names such as `onclick` are plain attributes, compiled (and server-rendered) like any other attribute.
333333

334334
Pass an array to bind a value: the second item is the first argument to the handler, the event is second.
335335

‎packages/babel-plugin/src/dom/element.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
isComponent,
1919
registerImportMethod,
2020
filterChildren,
21+
isEventName,
2122
toEventName,
2223
checkLength,
2324
getStaticExpression,
@@ -1043,7 +1044,7 @@ function transformAttributes(
10431044
}
10441045
} else if (key === "children") {
10451046
children = value;
1046-
} else if (key.startsWith("on")) {
1047+
} else if (isEventName(key)) {
10471048
const ev = toEventName(key);
10481049
if (
10491050
config.delegateEvents &&

‎packages/babel-plugin/src/shared/utils.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,12 @@ export function trimWhitespace(text: string): string {
302302
return text.replace(/\s+/g, " ");
303303
}
304304

305+
// Only `on` + an uppercase letter is an event handler. Lowercase `on*`
306+
// names (`onclick`) are plain attributes.
307+
export function isEventName(name: string): boolean {
308+
return /^on[A-Z]/.test(name);
309+
}
310+
305311
export function toEventName(name: string): string {
306312
return name.slice(2).toLowerCase();
307313
}

‎packages/babel-plugin/src/ssr/element.ts‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import {
1919
convertJSXIdentifier,
2020
inlineCallExpression,
2121
canChildSlotAllocateIds,
22-
isFunctionShapedHole
22+
isFunctionShapedHole,
23+
isEventName,
24+
toEventName
2325
} from "../shared/utils";
2426
import { transformNode, getCreateTemplate } from "../shared/transform";
2527
import { decodedAttrValue } from "../universal/element";
@@ -589,13 +591,11 @@ function transformAttributes(
589591
return;
590592
}
591593
if (key.startsWith("prop:")) return;
592-
if (key.startsWith("on")) {
594+
if (isEventName(key)) {
593595
// `onXxx` lowercases to the event name — the client runtime's own
594596
// derivation (`onClick` -> `click`); the position is bound under it.
595-
if (info.serverComponents) {
596-
const pos = key.slice(2).toLowerCase();
597-
if (pos) claims.push([pos, value.expression as babelTypes.Expression]);
598-
}
597+
if (info.serverComponents)
598+
claims.push([toEventName(key), value.expression as babelTypes.Expression]);
599599
return;
600600
}
601601
if (ChildProperties.has(key)) {
@@ -1082,12 +1082,11 @@ function createElement(
10821082
: node.name.name;
10831083

10841084
if (hasChildren && key === "children") return;
1085-
if (key === "ref" || key.startsWith("on")) {
1085+
if (key === "ref" || isEventName(key)) {
10861086
if (serverComponents && t.isJSXExpressionContainer(value)) {
10871087
const expression = value.expression;
1088-
const pos = key === "ref" ? "ref" : key.slice(2).toLowerCase();
1088+
const pos = key === "ref" ? "ref" : toEventName(key);
10891089
if (
1090-
pos &&
10911090
!(
10921091
t.isJSXEmptyExpression(expression) ||
10931092
t.isStringLiteral(expression) ||

‎packages/babel-plugin/test/__dom_fixtures__/eventExpressions/code.js‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,27 @@ const hoistedCustomEvent2 = () => console.log("hoisted");
1010

1111
const template = (
1212
<div id="main">
13-
<button onchange={() => console.log("bound")}>Change Bound</button>
13+
<button onChange={() => console.log("bound")}>Change Bound</button>
1414
<button onChange={[id => console.log("bound", id), id]}>Change Bound</button>
15-
<button onchange={handler}>Change Bound</button>
16-
<button onchange={[handler]}>Change Bound</button>
17-
<button onchange={hoisted1}>Change Bound</button>
18-
<button onclick={() => console.log("delegated")}>Click Delegated</button>
15+
<button onChange={handler}>Change Bound</button>
16+
<button onChange={[handler]}>Change Bound</button>
17+
<button onChange={hoisted1}>Change Bound</button>
18+
<button onClick={() => console.log("delegated")}>Click Delegated</button>
1919
<button onClick={[id => console.log("delegated", id), rowId]}>Click Delegated</button>
2020
<button onClick={handler}>Click Delegated</button>
2121
<button onClick={[handler]}>Click Delegated</button>
2222
<button onClick={hoisted2}>Click Delegated</button>
2323
</div>
2424
);
25+
26+
const lowercaseAttributes = (
27+
<div>
28+
<button onclick="console.log('static')">Static Attribute</button>
29+
<button onclick={code}>Identifier Attribute</button>
30+
<button onmouseover={state.code}>Dynamic Attribute</button>
31+
<button onclick={() => console.log("not a handler")}>Function Attribute</button>
32+
<button {...rest} onclick={code}>
33+
Spread Attribute
34+
</button>
35+
</div>
36+
);

‎packages/babel-plugin/test/__dom_fixtures__/eventExpressions/output.js‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import { template as _$template } from "r-dom";
22
import { delegateEvents as _$delegateEvents } from "r-dom";
3+
import { effect as _$effect } from "r-dom";
4+
import { spread as _$spread } from "r-dom";
5+
import { setAttribute as _$setAttribute } from "r-dom";
36
import { addEvent as _$addEvent } from "r-dom";
47
var _tmpl$ = /*#__PURE__*/ _$template(
5-
`<div id=main><button>Change Bound</button><button>Change Bound</button><button>Change Bound</button><button>Change Bound</button><button>Change Bound</button><button>Click Delegated</button><button>Click Delegated</button><button>Click Delegated</button><button>Click Delegated</button><button>Click Delegated`
6-
);
8+
`<div id=main><button>Change Bound</button><button>Change Bound</button><button>Change Bound</button><button>Change Bound</button><button>Change Bound</button><button>Click Delegated</button><button>Click Delegated</button><button>Click Delegated</button><button>Click Delegated</button><button>Click Delegated`
9+
),
10+
_tmpl$2 = /*#__PURE__*/ _$template(
11+
`<div><button onclick="console.log('static')">Static Attribute</button><button>Identifier Attribute</button><button>Dynamic Attribute</button><button>Function Attribute</button><button>Spread Attribute`
12+
);
713
function hoisted1() {
814
console.log("hoisted");
915
}
@@ -35,4 +41,29 @@ _$addEvent(_el$9, "click", handler, true);
3541
_el$0._$$click = handler;
3642
_el$1._$$click = hoisted2;
3743
const template = _el$;
44+
var _el$10 = _tmpl$2(),
45+
_el$11 = _el$10.firstChild,
46+
_el$12 = _el$11.nextSibling,
47+
_el$13 = _el$12.nextSibling,
48+
_el$14 = _el$13.nextSibling,
49+
_el$15 = _el$14.nextSibling;
50+
_$setAttribute(_el$12, "onclick", code);
51+
_$setAttribute(_el$14, "onclick", () => console.log("not a handler"));
52+
_$spread(
53+
_el$15,
54+
[
55+
rest,
56+
{
57+
onclick: code
58+
}
59+
],
60+
true
61+
);
62+
_$effect(
63+
() => state.code,
64+
_v$ => {
65+
_$setAttribute(_el$13, "onmouseover", _v$);
66+
}
67+
);
68+
const lowercaseAttributes = _el$10;
3869
_$delegateEvents(["click"]);

0 commit comments

Comments
 (0)