@pithyjs/core 0.1.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +513 -0
- package/dist/a11y/focus-trap.d.ts +109 -0
- package/dist/a11y/focus-trap.d.ts.map +1 -0
- package/dist/a11y/focus-trap.js +246 -0
- package/dist/a11y/focus-trap.js.map +1 -0
- package/dist/a11y/index.d.ts +12 -0
- package/dist/a11y/index.d.ts.map +1 -0
- package/dist/a11y/index.js +16 -0
- package/dist/a11y/index.js.map +1 -0
- package/dist/a11y/keyboard-utils.d.ts +165 -0
- package/dist/a11y/keyboard-utils.d.ts.map +1 -0
- package/dist/a11y/keyboard-utils.js +321 -0
- package/dist/a11y/keyboard-utils.js.map +1 -0
- package/dist/a11y/roving-tabindex.d.ts +118 -0
- package/dist/a11y/roving-tabindex.d.ts.map +1 -0
- package/dist/a11y/roving-tabindex.js +249 -0
- package/dist/a11y/roving-tabindex.js.map +1 -0
- package/dist/a11y/skip-link.d.ts +55 -0
- package/dist/a11y/skip-link.d.ts.map +1 -0
- package/dist/a11y/skip-link.js +164 -0
- package/dist/a11y/skip-link.js.map +1 -0
- package/dist/compile/bindSwitchCases.d.ts +16 -0
- package/dist/compile/bindSwitchCases.d.ts.map +1 -0
- package/dist/compile/bindSwitchCases.js +323 -0
- package/dist/compile/bindSwitchCases.js.map +1 -0
- package/dist/compile/binds/bindBoundAttributes.d.ts +14 -0
- package/dist/compile/binds/bindBoundAttributes.d.ts.map +1 -0
- package/dist/compile/binds/bindBoundAttributes.js +57 -0
- package/dist/compile/binds/bindBoundAttributes.js.map +1 -0
- package/dist/compile/binds/bindClassDirectives.d.ts +20 -0
- package/dist/compile/binds/bindClassDirectives.d.ts.map +1 -0
- package/dist/compile/binds/bindClassDirectives.js +94 -0
- package/dist/compile/binds/bindClassDirectives.js.map +1 -0
- package/dist/compile/binds/bindComponentsTags.d.ts +17 -0
- package/dist/compile/binds/bindComponentsTags.d.ts.map +1 -0
- package/dist/compile/binds/bindComponentsTags.js +355 -0
- package/dist/compile/binds/bindComponentsTags.js.map +1 -0
- package/dist/compile/binds/bindConditionals.d.ts +16 -0
- package/dist/compile/binds/bindConditionals.d.ts.map +1 -0
- package/dist/compile/binds/bindConditionals.js +102 -0
- package/dist/compile/binds/bindConditionals.js.map +1 -0
- package/dist/compile/binds/bindDefaultTwoWay.d.ts +35 -0
- package/dist/compile/binds/bindDefaultTwoWay.d.ts.map +1 -0
- package/dist/compile/binds/bindDefaultTwoWay.js +128 -0
- package/dist/compile/binds/bindDefaultTwoWay.js.map +1 -0
- package/dist/compile/binds/bindDeferDirectives.d.ts +21 -0
- package/dist/compile/binds/bindDeferDirectives.d.ts.map +1 -0
- package/dist/compile/binds/bindDeferDirectives.js +465 -0
- package/dist/compile/binds/bindDeferDirectives.js.map +1 -0
- package/dist/compile/binds/bindErrorBoundary.d.ts +71 -0
- package/dist/compile/binds/bindErrorBoundary.d.ts.map +1 -0
- package/dist/compile/binds/bindErrorBoundary.js +389 -0
- package/dist/compile/binds/bindErrorBoundary.js.map +1 -0
- package/dist/compile/binds/bindEventHandlers.d.ts +17 -0
- package/dist/compile/binds/bindEventHandlers.d.ts.map +1 -0
- package/dist/compile/binds/bindEventHandlers.js +174 -0
- package/dist/compile/binds/bindEventHandlers.js.map +1 -0
- package/dist/compile/binds/bindRefDirectives.d.ts +14 -0
- package/dist/compile/binds/bindRefDirectives.d.ts.map +1 -0
- package/dist/compile/binds/bindRefDirectives.js +35 -0
- package/dist/compile/binds/bindRefDirectives.js.map +1 -0
- package/dist/compile/binds/bindTextInterpolations.d.ts +16 -0
- package/dist/compile/binds/bindTextInterpolations.d.ts.map +1 -0
- package/dist/compile/binds/bindTextInterpolations.js +408 -0
- package/dist/compile/binds/bindTextInterpolations.js.map +1 -0
- package/dist/compile/binds/directiveRegistry.d.ts +46 -0
- package/dist/compile/binds/directiveRegistry.d.ts.map +1 -0
- package/dist/compile/binds/directiveRegistry.js +50 -0
- package/dist/compile/binds/directiveRegistry.js.map +1 -0
- package/dist/compile/compileExpression.d.ts +10 -0
- package/dist/compile/compileExpression.d.ts.map +1 -0
- package/dist/compile/compileExpression.js +26 -0
- package/dist/compile/compileExpression.js.map +1 -0
- package/dist/compile/compileNodes.d.ts +32 -0
- package/dist/compile/compileNodes.d.ts.map +1 -0
- package/dist/compile/compileNodes.js +104 -0
- package/dist/compile/compileNodes.js.map +1 -0
- package/dist/compile/compileTemplate.d.ts +64 -0
- package/dist/compile/compileTemplate.d.ts.map +1 -0
- package/dist/compile/compileTemplate.js +157 -0
- package/dist/compile/compileTemplate.js.map +1 -0
- package/dist/compile/contentProjection.d.ts +29 -0
- package/dist/compile/contentProjection.d.ts.map +1 -0
- package/dist/compile/contentProjection.js +210 -0
- package/dist/compile/contentProjection.js.map +1 -0
- package/dist/compile/forLoops/bindForLoops.d.ts +21 -0
- package/dist/compile/forLoops/bindForLoops.d.ts.map +1 -0
- package/dist/compile/forLoops/bindForLoops.js +151 -0
- package/dist/compile/forLoops/bindForLoops.js.map +1 -0
- package/dist/compile/forLoops/createLoopManager.d.ts +51 -0
- package/dist/compile/forLoops/createLoopManager.d.ts.map +1 -0
- package/dist/compile/forLoops/createLoopManager.js +429 -0
- package/dist/compile/forLoops/createLoopManager.js.map +1 -0
- package/dist/compile/forLoops/index.d.ts +6 -0
- package/dist/compile/forLoops/index.d.ts.map +1 -0
- package/dist/compile/forLoops/index.js +8 -0
- package/dist/compile/forLoops/index.js.map +1 -0
- package/dist/compile/forLoops/loopHelpers.d.ts +33 -0
- package/dist/compile/forLoops/loopHelpers.d.ts.map +1 -0
- package/dist/compile/forLoops/loopHelpers.js +69 -0
- package/dist/compile/forLoops/loopHelpers.js.map +1 -0
- package/dist/compile/forLoops/loopSignals.d.ts +36 -0
- package/dist/compile/forLoops/loopSignals.d.ts.map +1 -0
- package/dist/compile/forLoops/loopSignals.js +64 -0
- package/dist/compile/forLoops/loopSignals.js.map +1 -0
- package/dist/compile/forLoops/loopTraversal.d.ts +18 -0
- package/dist/compile/forLoops/loopTraversal.d.ts.map +1 -0
- package/dist/compile/forLoops/loopTraversal.js +38 -0
- package/dist/compile/forLoops/loopTraversal.js.map +1 -0
- package/dist/compile/handleConditionalChain.d.ts +15 -0
- package/dist/compile/handleConditionalChain.d.ts.map +1 -0
- package/dist/compile/handleConditionalChain.js +181 -0
- package/dist/compile/handleConditionalChain.js.map +1 -0
- package/dist/compile/index.d.ts +15 -0
- package/dist/compile/index.d.ts.map +1 -0
- package/dist/compile/index.js +19 -0
- package/dist/compile/index.js.map +1 -0
- package/dist/compile/injectScopedStyle.d.ts +16 -0
- package/dist/compile/injectScopedStyle.d.ts.map +1 -0
- package/dist/compile/injectScopedStyle.js +50 -0
- package/dist/compile/injectScopedStyle.js.map +1 -0
- package/dist/compile/nodeExpando.d.ts +12 -0
- package/dist/compile/nodeExpando.d.ts.map +1 -0
- package/dist/compile/nodeExpando.js +4 -0
- package/dist/compile/nodeExpando.js.map +1 -0
- package/dist/compile/parseHtml.d.ts +70 -0
- package/dist/compile/parseHtml.d.ts.map +1 -0
- package/dist/compile/parseHtml.js +164 -0
- package/dist/compile/parseHtml.js.map +1 -0
- package/dist/compile/pithyTreeToDom.d.ts +16 -0
- package/dist/compile/pithyTreeToDom.d.ts.map +1 -0
- package/dist/compile/pithyTreeToDom.js +118 -0
- package/dist/compile/pithyTreeToDom.js.map +1 -0
- package/dist/compile/renderChildren.d.ts +50 -0
- package/dist/compile/renderChildren.d.ts.map +1 -0
- package/dist/compile/renderChildren.js +76 -0
- package/dist/compile/renderChildren.js.map +1 -0
- package/dist/compile/ssrMode.d.ts +17 -0
- package/dist/compile/ssrMode.d.ts.map +1 -0
- package/dist/compile/ssrMode.js +20 -0
- package/dist/compile/ssrMode.js.map +1 -0
- package/dist/compile/textValue.d.ts +28 -0
- package/dist/compile/textValue.d.ts.map +1 -0
- package/dist/compile/textValue.js +30 -0
- package/dist/compile/textValue.js.map +1 -0
- package/dist/config/index.d.ts +37 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +16 -0
- package/dist/config/index.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/index.d.ts +3 -0
- package/dist/interfaces/index.d.ts.map +1 -0
- package/dist/interfaces/index.js +3 -0
- package/dist/interfaces/index.js.map +1 -0
- package/dist/interfaces/ref-signal.interface.d.ts +6 -0
- package/dist/interfaces/ref-signal.interface.d.ts.map +1 -0
- package/dist/interfaces/ref-signal.interface.js +2 -0
- package/dist/interfaces/ref-signal.interface.js.map +1 -0
- package/dist/interfaces/ref.interface.d.ts +5 -0
- package/dist/interfaces/ref.interface.d.ts.map +1 -0
- package/dist/interfaces/ref.interface.js +2 -0
- package/dist/interfaces/ref.interface.js.map +1 -0
- package/dist/runtime/applyBindings.d.ts +36 -0
- package/dist/runtime/applyBindings.d.ts.map +1 -0
- package/dist/runtime/applyBindings.js +165 -0
- package/dist/runtime/applyBindings.js.map +1 -0
- package/dist/runtime/bindings/applyComponent.d.ts +3 -0
- package/dist/runtime/bindings/applyComponent.d.ts.map +1 -0
- package/dist/runtime/bindings/applyComponent.js +190 -0
- package/dist/runtime/bindings/applyComponent.js.map +1 -0
- package/dist/runtime/bindings/applyDefer.d.ts +11 -0
- package/dist/runtime/bindings/applyDefer.d.ts.map +1 -0
- package/dist/runtime/bindings/applyDefer.js +422 -0
- package/dist/runtime/bindings/applyDefer.js.map +1 -0
- package/dist/runtime/bindings/applySimple.d.ts +11 -0
- package/dist/runtime/bindings/applySimple.d.ts.map +1 -0
- package/dist/runtime/bindings/applySimple.js +346 -0
- package/dist/runtime/bindings/applySimple.js.map +1 -0
- package/dist/runtime/bindings/applyStructural.d.ts +5 -0
- package/dist/runtime/bindings/applyStructural.d.ts.map +1 -0
- package/dist/runtime/bindings/applyStructural.js +312 -0
- package/dist/runtime/bindings/applyStructural.js.map +1 -0
- package/dist/runtime/bindings/helpers.d.ts +56 -0
- package/dist/runtime/bindings/helpers.d.ts.map +1 -0
- package/dist/runtime/bindings/helpers.js +121 -0
- package/dist/runtime/bindings/helpers.js.map +1 -0
- package/dist/runtime/bindings/index.d.ts +7 -0
- package/dist/runtime/bindings/index.d.ts.map +1 -0
- package/dist/runtime/bindings/index.js +6 -0
- package/dist/runtime/bindings/index.js.map +1 -0
- package/dist/runtime/bindings/types.d.ts +105 -0
- package/dist/runtime/bindings/types.d.ts.map +1 -0
- package/dist/runtime/bindings/types.js +5 -0
- package/dist/runtime/bindings/types.js.map +1 -0
- package/dist/runtime/bootstrap.d.ts +59 -0
- package/dist/runtime/bootstrap.d.ts.map +1 -0
- package/dist/runtime/bootstrap.js +82 -0
- package/dist/runtime/bootstrap.js.map +1 -0
- package/dist/runtime/createContextProxy.d.ts +10 -0
- package/dist/runtime/createContextProxy.d.ts.map +1 -0
- package/dist/runtime/createContextProxy.js +45 -0
- package/dist/runtime/createContextProxy.js.map +1 -0
- package/dist/runtime/deferHelpers.d.ts +49 -0
- package/dist/runtime/deferHelpers.d.ts.map +1 -0
- package/dist/runtime/deferHelpers.js +65 -0
- package/dist/runtime/deferHelpers.js.map +1 -0
- package/dist/runtime/html.d.ts +23 -0
- package/dist/runtime/html.d.ts.map +1 -0
- package/dist/runtime/html.js +97 -0
- package/dist/runtime/html.js.map +1 -0
- package/dist/runtime/index.d.ts +8 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +21 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/runtime/lazy.d.ts +144 -0
- package/dist/runtime/lazy.d.ts.map +1 -0
- package/dist/runtime/lazy.js +295 -0
- package/dist/runtime/lazy.js.map +1 -0
- package/dist/runtime/module-cache.d.ts +78 -0
- package/dist/runtime/module-cache.d.ts.map +1 -0
- package/dist/runtime/module-cache.js +109 -0
- package/dist/runtime/module-cache.js.map +1 -0
- package/dist/runtime/onDestroy.d.ts +29 -0
- package/dist/runtime/onDestroy.d.ts.map +1 -0
- package/dist/runtime/onDestroy.js +66 -0
- package/dist/runtime/onDestroy.js.map +1 -0
- package/dist/runtime/parseIdentifiersFromTemplate.d.ts +17 -0
- package/dist/runtime/parseIdentifiersFromTemplate.d.ts.map +1 -0
- package/dist/runtime/parseIdentifiersFromTemplate.js +68 -0
- package/dist/runtime/parseIdentifiersFromTemplate.js.map +1 -0
- package/dist/types/globals.d.ts +18 -0
- package/dist/types/globals.d.ts.map +1 -0
- package/dist/types/globals.js +4 -0
- package/dist/types/globals.js.map +1 -0
- package/dist/types/index.d.ts +11 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utilities/index.d.ts +3 -0
- package/dist/utilities/index.d.ts.map +1 -0
- package/dist/utilities/index.js +3 -0
- package/dist/utilities/index.js.map +1 -0
- package/dist/utilities/ref-signal.utility.d.ts +32 -0
- package/dist/utilities/ref-signal.utility.d.ts.map +1 -0
- package/dist/utilities/ref-signal.utility.js +46 -0
- package/dist/utilities/ref-signal.utility.js.map +1 -0
- package/dist/utilities/ref.untility.d.ts +37 -0
- package/dist/utilities/ref.untility.d.ts.map +1 -0
- package/dist/utilities/ref.untility.js +47 -0
- package/dist/utilities/ref.untility.js.map +1 -0
- package/package.json +55 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.runtime.events",
|
|
4
|
+
"title": "Event Handler Binding System",
|
|
5
|
+
"category": "runtime"
|
|
6
|
+
}
|
|
7
|
+
*/
|
|
8
|
+
import { boundaryForElement, setBoundaryError } from './bindErrorBoundary';
|
|
9
|
+
import { nodeExpando } from '../nodeExpando';
|
|
10
|
+
/**
|
|
11
|
+
* Walk up the DOM to find the nearest local PithyJS context attached by loops/components.
|
|
12
|
+
* Falls back to the provided root context.
|
|
13
|
+
*/
|
|
14
|
+
function nearestCtx(el, rootCtx) {
|
|
15
|
+
let n = el;
|
|
16
|
+
while (n) {
|
|
17
|
+
const local = nodeExpando(n).__pithyCtx;
|
|
18
|
+
if (local)
|
|
19
|
+
return local;
|
|
20
|
+
n = n.parentElement;
|
|
21
|
+
}
|
|
22
|
+
return rootCtx;
|
|
23
|
+
}
|
|
24
|
+
function parseEventAttr(name) {
|
|
25
|
+
// "data-on-click" or "data-on-click.once.prevent"
|
|
26
|
+
const raw = name.slice('data-on-'.length);
|
|
27
|
+
const parts = raw.split('.');
|
|
28
|
+
const type = parts[0];
|
|
29
|
+
const mods = {};
|
|
30
|
+
for (let i = 1; i < parts.length; i++) {
|
|
31
|
+
const p = parts[i];
|
|
32
|
+
if (p === 'once')
|
|
33
|
+
mods.once = true;
|
|
34
|
+
else if (p === 'prevent')
|
|
35
|
+
mods.prevent = true;
|
|
36
|
+
else if (p === 'stop')
|
|
37
|
+
mods.stop = true;
|
|
38
|
+
else if (p === 'self')
|
|
39
|
+
mods.self = true;
|
|
40
|
+
else if (p === 'capture')
|
|
41
|
+
mods.capture = true;
|
|
42
|
+
else if (p === 'passive')
|
|
43
|
+
mods.passive = true;
|
|
44
|
+
}
|
|
45
|
+
return { type, mods };
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Per-element, per-event registry of bound expressions,
|
|
49
|
+
* so we don't double-attach listeners when DOM is moved or reprocessed.
|
|
50
|
+
*/
|
|
51
|
+
const BOUND_KEY = Symbol('pithy:bound');
|
|
52
|
+
function getBoundSet(el, type) {
|
|
53
|
+
const host = el;
|
|
54
|
+
const store = host[BOUND_KEY] ??
|
|
55
|
+
(host[BOUND_KEY] = Object.create(null));
|
|
56
|
+
return (store[type] ?? (store[type] = new Set()));
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Compile an expression so it can see ctx + $event + $el.
|
|
60
|
+
* We intentionally do not use compileExpression here so we can inject $event/$el.
|
|
61
|
+
*/
|
|
62
|
+
function compileWithEvent(expr, ctx) {
|
|
63
|
+
try {
|
|
64
|
+
// Build a function that accepts (ctx, $event, $el) and returns the expression result.
|
|
65
|
+
// `with(ctx)` makes identifiers resolve against the provided context object.
|
|
66
|
+
// eslint-disable-next-line no-new-func -- JIT event-handler expression evaluation is a core framework feature
|
|
67
|
+
const fn = new Function('ctx', '$event', '$el', `with (ctx) { return (${expr}); }`);
|
|
68
|
+
return (ev, el) => fn(ctx, ev, el);
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
if (import.meta.env?.DEV) {
|
|
72
|
+
console.warn(`[bindEventHandlers] ❌ Failed to compile "${expr}"`, err);
|
|
73
|
+
}
|
|
74
|
+
// Return a noop runner
|
|
75
|
+
return (_ev, _el) => undefined;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Traverse all elements under `root` (Element or DocumentFragment) using a TreeWalker.
|
|
80
|
+
*/
|
|
81
|
+
function forEachElement(root, cb) {
|
|
82
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
|
83
|
+
let node;
|
|
84
|
+
while ((node = walker.nextNode())) {
|
|
85
|
+
cb(node);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Binds DOM event handlers within `root`.
|
|
90
|
+
* - Uses nearest element context (e.g., loop item ctx) when present.
|
|
91
|
+
* - Idempotent: won't re-bind the same element/event/expression+modifiers combo.
|
|
92
|
+
* - Supports modifiers: .once .prevent .stop .self .capture .passive
|
|
93
|
+
* - Injects `$event` and `$el` for expression handlers (e.g., @click="doThing($event)")
|
|
94
|
+
*/
|
|
95
|
+
/** @codexApi {"parent":"pithy.runtime.events","name":"bindEventHandlers","stability":"stable","signature":"(root: Element | DocumentFragment, rootCtx: Record<string, unknown>, disposers: (() => void)[]) => void"} */
|
|
96
|
+
export function bindEventHandlers(root, rootCtx, disposers) {
|
|
97
|
+
forEachElement(root, el => {
|
|
98
|
+
// Iterate attributes once; bind any data-on-* handlers
|
|
99
|
+
for (const attr of Array.from(el.attributes)) {
|
|
100
|
+
if (!attr.name.startsWith('data-on-'))
|
|
101
|
+
continue;
|
|
102
|
+
const { type: eventName, mods } = parseEventAttr(attr.name);
|
|
103
|
+
const handlerExpr = attr.value.trim();
|
|
104
|
+
if (!handlerExpr)
|
|
105
|
+
continue;
|
|
106
|
+
// Idempotency key includes attr name (mods) + expression
|
|
107
|
+
const boundSet = getBoundSet(el, eventName);
|
|
108
|
+
const boundKey = `${attr.name}=${handlerExpr}`;
|
|
109
|
+
if (boundSet.has(boundKey))
|
|
110
|
+
continue;
|
|
111
|
+
const ctx = nearestCtx(el, rootCtx);
|
|
112
|
+
const isSimpleIdentifier = /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(handlerExpr);
|
|
113
|
+
// If the expression is a bare identifier and resolves to a function on ctx,
|
|
114
|
+
// we'll call it directly (browser passes the event), preserving 'this' as ctx.
|
|
115
|
+
const directFn = isSimpleIdentifier && typeof ctx[handlerExpr] === 'function'
|
|
116
|
+
? ctx[handlerExpr]
|
|
117
|
+
: null;
|
|
118
|
+
// Otherwise, compile an event-aware evaluator that can see `$event` & `$el`.
|
|
119
|
+
const runWithEvent = directFn ? null : compileWithEvent(handlerExpr, ctx);
|
|
120
|
+
// Final listener with modifiers + $event/$el support + error boundary capture
|
|
121
|
+
const listener = (ev) => {
|
|
122
|
+
if (mods.self && ev.target !== el)
|
|
123
|
+
return;
|
|
124
|
+
if (mods.prevent)
|
|
125
|
+
ev.preventDefault();
|
|
126
|
+
if (mods.stop)
|
|
127
|
+
ev.stopPropagation();
|
|
128
|
+
try {
|
|
129
|
+
if (directFn) {
|
|
130
|
+
// Call the function from ctx with ctx as 'this'
|
|
131
|
+
return directFn.call(ctx, ev);
|
|
132
|
+
}
|
|
133
|
+
else if (runWithEvent) {
|
|
134
|
+
const out = runWithEvent(ev, el);
|
|
135
|
+
// If expression returns a function, treat that as a handler and pass the event
|
|
136
|
+
if (typeof out === 'function') {
|
|
137
|
+
return out.call(ctx, ev);
|
|
138
|
+
}
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
// Find the nearest error boundary and set its error signal with escalation
|
|
144
|
+
const boundaryEl = el.closest('[data-error]');
|
|
145
|
+
const boundary = boundaryForElement(boundaryEl);
|
|
146
|
+
if (boundary) {
|
|
147
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
148
|
+
setBoundaryError(boundary, err);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
// No boundary found, re-throw to maintain original behavior
|
|
152
|
+
throw error;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
// Attach with appropriate options
|
|
157
|
+
const options = {
|
|
158
|
+
capture: !!mods.capture,
|
|
159
|
+
passive: !!mods.passive,
|
|
160
|
+
once: !!mods.once,
|
|
161
|
+
};
|
|
162
|
+
el.addEventListener(eventName, listener, options);
|
|
163
|
+
boundSet.add(boundKey);
|
|
164
|
+
disposers.push(() => {
|
|
165
|
+
// Note: removeEventListener only considers `capture`; `once`/`passive` are ignored here.
|
|
166
|
+
el.removeEventListener(eventName, listener, {
|
|
167
|
+
capture: !!mods.capture,
|
|
168
|
+
});
|
|
169
|
+
boundSet.delete(boundKey);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
//# sourceMappingURL=bindEventHandlers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bindEventHandlers.js","sourceRoot":"","sources":["../../../src/compile/binds/bindEventHandlers.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAC3E,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C;;;GAGG;AACH,SAAS,UAAU,CACjB,EAAW,EACX,OAAgC;IAEhC,IAAI,CAAC,GAAmB,EAAE,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AAK,CAAC;oBACd,CAAC;gBACH,CAAC;YACH,CAAC,CAAC;YAEF,kCAAkC;YAClC,MAAM,OAAO,GAAG;gBACd,OAAO,EAAE,CAAC,CAAC,IAAI,CAAC,OAAO;gBACvB,OAAO,EAAE,CAAC,CAAC,IAAI,CAAC,OAAO;gBACvB,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI;aACS,CAAC;YAE7B,EAAE,CAAC,gBAAgB,CAAC,SAAS,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;YAElD,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;YACvB,SAAS,CAAC,IAAI,CAAC,GAAG,EAAE;gBAClB,yFAAyF;gBACzF,EAAE,CAAC,mBAAmB,CAAC,SAAS,EAAE,QAAQ,EAAE;oBAC1C,OAAO,EAAE,CAAC,CAAC,IAAI,CAAC,OAAO;iBACxB,CAAC,CAAC;gBACH,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YAC5B,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Binds ref directives to DOM elements using [data-ref] attributes.
|
|
3
|
+
*
|
|
4
|
+
* This function handles element references by binding elements with [data-ref]
|
|
5
|
+
* attributes to their corresponding Ref objects in the context. It automatically
|
|
6
|
+
* sets up cleanup when elements are removed.
|
|
7
|
+
*
|
|
8
|
+
* @param fragment - The root DOM node to search for ref directives
|
|
9
|
+
* @param context - Context object containing Ref objects for element references
|
|
10
|
+
* @param disposers - Array to collect cleanup functions that clear references
|
|
11
|
+
*/
|
|
12
|
+
/** @codexApi {"parent":"pithy.feature.refs","name":"bindRefDirectives","stability":"stable","signature":"(fragment: Element | DocumentFragment, context: Record<string, unknown>, disposers: (() => void)[]) => void"} */
|
|
13
|
+
export declare function bindRefDirectives(fragment: Element | DocumentFragment, context: Record<string, unknown>, disposers: (() => void)[]): void;
|
|
14
|
+
//# sourceMappingURL=bindRefDirectives.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bindRefDirectives.d.ts","sourceRoot":"","sources":["../../../src/compile/binds/bindRefDirectives.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AAEH,0NAA0N;AAC1N,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,OAAO,GAAG,gBAAgB,EACpC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,SAAS,EAAE,CAAC,MAAM,IAAI,CAAC,EAAE,GACxB,IAAI,CAyBN"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Binds ref directives to DOM elements using [data-ref] attributes.
|
|
3
|
+
*
|
|
4
|
+
* This function handles element references by binding elements with [data-ref]
|
|
5
|
+
* attributes to their corresponding Ref objects in the context. It automatically
|
|
6
|
+
* sets up cleanup when elements are removed.
|
|
7
|
+
*
|
|
8
|
+
* @param fragment - The root DOM node to search for ref directives
|
|
9
|
+
* @param context - Context object containing Ref objects for element references
|
|
10
|
+
* @param disposers - Array to collect cleanup functions that clear references
|
|
11
|
+
*/
|
|
12
|
+
/** @codexApi {"parent":"pithy.feature.refs","name":"bindRefDirectives","stability":"stable","signature":"(fragment: Element | DocumentFragment, context: Record<string, unknown>, disposers: (() => void)[]) => void"} */
|
|
13
|
+
export function bindRefDirectives(fragment, context, disposers) {
|
|
14
|
+
fragment.querySelectorAll('[data-ref]').forEach(el => {
|
|
15
|
+
Array.from(el.attributes).forEach(attr => {
|
|
16
|
+
if (!attr.name.startsWith('data-ref'))
|
|
17
|
+
return;
|
|
18
|
+
const refName = attr.value;
|
|
19
|
+
// Skip empty refs
|
|
20
|
+
if (!refName)
|
|
21
|
+
return;
|
|
22
|
+
const ref = context[refName];
|
|
23
|
+
if (ref && typeof ref.set === 'function') {
|
|
24
|
+
// Set the reference immediately
|
|
25
|
+
ref.set(el);
|
|
26
|
+
// Consume the attribute so a re-scan from an ancestor pipeline can't
|
|
27
|
+
// re-bind this element against another component's same-named ref.
|
|
28
|
+
el.removeAttribute(attr.name);
|
|
29
|
+
// Add cleanup to the disposers array
|
|
30
|
+
disposers.push(() => ref.set(null));
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=bindRefDirectives.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bindRefDirectives.js","sourceRoot":"","sources":["../../../src/compile/binds/bindRefDirectives.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AAEH,0NAA0N;AAC1N,MAAM,UAAU,iBAAiB,CAC/B,QAAoC,EACpC,OAAgC,EAChC,SAAyB;IAEzB,QAAQ,CAAC,gBAAgB,CAAC,YAAY,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE;QACnD,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;YACvC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC;gBAAE,OAAO;YAE9C,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC;YAE3B,kBAAkB;YAClB,IAAI,CAAC,OAAO;gBAAE,OAAO;YAErB,MAAM,GAAG,GAAG,OAAO,CAAC,OAAO,CAAmB,CAAC;YAE/C,IAAI,GAAG,IAAI,OAAO,GAAG,CAAC,GAAG,KAAK,UAAU,EAAE,CAAC;gBACzC,gCAAgC;gBAChC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;gBAEZ,qEAAqE;gBACrE,mEAAmE;gBACnE,EAAE,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBAE9B,qCAAqC;gBACrC,SAAS,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;YACtC,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.runtime.interpolations",
|
|
4
|
+
"title": "Text Interpolation Binding System",
|
|
5
|
+
"category": "runtime"
|
|
6
|
+
}
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Bind all text interpolations (e.g. "Hello {{name()}}") under `root`.
|
|
10
|
+
* - Skips nested-loop subtrees (tagged with __pithySkipText) and real @for/data-for hosts.
|
|
11
|
+
* - Binds per-text-node updates with fine-grained effects.
|
|
12
|
+
*/
|
|
13
|
+
/** @codexApi {"parent":"pithy.runtime.interpolations","name":"bindTextInterpolations","stability":"stable","signature":"(root: Element | DocumentFragment, ctx: Record<string, unknown>, disposers: (() => void)[]) => void"} */
|
|
14
|
+
export declare function bindTextInterpolations(root: Element | DocumentFragment, ctx: Record<string, unknown>, disposers: (() => void)[]): void;
|
|
15
|
+
export declare function flashUpdate(node: Text | HTMLElement): void;
|
|
16
|
+
//# sourceMappingURL=bindTextInterpolations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bindTextInterpolations.d.ts","sourceRoot":"","sources":["../../../src/compile/binds/bindTextInterpolations.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAWF;;;;GAIG;AACH,iOAAiO;AACjO,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,OAAO,GAAG,gBAAgB,EAChC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,SAAS,EAAE,CAAC,MAAM,IAAI,CAAC,EAAE,GACxB,IAAI,CAGN;AA6UD,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,GAAG,WAAW,GAAG,IAAI,CAU1D"}
|
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.runtime.interpolations",
|
|
4
|
+
"title": "Text Interpolation Binding System",
|
|
5
|
+
"category": "runtime"
|
|
6
|
+
}
|
|
7
|
+
*/
|
|
8
|
+
import { defaultConfig } from '../../config';
|
|
9
|
+
import { effect, isSignal } from '@pithyjs/signals';
|
|
10
|
+
import { toTextValue } from '../textValue';
|
|
11
|
+
import { compileExpression } from '../compileExpression';
|
|
12
|
+
import { isSSRMode } from '../ssrMode';
|
|
13
|
+
import { nodeExpando } from '../nodeExpando';
|
|
14
|
+
const INTERPOLATION_RE = /{{\s*([\s\S]*?)\s*}}/g;
|
|
15
|
+
/**
|
|
16
|
+
* Bind all text interpolations (e.g. "Hello {{name()}}") under `root`.
|
|
17
|
+
* - Skips nested-loop subtrees (tagged with __pithySkipText) and real @for/data-for hosts.
|
|
18
|
+
* - Binds per-text-node updates with fine-grained effects.
|
|
19
|
+
*/
|
|
20
|
+
/** @codexApi {"parent":"pithy.runtime.interpolations","name":"bindTextInterpolations","stability":"stable","signature":"(root: Element | DocumentFragment, ctx: Record<string, unknown>, disposers: (() => void)[]) => void"} */
|
|
21
|
+
export function bindTextInterpolations(root, ctx, disposers) {
|
|
22
|
+
processChildNodes(root, ctx, disposers);
|
|
23
|
+
bindRemainingTextInterpolations(root, ctx, disposers);
|
|
24
|
+
}
|
|
25
|
+
/* -----------------------------------------------------------------------------
|
|
26
|
+
* Internal: robust child iteration (Element | DocumentFragment)
|
|
27
|
+
* ---------------------------------------------------------------------------*/
|
|
28
|
+
function childElementsOf(root) {
|
|
29
|
+
if (root instanceof Element)
|
|
30
|
+
return Array.from(root.children);
|
|
31
|
+
// DocumentFragment doesn't have .children; filter childNodes to elements
|
|
32
|
+
const out = [];
|
|
33
|
+
root.childNodes.forEach(n => {
|
|
34
|
+
if (n.nodeType === Node.ELEMENT_NODE)
|
|
35
|
+
out.push(n);
|
|
36
|
+
});
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Walk immediate children and process text nodes inside them.
|
|
41
|
+
* We purposely avoid stepping into nested-loop subtrees to prevent compiling
|
|
42
|
+
* expressions like {{ item() }} against the parent context.
|
|
43
|
+
*/
|
|
44
|
+
function processChildNodes(root, ctx, disposers) {
|
|
45
|
+
for (const child of childElementsOf(root)) {
|
|
46
|
+
// Fast-path: if the child itself is a loop host, we skip it entirely.
|
|
47
|
+
if (child.hasAttribute?.('@for') || nodeExpando(child).__pithySkipText)
|
|
48
|
+
continue;
|
|
49
|
+
const walker = document.createTreeWalker(child, NodeFilter.SHOW_TEXT);
|
|
50
|
+
const targets = [];
|
|
51
|
+
let textNode;
|
|
52
|
+
while ((textNode = walker.nextNode())) {
|
|
53
|
+
const rawText = textNode?.nodeValue || '';
|
|
54
|
+
if (!rawText.includes('{{'))
|
|
55
|
+
continue;
|
|
56
|
+
// Skip anything inside a nested loop host or explicit @for/data-for
|
|
57
|
+
if (isInsideSkippedZone(textNode))
|
|
58
|
+
continue;
|
|
59
|
+
targets.push(textNode);
|
|
60
|
+
}
|
|
61
|
+
// Mutate after collecting to avoid invalidating the walker during traversal.
|
|
62
|
+
for (const node of targets) {
|
|
63
|
+
const rawText = node.nodeValue || '';
|
|
64
|
+
const parts = parseTextIntoParts(rawText);
|
|
65
|
+
const parent = node.parentElement;
|
|
66
|
+
const nextSibling = node.nextSibling;
|
|
67
|
+
if (!parent)
|
|
68
|
+
continue;
|
|
69
|
+
// Use the node's own __pithyCtx when available (set by pithyTreeToDom).
|
|
70
|
+
// Projected slot content carries the *parent* component's context this way,
|
|
71
|
+
// while the passed `ctx` is the *child* component's context.
|
|
72
|
+
// This mirrors the nearestCtx pattern used by bindEventHandlers.
|
|
73
|
+
const nodeCtx = nodeExpando(node).__pithyCtx || ctx;
|
|
74
|
+
parent.removeChild(node);
|
|
75
|
+
for (const part of parts) {
|
|
76
|
+
if (part.type === 'static') {
|
|
77
|
+
insertStaticTextNode(parent, nextSibling, part.value);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
// Determine interpolation mode based on config
|
|
81
|
+
const mode = defaultConfig.textInterpolationMode || 'text-node';
|
|
82
|
+
const isSignal = isSignalExpression(part.value, nodeCtx);
|
|
83
|
+
// Choose insertion strategy
|
|
84
|
+
if (mode === 'span') {
|
|
85
|
+
// Always use span wrappers
|
|
86
|
+
insertDynamicSpan(parent, nextSibling, part.value, nodeCtx, disposers);
|
|
87
|
+
}
|
|
88
|
+
else if (mode === 'text-node') {
|
|
89
|
+
// Always use text nodes with comment anchors
|
|
90
|
+
insertDynamicTextNode(parent, nextSibling, part.value, nodeCtx, disposers);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
// 'auto' mode: span for signals if autoWrapSignals is true, text node otherwise
|
|
94
|
+
if (isSignal && defaultConfig.autoWrapSignals) {
|
|
95
|
+
insertDynamicSpan(parent, nextSibling, part.value, nodeCtx, disposers);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
insertDynamicTextNode(parent, nextSibling, part.value, nodeCtx, disposers);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Second pass: text nodes directly under `root` (e.g., loose text, or nodes
|
|
108
|
+
* not caught by processChildNodes). Also respects skip zones.
|
|
109
|
+
*/
|
|
110
|
+
function bindRemainingTextInterpolations(root, ctx, disposers) {
|
|
111
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
|
|
112
|
+
let node;
|
|
113
|
+
while ((node = walker.nextNode())) {
|
|
114
|
+
const rawText = node?.nodeValue;
|
|
115
|
+
if (!rawText || !rawText.includes('{{'))
|
|
116
|
+
continue;
|
|
117
|
+
// Robust skip: private flag, @for, or data-for anywhere up the chain.
|
|
118
|
+
if (isInsideSkippedZone(node))
|
|
119
|
+
continue;
|
|
120
|
+
INTERPOLATION_RE.lastIndex = 0;
|
|
121
|
+
const matches = [...rawText.matchAll(INTERPOLATION_RE)];
|
|
122
|
+
if (matches.length === 0)
|
|
123
|
+
continue;
|
|
124
|
+
// Use the node's own __pithyCtx when available (projected slot content
|
|
125
|
+
// carries the parent component's context via this property).
|
|
126
|
+
const nodeCtx = nodeExpando(node).__pithyCtx || ctx;
|
|
127
|
+
const renderFns = [];
|
|
128
|
+
const matchStrings = [];
|
|
129
|
+
const expressions = [];
|
|
130
|
+
for (const match of matches) {
|
|
131
|
+
const expr = match[1].trim();
|
|
132
|
+
matchStrings.push(match[0]);
|
|
133
|
+
expressions.push(expr);
|
|
134
|
+
renderFns.push(compileExpression(expr, nodeCtx));
|
|
135
|
+
}
|
|
136
|
+
bindTextNodeUpdates(node, rawText, expressions, matchStrings, renderFns, disposers);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/* -----------------------------------------------------------------------------
|
|
140
|
+
* Node insertion helpers
|
|
141
|
+
* ---------------------------------------------------------------------------*/
|
|
142
|
+
function insertStaticTextNode(parent, nextSibling, text) {
|
|
143
|
+
const staticNode = document.createTextNode(text);
|
|
144
|
+
parent.insertBefore(staticNode, nextSibling);
|
|
145
|
+
}
|
|
146
|
+
function insertDynamicSpan(parent, nextSibling, expression, ctx, disposers) {
|
|
147
|
+
const span = document.createElement('span');
|
|
148
|
+
span.className = 'r-pithy';
|
|
149
|
+
span.style.display = 'inline';
|
|
150
|
+
const textNode = document.createTextNode('');
|
|
151
|
+
span.appendChild(textNode);
|
|
152
|
+
parent.insertBefore(span, nextSibling);
|
|
153
|
+
bindDynamicTextNode(textNode, expression, ctx, disposers, span);
|
|
154
|
+
}
|
|
155
|
+
function insertDynamicTextNode(parent, nextSibling, expression, ctx, disposers) {
|
|
156
|
+
// Create comment anchors for structural markers (Solid.js approach)
|
|
157
|
+
// This provides better debugging and potential future optimizations
|
|
158
|
+
const startComment = document.createComment('t');
|
|
159
|
+
const textNode = document.createTextNode('');
|
|
160
|
+
const endComment = document.createComment('/t');
|
|
161
|
+
// Insert in order: start comment, text node, end comment
|
|
162
|
+
parent.insertBefore(startComment, nextSibling);
|
|
163
|
+
parent.insertBefore(textNode, nextSibling);
|
|
164
|
+
parent.insertBefore(endComment, nextSibling);
|
|
165
|
+
bindDynamicTextNode(textNode, expression, ctx, disposers);
|
|
166
|
+
}
|
|
167
|
+
/* -----------------------------------------------------------------------------
|
|
168
|
+
* Binding helpers
|
|
169
|
+
* ---------------------------------------------------------------------------*/
|
|
170
|
+
function bindTextNodeUpdates(node, rawText, expressions, matchStrings, renderFns, disposers) {
|
|
171
|
+
function evaluate() {
|
|
172
|
+
let output = rawText;
|
|
173
|
+
// Evaluate each {{ expr }} and replace into the original text
|
|
174
|
+
expressions.forEach((_, i) => {
|
|
175
|
+
try {
|
|
176
|
+
const result = renderFns[i](); // compileExpression already closes over ctx
|
|
177
|
+
// Function replacer: never let `$&`, `$$`, `$\``, `$'`, `$n` in the
|
|
178
|
+
// value be interpreted as String.replace special patterns — the value
|
|
179
|
+
// must be inserted verbatim (e.g. literal code like `str.replace(/a/, '$&')`).
|
|
180
|
+
output = output.replace(matchStrings[i], () => toTextValue(result));
|
|
181
|
+
}
|
|
182
|
+
catch {
|
|
183
|
+
// On evaluation failure, blank out the interpolation instead of throwing
|
|
184
|
+
output = output.replace(matchStrings[i], '');
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
return output;
|
|
188
|
+
}
|
|
189
|
+
if (isSSRMode()) {
|
|
190
|
+
// SSR mode: evaluate once synchronously, no reactive effects
|
|
191
|
+
node.nodeValue = evaluate();
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
let lastInterpolatedText;
|
|
195
|
+
const stop = effect(() => {
|
|
196
|
+
const output = evaluate();
|
|
197
|
+
if (output !== lastInterpolatedText) {
|
|
198
|
+
node.nodeValue = output;
|
|
199
|
+
flashUpdate(node);
|
|
200
|
+
lastInterpolatedText = output;
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
disposers.push(stop);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Bind a single dynamic expression into a text node, with optional highlight target (e.g., wrapper span).
|
|
208
|
+
*/
|
|
209
|
+
function bindDynamicTextNode(textNode, expression, ctx, disposers, highlightTarget) {
|
|
210
|
+
try {
|
|
211
|
+
const compiledFn = compileExpression(expression, ctx);
|
|
212
|
+
if (isSSRMode()) {
|
|
213
|
+
// SSR mode: evaluate once synchronously, no reactive effects
|
|
214
|
+
try {
|
|
215
|
+
textNode.nodeValue = toTextValue(compiledFn());
|
|
216
|
+
}
|
|
217
|
+
catch (err) {
|
|
218
|
+
console.error(`[bindDynamicTextNode] ❌ Error evaluating '${expression}'`, err);
|
|
219
|
+
textNode.nodeValue = '';
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
let lastValue;
|
|
224
|
+
const stop = effect(() => {
|
|
225
|
+
try {
|
|
226
|
+
const result = compiledFn(); // re-evaluates reactively
|
|
227
|
+
const next = toTextValue(result); // ensure string
|
|
228
|
+
if (next !== lastValue) {
|
|
229
|
+
// only mutate/flash on change
|
|
230
|
+
textNode.nodeValue = next;
|
|
231
|
+
if (highlightTarget) {
|
|
232
|
+
flashUpdate(highlightTarget);
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
flashUpdate(textNode);
|
|
236
|
+
}
|
|
237
|
+
lastValue = next;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
catch (err) {
|
|
241
|
+
console.error(`[bindDynamicTextNode] ❌ Error evaluating '${expression}'`, err);
|
|
242
|
+
textNode.nodeValue = '';
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
disposers.push(stop);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
catch (err) {
|
|
249
|
+
console.error(`[bindDynamicTextNode] ❌ Failed to compile: ${expression}`, err);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/* -----------------------------------------------------------------------------
|
|
253
|
+
* Batched flashing (dev only). Reduces style/layout churn vs per-node inline updates.
|
|
254
|
+
*
|
|
255
|
+
* For text nodes, we create temporary positioned overlays at the exact text location
|
|
256
|
+
* to provide visual feedback without adding permanent DOM nodes or flashing parent elements.
|
|
257
|
+
* ---------------------------------------------------------------------------*/
|
|
258
|
+
export function flashUpdate(node) {
|
|
259
|
+
const { highlightColor, highlightDuration, devHighlightDomUpdates } = defaultConfig;
|
|
260
|
+
if (!devHighlightDomUpdates)
|
|
261
|
+
return;
|
|
262
|
+
if (node instanceof Text) {
|
|
263
|
+
flashTextNode(node, highlightColor || '', highlightDuration || 0.2);
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
enqueueFlash(node, highlightColor || '', highlightDuration || 0.2);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
const flashQueue = new Set();
|
|
270
|
+
let flashScheduled = false;
|
|
271
|
+
// Pending cleanup timers per element — cancelled on re-flash so a previous
|
|
272
|
+
// flash's timers can't clear the background/transition mid-fade of a newer
|
|
273
|
+
// one (rapidly-updating bindings flash the same element in overlapping
|
|
274
|
+
// windows).
|
|
275
|
+
const flashTimers = new WeakMap();
|
|
276
|
+
function enqueueFlash(el, color, duration) {
|
|
277
|
+
flashQueue.add(el);
|
|
278
|
+
if (flashScheduled)
|
|
279
|
+
return;
|
|
280
|
+
flashScheduled = true;
|
|
281
|
+
requestAnimationFrame(() => {
|
|
282
|
+
// Apply highlight to all queued nodes at once
|
|
283
|
+
flashQueue.forEach(t => {
|
|
284
|
+
const pending = flashTimers.get(t);
|
|
285
|
+
if (pending) {
|
|
286
|
+
clearTimeout(pending.fade);
|
|
287
|
+
if (pending.clear !== undefined)
|
|
288
|
+
clearTimeout(pending.clear);
|
|
289
|
+
}
|
|
290
|
+
t.style.transition = `background-color ${duration}s ease-out`;
|
|
291
|
+
t.style.backgroundColor = color;
|
|
292
|
+
const fade = setTimeout(() => {
|
|
293
|
+
// Clear, don't set "transparent" — an explicit inline value would
|
|
294
|
+
// permanently override the element's stylesheet background. Runs
|
|
295
|
+
// even for disconnected nodes so a reattached element doesn't keep
|
|
296
|
+
// stale inline highlight styles.
|
|
297
|
+
t.style.backgroundColor = '';
|
|
298
|
+
const clear = setTimeout(() => {
|
|
299
|
+
t.style.transition = '';
|
|
300
|
+
flashTimers.delete(t);
|
|
301
|
+
}, duration * 1000);
|
|
302
|
+
flashTimers.set(t, { fade, clear });
|
|
303
|
+
}, 80);
|
|
304
|
+
flashTimers.set(t, { fade });
|
|
305
|
+
});
|
|
306
|
+
flashQueue.clear();
|
|
307
|
+
flashScheduled = false;
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Flash a text node update by creating a temporary positioned overlay.
|
|
312
|
+
* This provides visual feedback for text-node mode without permanent DOM nodes.
|
|
313
|
+
*/
|
|
314
|
+
function flashTextNode(textNode, color, duration) {
|
|
315
|
+
if (!textNode.parentElement)
|
|
316
|
+
return;
|
|
317
|
+
// Get the exact bounding rect of the text node
|
|
318
|
+
const range = document.createRange();
|
|
319
|
+
range.selectNodeContents(textNode);
|
|
320
|
+
const rect = range.getBoundingClientRect();
|
|
321
|
+
range.detach();
|
|
322
|
+
// Skip if text has no dimensions (empty or not rendered)
|
|
323
|
+
if (rect.width === 0 || rect.height === 0)
|
|
324
|
+
return;
|
|
325
|
+
// Create temporary overlay positioned exactly over the text
|
|
326
|
+
const overlay = document.createElement('div');
|
|
327
|
+
overlay.style.cssText = `
|
|
328
|
+
position: fixed;
|
|
329
|
+
left: ${rect.left}px;
|
|
330
|
+
top: ${rect.top}px;
|
|
331
|
+
width: ${rect.width}px;
|
|
332
|
+
height: ${rect.height}px;
|
|
333
|
+
background-color: ${color};
|
|
334
|
+
pointer-events: none;
|
|
335
|
+
z-index: 999999;
|
|
336
|
+
transition: opacity ${duration}s ease-out;
|
|
337
|
+
opacity: 0.6;
|
|
338
|
+
border-radius: 2px;
|
|
339
|
+
`;
|
|
340
|
+
document.body.appendChild(overlay);
|
|
341
|
+
// Fade out and remove
|
|
342
|
+
requestAnimationFrame(() => {
|
|
343
|
+
overlay.style.opacity = '0';
|
|
344
|
+
setTimeout(() => {
|
|
345
|
+
overlay.remove();
|
|
346
|
+
}, duration * 1000);
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
/* -----------------------------------------------------------------------------
|
|
350
|
+
* Text parsing & expression helpers
|
|
351
|
+
* ---------------------------------------------------------------------------*/
|
|
352
|
+
function parseTextIntoParts(rawText) {
|
|
353
|
+
const parts = [];
|
|
354
|
+
let lastIndex = 0;
|
|
355
|
+
let match;
|
|
356
|
+
INTERPOLATION_RE.lastIndex = 0;
|
|
357
|
+
while ((match = INTERPOLATION_RE.exec(rawText)) !== null) {
|
|
358
|
+
if (match.index > lastIndex) {
|
|
359
|
+
parts.push({
|
|
360
|
+
type: 'static',
|
|
361
|
+
value: rawText.slice(lastIndex, match.index),
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
parts.push({ type: 'dynamic', value: match[1].trim() });
|
|
365
|
+
lastIndex = match.index + match[0].length;
|
|
366
|
+
}
|
|
367
|
+
if (lastIndex < rawText.length) {
|
|
368
|
+
parts.push({ type: 'static', value: rawText.slice(lastIndex) });
|
|
369
|
+
}
|
|
370
|
+
return parts;
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Is the expression a signal call? (e.g., count(), user.tags())
|
|
374
|
+
* We compile the callee (without "()") and ask isSignal() on the result.
|
|
375
|
+
*/
|
|
376
|
+
function isSignalExpression(expr, ctx) {
|
|
377
|
+
// Quick test: must end with "()"
|
|
378
|
+
if (!/\(\)$/.test(expr))
|
|
379
|
+
return false;
|
|
380
|
+
try {
|
|
381
|
+
const functionExpr = expr.replace(/\(\)$/, '');
|
|
382
|
+
const compiledFn = compileExpression(functionExpr, ctx);
|
|
383
|
+
const signalFunction = compiledFn();
|
|
384
|
+
return isSignal(signalFunction);
|
|
385
|
+
}
|
|
386
|
+
catch {
|
|
387
|
+
return false;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Returns true if `node` is inside any subtree we must not text-bind now:
|
|
392
|
+
* - A nested-loop host we tagged earlier: __pithySkipText
|
|
393
|
+
* - An element that still carries @for or data-for (depending on compiler phase)
|
|
394
|
+
*/
|
|
395
|
+
function isInsideSkippedZone(node) {
|
|
396
|
+
let p = node instanceof Element ? node : node.parentElement;
|
|
397
|
+
while (p) {
|
|
398
|
+
if (nodeExpando(p).__pithySkipText)
|
|
399
|
+
return true;
|
|
400
|
+
if (p.hasAttribute &&
|
|
401
|
+
(p.hasAttribute('@for') || p.hasAttribute('data-for'))) {
|
|
402
|
+
return true;
|
|
403
|
+
}
|
|
404
|
+
p = p.parentElement;
|
|
405
|
+
}
|
|
406
|
+
return false;
|
|
407
|
+
}
|
|
408
|
+
//# sourceMappingURL=bindTextInterpolations.js.map
|