@shadow-js/web 1.0.0 → 1.0.2
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/CHANGELOG.md +29 -0
- package/dist/dev/attributes.d.ts +10 -5
- package/dist/dev/attributes.js +28 -51
- package/dist/dev/attributes.js.map +1 -1
- package/dist/dev/autofocus.d.ts +0 -2
- package/dist/dev/autofocus.js +1 -2
- package/dist/dev/class-style.d.ts +6 -4
- package/dist/dev/class-style.js +25 -9
- package/dist/dev/class-style.js.map +1 -1
- package/dist/dev/component.d.ts +0 -2
- package/dist/dev/component.js +1 -2
- package/dist/dev/control-flow/activity.d.ts +0 -2
- package/dist/dev/control-flow/activity.js +1 -2
- package/dist/dev/control-flow/branch.js +0 -1
- package/dist/dev/control-flow/client-only.d.ts +0 -2
- package/dist/dev/control-flow/client-only.js +1 -2
- package/dist/dev/control-flow/dynamic.d.ts +11 -4
- package/dist/dev/control-flow/dynamic.js +57 -41
- package/dist/dev/control-flow/dynamic.js.map +1 -1
- package/dist/dev/control-flow/error-boundary.d.ts +0 -2
- package/dist/dev/control-flow/error-boundary.js +1 -2
- package/dist/dev/control-flow/for.d.ts +0 -2
- package/dist/dev/control-flow/for.js +1 -2
- package/dist/dev/control-flow/fragment.d.ts +0 -2
- package/dist/dev/control-flow/fragment.js +1 -2
- package/dist/dev/control-flow/hot-link.js +1 -2
- package/dist/dev/control-flow/index-list.d.ts +0 -2
- package/dist/dev/control-flow/index-list.js +1 -2
- package/dist/dev/control-flow/lazy.d.ts +0 -2
- package/dist/dev/control-flow/lazy.js +1 -2
- package/dist/dev/control-flow/no-hydration.d.ts +0 -2
- package/dist/dev/control-flow/no-hydration.js +1 -2
- package/dist/dev/control-flow/portal.d.ts +0 -2
- package/dist/dev/control-flow/portal.js +3 -2
- package/dist/dev/control-flow/portal.js.map +1 -1
- package/dist/dev/control-flow/root-errors.d.ts +0 -2
- package/dist/dev/control-flow/root-errors.js +1 -2
- package/dist/dev/control-flow/show.d.ts +0 -2
- package/dist/dev/control-flow/show.js +1 -2
- package/dist/dev/control-flow/suspense-list.d.ts +0 -2
- package/dist/dev/control-flow/suspense-list.js +1 -2
- package/dist/dev/control-flow/suspense.d.ts +0 -2
- package/dist/dev/control-flow/suspense.js +1 -2
- package/dist/dev/control-flow/switch.d.ts +0 -2
- package/dist/dev/control-flow/switch.js +1 -2
- package/dist/dev/derived.d.ts +0 -2
- package/dist/dev/derived.js +1 -2
- package/dist/dev/element.d.ts +0 -2
- package/dist/dev/element.js +2 -3
- package/dist/dev/element.js.map +1 -1
- package/dist/dev/events.d.ts +0 -2
- package/dist/dev/events.js +16 -46
- package/dist/dev/events.js.map +1 -1
- package/dist/dev/head/components.d.ts +0 -2
- package/dist/dev/head/components.js +1 -2
- package/dist/dev/head/registry.js +1 -2
- package/dist/dev/head/server-head.d.ts +0 -2
- package/dist/dev/head/server-head.js +1 -2
- package/dist/dev/head/sink.js +1 -2
- package/dist/dev/head/use-head.d.ts +0 -2
- package/dist/dev/head/use-head.js +1 -2
- package/dist/dev/head/validate.d.ts +0 -2
- package/dist/dev/head/validate.js +1 -2
- package/dist/dev/hmr.d.ts +0 -2
- package/dist/dev/hmr.js +1 -2
- package/dist/dev/hydration.d.ts +0 -2
- package/dist/dev/hydration.js +1 -2
- package/dist/dev/insert.d.ts +15 -7
- package/dist/dev/insert.js +36 -15
- package/dist/dev/insert.js.map +1 -1
- package/dist/dev/internal/claim.d.ts +2 -3
- package/dist/dev/internal/claim.js +19 -4
- package/dist/dev/internal/claim.js.map +1 -1
- package/dist/dev/internal/css-units.js +1 -2
- package/dist/dev/internal/diagnostics.d.ts +7 -5
- package/dist/dev/internal/diagnostics.js +15 -6
- package/dist/dev/internal/diagnostics.js.map +1 -1
- package/dist/dev/internal/early-events.js +21 -4
- package/dist/dev/internal/early-events.js.map +1 -1
- package/dist/dev/internal/hydration-hooks.d.ts +0 -2
- package/dist/dev/internal/hydration-hooks.js +0 -1
- package/dist/dev/internal/hydration-scope.d.ts +0 -2
- package/dist/dev/internal/hydration-scope.js +1 -2
- package/dist/dev/internal/names.d.ts +0 -2
- package/dist/dev/internal/names.js +1 -2
- package/dist/dev/internal/portal-context.d.ts +0 -2
- package/dist/dev/internal/portal-context.js +1 -2
- package/dist/dev/internal/react-names.js +26 -63
- package/dist/dev/internal/react-names.js.map +1 -1
- package/dist/dev/internal/reconcile.d.ts +0 -2
- package/dist/dev/internal/reconcile.js +4 -8
- package/dist/dev/internal/reconcile.js.map +1 -1
- package/dist/dev/internal/stream-context.d.ts +0 -2
- package/dist/dev/internal/stream-context.js +1 -2
- package/dist/dev/jsx/aria.d.ts +0 -2
- package/dist/dev/jsx/component.d.ts +0 -2
- package/dist/dev/jsx/events.d.ts +0 -2
- package/dist/dev/jsx/global.d.ts +5 -4
- package/dist/dev/jsx/html.d.ts +1 -2
- package/dist/dev/jsx/intrinsic.d.ts +0 -2
- package/dist/dev/jsx/mathml.d.ts +0 -2
- package/dist/dev/jsx/svg.d.ts +0 -2
- package/dist/dev/jsx/types.d.ts +0 -2
- package/dist/dev/jsx-runtime.d.ts +0 -2
- package/dist/dev/reactive.d.ts +0 -2
- package/dist/dev/reactive.js +1 -2
- package/dist/dev/render.d.ts +0 -2
- package/dist/dev/render.js +1 -2
- package/dist/dev/resource-hints.d.ts +0 -2
- package/dist/dev/resource-hints.js +5 -4
- package/dist/dev/resource-hints.js.map +1 -1
- package/dist/dev/select-value.d.ts +0 -2
- package/dist/dev/select-value.js +6 -17
- package/dist/dev/select-value.js.map +1 -1
- package/dist/dev/server/attributes.d.ts +3 -2
- package/dist/dev/server/attributes.js +6 -3
- package/dist/dev/server/attributes.js.map +1 -1
- package/dist/dev/server/class-style.d.ts +0 -2
- package/dist/dev/server/class-style.js +1 -2
- package/dist/dev/server/component.d.ts +0 -2
- package/dist/dev/server/component.js +1 -2
- package/dist/dev/server/derived.d.ts +0 -2
- package/dist/dev/server/derived.js +1 -2
- package/dist/dev/server/element.d.ts +0 -2
- package/dist/dev/server/element.js +9 -6
- package/dist/dev/server/element.js.map +1 -1
- package/dist/dev/server/errors.js +8 -3
- package/dist/dev/server/errors.js.map +1 -1
- package/dist/dev/server/escape.d.ts +0 -2
- package/dist/dev/server/escape.js +15 -8
- package/dist/dev/server/escape.js.map +1 -1
- package/dist/dev/server/form-controls.d.ts +0 -2
- package/dist/dev/server/form-controls.js +6 -4
- package/dist/dev/server/form-controls.js.map +1 -1
- package/dist/dev/server/hydration-script.d.ts +0 -2
- package/dist/dev/server/hydration-script.js +1 -2
- package/dist/dev/server/index.d.ts +2 -1
- package/dist/dev/server/index.js +2 -1
- package/dist/dev/server/node.d.ts +0 -2
- package/dist/dev/server/node.js +1 -2
- package/dist/dev/server/portals.d.ts +0 -2
- package/dist/dev/server/portals.js +0 -1
- package/dist/dev/server/raw-text.d.ts +28 -0
- package/dist/dev/server/raw-text.js +66 -0
- package/dist/dev/server/raw-text.js.map +1 -0
- package/dist/dev/server/render.d.ts +2 -3
- package/dist/dev/server/render.js +3 -4
- package/dist/dev/server/render.js.map +1 -1
- package/dist/dev/server/serialize.d.ts +0 -2
- package/dist/dev/server/serialize.js +1 -2
- package/dist/dev/server/stream.d.ts +1 -3
- package/dist/dev/server/stream.js +3 -4
- package/dist/dev/server/stream.js.map +1 -1
- package/dist/dev/server.d.ts +2 -1
- package/dist/dev/server.js +2 -1
- package/dist/dev/spread.d.ts +0 -2
- package/dist/dev/spread.js +8 -56
- package/dist/dev/spread.js.map +1 -1
- package/dist/dev/template.d.ts +0 -2
- package/dist/dev/template.js +1 -2
- package/dist/dev/view-transition.d.ts +0 -2
- package/dist/dev/view-transition.js +1 -2
- package/dist/prod/attributes.js +1 -1
- package/dist/prod/attributes.js.map +1 -1
- package/dist/prod/class-style.js +1 -1
- package/dist/prod/class-style.js.map +1 -1
- package/dist/prod/control-flow/dynamic.js +1 -1
- package/dist/prod/control-flow/dynamic.js.map +1 -1
- package/dist/prod/control-flow/portal.js +1 -1
- package/dist/prod/control-flow/portal.js.map +1 -1
- package/dist/prod/element.js +1 -1
- package/dist/prod/element.js.map +1 -1
- package/dist/prod/events.js +1 -1
- package/dist/prod/events.js.map +1 -1
- package/dist/prod/insert.js +1 -1
- package/dist/prod/insert.js.map +1 -1
- package/dist/prod/internal/claim.js +1 -1
- package/dist/prod/internal/claim.js.map +1 -1
- package/dist/prod/internal/diagnostics.js +1 -1
- package/dist/prod/internal/diagnostics.js.map +1 -1
- package/dist/prod/internal/early-events.js +1 -1
- package/dist/prod/internal/early-events.js.map +1 -1
- package/dist/prod/internal/react-names.js +1 -1
- package/dist/prod/internal/react-names.js.map +1 -1
- package/dist/prod/internal/reconcile.js +1 -1
- package/dist/prod/internal/reconcile.js.map +1 -1
- package/dist/prod/resource-hints.js +1 -1
- package/dist/prod/resource-hints.js.map +1 -1
- package/dist/prod/select-value.js +1 -1
- package/dist/prod/select-value.js.map +1 -1
- package/dist/prod/server/attributes.js +1 -1
- package/dist/prod/server/attributes.js.map +1 -1
- package/dist/prod/server/element.js +1 -1
- package/dist/prod/server/element.js.map +1 -1
- package/dist/prod/server/errors.js +1 -1
- package/dist/prod/server/errors.js.map +1 -1
- package/dist/prod/server/escape.js +1 -1
- package/dist/prod/server/escape.js.map +1 -1
- package/dist/prod/server/form-controls.js +2 -2
- package/dist/prod/server/form-controls.js.map +1 -1
- package/dist/prod/server/index.js +1 -1
- package/dist/prod/server/raw-text.js +2 -0
- package/dist/prod/server/raw-text.js.map +1 -0
- package/dist/prod/server/render.js +1 -1
- package/dist/prod/server/render.js.map +1 -1
- package/dist/prod/server/stream.js +1 -1
- package/dist/prod/server/stream.js.map +1 -1
- package/dist/prod/server.js +1 -1
- package/dist/prod/spread.js +1 -1
- package/dist/prod/spread.js.map +1 -1
- package/dist/server/attributes.js +1 -1
- package/dist/server/attributes.js.map +1 -1
- package/dist/server/class-style.js +1 -1
- package/dist/server/class-style.js.map +1 -1
- package/dist/server/control-flow/dynamic.js +1 -1
- package/dist/server/control-flow/dynamic.js.map +1 -1
- package/dist/server/control-flow/portal.js +1 -1
- package/dist/server/control-flow/portal.js.map +1 -1
- package/dist/server/element.js +1 -1
- package/dist/server/element.js.map +1 -1
- package/dist/server/events.js +1 -1
- package/dist/server/events.js.map +1 -1
- package/dist/server/insert.js +1 -1
- package/dist/server/insert.js.map +1 -1
- package/dist/server/internal/claim.js +1 -1
- package/dist/server/internal/claim.js.map +1 -1
- package/dist/server/internal/diagnostics.js +1 -1
- package/dist/server/internal/diagnostics.js.map +1 -1
- package/dist/server/internal/early-events.js +1 -1
- package/dist/server/internal/early-events.js.map +1 -1
- package/dist/server/internal/react-names.js +1 -1
- package/dist/server/internal/react-names.js.map +1 -1
- package/dist/server/internal/reconcile.js +1 -1
- package/dist/server/internal/reconcile.js.map +1 -1
- package/dist/server/resource-hints.js +1 -1
- package/dist/server/resource-hints.js.map +1 -1
- package/dist/server/select-value.js +1 -1
- package/dist/server/select-value.js.map +1 -1
- package/dist/server/server/attributes.js +1 -1
- package/dist/server/server/attributes.js.map +1 -1
- package/dist/server/server/element.js +1 -1
- package/dist/server/server/element.js.map +1 -1
- package/dist/server/server/errors.js +1 -1
- package/dist/server/server/errors.js.map +1 -1
- package/dist/server/server/escape.js +1 -1
- package/dist/server/server/escape.js.map +1 -1
- package/dist/server/server/form-controls.js +2 -2
- package/dist/server/server/form-controls.js.map +1 -1
- package/dist/server/server/index.js +1 -1
- package/dist/server/server/raw-text.js +2 -0
- package/dist/server/server/raw-text.js.map +1 -0
- package/dist/server/server/render.js +1 -1
- package/dist/server/server/render.js.map +1 -1
- package/dist/server/server/stream.js +1 -1
- package/dist/server/server/stream.js.map +1 -1
- package/dist/server/server.js +1 -1
- package/dist/server/spread.js +1 -1
- package/dist/server/spread.js.map +1 -1
- package/dist/server-dev/attributes.js +28 -51
- package/dist/server-dev/attributes.js.map +1 -1
- package/dist/server-dev/autofocus.js +1 -2
- package/dist/server-dev/class-style.js +25 -9
- package/dist/server-dev/class-style.js.map +1 -1
- package/dist/server-dev/component.js +1 -2
- package/dist/server-dev/control-flow/activity.js +1 -2
- package/dist/server-dev/control-flow/branch.js +0 -1
- package/dist/server-dev/control-flow/client-only.js +1 -2
- package/dist/server-dev/control-flow/dynamic.js +23 -12
- package/dist/server-dev/control-flow/dynamic.js.map +1 -1
- package/dist/server-dev/control-flow/error-boundary.js +1 -2
- package/dist/server-dev/control-flow/for.js +1 -2
- package/dist/server-dev/control-flow/fragment.js +1 -2
- package/dist/server-dev/control-flow/hot-link.js +1 -2
- package/dist/server-dev/control-flow/index-list.js +1 -2
- package/dist/server-dev/control-flow/lazy.js +1 -2
- package/dist/server-dev/control-flow/no-hydration.js +1 -2
- package/dist/server-dev/control-flow/portal.js +3 -2
- package/dist/server-dev/control-flow/portal.js.map +1 -1
- package/dist/server-dev/control-flow/root-errors.js +1 -2
- package/dist/server-dev/control-flow/show.js +1 -2
- package/dist/server-dev/control-flow/suspense-list.js +1 -2
- package/dist/server-dev/control-flow/suspense.js +1 -2
- package/dist/server-dev/control-flow/switch.js +1 -2
- package/dist/server-dev/derived.js +1 -2
- package/dist/server-dev/element.js +4 -3
- package/dist/server-dev/element.js.map +1 -1
- package/dist/server-dev/events.js +16 -46
- package/dist/server-dev/events.js.map +1 -1
- package/dist/server-dev/head/components.js +1 -2
- package/dist/server-dev/head/registry.js +1 -2
- package/dist/server-dev/head/server-head.js +1 -2
- package/dist/server-dev/head/sink.js +1 -2
- package/dist/server-dev/head/use-head.js +1 -2
- package/dist/server-dev/head/validate.js +1 -2
- package/dist/server-dev/hydration.js +1 -2
- package/dist/server-dev/insert.js +36 -15
- package/dist/server-dev/insert.js.map +1 -1
- package/dist/server-dev/internal/claim.js +19 -4
- package/dist/server-dev/internal/claim.js.map +1 -1
- package/dist/server-dev/internal/css-units.js +1 -2
- package/dist/server-dev/internal/diagnostics.js +15 -6
- package/dist/server-dev/internal/diagnostics.js.map +1 -1
- package/dist/server-dev/internal/early-events.js +21 -4
- package/dist/server-dev/internal/early-events.js.map +1 -1
- package/dist/server-dev/internal/hydration-hooks.js +0 -1
- package/dist/server-dev/internal/hydration-scope.js +1 -2
- package/dist/server-dev/internal/names.js +1 -2
- package/dist/server-dev/internal/portal-context.js +1 -2
- package/dist/server-dev/internal/react-names.js +26 -63
- package/dist/server-dev/internal/react-names.js.map +1 -1
- package/dist/server-dev/internal/reconcile.js +4 -8
- package/dist/server-dev/internal/reconcile.js.map +1 -1
- package/dist/server-dev/internal/stream-context.js +1 -2
- package/dist/server-dev/reactive.js +1 -2
- package/dist/server-dev/render.js +1 -2
- package/dist/server-dev/resource-hints.js +5 -4
- package/dist/server-dev/resource-hints.js.map +1 -1
- package/dist/server-dev/select-value.js +6 -17
- package/dist/server-dev/select-value.js.map +1 -1
- package/dist/server-dev/server/attributes.js +6 -3
- package/dist/server-dev/server/attributes.js.map +1 -1
- package/dist/server-dev/server/class-style.js +1 -2
- package/dist/server-dev/server/component.js +1 -2
- package/dist/server-dev/server/derived.js +1 -2
- package/dist/server-dev/server/element.js +9 -6
- package/dist/server-dev/server/element.js.map +1 -1
- package/dist/server-dev/server/errors.js +8 -3
- package/dist/server-dev/server/errors.js.map +1 -1
- package/dist/server-dev/server/escape.js +15 -8
- package/dist/server-dev/server/escape.js.map +1 -1
- package/dist/server-dev/server/form-controls.js +6 -4
- package/dist/server-dev/server/form-controls.js.map +1 -1
- package/dist/server-dev/server/hydration-script.js +1 -2
- package/dist/server-dev/server/index.js +2 -1
- package/dist/server-dev/server/node.js +1 -2
- package/dist/server-dev/server/portals.js +0 -1
- package/dist/server-dev/server/raw-text.js +86 -0
- package/dist/server-dev/server/raw-text.js.map +1 -0
- package/dist/server-dev/server/render.js +3 -4
- package/dist/server-dev/server/render.js.map +1 -1
- package/dist/server-dev/server/serialize.js +1 -2
- package/dist/server-dev/server/stream.js +3 -4
- package/dist/server-dev/server/stream.js.map +1 -1
- package/dist/server-dev/server.js +2 -1
- package/dist/server-dev/spread.js +8 -56
- package/dist/server-dev/spread.js.map +1 -1
- package/dist/server-dev/template.js +1 -2
- package/dist/server-dev/view-transition.js +1 -2
- package/package.json +3 -2
|
@@ -1,52 +1,24 @@
|
|
|
1
1
|
import { batch, getOwner, onCleanup, untrack } from "@shadow-js/core";
|
|
2
|
-
|
|
2
|
+
// src/events.ts
|
|
3
3
|
/**
|
|
4
4
|
* Events that may be delegated: composed events, which leave a shadow root and reach the document.
|
|
5
5
|
* `change` is not composed, so it is bound on its element (see the note on `bindEvent`).
|
|
6
6
|
*/
|
|
7
|
-
const DELEGATED_EVENTS =
|
|
8
|
-
"click",
|
|
9
|
-
"dblclick",
|
|
10
|
-
"input",
|
|
11
|
-
"keydown",
|
|
12
|
-
"keyup",
|
|
13
|
-
"keypress",
|
|
14
|
-
"pointerdown",
|
|
15
|
-
"pointerup",
|
|
16
|
-
"pointermove",
|
|
17
|
-
"mousedown",
|
|
18
|
-
"mouseup",
|
|
19
|
-
"mousemove",
|
|
20
|
-
"focusin",
|
|
21
|
-
"focusout",
|
|
22
|
-
"beforeinput",
|
|
23
|
-
"touchstart",
|
|
24
|
-
"touchend",
|
|
25
|
-
"touchmove",
|
|
26
|
-
"contextmenu",
|
|
27
|
-
"mouseover",
|
|
28
|
-
"mouseout",
|
|
29
|
-
"pointerover",
|
|
30
|
-
"pointerout"
|
|
31
|
-
]);
|
|
7
|
+
const DELEGATED_EVENTS = /^(?:click|dblclick|input|beforeinput|contextmenu|key(?:down|up|press)|(?:pointer|mouse)(?:down|up|move|over|out)|focus(?:in|out)|touch(?:start|end|move))$/;
|
|
32
8
|
/** Documents that already carry the delegated listener for a given event name. */
|
|
33
9
|
const installed = /* @__PURE__ */ new WeakMap();
|
|
34
10
|
/** Calls `fn` with `this` = node, retargeting `currentTarget` for the duration of the call. */
|
|
35
|
-
function invoke(
|
|
11
|
+
function invoke(fn, node, event, args) {
|
|
36
12
|
Object.defineProperty(event, "currentTarget", {
|
|
37
13
|
configurable: true,
|
|
38
14
|
get: () => node
|
|
39
15
|
});
|
|
40
16
|
try {
|
|
41
|
-
untrack(() =>
|
|
17
|
+
untrack(() => fn.call(node, ...args));
|
|
42
18
|
} finally {
|
|
43
19
|
Reflect.deleteProperty(event, "currentTarget");
|
|
44
20
|
}
|
|
45
21
|
}
|
|
46
|
-
/** Reads a compiler-style expando (`node.$$click`), if present. */
|
|
47
|
-
function expando(node, key) {
|
|
48
|
-
return Reflect.get(node, key);
|
|
49
|
-
}
|
|
50
22
|
/**
|
|
51
23
|
* The one listener per (document, event). It walks `composedPath()` from the target outward,
|
|
52
24
|
* calling `node.$$<type>` with `this = node`. Stops when a handler stops propagation.
|
|
@@ -60,16 +32,11 @@ function dispatchPath(event) {
|
|
|
60
32
|
const errors = [];
|
|
61
33
|
for (const node of event.composedPath()) {
|
|
62
34
|
if (event.cancelBubble) break;
|
|
63
|
-
const fn =
|
|
35
|
+
const fn = Reflect.get(node, key);
|
|
64
36
|
if (typeof fn !== "function") continue;
|
|
65
|
-
const data =
|
|
66
|
-
const binding = {
|
|
67
|
-
fn,
|
|
68
|
-
data,
|
|
69
|
-
tuple: data !== void 0
|
|
70
|
-
};
|
|
37
|
+
const data = Reflect.get(node, dataKey);
|
|
71
38
|
try {
|
|
72
|
-
invoke(
|
|
39
|
+
invoke(fn, node, event, data === void 0 ? [event] : [data, event]);
|
|
73
40
|
} catch (error) {
|
|
74
41
|
errors.push(error);
|
|
75
42
|
}
|
|
@@ -83,10 +50,13 @@ function isListenerObject(handler) {
|
|
|
83
50
|
}
|
|
84
51
|
/** Binds a listener object natively (never delegated), honoring its options. */
|
|
85
52
|
function bindListenerObject(el, name, object, capture) {
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
53
|
+
const { once, passive, signal } = object;
|
|
54
|
+
const options = {
|
|
55
|
+
capture: object.capture === true || capture,
|
|
56
|
+
once,
|
|
57
|
+
passive,
|
|
58
|
+
signal
|
|
59
|
+
};
|
|
90
60
|
const listener = (event) => {
|
|
91
61
|
batch(() => untrack(() => object.handleEvent(event)));
|
|
92
62
|
};
|
|
@@ -206,7 +176,8 @@ function bindEvent(el, name, handler, delegate, capture = false) {
|
|
|
206
176
|
if (isListenerObject(handler)) return bindListenerObject(el, name, handler, capture);
|
|
207
177
|
const binding = toBinding(handler);
|
|
208
178
|
const slot = `$$${name}`;
|
|
209
|
-
|
|
179
|
+
const plain = !binding.tuple || binding.data !== void 0;
|
|
180
|
+
if (delegate && plain && !capture && DELEGATED_EVENTS.test(name) && Reflect.get(el, slot) === void 0) {
|
|
210
181
|
delegateEvents([name], delegationDocument(el));
|
|
211
182
|
const dataKey = `${slot}Data`;
|
|
212
183
|
Reflect.set(el, slot, binding.fn);
|
|
@@ -259,7 +230,6 @@ function addEventListener(el, name, handler, delegate, capture) {
|
|
|
259
230
|
const unbind = bindEvent(el, name, handler, delegate === true, capture === true);
|
|
260
231
|
if (unbind !== void 0 && getOwner() !== null) onCleanup(unbind);
|
|
261
232
|
}
|
|
262
|
-
//#endregion
|
|
263
233
|
export { addEventListener, bindEvent, chainEvents, clearDelegatedEvents, delegateEvents };
|
|
264
234
|
|
|
265
235
|
//# sourceMappingURL=events.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"events.js","names":["holder"],"sources":["../../src/events.ts"],"sourcesContent":["import { batch, getOwner, onCleanup, untrack } from \"@shadow-js/core\";\n\n/**\n * Events that may be delegated: composed events, which leave a shadow root and reach the document.\n * `change` is not composed, so it is bound on its element (see the note on `bindEvent`).\n */\nconst DELEGATED_EVENTS = new Set([\n \"click\",\n \"dblclick\",\n \"input\",\n \"keydown\",\n \"keyup\",\n \"keypress\",\n \"pointerdown\",\n \"pointerup\",\n \"pointermove\",\n \"mousedown\",\n \"mouseup\",\n \"mousemove\",\n \"focusin\",\n \"focusout\",\n \"beforeinput\",\n \"touchstart\",\n \"touchend\",\n \"touchmove\",\n \"contextmenu\",\n \"mouseover\",\n \"mouseout\",\n \"pointerover\",\n \"pointerout\",\n]);\n\n/** Documents that already carry the delegated listener for a given event name. */\nconst installed = new WeakMap<Document, Set<string>>();\n\n/** A handler together with its optional data; `tuple` records the `[fn, data]` form. */\ninterface Binding {\n readonly fn: (this: EventTarget, ...args: unknown[]) => unknown;\n readonly data: unknown;\n readonly tuple: boolean;\n}\n\n/** Calls `fn` with `this` = node, retargeting `currentTarget` for the duration of the call. */\nfunction invoke(binding: Binding, node: EventTarget, event: Event, args: unknown[]): void {\n Object.defineProperty(event, \"currentTarget\", { configurable: true, get: () => node });\n try {\n untrack(() => binding.fn.call(node, ...args));\n } finally {\n Reflect.deleteProperty(event, \"currentTarget\");\n }\n}\n\n/** Reads a compiler-style expando (`node.$$click`), if present. */\nfunction expando(node: EventTarget, key: string): unknown {\n const holder: object = node;\n return Reflect.get(holder, key);\n}\n\n/**\n * The one listener per (document, event). It walks `composedPath()` from the target outward,\n * calling `node.$$<type>` with `this = node`. Stops when a handler stops propagation.\n */\nfunction dispatchDelegated(event: Event): void {\n // One batch per event: every handler on the path runs first, then the effects, so the DOM is\n // current when the event finishes (ADR-007).\n batch(() => dispatchPath(event));\n}\n\nfunction dispatchPath(event: Event): void {\n const key = `$$${event.type}`;\n const dataKey = `${key}Data`;\n const errors: unknown[] = [];\n for (const node of event.composedPath()) {\n if (event.cancelBubble) break;\n const fn = expando(node, key);\n if (typeof fn !== \"function\") continue;\n const data = expando(node, dataKey);\n const binding: Binding = {\n fn: fn as (this: EventTarget, ...args: unknown[]) => unknown,\n data,\n tuple: data !== undefined,\n };\n try {\n invoke(binding, node, event, binding.tuple ? [data, event] : [event]);\n } catch (error) {\n errors.push(error);\n }\n }\n const [first] = errors;\n if (errors.length === 1) throw first;\n if (errors.length > 1)\n throw new AggregateError(\n errors,\n __DEV__ ? \"Several event handlers threw.\" : \"SHADOW_EVENT_ERRORS\",\n );\n}\n\n/** A DOM-style listener object: `handleEvent` plus the `addEventListener` options. */\ninterface ListenerObject {\n handleEvent(event: Event): unknown;\n once?: boolean;\n passive?: boolean;\n capture?: boolean;\n signal?: AbortSignal;\n}\n\nfunction isListenerObject(handler: unknown): handler is ListenerObject {\n return (\n typeof handler === \"object\" &&\n handler !== null &&\n typeof (handler as { handleEvent?: unknown }).handleEvent === \"function\"\n );\n}\n\n/** Binds a listener object natively (never delegated), honoring its options. */\nfunction bindListenerObject(\n el: Element,\n name: string,\n object: ListenerObject,\n capture: boolean,\n): () => void {\n const options: AddEventListenerOptions = { capture: object.capture === true || capture };\n if (object.once !== undefined) options.once = object.once;\n if (object.passive !== undefined) options.passive = object.passive;\n if (object.signal !== undefined) options.signal = object.signal;\n const listener = (event: Event): void => {\n batch(() => untrack(() => object.handleEvent(event)));\n };\n el.addEventListener(name, listener, options);\n return () => {\n el.removeEventListener(name, listener, options);\n };\n}\n\n/** Normalizes a handler value into a binding; throws for values that are neither. */\nfunction toBinding(handler: unknown): Binding {\n if (typeof handler === \"function\") {\n return { fn: handler as Binding[\"fn\"], data: undefined, tuple: false };\n }\n if (Array.isArray(handler) && typeof handler[0] === \"function\") {\n return { fn: handler[0] as Binding[\"fn\"], data: handler[1], tuple: true };\n }\n throw new TypeError(\n __DEV__\n ? \"addEventListener(): the handler must be a function, [function, data] or an object with handleEvent.\"\n : \"SHADOW_EVENT_HANDLER\",\n );\n}\n\n/**\n * Combines several handlers for one event name on one element into one handler that calls them in\n * order with the same event. The compiler uses it when an element has, for example, both `onInput`\n * and a text input's `onChange`: the delegated slot holds one function.\n *\n * @param handlers The handlers, in source order. None gives a handler that does nothing.\n * @returns A handler that calls each of them with the same `this` and event. An error thrown by one\n * handler propagates and the later handlers do not run.\n * @example\n * import { addEventListener, chainEvents } from \"@shadow-js/web\";\n *\n * const input = document.createElement(\"input\");\n * const onInput = chainEvents(\n * (event) => console.log(\"validate\", event.type),\n * (event) => console.log(\"save\", event.type),\n * );\n * addEventListener(input, \"input\", onInput);\n * @since 1.0.0\n */\nexport function chainEvents(\n ...handlers: Array<(this: EventTarget, event: Event) => unknown>\n): (this: EventTarget, event: Event) => void {\n return function chained(this: EventTarget, event: Event): void {\n for (const handler of handlers) handler.call(this, event);\n };\n}\n\n/**\n * Installs the document listener that runs delegated handlers for each event name, once per\n * document and name; repeated calls do nothing. Compiled modules call it for the events they\n * delegate, so application code rarely needs it. The listener walks `composedPath()` from the target\n * outward and runs each element's delegated handler in one batch, stopping when a handler stops\n * propagation. A non-composed event raised inside a shadow root never reaches the document, so bind\n * such events natively.\n *\n * @param names Event names to delegate, such as `[\"click\", \"input\"]`.\n * @param target Document that receives the listeners. Default: the global `document`.\n * @example\n * import { delegateEvents } from \"@shadow-js/web\";\n *\n * delegateEvents([\"click\", \"input\"]);\n * @see clearDelegatedEvents\n * @since 1.0.0\n */\nexport function delegateEvents(names: string[], target: Document = globalThis.document): void {\n let known = installed.get(target);\n if (known === undefined) {\n known = new Set<string>();\n installed.set(target, known);\n }\n for (const name of names) {\n if (known.has(name)) continue;\n known.add(name);\n target.addEventListener(name, dispatchDelegated);\n }\n}\n\n/**\n * Removes the delegated listeners {@link delegateEvents} installed on `target`. Delegated handlers stop\n * running until `delegateEvents` is called again (Solid's `clearDelegatedEvents`). Use it when a page\n * unmounts ShadowJS entirely, for example a micro-frontend that hands the document back. A document\n * without delegated listeners is left as is.\n *\n * @param target Document whose listeners are removed. Default: the global `document`.\n * @example\n * import { clearDelegatedEvents } from \"@shadow-js/web\";\n *\n * export function unmountApp(dispose: () => void): void {\n * dispose();\n * clearDelegatedEvents();\n * }\n * @see delegateEvents\n * @since 1.0.0\n */\nexport function clearDelegatedEvents(target: Document = globalThis.document): void {\n const known = installed.get(target);\n if (known === undefined) return;\n for (const name of known) target.removeEventListener(name, dispatchDelegated);\n installed.delete(target);\n}\n\n/**\n * The document that must carry the delegated listener for `el`. A template clone is owned by the\n * template's inert document (no window, never receives an event) until it is inserted, and handlers\n * are bound before that: use the page's document then, which is where the element will live.\n */\nfunction delegationDocument(el: Element): Document {\n const owner = el.ownerDocument;\n return owner !== null && owner.defaultView !== null ? owner : globalThis.document;\n}\n\n/**\n * Binds one handler and returns the function that undoes it. A delegated event is stored on the\n * element as an expando; the document listener picks it up. Other events get a native listener.\n * Non-composed events (`change`, `select`, `load`, ...) never leave a shadow root, so they always get\n * a native listener, whatever the caller asked for.\n * @internal\n */\nexport function bindEvent(\n el: Element,\n name: string,\n handler: unknown,\n delegate: boolean,\n capture = false,\n): (() => void) | undefined {\n if (handler === null || handler === undefined || handler === false) return undefined;\n if (isListenerObject(handler)) return bindListenerObject(el, name, handler, capture);\n const binding = toBinding(handler);\n // One delegated slot per event name: a second handler for the same name on this element (React's\n // onInput and onChange on one text input) binds natively, so both run.\n const slot = `$$${name}`;\n if (delegate && !capture && DELEGATED_EVENTS.has(name) && Reflect.get(el, slot) === undefined) {\n delegateEvents([name], delegationDocument(el));\n const dataKey = `${slot}Data`;\n Reflect.set(el, slot, binding.fn);\n if (binding.tuple) Reflect.set(el, dataKey, binding.data);\n else Reflect.deleteProperty(el, dataKey);\n return () => {\n if (Reflect.get(el, slot) === binding.fn) {\n Reflect.deleteProperty(el, slot);\n Reflect.deleteProperty(el, dataKey);\n }\n };\n }\n const listener = (event: Event): void => {\n const args: unknown[] = binding.tuple ? [binding.data, event] : [event];\n batch(() => untrack(() => binding.fn.call(el, ...args)));\n };\n el.addEventListener(name, listener, capture);\n return () => {\n el.removeEventListener(name, listener, capture);\n };\n}\n\n/**\n * Binds an event handler to an element: the helper compiled templates call for `onX` and the `on:`\n * and `oncapture:` prefixes. Handlers run batched and untracked, with `this` set to the element.\n * Inside an owner, the listener is removed when the owner is disposed, even if the element outlives\n * it.\n *\n * @param el Target element.\n * @param name Event name, lowercase, such as `click`.\n * @param handler A function (called with the event), a `[fn, data]` tuple (called as\n * `fn(data, event)`), or a listener object `{ handleEvent, once, passive, capture, signal }`, which\n * is always bound natively with those options. `null`, `undefined` and `false` bind nothing.\n * @param delegate `true` stores the handler on the element for the document listener of\n * {@link delegateEvents} (installed on demand) when `name` is a delegated event (`click`, `input`,\n * `keydown`, pointer, mouse, touch and focus events, ...) and the element has no delegated handler\n * for it yet. Otherwise, and when undefined, the handler gets a native listener.\n * @param capture `true` listens in the capture phase, never delegated. Undefined means the bubble phase.\n * @throws TypeError `SHADOW_EVENT_HANDLER` when `handler` is not a function, a tuple whose first item\n * is a function, or a listener object.\n * @example\n * import { addEventListener } from \"@shadow-js/web\";\n *\n * const button = document.createElement(\"button\");\n * addEventListener(button, \"click\", () => console.log(\"pressed\"), true);\n * addEventListener(button, \"focus\", [(id: string, event: Event) => console.log(id, event.type), \"save\"]);\n * @see delegateEvents\n * @since 1.0.0\n */\nexport function addEventListener(\n el: Element,\n name: string,\n handler: unknown,\n delegate?: boolean,\n capture?: boolean,\n): void {\n const unbind = bindEvent(el, name, handler, delegate === true, capture === true);\n // The listener goes when the owner does, even if the element outlives it (moved, portaled, reused).\n if (unbind !== undefined && getOwner() !== null) onCleanup(unbind);\n}\n"],"mappings":";;;;;;AAMA,MAAM,mCAAmB,IAAI,IAAI;CAC/B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC;;AAGD,MAAM,4BAAY,IAAI,QAA+B;;AAUrD,SAAS,OAAO,SAAkB,MAAmB,OAAc,MAAuB;CACxF,OAAO,eAAe,OAAO,iBAAiB;EAAE,cAAc;EAAM,WAAW;CAAK,CAAC;CACrF,IAAI;EACF,cAAc,QAAQ,GAAG,KAAK,MAAM,GAAG,IAAI,CAAC;CAC9C,UAAU;EACR,QAAQ,eAAe,OAAO,eAAe;CAC/C;AACF;;AAGA,SAAS,QAAQ,MAAmB,KAAsB;CAExD,OAAO,QAAQ,IAAIA,MAAQ,GAAG;AAChC;;;;;AAMA,SAAS,kBAAkB,OAAoB;CAG7C,YAAY,aAAa,KAAK,CAAC;AACjC;AAEA,SAAS,aAAa,OAAoB;CACxC,MAAM,MAAM,KAAK,MAAM;CACvB,MAAM,UAAU,GAAG,IAAI;CACvB,MAAM,SAAoB,CAAC;CAC3B,KAAK,MAAM,QAA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AI,IAAI,IAAI,MAAM,KAAA,GAAW;EAC7F,eAAe,CAAC,IAAI,GAAG,mBAAmB,EAAE,CAAC;EAC7C,MAAM,UAAU,GAAG,KAAK;EACxB,QAAQ,IAAI,IAAI,MAAM,QAAQ,EAAE;EAChC,IAAI,QAAQ,OAAO,QAAQ,IAAI,IAAI,SAAS,QAAQ,IAAI;OACnD,QAAQ,eAAe,IAAI,OAAO;EACvC,aAAa;GACX,IAAI,QAAQ,IAAI,IAAI,IAAI,MAAM,QAAQ,IAAI;IACxC,QAAQ,eAAe,IAAI,IAAI;IAC/B,QAAQ,eAAe,IAAI,OAAO;GACpC;EACF;CACF;CACA,MAAM,YAAY,UAAuB;EACvC,MAAM,OAAkB,QAAQ,QAAQ,CAAC,QAAQ,MAAM,KAAK,IAAI,CAAC,KAAK;EACtE,YAAY,cAAc,QAAQ,GAAG,KAAK,IAAI,GAAG,IAAI,CAAC,CAAC;CACzD;CACA,GAAG,iBAAiB,MAAM,UAAU,OAAO;CAC3C,aAAa;EACX,GAAG,oBAAoB,MAAM,UAAU,OAAO;CAChD;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,SAAgB,iBACd,IACA,MACA,SACA,UACA,SACM;CACN,MAAM,SAAS,UAAU,IAAI,MAAM,SAAS,aAAa,MAAM,YAAY,IAAI;CAE/E,IAAI,WAAW,KAAA,KAAa,SAAS,MAAM,MAAM,UAAU,MAAM;AACnE"}
|
|
1
|
+
{"version":3,"file":"events.js","names":[],"sources":["../../src/events.ts"],"sourcesContent":["import { batch, getOwner, onCleanup, untrack } from \"@shadow-js/core\";\n\n/**\n * Events that may be delegated: composed events, which leave a shadow root and reach the document.\n * `change` is not composed, so it is bound on its element (see the note on `bindEvent`).\n */\nconst DELEGATED_EVENTS =\n /^(?:click|dblclick|input|beforeinput|contextmenu|key(?:down|up|press)|(?:pointer|mouse)(?:down|up|move|over|out)|focus(?:in|out)|touch(?:start|end|move))$/;\n\n/** Documents that already carry the delegated listener for a given event name. */\nconst installed = new WeakMap<Document, Set<string>>();\n\n/** A handler together with its optional data; `tuple` records the `[fn, data]` form. */\ninterface Binding {\n readonly fn: (this: EventTarget, ...args: unknown[]) => unknown;\n readonly data: unknown;\n readonly tuple: boolean;\n}\n\n/** Calls `fn` with `this` = node, retargeting `currentTarget` for the duration of the call. */\nfunction invoke(fn: Binding[\"fn\"], node: EventTarget, event: Event, args: unknown[]): void {\n Object.defineProperty(event, \"currentTarget\", { configurable: true, get: () => node });\n try {\n untrack(() => fn.call(node, ...args));\n } finally {\n Reflect.deleteProperty(event, \"currentTarget\");\n }\n}\n\n/**\n * The one listener per (document, event). It walks `composedPath()` from the target outward,\n * calling `node.$$<type>` with `this = node`. Stops when a handler stops propagation.\n */\nfunction dispatchDelegated(event: Event): void {\n // One batch per event: every handler on the path runs first, then the effects, so the DOM is\n // current when the event finishes (ADR-007).\n batch(() => dispatchPath(event));\n}\n\nfunction dispatchPath(event: Event): void {\n const key = `$$${event.type}`;\n const dataKey = `${key}Data`;\n const errors: unknown[] = [];\n for (const node of event.composedPath()) {\n if (event.cancelBubble) break;\n const fn = Reflect.get(node, key);\n if (typeof fn !== \"function\") continue;\n const data = Reflect.get(node, dataKey);\n try {\n invoke(fn as Binding[\"fn\"], node, event, data === undefined ? [event] : [data, event]);\n } catch (error) {\n errors.push(error);\n }\n }\n const [first] = errors;\n if (errors.length === 1) throw first;\n if (errors.length > 1)\n throw new AggregateError(\n errors,\n __DEV__ ? \"Several event handlers threw.\" : \"SHADOW_EVENT_ERRORS\",\n );\n}\n\n/** A DOM-style listener object: `handleEvent` plus the `addEventListener` options. */\ninterface ListenerObject {\n handleEvent(event: Event): unknown;\n once?: boolean;\n passive?: boolean;\n capture?: boolean;\n signal?: AbortSignal;\n}\n\nfunction isListenerObject(handler: unknown): handler is ListenerObject {\n return (\n typeof handler === \"object\" &&\n handler !== null &&\n typeof (handler as { handleEvent?: unknown }).handleEvent === \"function\"\n );\n}\n\n/** Binds a listener object natively (never delegated), honoring its options. */\nfunction bindListenerObject(\n el: Element,\n name: string,\n object: ListenerObject,\n capture: boolean,\n): () => void {\n const { once, passive, signal } = object;\n // A member that is `undefined` counts as absent to the DOM, so none of them needs a check.\n const options = {\n capture: object.capture === true || capture,\n once,\n passive,\n signal,\n } as AddEventListenerOptions;\n const listener = (event: Event): void => {\n batch(() => untrack(() => object.handleEvent(event)));\n };\n el.addEventListener(name, listener, options);\n return () => {\n el.removeEventListener(name, listener, options);\n };\n}\n\n/** Normalizes a handler value into a binding; throws for values that are neither. */\nfunction toBinding(handler: unknown): Binding {\n if (typeof handler === \"function\") {\n return { fn: handler as Binding[\"fn\"], data: undefined, tuple: false };\n }\n if (Array.isArray(handler) && typeof handler[0] === \"function\") {\n return { fn: handler[0] as Binding[\"fn\"], data: handler[1], tuple: true };\n }\n throw new TypeError(\n __DEV__\n ? \"addEventListener(): the handler must be a function, [function, data] or an object with handleEvent.\"\n : \"SHADOW_EVENT_HANDLER\",\n );\n}\n\n/**\n * Combines several handlers for one event name on one element into one handler that calls them in\n * order with the same event. The compiler uses it when an element has, for example, both `onInput`\n * and a text input's `onChange`: the delegated slot holds one function.\n *\n * @param handlers The handlers, in source order. None gives a handler that does nothing.\n * @returns A handler that calls each of them with the same `this` and event. An error thrown by one\n * handler propagates and the later handlers do not run.\n * @example\n * import { addEventListener, chainEvents } from \"@shadow-js/web\";\n *\n * const input = document.createElement(\"input\");\n * const onInput = chainEvents(\n * (event) => console.log(\"validate\", event.type),\n * (event) => console.log(\"save\", event.type),\n * );\n * addEventListener(input, \"input\", onInput);\n * @since 1.0.0\n */\nexport function chainEvents(\n ...handlers: Array<(this: EventTarget, event: Event) => unknown>\n): (this: EventTarget, event: Event) => void {\n return function chained(this: EventTarget, event: Event): void {\n for (const handler of handlers) handler.call(this, event);\n };\n}\n\n/**\n * Installs the document listener that runs delegated handlers for each event name, once per\n * document and name; repeated calls do nothing. Compiled modules call it for the events they\n * delegate, so application code rarely needs it. The listener walks `composedPath()` from the target\n * outward and runs each element's delegated handler in one batch, stopping when a handler stops\n * propagation. A non-composed event raised inside a shadow root never reaches the document, so bind\n * such events natively.\n *\n * @param names Event names to delegate, such as `[\"click\", \"input\"]`.\n * @param target Document that receives the listeners. Default: the global `document`.\n * @example\n * import { delegateEvents } from \"@shadow-js/web\";\n *\n * delegateEvents([\"click\", \"input\"]);\n * @see clearDelegatedEvents\n * @since 1.0.0\n */\nexport function delegateEvents(names: string[], target: Document = globalThis.document): void {\n let known = installed.get(target);\n if (known === undefined) {\n known = new Set<string>();\n installed.set(target, known);\n }\n for (const name of names) {\n if (known.has(name)) continue;\n known.add(name);\n target.addEventListener(name, dispatchDelegated);\n }\n}\n\n/**\n * Removes the delegated listeners {@link delegateEvents} installed on `target`. Delegated handlers stop\n * running until `delegateEvents` is called again (Solid's `clearDelegatedEvents`). Use it when a page\n * unmounts ShadowJS entirely, for example a micro-frontend that hands the document back. A document\n * without delegated listeners is left as is.\n *\n * @param target Document whose listeners are removed. Default: the global `document`.\n * @example\n * import { clearDelegatedEvents } from \"@shadow-js/web\";\n *\n * export function unmountApp(dispose: () => void): void {\n * dispose();\n * clearDelegatedEvents();\n * }\n * @see delegateEvents\n * @since 1.0.0\n */\nexport function clearDelegatedEvents(target: Document = globalThis.document): void {\n const known = installed.get(target);\n if (known === undefined) return;\n for (const name of known) target.removeEventListener(name, dispatchDelegated);\n installed.delete(target);\n}\n\n/**\n * The document that must carry the delegated listener for `el`. A template clone is owned by the\n * template's inert document (no window, never receives an event) until it is inserted, and handlers\n * are bound before that: use the page's document then, which is where the element will live.\n */\nfunction delegationDocument(el: Element): Document {\n const owner = el.ownerDocument;\n return owner !== null && owner.defaultView !== null ? owner : globalThis.document;\n}\n\n/**\n * Binds one handler and returns the function that undoes it. A delegated event is stored on the\n * element as an expando; the document listener picks it up. Other events get a native listener.\n * Non-composed events (`change`, `select`, `load`, ...) never leave a shadow root, so they always get\n * a native listener, whatever the caller asked for.\n * @internal\n */\nexport function bindEvent(\n el: Element,\n name: string,\n handler: unknown,\n delegate: boolean,\n capture = false,\n): (() => void) | undefined {\n if (handler === null || handler === undefined || handler === false) return undefined;\n if (isListenerObject(handler)) return bindListenerObject(el, name, handler, capture);\n const binding = toBinding(handler);\n // One delegated slot per event name: a second handler for the same name on this element (React's\n // onInput and onChange on one text input) binds natively, so both run.\n const slot = `$$${name}`;\n // The delegated slot tells a tuple from a plain handler by its data being defined, so a tuple whose\n // data is undefined is bound natively, where it still receives `(undefined, event)`.\n const plain = !binding.tuple || binding.data !== undefined;\n if (\n delegate &&\n plain &&\n !capture &&\n DELEGATED_EVENTS.test(name) &&\n Reflect.get(el, slot) === undefined\n ) {\n delegateEvents([name], delegationDocument(el));\n const dataKey = `${slot}Data`;\n Reflect.set(el, slot, binding.fn);\n if (binding.tuple) Reflect.set(el, dataKey, binding.data);\n else Reflect.deleteProperty(el, dataKey);\n return () => {\n if (Reflect.get(el, slot) === binding.fn) {\n Reflect.deleteProperty(el, slot);\n Reflect.deleteProperty(el, dataKey);\n }\n };\n }\n const listener = (event: Event): void => {\n const args: unknown[] = binding.tuple ? [binding.data, event] : [event];\n batch(() => untrack(() => binding.fn.call(el, ...args)));\n };\n el.addEventListener(name, listener, capture);\n return () => {\n el.removeEventListener(name, listener, capture);\n };\n}\n\n/**\n * Binds an event handler to an element: the helper compiled templates call for `onX` and the `on:`\n * and `oncapture:` prefixes. Handlers run batched and untracked, with `this` set to the element.\n * Inside an owner, the listener is removed when the owner is disposed, even if the element outlives\n * it.\n *\n * @param el Target element.\n * @param name Event name, lowercase, such as `click`.\n * @param handler A function (called with the event), a `[fn, data]` tuple (called as\n * `fn(data, event)`), or a listener object `{ handleEvent, once, passive, capture, signal }`, which\n * is always bound natively with those options. `null`, `undefined` and `false` bind nothing.\n * @param delegate `true` stores the handler on the element for the document listener of\n * {@link delegateEvents} (installed on demand) when `name` is a delegated event (`click`, `input`,\n * `keydown`, pointer, mouse, touch and focus events, ...) and the element has no delegated handler\n * for it yet. Otherwise, and when undefined, the handler gets a native listener.\n * @param capture `true` listens in the capture phase, never delegated. Undefined means the bubble phase.\n * @throws TypeError `SHADOW_EVENT_HANDLER` when `handler` is not a function, a tuple whose first item\n * is a function, or a listener object.\n * @example\n * import { addEventListener } from \"@shadow-js/web\";\n *\n * const button = document.createElement(\"button\");\n * addEventListener(button, \"click\", () => console.log(\"pressed\"), true);\n * addEventListener(button, \"focus\", [(id: string, event: Event) => console.log(id, event.type), \"save\"]);\n * @see delegateEvents\n * @since 1.0.0\n */\nexport function addEventListener(\n el: Element,\n name: string,\n handler: unknown,\n delegate?: boolean,\n capture?: boolean,\n): void {\n const unbind = bindEvent(el, name, handler, delegate === true, capture === true);\n // The listener goes when the owner does, even if the element outlives it (moved, portaled, reused).\n if (unbind !== undefined && getOwner() !== null) onCleanup(unbind);\n}\n"],"mappings":";;;;;;AAMA,MAAM,mBACJ;;AAGF,MAAM,4BAAY,IAAI,QAA+B;;AAUrD,SAAS,OAAO,IAAmB,MAAmB,OAAc,MAAuB;CACzF,OAAO,eAAe,OAAO,iBAAiB;EAAE,cAAc;EAAM,WAAW;CAAK,CAAC;CACrF,IAAI;EACF,cAAc,GAAG,KAAK,MAAM,GAAG,IAAI,CAAC;CACtC,UAAU;EACR,QAAQ,eAAe,OAAO,eAAe;CAC/C;AACF;;;;;AAMA,SAAS,kBAAkB,OAAoB;CAG7C,YAAY,aAAa,KAAK,CAAC;AACjC;AAEA,SAAS,aAAa,OAAoB;CACxC,MAAM,MAAM,KAAK,MAAM;CACvB,MAAM,UAAU,GAAG,IAAI;CACvB,MAAM,SAAoB,CAAC;CAC3B,KAAK,MAAM,QAAQ,MAAM,aAAa,GAAG;EACvC,IAAI,MAAM,cAAc;EACxB,MAAM,KAAK,QAAQ,IAAI,MAAM,GAAG;EAChC,IAAI,OAAO,OAAO,YAAY;EAC9B,MAAM,OAAO,QAAQ,IAAI,MAAM,OAAO;EACtC,IAAI;GACF,OAAO,IAAqB,MAAM,OAAO,SAAS,KAAA,IAAY,CAAC,K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,eAAe,IAAI,IAAI;IAC/B,QAAQ,eAAe,IAAI,OAAO;GACpC;EACF;CACF;CACA,MAAM,YAAY,UAAuB;EACvC,MAAM,OAAkB,QAAQ,QAAQ,CAAC,QAAQ,MAAM,KAAK,IAAI,CAAC,KAAK;EACtE,YAAY,cAAc,QAAQ,GAAG,KAAK,IAAI,GAAG,IAAI,CAAC,CAAC;CACzD;CACA,GAAG,iBAAiB,MAAM,UAAU,OAAO;CAC3C,aAAa;EACX,GAAG,oBAAoB,MAAM,UAAU,OAAO;CAChD;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,SAAgB,iBACd,IACA,MACA,SACA,UACA,SACM;CACN,MAAM,SAAS,UAAU,IAAI,MAAM,SAAS,aAAa,MAAM,YAAY,IAAI;CAE/E,IAAI,WAAW,KAAA,KAAa,SAAS,MAAM,MAAM,UAAU,MAAM;AACnE"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useHead } from "./use-head.js";
|
|
2
|
-
|
|
2
|
+
// src/head/components.ts
|
|
3
3
|
/** Text of a value that may be an accessor. Anything but text or an array of text throws. */
|
|
4
4
|
function partText(value) {
|
|
5
5
|
if (typeof value === "string") return value;
|
|
@@ -164,7 +164,6 @@ function Base(props) {
|
|
|
164
164
|
useHead(() => attributeTag("base", props, ["children"]));
|
|
165
165
|
return null;
|
|
166
166
|
}
|
|
167
|
-
//#endregion
|
|
168
167
|
export { Base, Link, Meta, Style, Title };
|
|
169
168
|
|
|
170
169
|
//# sourceMappingURL=components.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/head/registry.ts
|
|
2
2
|
const noop = () => void 0;
|
|
3
3
|
/**
|
|
4
4
|
* Creates an empty head registry. `onChange` runs after every effective add or removal, so the
|
|
@@ -62,7 +62,6 @@ function createHeadRegistry(onChange) {
|
|
|
62
62
|
}
|
|
63
63
|
};
|
|
64
64
|
}
|
|
65
|
-
//#endregion
|
|
66
65
|
export { createHeadRegistry };
|
|
67
66
|
|
|
68
67
|
//# sourceMappingURL=registry.js.map
|
|
@@ -2,7 +2,7 @@ import { escapeHtml } from "../server/escape.js";
|
|
|
2
2
|
import { createHeadRegistry } from "./registry.js";
|
|
3
3
|
import { HEAD_KEY, HEAD_MARKER } from "./validate.js";
|
|
4
4
|
import { createContext } from "@shadow-js/core";
|
|
5
|
-
|
|
5
|
+
// src/head/server-head.ts
|
|
6
6
|
/** Elements that never have content or a closing tag. */
|
|
7
7
|
const VOID_TAGS = /* @__PURE__ */ new Set([
|
|
8
8
|
"meta",
|
|
@@ -70,7 +70,6 @@ function runWithServerHead(head, fn) {
|
|
|
70
70
|
});
|
|
71
71
|
return holder.value;
|
|
72
72
|
}
|
|
73
|
-
//#endregion
|
|
74
73
|
export { ServerHeadContext, createServerHead, runWithServerHead };
|
|
75
74
|
|
|
76
75
|
//# sourceMappingURL=server-head.js.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { HEAD_KEY, HEAD_MARKER, identityOfElement } from "./validate.js";
|
|
2
|
-
|
|
2
|
+
// src/head/sink.ts
|
|
3
3
|
/** Removes or restores one element the sink owned. Elements already gone are ignored. */
|
|
4
4
|
function release(head, record) {
|
|
5
5
|
const el = record.el;
|
|
@@ -90,7 +90,6 @@ function createDomHeadSink(doc) {
|
|
|
90
90
|
}
|
|
91
91
|
};
|
|
92
92
|
}
|
|
93
|
-
//#endregion
|
|
94
93
|
export { createDomHeadSink };
|
|
95
94
|
|
|
96
95
|
//# sourceMappingURL=sink.js.map
|
|
@@ -3,7 +3,7 @@ import { normalizeTags } from "./validate.js";
|
|
|
3
3
|
import { ServerHeadContext } from "./server-head.js";
|
|
4
4
|
import { createDomHeadSink } from "./sink.js";
|
|
5
5
|
import { createRoot, getOwner, isServer, untrack, useContext, useEffect, useRenderEffect, useSignal } from "@shadow-js/core";
|
|
6
|
-
|
|
6
|
+
// src/head/use-head.ts
|
|
7
7
|
/** One registry per document, shared by every `useHead` call on it. */
|
|
8
8
|
const registries = /* @__PURE__ */ new WeakMap();
|
|
9
9
|
/**
|
|
@@ -78,7 +78,6 @@ function useHead(input) {
|
|
|
78
78
|
const registry = registryFor(document);
|
|
79
79
|
useRenderEffect(() => normalizeTags(typeof input === "function" ? input() : input), (tags) => registry.add(tags));
|
|
80
80
|
}
|
|
81
|
-
//#endregion
|
|
82
81
|
export { useHead };
|
|
83
82
|
|
|
84
83
|
//# sourceMappingURL=use-head.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { warnOnce } from "../internal/diagnostics.js";
|
|
2
2
|
import { isUnsafeUrlAttribute } from "../attributes.js";
|
|
3
|
-
|
|
3
|
+
// src/head/validate.ts
|
|
4
4
|
/** Marks every element the sink created or adopted. Callers may not supply it. @internal */
|
|
5
5
|
const HEAD_MARKER = "data-shadow-head";
|
|
6
6
|
/** Carries a style's key so a server-rendered style can be adopted. @internal */
|
|
@@ -262,7 +262,6 @@ function identityOfElement(element) {
|
|
|
262
262
|
}
|
|
263
263
|
return identityOf(name, lookup, element.getAttribute("data-shadow-key") ?? void 0);
|
|
264
264
|
}
|
|
265
|
-
//#endregion
|
|
266
265
|
export { HEAD_KEY, HEAD_MARKER, identityOf, identityOfElement, isHeadTagName, normalizeTag, normalizeTags };
|
|
267
266
|
|
|
268
267
|
//# sourceMappingURL=validate.js.map
|
|
@@ -6,7 +6,7 @@ import { attachRootErrors } from "./control-flow/root-errors.js";
|
|
|
6
6
|
import { replayEarlyEvents } from "./internal/early-events.js";
|
|
7
7
|
import { ResourceCacheContext, batch, createRoot, flushSync, runWithOwner, untrack } from "@shadow-js/core";
|
|
8
8
|
import { withHydration } from "@shadow-js/core/extend";
|
|
9
|
-
|
|
9
|
+
// src/hydration.ts
|
|
10
10
|
function dataCache(data) {
|
|
11
11
|
const used = /* @__PURE__ */ new Set();
|
|
12
12
|
return {
|
|
@@ -114,7 +114,6 @@ function hydrate(code, container, options = {}) {
|
|
|
114
114
|
removeRegion(region);
|
|
115
115
|
};
|
|
116
116
|
}
|
|
117
|
-
//#endregion
|
|
118
117
|
export { hydrate };
|
|
119
118
|
|
|
120
119
|
//# sourceMappingURL=hydration.js.map
|
|
@@ -2,10 +2,12 @@ import { checkClaimedText } from "./internal/claim.js";
|
|
|
2
2
|
import { claims } from "./internal/hydration-hooks.js";
|
|
3
3
|
import { reconcileNodes } from "./internal/reconcile.js";
|
|
4
4
|
import { useRenderEffect } from "@shadow-js/core";
|
|
5
|
-
import { createComputation, currentOwner, disposeOwner, readComputation } from "@shadow-js/core/extend";
|
|
6
|
-
|
|
5
|
+
import { addCleanup, createComputation, currentOwner, disposeOwner, readComputation } from "@shadow-js/core/extend";
|
|
6
|
+
// src/insert.ts
|
|
7
7
|
/** Longest chain of accessors a single value may hold (a function returning a function ...). */
|
|
8
8
|
const MAX_ACCESSOR_DEPTH = 1e4;
|
|
9
|
+
/** The newest reactive unanchored region of each parent: the one the next such region may have to give an anchor. */
|
|
10
|
+
const tails = /* @__PURE__ */ new WeakMap();
|
|
9
11
|
/** The document a node belongs to (a Document is its own document). */
|
|
10
12
|
function documentOf(node) {
|
|
11
13
|
return node instanceof Document ? node : node.ownerDocument ?? document;
|
|
@@ -57,9 +59,7 @@ function flatten(root, call = (fn) => fn()) {
|
|
|
57
59
|
item = call(item);
|
|
58
60
|
}
|
|
59
61
|
if (item === null || item === void 0 || typeof item === "boolean") continue;
|
|
60
|
-
if (
|
|
61
|
-
else if (typeof item === "number") parts.push(String(item));
|
|
62
|
-
else if (item instanceof DocumentFragment) for (const child of item.childNodes) parts.push(child);
|
|
62
|
+
if (item instanceof DocumentFragment) for (const child of item.childNodes) parts.push(child);
|
|
63
63
|
else if (item instanceof Node) parts.push(item);
|
|
64
64
|
else if (Array.isArray(item)) {
|
|
65
65
|
if (active.has(item)) throw new TypeError("insert(): a value contains itself.");
|
|
@@ -113,10 +113,9 @@ function materialize(region, parts) {
|
|
|
113
113
|
nodes.length = count;
|
|
114
114
|
return nodes;
|
|
115
115
|
}
|
|
116
|
-
/** The node after the region: its anchor, or the node after its last attached child, or its marker. */
|
|
117
|
-
function endOf(region,
|
|
116
|
+
/** The node after the region: its anchor, or the node after its last attached child (`last`), or its marker. */
|
|
117
|
+
function endOf(region, last) {
|
|
118
118
|
if (region.anchor !== null) return region.anchor;
|
|
119
|
-
const last = attached.at(-1);
|
|
120
119
|
if (last !== void 0) return last.nextSibling;
|
|
121
120
|
const marker = region.marker;
|
|
122
121
|
if (marker === null) return region.parent.firstChild;
|
|
@@ -127,7 +126,7 @@ function endOf(region, attached) {
|
|
|
127
126
|
function reconcileRegion(region, next) {
|
|
128
127
|
const parent = region.parent;
|
|
129
128
|
const attached = region.nodes.filter((node) => node.parentNode === parent);
|
|
130
|
-
const end = endOf(region, attached);
|
|
129
|
+
const end = endOf(region, attached.at(-1));
|
|
131
130
|
const first = attached[0];
|
|
132
131
|
if (next.length === 0 && first !== void 0) region.marker = first.previousSibling;
|
|
133
132
|
reconcileNodes(parent, attached, next, end);
|
|
@@ -160,7 +159,7 @@ function commitValue(region, raw) {
|
|
|
160
159
|
} else if (nodes.length === 0) {
|
|
161
160
|
const created = documentOf(region.parent).createTextNode(text);
|
|
162
161
|
addText(region, created);
|
|
163
|
-
region.parent.insertBefore(created, endOf(region,
|
|
162
|
+
region.parent.insertBefore(created, endOf(region, void 0));
|
|
164
163
|
region.nodes = [created];
|
|
165
164
|
return;
|
|
166
165
|
}
|
|
@@ -197,6 +196,24 @@ function createRegion(parent, anchorNode, initial) {
|
|
|
197
196
|
return region;
|
|
198
197
|
}
|
|
199
198
|
/**
|
|
199
|
+
* Called for a region that keeps changing and has no anchor. When the previous such region of the parent is alive
|
|
200
|
+
* and ends exactly where this one starts, that region gets an empty Text node there as its anchor. Its content then
|
|
201
|
+
* always goes before that node, and this region's marker is the node, so no region has a marker that a sibling
|
|
202
|
+
* region can replace: the order of the calls holds however the regions change. A region with static content
|
|
203
|
+
* between it and this one needs nothing, because its marker and its end are the static nodes.
|
|
204
|
+
*/
|
|
205
|
+
function linkRegion(region) {
|
|
206
|
+
const parent = region.parent;
|
|
207
|
+
const tail = tails.get(parent);
|
|
208
|
+
region.owner = currentOwner();
|
|
209
|
+
tails.set(parent, region);
|
|
210
|
+
const start = region.nodes[0] ?? null;
|
|
211
|
+
if (tail === void 0 || tail.owner?.disposed || endOf(tail, tail.nodes.at(-1)) !== start) return;
|
|
212
|
+
const text = anchor(parent, start);
|
|
213
|
+
tail.anchor = region.marker = text;
|
|
214
|
+
if (tail.owner) addCleanup(tail.owner, () => text.remove());
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
200
217
|
* True when a function appears anywhere in `root`, at any array depth. Scans without calling
|
|
201
218
|
* anything; `seen` stops it from looping on cyclic arrays (flatten reports those).
|
|
202
219
|
*/
|
|
@@ -256,15 +273,17 @@ function createCallCache(owner) {
|
|
|
256
273
|
/**
|
|
257
274
|
* Keeps a region in sync with `value`. A value that is or contains a function (an accessor, or an
|
|
258
275
|
* array with accessor members) runs inside a render effect, so its reads are tracked. Anything
|
|
259
|
-
* else is inserted once.
|
|
276
|
+
* else is inserted once. `ordered` is for `insert()` without an anchor: a region that keeps changing then keeps
|
|
277
|
+
* its place among the other such regions of its parent (see `linkRegion`).
|
|
260
278
|
* @internal
|
|
261
279
|
*/
|
|
262
|
-
function bindRegion(region, value) {
|
|
280
|
+
function bindRegion(region, value, ordered) {
|
|
263
281
|
if (!containsFunction(value)) {
|
|
264
282
|
commitValue(region, value);
|
|
265
283
|
return value instanceof DocumentFragment ? [...region.nodes] : value;
|
|
266
284
|
}
|
|
267
285
|
let latest;
|
|
286
|
+
if (ordered === true) linkRegion(region);
|
|
268
287
|
useRenderEffect(typeof value === "function" ? value : () => value, (raw) => {
|
|
269
288
|
latest = raw;
|
|
270
289
|
if (containsFunction(raw)) {
|
|
@@ -319,7 +338,10 @@ function anchor(parent, before) {
|
|
|
319
338
|
* - `null`, `undefined`, booleans render nothing. Strings and numbers become one Text node, reused
|
|
320
339
|
* when the region already holds one. A Node is inserted itself. A DocumentFragment inserts a
|
|
321
340
|
* snapshot of its children. Arrays are flattened, nested arrays included.
|
|
322
|
-
* - Content goes before `anchorNode`, or at the end when it is null or omitted.
|
|
341
|
+
* - Content goes before `anchorNode`, or at the end when it is null or omitted. Regions without an
|
|
342
|
+
* anchor on one parent keep the order of the calls, whatever they show and however often a sibling
|
|
343
|
+
* region replaces its nodes: when a reactive region is created right where the previous one ends, the
|
|
344
|
+
* previous one gets an empty Text node as its anchor there (removed again when that region is disposed).
|
|
323
345
|
* - Arrays are reconciled in place: nodes that remain are never removed and re-added.
|
|
324
346
|
* - `initial` lists nodes already in the DOM (from hydration or splitText) so nothing is recreated.
|
|
325
347
|
* - During `hydrate()`, a region that adopted server text is compared with its first commit: in
|
|
@@ -349,9 +371,8 @@ function anchor(parent, before) {
|
|
|
349
371
|
*/
|
|
350
372
|
function insert(parent, value, anchorNode, initial) {
|
|
351
373
|
const resolvedAnchor = anchorNode ?? null;
|
|
352
|
-
return bindRegion(createRegion(parent, resolvedAnchor, initial === void 0 ? claims.current.takeRegion(parent, resolvedAnchor) : initial), value);
|
|
374
|
+
return bindRegion(createRegion(parent, resolvedAnchor, initial === void 0 ? claims.current.takeRegion(parent, resolvedAnchor) : initial), value, resolvedAnchor === null);
|
|
353
375
|
}
|
|
354
|
-
//#endregion
|
|
355
376
|
export { anchor, bindRegion, createRegion, insert, removeRegion };
|
|
356
377
|
|
|
357
378
|
//# sourceMappingURL=insert.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"insert.js","names":[],"sources":["../../src/insert.ts"],"sourcesContent":["import { useRenderEffect } from \"@shadow-js/core\";\nimport {\n type ComputationNode,\n createComputation,\n currentOwner,\n disposeOwner,\n readComputation,\n} from \"@shadow-js/core/extend\";\nimport { checkClaimedText } from \"./internal/claim\";\nimport { claims } from \"./internal/hydration-hooks\";\nimport { reconcileNodes } from \"./internal/reconcile\";\n\n/** One piece of a value after flattening: a DOM node, or text to be turned into a Text node. */\ntype Part = Node | string;\n\n/** Longest chain of accessors a single value may hold (a function returning a function ...). */\nconst MAX_ACCESSOR_DEPTH = 10_000;\n\n/**\n * State of one dynamic child region. `nodes` are the DOM nodes the region owns, in order. When\n * `anchor` is set, content goes before it. Without one, an empty region is placed after `marker`\n * (the node that preceded it), so it keeps its position while it is empty.\n * @internal\n */\nexport interface Region {\n readonly parent: Node;\n readonly anchor: Node | null;\n marker: Node | null;\n nodes: Node[];\n /**\n * Text nodes this region created or adopted; only these may be reused by string updates. Null for none, the node\n * itself for one (the usual region: a single dynamic text), a Set for more, so most regions allocate no Set.\n */\n texts: Text | Set<Text> | null;\n /**\n * Development only: the server nodes a hydration pass handed this region, kept until its first\n * commit compares their text with the value (`checkClaimedText`).\n */\n claimed?: readonly Node[] | undefined;\n}\n\n/** The document a node belongs to (a Document is its own document). */\nfunction documentOf(node: Node): Document {\n return node instanceof Document ? node : (node.ownerDocument ?? document);\n}\n\n/** True when the region created or adopted `node`. */\nfunction ownsText(region: Region, node: Node): boolean {\n const texts = region.texts;\n return texts === node || (texts instanceof Set && texts.has(node as Text));\n}\n\n/** Records that the region owns `text`. */\nfunction addText(region: Region, text: Text): void {\n const texts = region.texts;\n if (texts === null) region.texts = text;\n else if (texts instanceof Set) texts.add(text);\n else if (texts !== text) region.texts = new Set([texts, text]);\n}\n\n/** Forgets the Text nodes of the region that are not in `keep`. */\nfunction pruneTexts(region: Region, keep: readonly Node[]): void {\n const texts = region.texts;\n if (texts === null) return;\n if (texts instanceof Set) {\n const kept = new Set<Node>(keep);\n for (const text of texts) {\n if (!kept.has(text)) texts.delete(text);\n }\n } else if (!keep.includes(texts)) region.texts = null;\n}\n\n/**\n * Flattens a value into parts without recursion. Functions are called (and their results unwrapped)\n * through `call`, which runs inside the render effect that owns the value, so reads are tracked.\n */\nfunction flatten(root: unknown, call: (fn: () => unknown) => unknown = (fn) => fn()): Part[] {\n const parts: Part[] = [];\n const frames: unknown[][] = [[root]];\n const cursors: number[] = [0];\n const active = new Set<unknown[]>();\n while (frames.length > 0) {\n const top = frames.length - 1;\n const frame = frames[top];\n const cursor = cursors[top] ?? 0;\n if (frame === undefined || cursor >= frame.length) {\n const done = frames.pop();\n if (done !== undefined) active.delete(done);\n cursors.pop();\n continue;\n }\n cursors[top] = cursor + 1;\n let item: unknown = frame[cursor];\n for (let depth = 0; typeof item === \"function\"; depth += 1) {\n if (depth >= MAX_ACCESSOR_DEPTH)\n throw new TypeError(\n __DEV__ ? \"insert(): accessor chain is too deep.\" : \"SHADOW_INSERT_DEPTH\",\n );\n item = call(item as () => unknown);\n }\n if (item === null || item === undefined || typeof item === \"boolean\") continue;\n if (typeof item === \"string\") parts.push(item);\n else if (typeof item === \"number\") parts.push(String(item));\n else if (item instanceof DocumentFragment) {\n for (const child of item.childNodes) parts.push(child);\n } else if (item instanceof Node) parts.push(item);\n else if (Array.isArray(item)) {\n if (active.has(item))\n throw new TypeError(__DEV__ ? \"insert(): a value contains itself.\" : \"SHADOW_INSERT_CYCLE\");\n active.add(item);\n frames.push(item);\n cursors.push(0);\n } else parts.push(String(item));\n }\n return parts;\n}\n\n/** Text nodes the region currently owns, keyed by their data, for reuse by equal strings. */\nfunction reusablePool(region: Region): Map<string, Text[]> {\n const pool = new Map<string, Text[]>();\n for (const node of region.nodes) {\n if (!(node instanceof Text) || !ownsText(region, node)) continue;\n const list = pool.get(node.data);\n if (list === undefined) pool.set(node.data, [node]);\n else list.push(node);\n }\n return pool;\n}\n\n/** Turns parts into the nodes the region should hold, reusing its own Text nodes where possible. */\nfunction materialize(region: Region, parts: readonly Part[]): Node[] {\n const only = parts.length === 1 ? parts[0] : undefined;\n if (typeof only === \"string\") {\n const current = region.nodes.length === 1 ? region.nodes[0] : undefined;\n if (current instanceof Text && ownsText(region, current)) {\n if (current.data !== only) current.data = only;\n return [current];\n }\n }\n // Sized to the parts: `[]` and `push` would reserve room for sixteen nodes in every dynamic child (a row has two).\n const nodes: Node[] = new Array<Node>(parts.length);\n let count = 0;\n const seen = new Set<Node>();\n let pool: Map<string, Text[]> | undefined;\n for (const part of parts) {\n if (typeof part === \"string\") {\n pool ??= reusablePool(region);\n const reused = pool.get(part)?.pop();\n let text = reused;\n if (text === undefined) {\n text = documentOf(region.parent).createTextNode(part);\n addText(region, text);\n }\n seen.add(text);\n nodes[count] = text;\n count += 1;\n } else if (!seen.has(part)) {\n seen.add(part);\n nodes[count] = part;\n count += 1;\n }\n }\n nodes.length = count;\n return nodes;\n}\n\n/** The node after the region: its anchor, or the node after its last attached child, or its marker. */\nfunction endOf(region: Region, attached: readonly Node[]): Node | null {\n if (region.anchor !== null) return region.anchor;\n const last = attached.at(-1);\n if (last !== undefined) return last.nextSibling;\n const marker = region.marker;\n if (marker === null) return region.parent.firstChild;\n if (marker.parentNode !== region.parent) return null;\n return marker.nextSibling;\n}\n\n/** Makes the DOM show `next` for this region. */\nfunction reconcileRegion(region: Region, next: Node[]): void {\n const parent = region.parent;\n const attached = region.nodes.filter((node) => node.parentNode === parent);\n const end = endOf(region, attached);\n const first = attached[0];\n if (next.length === 0 && first !== undefined) region.marker = first.previousSibling;\n reconcileNodes(parent, attached, next, end);\n region.nodes = next;\n pruneTexts(region, next);\n}\n\n/** Applies a flattened value to a region. */\nfunction commit(region: Region, parts: readonly Part[]): void {\n if (__DEV__ && region.claimed !== undefined) {\n // Only the first commit of a claimed region is compared; later changes are ordinary updates.\n checkClaimedText(region.claimed, parts);\n region.claimed = undefined;\n }\n reconcileRegion(region, materialize(region, parts));\n}\n\n/**\n * Applies a resolved value to a region. A string or number is the common case (a dynamic text): when the region\n * already shows its own Text node only the data is rewritten, and an empty region gets one new Text node, with none of\n * the flattening and reconciling a general value needs.\n */\nfunction commitValue(region: Region, raw: unknown): void {\n if ((typeof raw === \"string\" || typeof raw === \"number\") && !(__DEV__ && region.claimed)) {\n const nodes = region.nodes;\n const text = typeof raw === \"string\" ? raw : String(raw);\n if (nodes.length === 1) {\n const current = nodes[0];\n if (\n current instanceof Text &&\n current.parentNode === region.parent &&\n ownsText(region, current)\n ) {\n if (current.data !== text) current.data = text;\n return;\n }\n } else if (nodes.length === 0) {\n const created = documentOf(region.parent).createTextNode(text);\n addText(region, created);\n region.parent.insertBefore(created, endOf(region, nodes));\n region.nodes = [created];\n return;\n }\n }\n commit(region, flatten(raw));\n}\n\n/** Nodes of `initial` (already in the parent) that the region adopts as its current content. */\nfunction adoptInitial(parent: Node, initial: unknown): Node[] {\n const adopted: Node[] = [];\n const seen = new Set<Node>();\n for (const part of flatten(initial)) {\n if (typeof part === \"string\" || seen.has(part) || part.parentNode !== parent) continue;\n seen.add(part);\n adopted.push(part);\n }\n return adopted;\n}\n\n/**\n * Creates the state of a region in `parent`. `anchorNode` (null to append) and `initial` (nodes\n * already in the DOM, from hydration or splitText) are both optional.\n * @internal\n */\nexport function createRegion(parent: Node, anchorNode: Node | null, initial: unknown): Region {\n const nodes = initial === undefined ? [] : adoptInitial(parent, initial);\n const marker = nodes.length > 0 ? (nodes[0]?.previousSibling ?? null) : parent.lastChild;\n const region: Region = { parent, anchor: anchorNode, marker, nodes, texts: null };\n for (const node of nodes) if (node instanceof Text) addText(region, node);\n // A region that adopts server nodes during a hydration pass is checked against its first commit.\n if (__DEV__ && initial !== undefined && claims.current.isClaiming()) region.claimed = nodes;\n return region;\n}\n\n/**\n * True when a function appears anywhere in `root`, at any array depth. Scans without calling\n * anything; `seen` stops it from looping on cyclic arrays (flatten reports those).\n */\nfunction containsFunction(root: unknown): boolean {\n if (typeof root === \"function\") return true;\n if (!Array.isArray(root)) return false;\n const stack: unknown[][] = [root];\n const seen = new Set<unknown[]>([root]);\n while (stack.length > 0) {\n const frame = stack.pop() ?? [];\n for (const item of frame) {\n if (typeof item === \"function\") return true;\n if (Array.isArray(item) && !seen.has(item)) {\n seen.add(item);\n stack.push(item);\n }\n }\n }\n return false;\n}\n\n/**\n * One memo per accessor function, owned by `owner` (the effect that evaluates the region's expression,\n * so a new result drops them all; the effect that resolves the result runs many times and must not\n * own them). `prune` disposes the memos of accessors the last\n * run did not meet, which bounds the cache by what the region currently shows.\n */\nfunction createCallCache(owner: ReturnType<typeof currentOwner>): {\n begin(): void;\n call(fn: () => unknown): unknown;\n prune(): void;\n} {\n const memos = new Map<() => unknown, ComputationNode>();\n let touched = new Set<() => unknown>();\n return {\n begin(): void {\n touched = new Set();\n },\n call(fn: () => unknown): unknown {\n touched.add(fn);\n let node = memos.get(fn);\n if (node === undefined) {\n // Component setup runs inside these memos, which may write the state it owns.\n node = createComputation({ kind: \"memo\", fn: () => fn(), allowWrites: true, owner });\n memos.set(fn, node);\n }\n return readComputation(node);\n },\n prune(): void {\n for (const [fn, node] of memos) {\n if (touched.has(fn)) continue;\n memos.delete(fn);\n disposeOwner(node);\n }\n },\n };\n}\n\n/**\n * Keeps a region in sync with `value`. A value that is or contains a function (an accessor, or an\n * array with accessor members) runs inside a render effect, so its reads are tracked. Anything\n * else is inserted once.\n * @internal\n */\nexport function bindRegion(region: Region, value: unknown): unknown {\n if (!containsFunction(value)) {\n commitValue(region, value);\n return value instanceof DocumentFragment ? [...region.nodes] : value;\n }\n let latest: unknown;\n const accessor = typeof value === \"function\" ? (value as () => unknown) : () => value;\n // Every accessor met while resolving the result is evaluated by a memo of its own, kept between\n // runs. An accessor that builds components (`() => show() ? <Late /> : null`) then runs again\n // only when what it reads changes, not whenever something it returned (the component's own\n // accessor, reading its own data) does.\n // Two stages. The outer effect runs the expression and tracks only what the expression itself\n // reads. When the result holds accessors (a component returned one, or `props.children` did),\n // a nested effect resolves them, so a change inside the result re-renders the result but never\n // re-runs the expression: re-running `() => props.children` would build the child components\n // again, discarding their state.\n useRenderEffect(accessor, (raw) => {\n latest = raw;\n if (containsFunction(raw)) {\n const cache = createCallCache(currentOwner());\n useRenderEffect(\n () => {\n cache.begin();\n const parts = flatten(raw, cache.call);\n cache.prune();\n return parts;\n },\n (parts) => {\n commit(region, parts);\n },\n );\n } else {\n commitValue(region, raw);\n }\n });\n return latest;\n}\n\n/**\n * Removes a region's nodes from its parent. Anchors and nodes that were moved elsewhere are not\n * touched.\n * @internal\n */\nexport function removeRegion(region: Region): void {\n for (const node of region.nodes) {\n if (node.parentNode === region.parent) region.parent.removeChild(node);\n }\n region.nodes = [];\n region.texts = null;\n}\n\n/**\n * Creates a text anchor: an empty Text node placed before `before` (appended when null). Use it as\n * the stable insertion point between two dynamic regions; it is never removed by the runtime.\n *\n * @param parent Node that receives the anchor.\n * @param before Node the anchor goes before, or null to append.\n * @returns The new empty Text node.\n * @example\n * import { anchor, insert } from \"@shadow-js/web\";\n *\n * const list = document.createElement(\"ul\");\n * const slot = anchor(list, null);\n * insert(list, \"first\", slot); // goes before the anchor\n * @see insert\n * @since 1.0.0\n */\nexport function anchor(parent: Node, before: Node | null): Text {\n const text = documentOf(parent).createTextNode(\"\");\n parent.insertBefore(text, before);\n return text;\n}\n\n/**\n * Reactive child insertion: keeps the content of a region in sync with `value`.\n *\n * - A function is an accessor, wrapped in a render effect; it re-runs when its dependencies change.\n * - `null`, `undefined`, booleans render nothing. Strings and numbers become one Text node, reused\n * when the region already holds one. A Node is inserted itself. A DocumentFragment inserts a\n * snapshot of its children. Arrays are flattened, nested arrays included.\n * - Content goes before `anchorNode`, or at the end when it is null or omitted.\n * - Arrays are reconciled in place: nodes that remain are never removed and re-added.\n * - `initial` lists nodes already in the DOM (from hydration or splitText) so nothing is recreated.\n * - During `hydrate()`, a region that adopted server text is compared with its first commit: in\n * development, different text warns `HYDRATION_TEXT_MISMATCH` (on the console and in devtools) and\n * the region shows the value computed by the client.\n *\n * @param parent Node that holds the region.\n * @param value The value or an accessor returning it.\n * @param anchorNode Node the content goes before; null or omitted appends.\n * @param initial Nodes already in place for this region. Undefined adopts the server's nodes while\n * hydrating and starts empty otherwise.\n * @returns The value as accepted (a fragment yields the array of its nodes).\n * @throws TypeError (production message `SHADOW_INSERT_CYCLE`) when an array contains itself, and\n * (`SHADOW_INSERT_DEPTH`) when an accessor returns accessors more than 10,000 levels deep. When the\n * value is reactive, the error is raised inside its render effect and goes to the owner's error\n * handler, like any effect error.\n * @example\n * import { useSignal } from \"@shadow-js/core\";\n * import { anchor, insert } from \"@shadow-js/web\";\n *\n * const list = document.createElement(\"ul\");\n * const end = anchor(list, null);\n * const [items] = useSignal([\"a\", \"b\"]);\n * insert(list, () => items().map((name) => document.createTextNode(name)), end);\n * @see anchor\n * @since 1.0.0\n */\nexport function insert(\n parent: Node,\n value: unknown,\n anchorNode?: Node | null,\n initial?: unknown,\n): unknown {\n const resolvedAnchor = anchorNode ?? null;\n const adopted =\n initial === undefined ? claims.current.takeRegion(parent, resolvedAnchor) : initial;\n const region = createRegion(parent, resolvedAnchor, adopted);\n return bindRegion(region, value);\n}\n"],"mappings":";;;;;;;AAgBA,MAAM,qBAAqB;;AA0B3B,SAAS,WAAW,MAAsB;CACxC,OAAO,gBAAgB,WAAW,OAAQ,KAAK,iBAAiB;AAClE;;AAGA,SAAS,SAAS,QAAgB,MAAqB;CACrD,MAAM,QAAQ,OAAO;CACrB,OAAO,UAAU,QAAS,iBAAiB,OAAO,MAAM,IAAI,IAAY;AAC1E;;AAGA,SAAS,QAAQ,QAAgB,MAAkB;CACjD,MAAM,QAAQ,OAAO;CACrB,IAAI,UAAU,MAAM,OAAO,QAAQ;MAC9B,IAAI,iBAAiB,KAAK,MAAM,IAAI,IAAI;MACxC,IAAI,UAAU,MAAM,OAAO,wBAAQ,IAAI,IAAI,CAAC,OAAO,IAAI,CAAC;AAC/D;;AAGA,SAAS,WAAW,QAAgB,MAA6B;CAC/D,MAAM,QAAQ,OAAO;CACrB,IAAI,UAAU,MAAM;CACpB,IAAI,iBAAiB,KAAK;EACxB,MAAM,OAAO,IAAI,IAAU,IAAI;EAC/B,KAAK,MAAM,QAAQ,OACjB,IAAI,CAAC,KAAK,IAAI,IAAI,GAAG,MAAM,OAAO,IAAI;CAE1C,OAAO,IAAI,CAAC,KAAK,SAAS,KAAK,GAAG,OAAO,QAAQ;AACnD;;;;;AAMA,SAAS,QAAQ,MAAe,QAAwC,O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AAK,eAAe,OAAO,QAAQ,OAAO,OAAO,YAAY,IAAI;CAEvE,OAAO,QAAQ,CAAC;CAChB,OAAO,QAAQ;AACjB;;;;;;;;;;;;;;;;;AAkBA,SAAgB,OAAO,QAAc,QAA2B;CAC9D,MAAM,OAAO,WAAW,MAAM,CAAC,CAAC,eAAe,EAAE;CACjD,OAAO,aAAa,MAAM,MAAM;CAChC,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA,SAAgB,OACd,QACA,OACA,YACA,SACS;CACT,MAAM,iBAAiB,cAAc;CAIrC,OAAO,WADQ,aAAa,QAAQ,gBADlC,YAAY,KAAA,IAAY,OAAO,QAAQ,WAAW,QAAQ,cAAc,IAAI,OAEvD,GAAG,KAAK;AACjC"}
|
|
1
|
+
{"version":3,"file":"insert.js","names":[],"sources":["../../src/insert.ts"],"sourcesContent":["import { useRenderEffect } from \"@shadow-js/core\";\nimport {\n addCleanup,\n type ComputationNode,\n createComputation,\n currentOwner,\n disposeOwner,\n type Owner,\n readComputation,\n} from \"@shadow-js/core/extend\";\nimport { checkClaimedText } from \"./internal/claim\";\nimport { claims } from \"./internal/hydration-hooks\";\nimport { reconcileNodes } from \"./internal/reconcile\";\n\n/** One piece of a value after flattening: a DOM node, or text to be turned into a Text node. */\ntype Part = Node | string;\n\n/** Longest chain of accessors a single value may hold (a function returning a function ...). */\nconst MAX_ACCESSOR_DEPTH = 10_000;\n\n/**\n * State of one dynamic child region. `nodes` are the DOM nodes the region owns, in order. When\n * `anchor` is set, content goes before it. Without one, an empty region is placed after `marker`\n * (the node that preceded it), so it keeps its position while it is empty. A marker can be a node\n * another region owns and later replaces, so the region created right behind another one on the same parent\n * gives that one an anchor of its own (see `linkRegion`): the marker of every region is then either static\n * content or an anchor nobody replaces.\n * @internal\n */\nexport interface Region {\n readonly parent: Node;\n anchor: Node | null;\n marker: Node | null;\n /** The owner of the effect that keeps a reactive region up to date; unset until then (and for a static region). */\n owner?: Owner | null;\n nodes: Node[];\n /**\n * Text nodes this region created or adopted; only these may be reused by string updates. Null for none, the node\n * itself for one (the usual region: a single dynamic text), a Set for more, so most regions allocate no Set.\n */\n texts: Text | Set<Text> | null;\n /**\n * Development only: the server nodes a hydration pass handed this region, kept until its first\n * commit compares their text with the value (`checkClaimedText`).\n */\n claimed?: readonly Node[] | undefined;\n}\n\n/** The newest reactive unanchored region of each parent: the one the next such region may have to give an anchor. */\nconst tails = new WeakMap<Node, Region>();\n\n/** The document a node belongs to (a Document is its own document). */\nfunction documentOf(node: Node): Document {\n return node instanceof Document ? node : (node.ownerDocument ?? document);\n}\n\n/** True when the region created or adopted `node`. */\nfunction ownsText(region: Region, node: Node): boolean {\n const texts = region.texts;\n return texts === node || (texts instanceof Set && texts.has(node as Text));\n}\n\n/** Records that the region owns `text`. */\nfunction addText(region: Region, text: Text): void {\n const texts = region.texts;\n if (texts === null) region.texts = text;\n else if (texts instanceof Set) texts.add(text);\n else if (texts !== text) region.texts = new Set([texts, text]);\n}\n\n/** Forgets the Text nodes of the region that are not in `keep`. */\nfunction pruneTexts(region: Region, keep: readonly Node[]): void {\n const texts = region.texts;\n if (texts === null) return;\n if (texts instanceof Set) {\n const kept = new Set<Node>(keep);\n for (const text of texts) {\n if (!kept.has(text)) texts.delete(text);\n }\n } else if (!keep.includes(texts)) region.texts = null;\n}\n\n/**\n * Flattens a value into parts without recursion. Functions are called (and their results unwrapped)\n * through `call`, which runs inside the render effect that owns the value, so reads are tracked.\n */\nfunction flatten(root: unknown, call: (fn: () => unknown) => unknown = (fn) => fn()): Part[] {\n const parts: Part[] = [];\n const frames: unknown[][] = [[root]];\n const cursors: number[] = [0];\n const active = new Set<unknown[]>();\n while (frames.length > 0) {\n const top = frames.length - 1;\n const frame = frames[top];\n const cursor = cursors[top] ?? 0;\n if (frame === undefined || cursor >= frame.length) {\n const done = frames.pop();\n if (done !== undefined) active.delete(done);\n cursors.pop();\n continue;\n }\n cursors[top] = cursor + 1;\n let item: unknown = frame[cursor];\n for (let depth = 0; typeof item === \"function\"; depth += 1) {\n if (depth >= MAX_ACCESSOR_DEPTH)\n throw new TypeError(\n __DEV__ ? \"insert(): accessor chain is too deep.\" : \"SHADOW_INSERT_DEPTH\",\n );\n item = call(item as () => unknown);\n }\n if (item === null || item === undefined || typeof item === \"boolean\") continue;\n if (item instanceof DocumentFragment) {\n for (const child of item.childNodes) parts.push(child);\n } else if (item instanceof Node) parts.push(item);\n else if (Array.isArray(item)) {\n if (active.has(item))\n throw new TypeError(__DEV__ ? \"insert(): a value contains itself.\" : \"SHADOW_INSERT_CYCLE\");\n active.add(item);\n frames.push(item);\n cursors.push(0);\n } else parts.push(String(item)); // strings, numbers and anything else that is text\n }\n return parts;\n}\n\n/** Text nodes the region currently owns, keyed by their data, for reuse by equal strings. */\nfunction reusablePool(region: Region): Map<string, Text[]> {\n const pool = new Map<string, Text[]>();\n for (const node of region.nodes) {\n if (!(node instanceof Text) || !ownsText(region, node)) continue;\n const list = pool.get(node.data);\n if (list === undefined) pool.set(node.data, [node]);\n else list.push(node);\n }\n return pool;\n}\n\n/** Turns parts into the nodes the region should hold, reusing its own Text nodes where possible. */\nfunction materialize(region: Region, parts: readonly Part[]): Node[] {\n const only = parts.length === 1 ? parts[0] : undefined;\n if (typeof only === \"string\") {\n const current = region.nodes.length === 1 ? region.nodes[0] : undefined;\n if (current instanceof Text && ownsText(region, current)) {\n if (current.data !== only) current.data = only;\n return [current];\n }\n }\n // Sized to the parts: `[]` and `push` would reserve room for sixteen nodes in every dynamic child (a row has two).\n const nodes: Node[] = new Array<Node>(parts.length);\n let count = 0;\n const seen = new Set<Node>();\n let pool: Map<string, Text[]> | undefined;\n for (const part of parts) {\n if (typeof part === \"string\") {\n pool ??= reusablePool(region);\n const reused = pool.get(part)?.pop();\n let text = reused;\n if (text === undefined) {\n text = documentOf(region.parent).createTextNode(part);\n addText(region, text);\n }\n seen.add(text);\n nodes[count] = text;\n count += 1;\n } else if (!seen.has(part)) {\n seen.add(part);\n nodes[count] = part;\n count += 1;\n }\n }\n nodes.length = count;\n return nodes;\n}\n\n/** The node after the region: its anchor, or the node after its last attached child (`last`), or its marker. */\nfunction endOf(region: Region, last: Node | undefined): Node | null {\n if (region.anchor !== null) return region.anchor;\n if (last !== undefined) return last.nextSibling;\n const marker = region.marker;\n if (marker === null) return region.parent.firstChild;\n if (marker.parentNode !== region.parent) return null;\n return marker.nextSibling;\n}\n\n/** Makes the DOM show `next` for this region. */\nfunction reconcileRegion(region: Region, next: Node[]): void {\n const parent = region.parent;\n const attached = region.nodes.filter((node) => node.parentNode === parent);\n const end = endOf(region, attached.at(-1));\n const first = attached[0];\n if (next.length === 0 && first !== undefined) region.marker = first.previousSibling;\n reconcileNodes(parent, attached, next, end);\n region.nodes = next;\n pruneTexts(region, next);\n}\n\n/** Applies a flattened value to a region. */\nfunction commit(region: Region, parts: readonly Part[]): void {\n if (__DEV__ && region.claimed !== undefined) {\n // Only the first commit of a claimed region is compared; later changes are ordinary updates.\n checkClaimedText(region.claimed, parts);\n region.claimed = undefined;\n }\n reconcileRegion(region, materialize(region, parts));\n}\n\n/**\n * Applies a resolved value to a region. A string or number is the common case (a dynamic text): when the region\n * already shows its own Text node only the data is rewritten, and an empty region gets one new Text node, with none of\n * the flattening and reconciling a general value needs.\n */\nfunction commitValue(region: Region, raw: unknown): void {\n if ((typeof raw === \"string\" || typeof raw === \"number\") && !(__DEV__ && region.claimed)) {\n const nodes = region.nodes;\n const text = typeof raw === \"string\" ? raw : String(raw);\n if (nodes.length === 1) {\n const current = nodes[0];\n if (\n current instanceof Text &&\n current.parentNode === region.parent &&\n ownsText(region, current)\n ) {\n if (current.data !== text) current.data = text;\n return;\n }\n } else if (nodes.length === 0) {\n const created = documentOf(region.parent).createTextNode(text);\n addText(region, created);\n region.parent.insertBefore(created, endOf(region, undefined));\n region.nodes = [created];\n return;\n }\n }\n commit(region, flatten(raw));\n}\n\n/** Nodes of `initial` (already in the parent) that the region adopts as its current content. */\nfunction adoptInitial(parent: Node, initial: unknown): Node[] {\n const adopted: Node[] = [];\n const seen = new Set<Node>();\n for (const part of flatten(initial)) {\n if (typeof part === \"string\" || seen.has(part) || part.parentNode !== parent) continue;\n seen.add(part);\n adopted.push(part);\n }\n return adopted;\n}\n\n/**\n * Creates the state of a region in `parent`. `anchorNode` (null to append) and `initial` (nodes\n * already in the DOM, from hydration or splitText) are both optional.\n * @internal\n */\nexport function createRegion(parent: Node, anchorNode: Node | null, initial: unknown): Region {\n const nodes = initial === undefined ? [] : adoptInitial(parent, initial);\n const marker = nodes.length > 0 ? (nodes[0]?.previousSibling ?? null) : parent.lastChild;\n const region: Region = {\n parent,\n anchor: anchorNode,\n marker,\n nodes,\n texts: null,\n };\n for (const node of nodes) if (node instanceof Text) addText(region, node);\n // A region that adopts server nodes during a hydration pass is checked against its first commit.\n if (__DEV__ && initial !== undefined && claims.current.isClaiming()) region.claimed = nodes;\n return region;\n}\n\n/**\n * Called for a region that keeps changing and has no anchor. When the previous such region of the parent is alive\n * and ends exactly where this one starts, that region gets an empty Text node there as its anchor. Its content then\n * always goes before that node, and this region's marker is the node, so no region has a marker that a sibling\n * region can replace: the order of the calls holds however the regions change. A region with static content\n * between it and this one needs nothing, because its marker and its end are the static nodes.\n */\nfunction linkRegion(region: Region): void {\n const parent = region.parent;\n const tail = tails.get(parent);\n region.owner = currentOwner();\n tails.set(parent, region);\n const start = region.nodes[0] ?? null;\n if (tail === undefined || tail.owner?.disposed || endOf(tail, tail.nodes.at(-1)) !== start)\n return;\n const text = anchor(parent, start);\n tail.anchor = region.marker = text;\n // The anchor is the tail's alone and goes with it, so a region replaced by another one over and over adds\n // nothing to the parent. The region behind it, if empty, is then placed at the end like any region whose marker\n // is gone.\n if (tail.owner) addCleanup(tail.owner, () => text.remove());\n}\n\n/**\n * True when a function appears anywhere in `root`, at any array depth. Scans without calling\n * anything; `seen` stops it from looping on cyclic arrays (flatten reports those).\n */\nfunction containsFunction(root: unknown): boolean {\n if (typeof root === \"function\") return true;\n if (!Array.isArray(root)) return false;\n const stack: unknown[][] = [root];\n const seen = new Set<unknown[]>([root]);\n while (stack.length > 0) {\n const frame = stack.pop() ?? [];\n for (const item of frame) {\n if (typeof item === \"function\") return true;\n if (Array.isArray(item) && !seen.has(item)) {\n seen.add(item);\n stack.push(item);\n }\n }\n }\n return false;\n}\n\n/**\n * One memo per accessor function, owned by `owner` (the effect that evaluates the region's expression,\n * so a new result drops them all; the effect that resolves the result runs many times and must not\n * own them). `prune` disposes the memos of accessors the last\n * run did not meet, which bounds the cache by what the region currently shows.\n */\nfunction createCallCache(owner: ReturnType<typeof currentOwner>): {\n begin(): void;\n call(fn: () => unknown): unknown;\n prune(): void;\n} {\n const memos = new Map<() => unknown, ComputationNode>();\n let touched = new Set<() => unknown>();\n return {\n begin(): void {\n touched = new Set();\n },\n call(fn: () => unknown): unknown {\n touched.add(fn);\n let node = memos.get(fn);\n if (node === undefined) {\n // Component setup runs inside these memos, which may write the state it owns.\n node = createComputation({ kind: \"memo\", fn: () => fn(), allowWrites: true, owner });\n memos.set(fn, node);\n }\n return readComputation(node);\n },\n prune(): void {\n for (const [fn, node] of memos) {\n if (touched.has(fn)) continue;\n memos.delete(fn);\n disposeOwner(node);\n }\n },\n };\n}\n\n/**\n * Keeps a region in sync with `value`. A value that is or contains a function (an accessor, or an\n * array with accessor members) runs inside a render effect, so its reads are tracked. Anything\n * else is inserted once. `ordered` is for `insert()` without an anchor: a region that keeps changing then keeps\n * its place among the other such regions of its parent (see `linkRegion`).\n * @internal\n */\nexport function bindRegion(region: Region, value: unknown, ordered?: boolean): unknown {\n if (!containsFunction(value)) {\n commitValue(region, value);\n return value instanceof DocumentFragment ? [...region.nodes] : value;\n }\n let latest: unknown;\n if (ordered === true) linkRegion(region);\n const accessor = typeof value === \"function\" ? (value as () => unknown) : () => value;\n // Every accessor met while resolving the result is evaluated by a memo of its own, kept between\n // runs. An accessor that builds components (`() => show() ? <Late /> : null`) then runs again\n // only when what it reads changes, not whenever something it returned (the component's own\n // accessor, reading its own data) does.\n // Two stages. The outer effect runs the expression and tracks only what the expression itself\n // reads. When the result holds accessors (a component returned one, or `props.children` did),\n // a nested effect resolves them, so a change inside the result re-renders the result but never\n // re-runs the expression: re-running `() => props.children` would build the child components\n // again, discarding their state.\n useRenderEffect(accessor, (raw) => {\n latest = raw;\n if (containsFunction(raw)) {\n const cache = createCallCache(currentOwner());\n useRenderEffect(\n () => {\n cache.begin();\n const parts = flatten(raw, cache.call);\n cache.prune();\n return parts;\n },\n (parts) => {\n commit(region, parts);\n },\n );\n } else {\n commitValue(region, raw);\n }\n });\n return latest;\n}\n\n/**\n * Removes a region's nodes from its parent. Anchors and nodes that were moved elsewhere are not\n * touched.\n * @internal\n */\nexport function removeRegion(region: Region): void {\n for (const node of region.nodes) {\n if (node.parentNode === region.parent) region.parent.removeChild(node);\n }\n region.nodes = [];\n region.texts = null;\n}\n\n/**\n * Creates a text anchor: an empty Text node placed before `before` (appended when null). Use it as\n * the stable insertion point between two dynamic regions; it is never removed by the runtime.\n *\n * @param parent Node that receives the anchor.\n * @param before Node the anchor goes before, or null to append.\n * @returns The new empty Text node.\n * @example\n * import { anchor, insert } from \"@shadow-js/web\";\n *\n * const list = document.createElement(\"ul\");\n * const slot = anchor(list, null);\n * insert(list, \"first\", slot); // goes before the anchor\n * @see insert\n * @since 1.0.0\n */\nexport function anchor(parent: Node, before: Node | null): Text {\n const text = documentOf(parent).createTextNode(\"\");\n parent.insertBefore(text, before);\n return text;\n}\n\n/**\n * Reactive child insertion: keeps the content of a region in sync with `value`.\n *\n * - A function is an accessor, wrapped in a render effect; it re-runs when its dependencies change.\n * - `null`, `undefined`, booleans render nothing. Strings and numbers become one Text node, reused\n * when the region already holds one. A Node is inserted itself. A DocumentFragment inserts a\n * snapshot of its children. Arrays are flattened, nested arrays included.\n * - Content goes before `anchorNode`, or at the end when it is null or omitted. Regions without an\n * anchor on one parent keep the order of the calls, whatever they show and however often a sibling\n * region replaces its nodes: when a reactive region is created right where the previous one ends, the\n * previous one gets an empty Text node as its anchor there (removed again when that region is disposed).\n * - Arrays are reconciled in place: nodes that remain are never removed and re-added.\n * - `initial` lists nodes already in the DOM (from hydration or splitText) so nothing is recreated.\n * - During `hydrate()`, a region that adopted server text is compared with its first commit: in\n * development, different text warns `HYDRATION_TEXT_MISMATCH` (on the console and in devtools) and\n * the region shows the value computed by the client.\n *\n * @param parent Node that holds the region.\n * @param value The value or an accessor returning it.\n * @param anchorNode Node the content goes before; null or omitted appends.\n * @param initial Nodes already in place for this region. Undefined adopts the server's nodes while\n * hydrating and starts empty otherwise.\n * @returns The value as accepted (a fragment yields the array of its nodes).\n * @throws TypeError (production message `SHADOW_INSERT_CYCLE`) when an array contains itself, and\n * (`SHADOW_INSERT_DEPTH`) when an accessor returns accessors more than 10,000 levels deep. When the\n * value is reactive, the error is raised inside its render effect and goes to the owner's error\n * handler, like any effect error.\n * @example\n * import { useSignal } from \"@shadow-js/core\";\n * import { anchor, insert } from \"@shadow-js/web\";\n *\n * const list = document.createElement(\"ul\");\n * const end = anchor(list, null);\n * const [items] = useSignal([\"a\", \"b\"]);\n * insert(list, () => items().map((name) => document.createTextNode(name)), end);\n * @see anchor\n * @since 1.0.0\n */\nexport function insert(\n parent: Node,\n value: unknown,\n anchorNode?: Node | null,\n initial?: unknown,\n): unknown {\n const resolvedAnchor = anchorNode ?? null;\n const adopted =\n initial === undefined ? claims.current.takeRegion(parent, resolvedAnchor) : initial;\n const region = createRegion(parent, resolvedAnchor, adopted);\n return bindRegion(region, value, resolvedAnchor === null);\n}\n"],"mappings":";;;;;;;AAkBA,MAAM,qBAAqB;;AA+B3B,MAAM,wBAAQ,IAAI,QAAsB;;AAGxC,SAAS,WAAW,MAAsB;CACxC,OAAO,gBAAgB,WAAW,OAAQ,KAAK,iBAAiB;AAClE;;AAGA,SAAS,SAAS,QAAgB,MAAqB;CACrD,MAAM,QAAQ,OAAO;CACrB,OAAO,UAAU,QAAS,iBAAiB,OAAO,MAAM,IAAI,IAAY;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d,QACA,OACA,YACA,SACS;CACT,MAAM,iBAAiB,cAAc;CAIrC,OAAO,WADQ,aAAa,QAAQ,gBADlC,YAAY,KAAA,IAAY,OAAO,QAAQ,WAAW,QAAQ,cAAc,IAAI,OAEvD,GAAG,OAAO,mBAAmB,IAAI;AAC1D"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DEVTOOLS_EVENT, devtoolsReport } from "@shadow-js/core/extend";
|
|
2
|
-
|
|
2
|
+
// src/internal/claim.ts
|
|
3
3
|
let state = null;
|
|
4
4
|
/** Starts a claiming pass over the given server elements. @internal */
|
|
5
5
|
function beginClaiming(registry, recoverable) {
|
|
@@ -211,6 +211,21 @@ function checkClaimedText(claimed, parts) {
|
|
|
211
211
|
console.warn(`[shadow HYDRATION_TEXT_MISMATCH] ${detail}`);
|
|
212
212
|
devtoolsReport(DEVTOOLS_EVENT.HYDRATION_MISMATCH, detail, void 0, void 0);
|
|
213
213
|
}
|
|
214
|
+
/** HTML elements whose content is text and nothing else: no comment can mark a region inside them. */
|
|
215
|
+
const TEXT_ONLY = /* @__PURE__ */ new Set([
|
|
216
|
+
"script",
|
|
217
|
+
"style",
|
|
218
|
+
"title",
|
|
219
|
+
"textarea"
|
|
220
|
+
]);
|
|
221
|
+
/**
|
|
222
|
+
* True for a `script`, `style`, `title` or `textarea`, in whichever namespace the parser put it: `Dynamic` knows no
|
|
223
|
+
* parent, so the server writes the children of such a tag without region markers even where the page parses it as
|
|
224
|
+
* an SVG `title` or `style`. Its whole content is then the one region.
|
|
225
|
+
*/
|
|
226
|
+
function isTextOnly(node) {
|
|
227
|
+
return node.nodeType === 1 && TEXT_ONLY.has(node.localName);
|
|
228
|
+
}
|
|
214
229
|
/**
|
|
215
230
|
* Adopts the server content of the next region of a claimed parent while `hydrate()` runs: the nodes
|
|
216
231
|
* that sat between the region's markers are put back before `anchorNode` and returned, so a custom
|
|
@@ -219,7 +234,8 @@ function checkClaimedText(claimed, parts) {
|
|
|
219
234
|
* @param parent An element returned by {@link claimElement} (or one of its descendants).
|
|
220
235
|
* @param anchorNode The node the content goes before; `null` appends it at the end of `parent`.
|
|
221
236
|
* @returns The adopted nodes; undefined when no hydration pass is running or `parent` has no region
|
|
222
|
-
* left to adopt (the caller then inserts fresh nodes).
|
|
237
|
+
* left to adopt (the caller then inserts fresh nodes). A `script`, `style`, `title` or `textarea` has no
|
|
238
|
+
* markers (the server writes its text as the parser reads it): its current children are the region.
|
|
223
239
|
* @example
|
|
224
240
|
* import { takeRegion } from "@shadow-js/web/extend";
|
|
225
241
|
*
|
|
@@ -237,7 +253,7 @@ function takeRegion(parent, anchorNode) {
|
|
|
237
253
|
const current = state;
|
|
238
254
|
if (current === null) return void 0;
|
|
239
255
|
const content = current.pending.get(parent)?.shift();
|
|
240
|
-
if (content === void 0) return void 0;
|
|
256
|
+
if (content === void 0) return isTextOnly(parent) ? Array.from(parent.childNodes) : void 0;
|
|
241
257
|
for (const node of content) parent.insertBefore(node, anchorNode);
|
|
242
258
|
return content;
|
|
243
259
|
}
|
|
@@ -351,7 +367,6 @@ function isComment(node, data) {
|
|
|
351
367
|
function isRegionMarker(comment) {
|
|
352
368
|
return comment.data === "[" || comment.data === "]";
|
|
353
369
|
}
|
|
354
|
-
//#endregion
|
|
355
370
|
export { beginClaiming, checkClaimedTag, checkClaimedText, claimElement, claimPortal, endClaiming, isClaiming, liftedRegions, releaseUnclaimedPortals, takeRegion, withoutClaiming };
|
|
356
371
|
|
|
357
372
|
//# sourceMappingURL=claim.js.map
|