@pithyjs/core 0.1.0-beta.14 → 0.1.0-beta.16
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/README.md +14 -0
- package/dist/compile/bindSwitchCases.d.ts.map +1 -1
- package/dist/compile/bindSwitchCases.js +14 -7
- package/dist/compile/bindSwitchCases.js.map +1 -1
- package/dist/compile/binds/bindAfterComponents.d.ts +4 -0
- package/dist/compile/binds/bindAfterComponents.d.ts.map +1 -0
- package/dist/compile/binds/bindAfterComponents.js +26 -0
- package/dist/compile/binds/bindAfterComponents.js.map +1 -0
- package/dist/compile/binds/bindAfterProjection.d.ts.map +1 -1
- package/dist/compile/binds/bindAfterProjection.js +2 -21
- package/dist/compile/binds/bindAfterProjection.js.map +1 -1
- package/dist/compile/binds/bindBoundAttributes.d.ts +4 -0
- package/dist/compile/binds/bindBoundAttributes.d.ts.map +1 -1
- package/dist/compile/binds/bindBoundAttributes.js +63 -3
- package/dist/compile/binds/bindBoundAttributes.js.map +1 -1
- package/dist/compile/binds/bindClassDirectives.d.ts.map +1 -1
- package/dist/compile/binds/bindClassDirectives.js +3 -2
- package/dist/compile/binds/bindClassDirectives.js.map +1 -1
- package/dist/compile/binds/bindComponentsTags.d.ts.map +1 -1
- package/dist/compile/binds/bindComponentsTags.js +47 -11
- package/dist/compile/binds/bindComponentsTags.js.map +1 -1
- package/dist/compile/binds/bindDefaultTwoWay.d.ts.map +1 -1
- package/dist/compile/binds/bindDefaultTwoWay.js +2 -1
- package/dist/compile/binds/bindDefaultTwoWay.js.map +1 -1
- package/dist/compile/binds/bindDeferDirectives.d.ts.map +1 -1
- package/dist/compile/binds/bindDeferDirectives.js +17 -15
- package/dist/compile/binds/bindDeferDirectives.js.map +1 -1
- package/dist/compile/binds/bindErrorBoundary.d.ts +11 -1
- package/dist/compile/binds/bindErrorBoundary.d.ts.map +1 -1
- package/dist/compile/binds/bindErrorBoundary.js +50 -13
- package/dist/compile/binds/bindErrorBoundary.js.map +1 -1
- package/dist/compile/binds/bindEventHandlers.d.ts.map +1 -1
- package/dist/compile/binds/bindEventHandlers.js +2 -1
- package/dist/compile/binds/bindEventHandlers.js.map +1 -1
- package/dist/compile/binds/bindRefDirectives.d.ts.map +1 -1
- package/dist/compile/binds/bindRefDirectives.js +2 -1
- package/dist/compile/binds/bindRefDirectives.js.map +1 -1
- package/dist/compile/binds/bindStyleDirectives.d.ts.map +1 -1
- package/dist/compile/binds/bindStyleDirectives.js +2 -1
- package/dist/compile/binds/bindStyleDirectives.js.map +1 -1
- package/dist/compile/binds/bindTextInterpolations.d.ts +2 -1
- package/dist/compile/binds/bindTextInterpolations.d.ts.map +1 -1
- package/dist/compile/binds/bindTextInterpolations.js +25 -14
- package/dist/compile/binds/bindTextInterpolations.js.map +1 -1
- package/dist/compile/binds/declaredDirective.d.ts +12 -0
- package/dist/compile/binds/declaredDirective.d.ts.map +1 -0
- package/dist/compile/binds/declaredDirective.js +42 -0
- package/dist/compile/binds/declaredDirective.js.map +1 -0
- package/dist/compile/binds/forwardedAttrs.d.ts +4 -2
- package/dist/compile/binds/forwardedAttrs.d.ts.map +1 -1
- package/dist/compile/binds/forwardedAttrs.js +10 -2
- package/dist/compile/binds/forwardedAttrs.js.map +1 -1
- package/dist/compile/binds/staleRowProps.d.ts +9 -0
- package/dist/compile/binds/staleRowProps.d.ts.map +1 -0
- package/dist/compile/binds/staleRowProps.js +86 -0
- package/dist/compile/binds/staleRowProps.js.map +1 -0
- package/dist/compile/compileNodes.d.ts.map +1 -1
- package/dist/compile/compileNodes.js +2 -0
- package/dist/compile/compileNodes.js.map +1 -1
- package/dist/compile/compileTemplate.d.ts.map +1 -1
- package/dist/compile/compileTemplate.js +2 -0
- package/dist/compile/compileTemplate.js.map +1 -1
- package/dist/compile/contentProjection.d.ts +3 -0
- package/dist/compile/contentProjection.d.ts.map +1 -1
- package/dist/compile/contentProjection.js +8 -0
- package/dist/compile/contentProjection.js.map +1 -1
- package/dist/compile/disposeOrder.d.ts +35 -0
- package/dist/compile/disposeOrder.d.ts.map +1 -0
- package/dist/compile/disposeOrder.js +81 -0
- package/dist/compile/disposeOrder.js.map +1 -0
- package/dist/compile/forLoops/bindForLoops.d.ts +1 -1
- package/dist/compile/forLoops/bindForLoops.d.ts.map +1 -1
- package/dist/compile/forLoops/bindForLoops.js +10 -3
- package/dist/compile/forLoops/bindForLoops.js.map +1 -1
- package/dist/compile/forLoops/createLoopManager.d.ts.map +1 -1
- package/dist/compile/forLoops/createLoopManager.js +19 -2
- package/dist/compile/forLoops/createLoopManager.js.map +1 -1
- package/dist/compile/forLoops/loopSignals.d.ts.map +1 -1
- package/dist/compile/forLoops/loopSignals.js +15 -1
- package/dist/compile/forLoops/loopSignals.js.map +1 -1
- package/dist/compile/handleConditionalChain.d.ts.map +1 -1
- package/dist/compile/handleConditionalChain.js +2 -3
- package/dist/compile/handleConditionalChain.js.map +1 -1
- package/dist/compile/nodeExpando.d.ts +6 -0
- package/dist/compile/nodeExpando.d.ts.map +1 -1
- package/dist/compile/nodeExpando.js.map +1 -1
- package/dist/compile/pithyTreeToDom.js +1 -0
- package/dist/compile/pithyTreeToDom.js.map +1 -1
- package/dist/compile/renderBranch.d.ts +1 -1
- package/dist/compile/renderBranch.d.ts.map +1 -1
- package/dist/compile/renderBranch.js +5 -3
- package/dist/compile/renderBranch.js.map +1 -1
- package/dist/compile/renderChildren.d.ts.map +1 -1
- package/dist/compile/renderChildren.js +3 -2
- package/dist/compile/renderChildren.js.map +1 -1
- package/dist/runtime/applyBindings.d.ts.map +1 -1
- package/dist/runtime/applyBindings.js +5 -1
- package/dist/runtime/applyBindings.js.map +1 -1
- package/dist/runtime/bindings/applyComponent.d.ts.map +1 -1
- package/dist/runtime/bindings/applyComponent.js +19 -7
- package/dist/runtime/bindings/applyComponent.js.map +1 -1
- package/dist/runtime/bindings/applyDefer.d.ts.map +1 -1
- package/dist/runtime/bindings/applyDefer.js +10 -9
- package/dist/runtime/bindings/applyDefer.js.map +1 -1
- package/dist/runtime/bindings/applySimple.d.ts +2 -0
- package/dist/runtime/bindings/applySimple.d.ts.map +1 -1
- package/dist/runtime/bindings/applySimple.js +50 -10
- package/dist/runtime/bindings/applySimple.js.map +1 -1
- package/dist/runtime/bindings/applyStructural.d.ts.map +1 -1
- package/dist/runtime/bindings/applyStructural.js +11 -13
- package/dist/runtime/bindings/applyStructural.js.map +1 -1
- package/dist/runtime/bindings/helpers.d.ts +7 -0
- package/dist/runtime/bindings/helpers.d.ts.map +1 -1
- package/dist/runtime/bindings/helpers.js +35 -1
- package/dist/runtime/bindings/helpers.js.map +1 -1
- package/dist/runtime/bindings/index.d.ts +1 -1
- package/dist/runtime/bindings/index.d.ts.map +1 -1
- package/dist/runtime/bindings/index.js +1 -1
- package/dist/runtime/bindings/index.js.map +1 -1
- package/dist/runtime/html.d.ts.map +1 -1
- package/dist/runtime/html.js +2 -3
- package/dist/runtime/html.js.map +1 -1
- package/dist/types/globals.d.ts +2 -0
- package/dist/types/globals.d.ts.map +1 -1
- package/dist/types/globals.js.map +1 -1
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -281,6 +281,10 @@ The `@for` directive provides keyed, reactive list rendering with:
|
|
|
281
281
|
| compileNodes | compile-nodes | `(nodes: PithyNode[], context: Record<string, unknown>) => { el: DocumentFragment; destroy: () => void }` | stable | - |
|
|
282
282
|
| compileTemplate | renderer | `(html: string, context: Record<string, any>, css?: string, scopeId?: string, importedIdentifiers?: Set<string>) => { el: HTMLElement | DocumentFragment; destroy: () => void }` | stable | - |
|
|
283
283
|
| bindTemplateDirectives | renderer | `(fragment: DocumentFragment, context: Record<string, any>, disposers: (() => void)[]) => void` | stable | - |
|
|
284
|
+
| anchorDisposer | dispose-order | `<T extends () => void>(dispose: T, node: Node | null | undefined) => T` | internal | - |
|
|
285
|
+
| orderByDocument | dispose-order | `(disposers: (() => void)[]) => void` | internal | - |
|
|
286
|
+
| blockDisposer | dispose-order | `(stop: () => void, clear: () => void, node: Node | null | undefined) => () => void` | internal | - |
|
|
287
|
+
| componentDisposer | dispose-order | `(destroy: unknown, disposeSetup: (() => void) | undefined, node: Node | null | undefined) => (() => void) | undefined` | internal | - |
|
|
284
288
|
| clearSSRRenderCrash | ssr-crashes | `(el: Element) => void` | internal | - |
|
|
285
289
|
| resetSSRRenderCrashes | ssr-crashes | `() => void` | stable | - |
|
|
286
290
|
| collectUnhandledSSRRenderCrashes | ssr-crashes | `() => SSRRenderCrash[]` | stable | - |
|
|
@@ -295,6 +299,8 @@ The `@for` directive provides keyed, reactive list rendering with:
|
|
|
295
299
|
| html | html | `(template: TemplateStringsArray | string, contextOrCss?: Record<string, unknown> | string, cssIfContextPassed?: string) => { el: HTMLElement | DocumentFragment; destroy: () => void }` | stable | - |
|
|
296
300
|
| onDestroy | onDestroy | `(fn: () => void) => void` | stable | - |
|
|
297
301
|
| bindBoundAttributes | attributes | `(root: Element | DocumentFragment, context: Record<string, unknown>, disposers: (() => void)[]) => void` | stable | - |
|
|
302
|
+
| styleWriter | attributes | `(el: Element) => (text: string) => void` | internal | - |
|
|
303
|
+
| setStyleProp | attributes | `(el: Element, prop: string, value: string | null, priority?: string) => void` | internal | - |
|
|
298
304
|
| bindClassDirectives | classes | `(root: Element | DocumentFragment, ctx: Record<string, unknown>, disposers: (() => void)[]) => void` | stable | - |
|
|
299
305
|
| applyClassGroup | classes | `(el: Element, result: unknown, owned: Set<string>) => void` | internal | - |
|
|
300
306
|
| bindComponentTags | components | `(root: Element | DocumentFragment, rootCtx: Record<string, unknown>, disposers: (() => void)[]) => void` | stable | - |
|
|
@@ -306,6 +312,7 @@ The `@for` directive provides keyed, reactive list rendering with:
|
|
|
306
312
|
| buildPidMap | binding-helpers | `(fragment: DocumentFragment) => Map<string, Element>` | internal | - |
|
|
307
313
|
| buildCommentMap | binding-helpers | `(fragment: DocumentFragment) => Map<string, Comment>` | internal | - |
|
|
308
314
|
| nearestCtx | binding-helpers | `(el: Node, rootCtx: BindingContext) => BindingContext` | internal | - |
|
|
315
|
+
| spreadLive | binding-helpers | `(...sources: (BindingContext | undefined)[]) => BindingContext` | internal | - |
|
|
309
316
|
| mergeContexts | binding-helpers | `(context: BindingContext) => BindingContext` | internal | - |
|
|
310
317
|
| compileWithEvent | binding-helpers | `(expr: string, ctx: BindingContext) => (ev: Event, el: Element) => unknown` | internal | - |
|
|
311
318
|
<!-- @codex:end -->
|
|
@@ -409,6 +416,10 @@ pnpm test -- --watch # Watch mode
|
|
|
409
416
|
| compileNodes | - | - | - | Missing unit, integration, e2e |
|
|
410
417
|
| compileTemplate | - | - | - | Missing unit, integration, e2e |
|
|
411
418
|
| bindTemplateDirectives | - | - | - | Missing unit, integration, e2e |
|
|
419
|
+
| anchorDisposer | - | - | - | Missing unit, integration, e2e |
|
|
420
|
+
| orderByDocument | - | - | - | Missing unit, integration, e2e |
|
|
421
|
+
| blockDisposer | - | - | - | Missing unit, integration, e2e |
|
|
422
|
+
| componentDisposer | - | - | - | Missing unit, integration, e2e |
|
|
412
423
|
| clearSSRRenderCrash | - | - | - | Missing unit, integration, e2e |
|
|
413
424
|
| resetSSRRenderCrashes | - | - | - | Missing unit, integration, e2e |
|
|
414
425
|
| collectUnhandledSSRRenderCrashes | - | - | - | Missing unit, integration, e2e |
|
|
@@ -423,6 +434,8 @@ pnpm test -- --watch # Watch mode
|
|
|
423
434
|
| html | - | - | - | Missing unit, integration, e2e |
|
|
424
435
|
| onDestroy | ✓ | - | - | Missing integration, e2e |
|
|
425
436
|
| bindBoundAttributes | - | - | - | Missing unit, integration, e2e |
|
|
437
|
+
| styleWriter | - | - | - | Missing unit, integration, e2e |
|
|
438
|
+
| setStyleProp | - | - | - | Missing unit, integration, e2e |
|
|
426
439
|
| bindClassDirectives | - | - | - | Missing unit, integration, e2e |
|
|
427
440
|
| applyClassGroup | - | - | - | Missing unit, integration, e2e |
|
|
428
441
|
| bindComponentTags | - | - | - | Missing unit, integration, e2e |
|
|
@@ -434,6 +447,7 @@ pnpm test -- --watch # Watch mode
|
|
|
434
447
|
| buildPidMap | - | - | - | Missing unit, integration, e2e |
|
|
435
448
|
| buildCommentMap | - | - | - | Missing unit, integration, e2e |
|
|
436
449
|
| nearestCtx | - | - | - | Missing unit, integration, e2e |
|
|
450
|
+
| spreadLive | - | - | - | Missing unit, integration, e2e |
|
|
437
451
|
| mergeContexts | - | - | - | Missing unit, integration, e2e |
|
|
438
452
|
| compileWithEvent | - | - | - | Missing unit, integration, e2e |
|
|
439
453
|
<!-- @codex:end -->
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bindSwitchCases.d.ts","sourceRoot":"","sources":["../../src/compile/bindSwitchCases.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,OAAO,
|
|
1
|
+
{"version":3,"file":"bindSwitchCases.d.ts","sourceRoot":"","sources":["../../src/compile/bindSwitchCases.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,OAAO,EAGL,KAAK,SAAS,EACf,MAAM,mBAAmB,CAAC;AAS3B;;;GAGG;AACH,kRAAkR;AAClR,wBAAgB,eAAe,CAC7B,KAAK,EAAE,SAAS,EAAE,EAClB,MAAM,EAAE,OAAO,GAAG,gBAAgB,EAClC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,gBAAgB,GAAE,GAAG,CAAC,MAAM,CAAa,EAAE,gCAAgC;AAC3E,SAAS,GAAE,CAAC,MAAM,IAAI,CAAC,EAAO,GAC7B,GAAG,CAAC,MAAM,CAAC,CAyEb;AAUD;;GAEG;AACH,yQAAyQ;AACzQ,wBAAgB,eAAe,CAC7B,UAAU,EAAE,SAAS,EACrB,aAAa,EAAE,OAAO,EACtB,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,gBAAgB,EAAE,GAAG,CAAC,MAAM,CAAC,EAC7B,SAAS,EAAE,CAAC,MAAM,IAAI,CAAC,EAAE,GACxB,OAAO,CA2CT"}
|
|
@@ -5,10 +5,11 @@
|
|
|
5
5
|
"category": "directive"
|
|
6
6
|
}
|
|
7
7
|
*/
|
|
8
|
-
import { isDefaultCase } from '@pithyjs/compiler';
|
|
8
|
+
import { isDefaultCase, loopConflictMessage, } from '@pithyjs/compiler';
|
|
9
9
|
import { effect } from '@pithyjs/signals';
|
|
10
10
|
import { compileExpression } from './compileExpression.js';
|
|
11
11
|
import { findAllNestedLoopNodes } from './forLoops/loopTraversal.js';
|
|
12
|
+
import { blockDisposer } from './disposeOrder.js';
|
|
12
13
|
import { nodeExpando } from './nodeExpando.js';
|
|
13
14
|
import { clearBranch, renderBranch } from './renderBranch.js';
|
|
14
15
|
import { isSSRMode } from './ssrMode.js';
|
|
@@ -44,7 +45,7 @@ disposers = [] // Collect disposers for cleanup
|
|
|
44
45
|
elMap.get(node.nodeId)?.remove();
|
|
45
46
|
if (import.meta.env?.DEV && !warnedOrphans.has(node)) {
|
|
46
47
|
warnedOrphans.add(node);
|
|
47
|
-
console.warn(`[bindSwitchCases] ${node.directives?.case !== undefined ? '@case' : '@default'} has no @switch parent, or its @switch is on a @for element
|
|
48
|
+
console.warn(`[bindSwitchCases] ${node.directives?.case !== undefined ? '@case' : '@default'} has no @switch parent, or its @switch is on a @for element, which ignores it; <${node.originalTagName ?? node.tagName}> is dropped, as in a build`);
|
|
48
49
|
}
|
|
49
50
|
continue;
|
|
50
51
|
}
|
|
@@ -91,8 +92,10 @@ function isConditionalBranch(node) {
|
|
|
91
92
|
export function bindSwitchBlock(switchNode, parentElement, ctx, processedNodeIds, disposers) {
|
|
92
93
|
const caseNodes = (switchNode.children || []).filter(child => child.type === 'element' &&
|
|
93
94
|
(child.directives?.case !== undefined || isDefaultCase(child)));
|
|
94
|
-
if (caseNodes.length === 0)
|
|
95
|
-
|
|
95
|
+
if (caseNodes.length === 0) {
|
|
96
|
+
removeNonCaseChildren(parentElement, [], switchNode);
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
96
99
|
for (const node of caseNodes) {
|
|
97
100
|
if (node.nodeId)
|
|
98
101
|
processedNodeIds.add(node.nodeId);
|
|
@@ -119,6 +122,7 @@ export function bindSwitchBlock(switchNode, parentElement, ctx, processedNodeIds
|
|
|
119
122
|
return true;
|
|
120
123
|
}
|
|
121
124
|
const warnedSwitches = new WeakSet();
|
|
125
|
+
const warnedConflicts = new WeakSet();
|
|
122
126
|
const warnedOrphans = new WeakSet();
|
|
123
127
|
function removeNonCaseChildren(parentElement, caseElements, switchNode) {
|
|
124
128
|
const cases = new Set(caseElements);
|
|
@@ -131,6 +135,11 @@ function removeNonCaseChildren(parentElement, caseElements, switchNode) {
|
|
|
131
135
|
continue;
|
|
132
136
|
}
|
|
133
137
|
const node = nodeExpando(child).__pithyNode;
|
|
138
|
+
if (import.meta.env?.DEV && node && !warnedConflicts.has(node)) {
|
|
139
|
+
warnedConflicts.add(node);
|
|
140
|
+
for (const name of node.loopConflicts ?? [])
|
|
141
|
+
console.warn(`[pithy] ${loopConflictMessage(node.originalTagName ?? node.tagName ?? '', name)}`);
|
|
142
|
+
}
|
|
134
143
|
if (child.nodeType === Node.TEXT_NODE)
|
|
135
144
|
dropped ?? (dropped = JSON.stringify(child.textContent?.trim()));
|
|
136
145
|
else if (node?.type === 'slot')
|
|
@@ -265,9 +274,7 @@ function handleSwitchBlock(parentElement, caseElements, switchNode, caseNodes, c
|
|
|
265
274
|
// 1. When match changes: cleanupCurrentBranch() is called above
|
|
266
275
|
// 2. When effect stops: cleanupCurrentBranch is in disposers array
|
|
267
276
|
});
|
|
268
|
-
|
|
269
|
-
disposers.push(cleanupCurrentBranch);
|
|
270
|
-
disposers.push(stopEffect);
|
|
277
|
+
disposers.push(blockDisposer(stopEffect, cleanupCurrentBranch, placeholder));
|
|
271
278
|
}
|
|
272
279
|
}
|
|
273
280
|
//# sourceMappingURL=bindSwitchCases.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bindSwitchCases.js","sourceRoot":"","sources":["../../src/compile/bindSwitchCases.ts"],"names":[],"mappings":"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S,EAAE,EAAE,CAAC;QAChB,6DAA6D;QAC7D,MAAM,UAAU,GAAG,gBAAgB,EAAE,CAAC;QACtC,MAAM,MAAM,GAAG,iBAAiB,CAAC,UAAU,CAAC,CAAC;QAC7C,iEAAiE;QACjE,gEAAgE;QAChE,IAAI,MAAM;YAAE,SAAS,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC;IAClD,CAAC;SAAM,CAAC;QACN,mDAAmD;QACnD,qEAAqE;QACrE,IAAI,kBAAkB,GAAG,CAAC,CAAC,CAAC;QAE5B,2DAA2D;QAC3D,MAAM,UAAU,GAAG,MAAM,CAAC,GAAG,EAAE;YAC7B,MAAM,UAAU,GAAG,gBAAgB,EAAE,CAAC;YAEtC,4CAA4C;YAC5C,IAAI,UAAU,KAAK,kBAAkB,EAAE,CAAC;gBACtC,OAAO;YACT,CAAC;YAED,uDAAuD;YACvD,oBAAoB,EAAE,CAAC;YACvB,kCAAkC;YAClC,kBAAkB,GAAG,UAAU,CAAC;YAChC,OAAO,GAAG,iBAAiB,CAAC,UAAU,CAAC,CAAC;YAExC,4DAA4D;YAC5D,gEAAgE;YAChE,mEAAmE;QACrE,CAAC,CAAC,CAAC;QAEH,2EAA2E;QAC3E,SAAS,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;QACrC,SAAS,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAC7B,CAAC;AACH,CAAC","sourcesContent":["/** @codex\n{\n \"id\": \"pithy.directive.switch\",\n \"title\": \"@switch directive\",\n \"category\": \"directive\"\n}\n*/\n\nimport { isDefaultCase, type PithyNode } from '@pithyjs/compiler';\nimport { effect } from '@pithyjs/signals';\nimport { compileExpression } from './compileExpression';\nimport { findAllNestedLoopNodes } from './forLoops/loopTraversal';\nimport { nodeExpando } from './nodeExpando';\nimport { clearBranch, renderBranch, type Branch } from './renderBranch';\nimport { isSSRMode } from './ssrMode';\n\n/**\n * Processes switch case directives (@switch, @case, @default) in the template.\n * Identifies switch blocks and delegates their handling to handleSwitchBlock.\n */\n/** @codexApi {\"parent\":\"pithy.directive.switch\",\"name\":\"bindSwitchCases\",\"stability\":\"stable\",\"signature\":\"(nodes: PithyNode[], rootEl: Element | DocumentFragment, ctx: Record<string, unknown>, processedNodeIds?: Set<string>, disposers?: (() => void)[]) => Set<string>\"} */\nexport function bindSwitchCases(\n nodes: PithyNode[],\n rootEl: Element | DocumentFragment,\n ctx: Record<string, unknown>,\n processedNodeIds: Set<string> = new Set(), // Track already processed nodes\n disposers: (() => void)[] = [] // Collect disposers for cleanup\n): Set<string> {\n // Performance optimization: Create a Map of all elements by data-pid\n // This makes element lookups O(1) instead of O(n) with querySelector\n const elMap = new Map<string, Element>();\n if (rootEl instanceof Element || rootEl instanceof DocumentFragment) {\n const elements = rootEl.querySelectorAll('[data-pid]');\n elements.forEach(el => {\n const pid = el.getAttribute('data-pid');\n if (pid) {\n elMap.set(pid, el as Element);\n }\n });\n }\n\n const switchIds = new Set<string>();\n\n for (const node of nodes) {\n if (node.type !== 'element') continue;\n if (\n (node.directives?.case !== undefined || isDefaultCase(node)) &&\n node.nodeId &&\n !processedNodeIds.has(node.nodeId) &&\n !isConditionalBranch(node)\n ) {\n processedNodeIds.add(node.nodeId);\n elMap.get(node.nodeId)?.remove();\n if (import.meta.env?.DEV && !warnedOrphans.has(node)) {\n warnedOrphans.add(node);\n console.warn(\n `[bindSwitchCases] ${node.directives?.case !== undefined ? '@case' : '@default'} has no @switch parent, or its @switch is on a @for element (BUG-070); <${node.originalTagName ?? node.tagName}> is dropped, as in a build`\n );\n }\n continue;\n }\n if (node.directives?.switch === undefined) continue;\n if (isConditionalBranch(node)) continue;\n\n const parentElement = node.nodeId ? elMap.get(node.nodeId) : null;\n if (!parentElement) {\n console.error(\n '[bindSwitchCases] Could not find parent element for switch block'\n );\n continue;\n }\n if (\n bindSwitchBlock(node, parentElement, ctx, processedNodeIds, disposers) &&\n node.nodeId\n )\n switchIds.add(node.nodeId);\n }\n\n for (const node of nodes) {\n if (node.nodeId && switchIds.has(node.nodeId)) continue;\n if (node.nodeId && processedNodeIds.has(node.nodeId)) continue;\n if (isConditionalBranch(node)) continue;\n if (node.isComponent) continue;\n\n if (node.type === 'element' && node.children?.length) {\n const element = node.nodeId ? elMap.get(node.nodeId) : null;\n\n if (element instanceof Element) {\n bindSwitchCases(\n node.children,\n element,\n ctx,\n processedNodeIds,\n disposers\n );\n }\n }\n }\n\n return processedNodeIds;\n}\n\nfunction isConditionalBranch(node: PithyNode): boolean {\n return (\n node.directives?.if !== undefined ||\n node.directives?.elseIf !== undefined ||\n node.directives?.else !== undefined\n );\n}\n\n/**\n * Binds one @switch element whose DOM is already built.\n */\n/** @codexApi {\"parent\":\"pithy.directive.switch\",\"name\":\"bindSwitchBlock\",\"stability\":\"experimental\",\"signature\":\"(switchNode: PithyNode, parentElement: Element, ctx: Record<string, unknown>, processedNodeIds: Set<string>, disposers: (() => void)[]) => boolean\"} */\nexport function bindSwitchBlock(\n switchNode: PithyNode,\n parentElement: Element,\n ctx: Record<string, unknown>,\n processedNodeIds: Set<string>,\n disposers: (() => void)[]\n): boolean {\n const caseNodes = (switchNode.children || []).filter(\n child =>\n child.type === 'element' &&\n (child.directives?.case !== undefined || isDefaultCase(child))\n );\n if (caseNodes.length === 0) return false;\n\n for (const node of caseNodes) {\n if (node.nodeId) processedNodeIds.add(node.nodeId);\n for (const loopNode of findAllNestedLoopNodes(node)) {\n if (loopNode.nodeId) processedNodeIds.add(loopNode.nodeId);\n }\n }\n\n const childByPid = new Map<string, Element>();\n for (const child of Array.from(parentElement.children)) {\n const pid = child.getAttribute('data-pid');\n if (pid) childByPid.set(pid, child as Element);\n }\n const caseElements = caseNodes\n .map(node => (node.nodeId ? childByPid.get(node.nodeId) : undefined))\n .filter((el): el is Element => el !== undefined);\n\n if (caseElements.length !== caseNodes.length) {\n console.error('[bindSwitchCases] Could not find all case elements');\n return true;\n }\n\n removeNonCaseChildren(parentElement, caseElements, switchNode);\n\n handleSwitchBlock(\n parentElement,\n caseElements,\n switchNode,\n caseNodes,\n ctx,\n disposers\n );\n return true;\n}\n\nconst warnedSwitches = new WeakSet<PithyNode>();\nconst warnedOrphans = new WeakSet<PithyNode>();\n\nfunction removeNonCaseChildren(\n parentElement: Element,\n caseElements: Element[],\n switchNode: PithyNode\n): void {\n const cases = new Set<Node>(caseElements);\n let dropped: string | undefined;\n for (const child of Array.from(parentElement.childNodes)) {\n if (cases.has(child)) continue;\n if (child.nodeType === Node.TEXT_NODE && !child.textContent?.trim()) {\n child.remove();\n continue;\n }\n const node = nodeExpando(child).__pithyNode;\n if (child.nodeType === Node.TEXT_NODE)\n dropped ??= JSON.stringify(child.textContent?.trim());\n else if (node?.type === 'slot') dropped ??= '<slot>';\n else if (node) dropped ??= `<${node.originalTagName ?? node.tagName}>`;\n else if (child.nodeType === Node.ELEMENT_NODE)\n dropped ??= `<${(child as Element).localName}>`;\n child.remove();\n }\n if (dropped && import.meta.env?.DEV && !warnedSwitches.has(switchNode)) {\n warnedSwitches.add(switchNode);\n console.warn(\n `[bindSwitchCases] @switch renders only its @case/@default children; ${dropped} is dropped, as in a build`\n );\n }\n}\n\n/**\n * Handles rendering and reactivity for an @switch/@case/@default block.\n */\nfunction handleSwitchBlock(\n parentElement: Element,\n caseElements: Element[],\n switchNode: PithyNode,\n caseNodes: PithyNode[],\n ctx: Record<string, unknown>,\n disposers: (() => void)[]\n) {\n // Generate a unique ID for this switch block for debugging\n const switchId = Math.random().toString(36).substring(2, 8);\n\n // Create a placeholder comment to mark where our switch content should go\n const placeholder = document.createComment(`switch-block-${switchId}`);\n\n // Insert the placeholder at the beginning of the parent element\n parentElement.insertBefore(placeholder, parentElement.firstChild);\n\n // Remove all case elements from the DOM immediately\n for (const el of caseElements) {\n el.remove();\n }\n\n let current: Branch | null = null;\n\n // Cache for compiled expressions to avoid recompiling on each change\n const compiledExpressions = new Map<string, () => unknown>();\n\n // Helper function to compile and cache expressions\n function getCompiledExpression(expr: string, kind: string): () => unknown {\n if (!expr || expr.trim() === '') {\n return () => undefined;\n }\n\n // Check if we already compiled this expression\n const cacheKey = `${kind}:${expr}`;\n if (compiledExpressions.has(cacheKey)) {\n return compiledExpressions.get(cacheKey)!;\n }\n\n try {\n const fn = compileExpression(expr, ctx, undefined);\n compiledExpressions.set(cacheKey, fn);\n return fn;\n } catch (err) {\n console.error(`[Switch-${switchId}] Error compiling ${kind}:`, err);\n const fallbackFn = () => undefined;\n compiledExpressions.set(cacheKey, fallbackFn);\n return fallbackFn;\n }\n }\n\n // Helper function to safely evaluate expressions\n function safeEvaluate(expr: string, kind: string): unknown {\n const fn = getCompiledExpression(expr, kind);\n try {\n return fn();\n } catch (err) {\n console.error(`[Switch-${switchId}] Error evaluating ${kind}:`, err);\n return undefined;\n }\n }\n\n function cleanupCurrentBranch() {\n if (current) clearBranch(current);\n current = null;\n }\n\n // Shared logic for finding the matching case\n function findMatchingCase(): number {\n // Evaluate the switch expression\n const switchValue = safeEvaluate(switchNode.directives!.switch!, '@switch');\n\n // Find the matching case\n let matchIndex = -1;\n let defaultIndex = -1;\n\n for (let i = 0; i < caseNodes.length; i++) {\n const node = caseNodes[i];\n\n if (node.directives?.case !== undefined) {\n // This is a @case node\n const caseValue = safeEvaluate(node.directives.case, '@case');\n\n // Check if the case value matches the switch value\n if (caseValue === switchValue) {\n matchIndex = i;\n break;\n }\n } else if (isDefaultCase(node)) {\n // This is a @default node, remember its index but continue checking other cases\n if (defaultIndex === -1) defaultIndex = i;\n }\n }\n\n // If no case matched but we have a default, use that\n if (matchIndex === -1 && defaultIndex !== -1) {\n matchIndex = defaultIndex;\n }\n\n return matchIndex;\n }\n\n function renderMatchedCase(matchIndex: number): Branch | null {\n if (matchIndex < 0 || !placeholder.parentNode) return null;\n return renderBranch(caseNodes[matchIndex], ctx, placeholder);\n }\n\n if (isSSRMode()) {\n // SSR mode: evaluate once synchronously, no reactive effects\n const matchIndex = findMatchingCase();\n const branch = renderMatchedCase(matchIndex);\n // Ensure nested directive disposers created during SSR are added\n // to the parent disposers array for proper cleanup on destroy()\n if (branch) disposers.push(...branch.disposers);\n } else {\n // Client mode: reactive effect for dynamic updates\n // Track the previous match index to avoid unnecessary DOM operations\n let previousMatchIndex = -1;\n\n // Set up the reactive effect and capture the stop function\n const stopEffect = effect(() => {\n const matchIndex = findMatchingCase();\n\n // Skip re-insertion if match hasn't changed\n if (matchIndex === previousMatchIndex) {\n return;\n }\n\n // Clean up the current branch before showing a new one\n cleanupCurrentBranch();\n // Update the previous match index\n previousMatchIndex = matchIndex;\n current = renderMatchedCase(matchIndex);\n\n // Don't return cleanup here - we handle cleanup explicitly:\n // 1. When match changes: cleanupCurrentBranch() is called above\n // 2. When effect stops: cleanupCurrentBranch is in disposers array\n });\n\n // Add cleanup and effect stop to disposers for proper cleanup on destroy()\n disposers.push(cleanupCurrentBranch);\n disposers.push(stopEffect);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"bindSwitchCases.js","sourceRoot":"","sources":["../../src/compile/bindSwitchCases.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,OAAO,EACL,aAAa,EACb,mBAAmB,GAEpB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,YAAY,EAAe,MAAM,gBAAgB,CAAC;AACxE,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC;;;GAGG;AACH,kRAAkR;AAClR,MAAM,UAAU,eAAe,CAC7B,KAAkB,EAClB,MAAkC,EAClC,GAA4B,EAC5B,mBAAgC,IAAI,GAAG,EAAE,EAAE,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,EAAE,EAAE,CAAC;QAChB,6DAA6D;QAC7D,MAAM,UAAU,GAAG,gBAAgB,EAAE,CAAC;QACtC,MAAM,MAAM,GAAG,iBAAiB,CAAC,UAAU,CAAC,CAAC;QAC7C,iEAAiE;QACjE,gEAAgE;QAChE,IAAI,MAAM;YAAE,SAAS,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC;IAClD,CAAC;SAAM,CAAC;QACN,mDAAmD;QACnD,qEAAqE;QACrE,IAAI,kBAAkB,GAAG,CAAC,CAAC,CAAC;QAE5B,2DAA2D;QAC3D,MAAM,UAAU,GAAG,MAAM,CAAC,GAAG,EAAE;YAC7B,MAAM,UAAU,GAAG,gBAAgB,EAAE,CAAC;YAEtC,4CAA4C;YAC5C,IAAI,UAAU,KAAK,kBAAkB,EAAE,CAAC;gBACtC,OAAO;YACT,CAAC;YAED,uDAAuD;YACvD,oBAAoB,EAAE,CAAC;YACvB,kCAAkC;YAClC,kBAAkB,GAAG,UAAU,CAAC;YAChC,OAAO,GAAG,iBAAiB,CAAC,UAAU,CAAC,CAAC;YAExC,4DAA4D;YAC5D,gEAAgE;YAChE,mEAAmE;QACrE,CAAC,CAAC,CAAC;QAEH,SAAS,CAAC,IAAI,CACZ,aAAa,CAAC,UAAU,EAAE,oBAAoB,EAAE,WAAW,CAAC,CAC7D,CAAC;IACJ,CAAC;AACH,CAAC","sourcesContent":["/** @codex\n{\n \"id\": \"pithy.directive.switch\",\n \"title\": \"@switch directive\",\n \"category\": \"directive\"\n}\n*/\n\nimport {\n isDefaultCase,\n loopConflictMessage,\n type PithyNode,\n} from '@pithyjs/compiler';\nimport { effect } from '@pithyjs/signals';\nimport { compileExpression } from './compileExpression';\nimport { findAllNestedLoopNodes } from './forLoops/loopTraversal';\nimport { blockDisposer } from './disposeOrder';\nimport { nodeExpando } from './nodeExpando';\nimport { clearBranch, renderBranch, type Branch } from './renderBranch';\nimport { isSSRMode } from './ssrMode';\n\n/**\n * Processes switch case directives (@switch, @case, @default) in the template.\n * Identifies switch blocks and delegates their handling to handleSwitchBlock.\n */\n/** @codexApi {\"parent\":\"pithy.directive.switch\",\"name\":\"bindSwitchCases\",\"stability\":\"stable\",\"signature\":\"(nodes: PithyNode[], rootEl: Element | DocumentFragment, ctx: Record<string, unknown>, processedNodeIds?: Set<string>, disposers?: (() => void)[]) => Set<string>\"} */\nexport function bindSwitchCases(\n nodes: PithyNode[],\n rootEl: Element | DocumentFragment,\n ctx: Record<string, unknown>,\n processedNodeIds: Set<string> = new Set(), // Track already processed nodes\n disposers: (() => void)[] = [] // Collect disposers for cleanup\n): Set<string> {\n // Performance optimization: Create a Map of all elements by data-pid\n // This makes element lookups O(1) instead of O(n) with querySelector\n const elMap = new Map<string, Element>();\n if (rootEl instanceof Element || rootEl instanceof DocumentFragment) {\n const elements = rootEl.querySelectorAll('[data-pid]');\n elements.forEach(el => {\n const pid = el.getAttribute('data-pid');\n if (pid) {\n elMap.set(pid, el as Element);\n }\n });\n }\n\n const switchIds = new Set<string>();\n\n for (const node of nodes) {\n if (node.type !== 'element') continue;\n if (\n (node.directives?.case !== undefined || isDefaultCase(node)) &&\n node.nodeId &&\n !processedNodeIds.has(node.nodeId) &&\n !isConditionalBranch(node)\n ) {\n processedNodeIds.add(node.nodeId);\n elMap.get(node.nodeId)?.remove();\n if (import.meta.env?.DEV && !warnedOrphans.has(node)) {\n warnedOrphans.add(node);\n console.warn(\n `[bindSwitchCases] ${node.directives?.case !== undefined ? '@case' : '@default'} has no @switch parent, or its @switch is on a @for element, which ignores it; <${node.originalTagName ?? node.tagName}> is dropped, as in a build`\n );\n }\n continue;\n }\n if (node.directives?.switch === undefined) continue;\n if (isConditionalBranch(node)) continue;\n\n const parentElement = node.nodeId ? elMap.get(node.nodeId) : null;\n if (!parentElement) {\n console.error(\n '[bindSwitchCases] Could not find parent element for switch block'\n );\n continue;\n }\n if (\n bindSwitchBlock(node, parentElement, ctx, processedNodeIds, disposers) &&\n node.nodeId\n )\n switchIds.add(node.nodeId);\n }\n\n for (const node of nodes) {\n if (node.nodeId && switchIds.has(node.nodeId)) continue;\n if (node.nodeId && processedNodeIds.has(node.nodeId)) continue;\n if (isConditionalBranch(node)) continue;\n if (node.isComponent) continue;\n\n if (node.type === 'element' && node.children?.length) {\n const element = node.nodeId ? elMap.get(node.nodeId) : null;\n\n if (element instanceof Element) {\n bindSwitchCases(\n node.children,\n element,\n ctx,\n processedNodeIds,\n disposers\n );\n }\n }\n }\n\n return processedNodeIds;\n}\n\nfunction isConditionalBranch(node: PithyNode): boolean {\n return (\n node.directives?.if !== undefined ||\n node.directives?.elseIf !== undefined ||\n node.directives?.else !== undefined\n );\n}\n\n/**\n * Binds one @switch element whose DOM is already built.\n */\n/** @codexApi {\"parent\":\"pithy.directive.switch\",\"name\":\"bindSwitchBlock\",\"stability\":\"experimental\",\"signature\":\"(switchNode: PithyNode, parentElement: Element, ctx: Record<string, unknown>, processedNodeIds: Set<string>, disposers: (() => void)[]) => boolean\"} */\nexport function bindSwitchBlock(\n switchNode: PithyNode,\n parentElement: Element,\n ctx: Record<string, unknown>,\n processedNodeIds: Set<string>,\n disposers: (() => void)[]\n): boolean {\n const caseNodes = (switchNode.children || []).filter(\n child =>\n child.type === 'element' &&\n (child.directives?.case !== undefined || isDefaultCase(child))\n );\n if (caseNodes.length === 0) {\n removeNonCaseChildren(parentElement, [], switchNode);\n return true;\n }\n\n for (const node of caseNodes) {\n if (node.nodeId) processedNodeIds.add(node.nodeId);\n for (const loopNode of findAllNestedLoopNodes(node)) {\n if (loopNode.nodeId) processedNodeIds.add(loopNode.nodeId);\n }\n }\n\n const childByPid = new Map<string, Element>();\n for (const child of Array.from(parentElement.children)) {\n const pid = child.getAttribute('data-pid');\n if (pid) childByPid.set(pid, child as Element);\n }\n const caseElements = caseNodes\n .map(node => (node.nodeId ? childByPid.get(node.nodeId) : undefined))\n .filter((el): el is Element => el !== undefined);\n\n if (caseElements.length !== caseNodes.length) {\n console.error('[bindSwitchCases] Could not find all case elements');\n return true;\n }\n\n removeNonCaseChildren(parentElement, caseElements, switchNode);\n\n handleSwitchBlock(\n parentElement,\n caseElements,\n switchNode,\n caseNodes,\n ctx,\n disposers\n );\n return true;\n}\n\nconst warnedSwitches = new WeakSet<PithyNode>();\nconst warnedConflicts = new WeakSet<PithyNode>();\nconst warnedOrphans = new WeakSet<PithyNode>();\n\nfunction removeNonCaseChildren(\n parentElement: Element,\n caseElements: Element[],\n switchNode: PithyNode\n): void {\n const cases = new Set<Node>(caseElements);\n let dropped: string | undefined;\n for (const child of Array.from(parentElement.childNodes)) {\n if (cases.has(child)) continue;\n if (child.nodeType === Node.TEXT_NODE && !child.textContent?.trim()) {\n child.remove();\n continue;\n }\n const node = nodeExpando(child).__pithyNode;\n if (import.meta.env?.DEV && node && !warnedConflicts.has(node)) {\n warnedConflicts.add(node);\n for (const name of node.loopConflicts ?? [])\n console.warn(\n `[pithy] ${loopConflictMessage(node.originalTagName ?? node.tagName ?? '', name)}`\n );\n }\n if (child.nodeType === Node.TEXT_NODE)\n dropped ??= JSON.stringify(child.textContent?.trim());\n else if (node?.type === 'slot') dropped ??= '<slot>';\n else if (node) dropped ??= `<${node.originalTagName ?? node.tagName}>`;\n else if (child.nodeType === Node.ELEMENT_NODE)\n dropped ??= `<${(child as Element).localName}>`;\n child.remove();\n }\n if (dropped && import.meta.env?.DEV && !warnedSwitches.has(switchNode)) {\n warnedSwitches.add(switchNode);\n console.warn(\n `[bindSwitchCases] @switch renders only its @case/@default children; ${dropped} is dropped, as in a build`\n );\n }\n}\n\n/**\n * Handles rendering and reactivity for an @switch/@case/@default block.\n */\nfunction handleSwitchBlock(\n parentElement: Element,\n caseElements: Element[],\n switchNode: PithyNode,\n caseNodes: PithyNode[],\n ctx: Record<string, unknown>,\n disposers: (() => void)[]\n) {\n // Generate a unique ID for this switch block for debugging\n const switchId = Math.random().toString(36).substring(2, 8);\n\n // Create a placeholder comment to mark where our switch content should go\n const placeholder = document.createComment(`switch-block-${switchId}`);\n\n // Insert the placeholder at the beginning of the parent element\n parentElement.insertBefore(placeholder, parentElement.firstChild);\n\n // Remove all case elements from the DOM immediately\n for (const el of caseElements) {\n el.remove();\n }\n\n let current: Branch | null = null;\n\n // Cache for compiled expressions to avoid recompiling on each change\n const compiledExpressions = new Map<string, () => unknown>();\n\n // Helper function to compile and cache expressions\n function getCompiledExpression(expr: string, kind: string): () => unknown {\n if (!expr || expr.trim() === '') {\n return () => undefined;\n }\n\n // Check if we already compiled this expression\n const cacheKey = `${kind}:${expr}`;\n if (compiledExpressions.has(cacheKey)) {\n return compiledExpressions.get(cacheKey)!;\n }\n\n try {\n const fn = compileExpression(expr, ctx, undefined);\n compiledExpressions.set(cacheKey, fn);\n return fn;\n } catch (err) {\n console.error(`[Switch-${switchId}] Error compiling ${kind}:`, err);\n const fallbackFn = () => undefined;\n compiledExpressions.set(cacheKey, fallbackFn);\n return fallbackFn;\n }\n }\n\n // Helper function to safely evaluate expressions\n function safeEvaluate(expr: string, kind: string): unknown {\n const fn = getCompiledExpression(expr, kind);\n try {\n return fn();\n } catch (err) {\n console.error(`[Switch-${switchId}] Error evaluating ${kind}:`, err);\n return undefined;\n }\n }\n\n function cleanupCurrentBranch() {\n if (current) clearBranch(current);\n current = null;\n }\n\n // Shared logic for finding the matching case\n function findMatchingCase(): number {\n // Evaluate the switch expression\n const switchValue = safeEvaluate(switchNode.directives!.switch!, '@switch');\n\n // Find the matching case\n let matchIndex = -1;\n let defaultIndex = -1;\n\n for (let i = 0; i < caseNodes.length; i++) {\n const node = caseNodes[i];\n\n if (node.directives?.case !== undefined) {\n // This is a @case node\n const caseValue = safeEvaluate(node.directives.case, '@case');\n\n // Check if the case value matches the switch value\n if (caseValue === switchValue) {\n matchIndex = i;\n break;\n }\n } else if (isDefaultCase(node)) {\n // This is a @default node, remember its index but continue checking other cases\n if (defaultIndex === -1) defaultIndex = i;\n }\n }\n\n // If no case matched but we have a default, use that\n if (matchIndex === -1 && defaultIndex !== -1) {\n matchIndex = defaultIndex;\n }\n\n return matchIndex;\n }\n\n function renderMatchedCase(matchIndex: number): Branch | null {\n if (matchIndex < 0 || !placeholder.parentNode) return null;\n return renderBranch(caseNodes[matchIndex], ctx, placeholder);\n }\n\n if (isSSRMode()) {\n // SSR mode: evaluate once synchronously, no reactive effects\n const matchIndex = findMatchingCase();\n const branch = renderMatchedCase(matchIndex);\n // Ensure nested directive disposers created during SSR are added\n // to the parent disposers array for proper cleanup on destroy()\n if (branch) disposers.push(...branch.disposers);\n } else {\n // Client mode: reactive effect for dynamic updates\n // Track the previous match index to avoid unnecessary DOM operations\n let previousMatchIndex = -1;\n\n // Set up the reactive effect and capture the stop function\n const stopEffect = effect(() => {\n const matchIndex = findMatchingCase();\n\n // Skip re-insertion if match hasn't changed\n if (matchIndex === previousMatchIndex) {\n return;\n }\n\n // Clean up the current branch before showing a new one\n cleanupCurrentBranch();\n // Update the previous match index\n previousMatchIndex = matchIndex;\n current = renderMatchedCase(matchIndex);\n\n // Don't return cleanup here - we handle cleanup explicitly:\n // 1. When match changes: cleanupCurrentBranch() is called above\n // 2. When effect stops: cleanupCurrentBranch is in disposers array\n });\n\n disposers.push(\n blockDisposer(stopEffect, cleanupCurrentBranch, placeholder)\n );\n }\n}\n"]}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** The passes after components: what a component's forwarded directives need. */
|
|
2
|
+
/** @codexApi {"parent":"pithy.runtime.renderer","name":"bindAfterComponents","stability":"internal","signature":"(fragment: DocumentFragment, context: Record<string, unknown>, disposers: (() => void)[]) => void"} */
|
|
3
|
+
export declare function bindAfterComponents(fragment: DocumentFragment, context: Record<string, unknown>, disposers: (() => void)[]): void;
|
|
4
|
+
//# sourceMappingURL=bindAfterComponents.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bindAfterComponents.d.ts","sourceRoot":"","sources":["../../../src/compile/binds/bindAfterComponents.ts"],"names":[],"mappings":"AAQA,iFAAiF;AACjF,wNAAwN;AACxN,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,gBAAgB,EAC1B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,SAAS,EAAE,CAAC,MAAM,IAAI,CAAC,EAAE,GACxB,IAAI,CAqBN"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { bindDefaultTwoWay } from './bindDefaultTwoWay.js';
|
|
2
|
+
import { bindTextInterpolations } from './bindTextInterpolations.js';
|
|
3
|
+
import { bindEventHandlers } from './bindEventHandlers.js';
|
|
4
|
+
import { bindClassDirectives } from './bindClassDirectives.js';
|
|
5
|
+
import { bindBoundAttributes } from './bindBoundAttributes.js';
|
|
6
|
+
import { executeCustomDirectiveBinders } from './directiveRegistry.js';
|
|
7
|
+
import { bindDeferDirectives } from './bindDeferDirectives.js';
|
|
8
|
+
/** The passes after components: what a component's forwarded directives need. */
|
|
9
|
+
/** @codexApi {"parent":"pithy.runtime.renderer","name":"bindAfterComponents","stability":"internal","signature":"(fragment: DocumentFragment, context: Record<string, unknown>, disposers: (() => void)[]) => void"} */
|
|
10
|
+
export function bindAfterComponents(fragment, context, disposers) {
|
|
11
|
+
// two-way model bindings for native controls at this level
|
|
12
|
+
bindDefaultTwoWay(fragment, context, disposers);
|
|
13
|
+
// text interpolations — our binder already skips nested loops & component roots
|
|
14
|
+
bindTextInterpolations(fragment, context, disposers);
|
|
15
|
+
// events (idempotent; supports modifiers; nearest ctx; $event/$el)
|
|
16
|
+
bindEventHandlers(fragment, context, disposers);
|
|
17
|
+
// classes and style properties
|
|
18
|
+
bindClassDirectives(fragment, context, disposers);
|
|
19
|
+
// bound attributes
|
|
20
|
+
bindBoundAttributes(fragment, context, disposers);
|
|
21
|
+
// custom directives — allows external packages (router, etc.) to register binders
|
|
22
|
+
executeCustomDirectiveBinders(fragment, context, disposers);
|
|
23
|
+
// defer directives — lazy loading with @defer, @load, @placeholder
|
|
24
|
+
bindDeferDirectives(fragment, context, disposers);
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=bindAfterComponents.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bindAfterComponents.js","sourceRoot":"","sources":["../../../src/compile/binds/bindAfterComponents.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,6BAA6B,EAAE,MAAM,qBAAqB,CAAC;AACpE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,iFAAiF;AACjF,wNAAwN;AACxN,MAAM,UAAU,mBAAmB,CACjC,QAA0B,EAC1B,OAAgC,EAChC,SAAyB;IAEzB,2DAA2D;IAC3D,iBAAiB,CAAC,QAAQ,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;IAEhD,gFAAgF;IAChF,sBAAsB,CAAC,QAAQ,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;IAErD,mEAAmE;IACnE,iBAAiB,CAAC,QAAQ,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;IAEhD,+BAA+B;IAC/B,mBAAmB,CAAC,QAAQ,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;IAElD,mBAAmB;IACnB,mBAAmB,CAAC,QAAQ,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;IAElD,kFAAkF;IAClF,6BAA6B,CAAC,QAAQ,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;IAE5D,mEAAmE;IACnE,mBAAmB,CAAC,QAAQ,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;AACpD,CAAC","sourcesContent":["import { bindDefaultTwoWay } from './bindDefaultTwoWay';\nimport { bindTextInterpolations } from './bindTextInterpolations';\nimport { bindEventHandlers } from './bindEventHandlers';\nimport { bindClassDirectives } from './bindClassDirectives';\nimport { bindBoundAttributes } from './bindBoundAttributes';\nimport { executeCustomDirectiveBinders } from './directiveRegistry';\nimport { bindDeferDirectives } from './bindDeferDirectives';\n\n/** The passes after components: what a component's forwarded directives need. */\n/** @codexApi {\"parent\":\"pithy.runtime.renderer\",\"name\":\"bindAfterComponents\",\"stability\":\"internal\",\"signature\":\"(fragment: DocumentFragment, context: Record<string, unknown>, disposers: (() => void)[]) => void\"} */\nexport function bindAfterComponents(\n fragment: DocumentFragment,\n context: Record<string, unknown>,\n disposers: (() => void)[]\n): void {\n // two-way model bindings for native controls at this level\n bindDefaultTwoWay(fragment, context, disposers);\n\n // text interpolations — our binder already skips nested loops & component roots\n bindTextInterpolations(fragment, context, disposers);\n\n // events (idempotent; supports modifiers; nearest ctx; $event/$el)\n bindEventHandlers(fragment, context, disposers);\n\n // classes and style properties\n bindClassDirectives(fragment, context, disposers);\n\n // bound attributes\n bindBoundAttributes(fragment, context, disposers);\n\n // custom directives — allows external packages (router, etc.) to register binders\n executeCustomDirectiveBinders(fragment, context, disposers);\n\n // defer directives — lazy loading with @defer, @load, @placeholder\n bindDeferDirectives(fragment, context, disposers);\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bindAfterProjection.d.ts","sourceRoot":"","sources":["../../../src/compile/binds/bindAfterProjection.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"bindAfterProjection.d.ts","sourceRoot":"","sources":["../../../src/compile/binds/bindAfterProjection.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,wNAAwN;AACxN,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,gBAAgB,EAC1B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,SAAS,EAAE,CAAC,MAAM,IAAI,CAAC,EAAE,GACxB,IAAI,CAIN"}
|
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
import { bindComponentTags } from './bindComponentsTags.js';
|
|
2
|
-
import {
|
|
3
|
-
import { bindTextInterpolations } from './bindTextInterpolations.js';
|
|
4
|
-
import { bindEventHandlers } from './bindEventHandlers.js';
|
|
5
|
-
import { bindClassDirectives } from './bindClassDirectives.js';
|
|
6
|
-
import { bindBoundAttributes } from './bindBoundAttributes.js';
|
|
7
|
-
import { executeCustomDirectiveBinders } from './directiveRegistry.js';
|
|
8
|
-
import { bindDeferDirectives } from './bindDeferDirectives.js';
|
|
2
|
+
import { bindAfterComponents } from './bindAfterComponents.js';
|
|
9
3
|
/**
|
|
10
4
|
* The non-structural passes that follow content projection, in order. Each
|
|
11
5
|
* consumes what it binds, so a second run over the same nodes binds nothing.
|
|
@@ -14,19 +8,6 @@ import { bindDeferDirectives } from './bindDeferDirectives.js';
|
|
|
14
8
|
export function bindAfterProjection(fragment, context, disposers) {
|
|
15
9
|
// components — materialize custom tags, including those in projected content
|
|
16
10
|
bindComponentTags(fragment, context, disposers);
|
|
17
|
-
|
|
18
|
-
bindDefaultTwoWay(fragment, context, disposers);
|
|
19
|
-
// text interpolations — our binder already skips nested loops & component roots
|
|
20
|
-
bindTextInterpolations(fragment, context, disposers);
|
|
21
|
-
// events (idempotent; supports modifiers; nearest ctx; $event/$el)
|
|
22
|
-
bindEventHandlers(fragment, context, disposers);
|
|
23
|
-
// classes and style properties
|
|
24
|
-
bindClassDirectives(fragment, context, disposers);
|
|
25
|
-
// bound attributes
|
|
26
|
-
bindBoundAttributes(fragment, context, disposers);
|
|
27
|
-
// custom directives — allows external packages (router, etc.) to register binders
|
|
28
|
-
executeCustomDirectiveBinders(fragment, context, disposers);
|
|
29
|
-
// defer directives — lazy loading with @defer, @load, @placeholder
|
|
30
|
-
bindDeferDirectives(fragment, context, disposers);
|
|
11
|
+
bindAfterComponents(fragment, context, disposers);
|
|
31
12
|
}
|
|
32
13
|
//# sourceMappingURL=bindAfterProjection.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bindAfterProjection.js","sourceRoot":"","sources":["../../../src/compile/binds/bindAfterProjection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"bindAfterProjection.js","sourceRoot":"","sources":["../../../src/compile/binds/bindAfterProjection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D;;;GAGG;AACH,wNAAwN;AACxN,MAAM,UAAU,mBAAmB,CACjC,QAA0B,EAC1B,OAAgC,EAChC,SAAyB;IAEzB,6EAA6E;IAC7E,iBAAiB,CAAC,QAAQ,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;IAChD,mBAAmB,CAAC,QAAQ,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;AACpD,CAAC","sourcesContent":["import { bindComponentTags } from './bindComponentsTags';\nimport { bindAfterComponents } from './bindAfterComponents';\n\n/**\n * The non-structural passes that follow content projection, in order. Each\n * consumes what it binds, so a second run over the same nodes binds nothing.\n */\n/** @codexApi {\"parent\":\"pithy.runtime.renderer\",\"name\":\"bindAfterProjection\",\"stability\":\"internal\",\"signature\":\"(fragment: DocumentFragment, context: Record<string, unknown>, disposers: (() => void)[]) => void\"} */\nexport function bindAfterProjection(\n fragment: DocumentFragment,\n context: Record<string, unknown>,\n disposers: (() => void)[]\n): void {\n // components — materialize custom tags, including those in projected content\n bindComponentTags(fragment, context, disposers);\n bindAfterComponents(fragment, context, disposers);\n}\n"]}
|
|
@@ -11,5 +11,9 @@
|
|
|
11
11
|
*/
|
|
12
12
|
/** @codexApi {"parent":"pithy.runtime.attributes","name":"bindBoundAttributes","stability":"stable","signature":"(root: Element | DocumentFragment, context: Record<string, unknown>, disposers: (() => void)[]) => void"} */
|
|
13
13
|
export declare function bindBoundAttributes(root: Element | DocumentFragment, context: Record<string, unknown>, disposers: (() => void)[]): void;
|
|
14
|
+
/** @codexApi {"parent":"pithy.runtime.attributes","name":"styleWriter","stability":"internal","signature":"(el: Element) => (text: string) => void"} */
|
|
15
|
+
export declare function styleWriter(el: Element): (text: string) => void;
|
|
16
|
+
/** @codexApi {"parent":"pithy.runtime.attributes","name":"setStyleProp","stability":"internal","signature":"(el: Element, prop: string, value: string | null, priority?: string) => void"} */
|
|
17
|
+
export declare function setStyleProp(el: Element, prop: string, value: string | null, priority?: string): void;
|
|
14
18
|
export declare function rejectsScriptSink(el: Element, attr: string, text: string): boolean;
|
|
15
19
|
//# sourceMappingURL=bindBoundAttributes.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bindBoundAttributes.d.ts","sourceRoot":"","sources":["../../../src/compile/binds/bindBoundAttributes.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;
|
|
1
|
+
{"version":3,"file":"bindBoundAttributes.d.ts","sourceRoot":"","sources":["../../../src/compile/binds/bindBoundAttributes.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAMF;;;GAGG;AACH,8NAA8N;AAC9N,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,OAAO,GAAG,gBAAgB,EAChC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,SAAS,EAAE,CAAC,MAAM,IAAI,CAAC,EAAE,GACxB,IAAI,CA4CN;AAsBD,wJAAwJ;AACxJ,wBAAgB,WAAW,CAAC,EAAE,EAAE,OAAO,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAY/D;AAED,8LAA8L;AAC9L,wBAAgB,YAAY,CAC1B,EAAE,EAAE,OAAO,EACX,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,GAAG,IAAI,EACpB,QAAQ,SAAK,GACZ,IAAI,CAYN;AA2BD,wBAAgB,iBAAiB,CAC/B,EAAE,EAAE,OAAO,EACX,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,GACX,OAAO,CAiBT"}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
"category": "runtime"
|
|
6
6
|
}
|
|
7
7
|
*/
|
|
8
|
+
import { isDeclaredDirective } from './declaredDirective.js';
|
|
8
9
|
import { effect } from '@pithyjs/signals';
|
|
9
10
|
import { nodeExpando } from '../nodeExpando.js';
|
|
10
11
|
/**
|
|
@@ -18,16 +19,20 @@ export function bindBoundAttributes(root, context, disposers) {
|
|
|
18
19
|
// carries the parent component's context via this property).
|
|
19
20
|
const elCtx = nodeExpando(el).__pithyCtx || context;
|
|
20
21
|
for (const attr of Array.from(el.attributes)) {
|
|
21
|
-
if (!attr.name.startsWith('data-bind-'))
|
|
22
|
+
if (!attr.name.startsWith('data-bind-') || !isDeclaredDirective(attr))
|
|
22
23
|
continue;
|
|
23
24
|
const attrName = attr.name.slice('data-bind-'.length);
|
|
24
25
|
const expr = attr.value;
|
|
25
26
|
// Remove data-bind-* attribute after capturing expression to prevent
|
|
26
27
|
// double-processing when component subtrees are inserted into outer DOM
|
|
27
28
|
el.removeAttribute(attr.name);
|
|
29
|
+
const writeStyle = attrName === 'style' ? styleWriter(el) : null;
|
|
28
30
|
const stop = effect(() => {
|
|
29
31
|
const value = evaluateExpression(expr, elCtx);
|
|
30
|
-
if (typeof value === 'boolean') {
|
|
32
|
+
if (writeStyle && typeof value === 'boolean') {
|
|
33
|
+
writeStyle('');
|
|
34
|
+
}
|
|
35
|
+
else if (typeof value === 'boolean') {
|
|
31
36
|
if (value) {
|
|
32
37
|
el.setAttribute(attrName, '');
|
|
33
38
|
}
|
|
@@ -37,7 +42,10 @@ export function bindBoundAttributes(root, context, disposers) {
|
|
|
37
42
|
}
|
|
38
43
|
else {
|
|
39
44
|
const text = String(value ?? '');
|
|
40
|
-
if (
|
|
45
|
+
if (writeStyle) {
|
|
46
|
+
writeStyle(text);
|
|
47
|
+
}
|
|
48
|
+
else if (rejectsScriptSink(el, attrName, text)) {
|
|
41
49
|
el.removeAttribute(attrName);
|
|
42
50
|
}
|
|
43
51
|
else {
|
|
@@ -49,6 +57,58 @@ export function bindBoundAttributes(root, context, disposers) {
|
|
|
49
57
|
}
|
|
50
58
|
});
|
|
51
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Every binding on one element's `style`. Strings compose in bind order, then
|
|
62
|
+
* each `[style.x]` applies on top, so nothing depends on reading the CSSOM back:
|
|
63
|
+
* a value there cannot show which binding wrote it.
|
|
64
|
+
*/
|
|
65
|
+
function styleOwners(el) {
|
|
66
|
+
const node = nodeExpando(el);
|
|
67
|
+
return (node.__styleOwners ?? (node.__styleOwners = { strings: [], props: new Map() }));
|
|
68
|
+
}
|
|
69
|
+
function renderStyle(el, owners) {
|
|
70
|
+
const text = owners.strings.filter(Boolean).join('; ');
|
|
71
|
+
if (text)
|
|
72
|
+
el.setAttribute('style', text);
|
|
73
|
+
else
|
|
74
|
+
el.removeAttribute('style');
|
|
75
|
+
const style = el.style;
|
|
76
|
+
for (const [prop, [value, priority]] of owners.props) {
|
|
77
|
+
style?.setProperty(prop, value, priority);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** @codexApi {"parent":"pithy.runtime.attributes","name":"styleWriter","stability":"internal","signature":"(el: Element) => (text: string) => void"} */
|
|
81
|
+
export function styleWriter(el) {
|
|
82
|
+
const owners = styleOwners(el);
|
|
83
|
+
if (!owners.strings.length) {
|
|
84
|
+
const style = el.style;
|
|
85
|
+
for (const prop of owners.props.keys())
|
|
86
|
+
style?.removeProperty(prop);
|
|
87
|
+
owners.strings.push(el.getAttribute('style') ?? '');
|
|
88
|
+
}
|
|
89
|
+
const slot = owners.strings.push('') - 1;
|
|
90
|
+
return text => {
|
|
91
|
+
owners.strings[slot] = text;
|
|
92
|
+
renderStyle(el, owners);
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/** @codexApi {"parent":"pithy.runtime.attributes","name":"setStyleProp","stability":"internal","signature":"(el: Element, prop: string, value: string | null, priority?: string) => void"} */
|
|
96
|
+
export function setStyleProp(el, prop, value, priority = '') {
|
|
97
|
+
const owners = styleOwners(el);
|
|
98
|
+
const style = el.style;
|
|
99
|
+
owners.props.delete(prop);
|
|
100
|
+
if (value !== null)
|
|
101
|
+
owners.props.set(prop, [value, priority]);
|
|
102
|
+
if (owners.strings.length) {
|
|
103
|
+
renderStyle(el, owners);
|
|
104
|
+
}
|
|
105
|
+
else if (value === null) {
|
|
106
|
+
style?.removeProperty(prop);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
style?.setProperty(prop, value, priority);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
52
112
|
const LINK_SCHEME_RE = /^(?:javascript|vbscript|data):/i;
|
|
53
113
|
// A data: frame, form target or animated href loads in an opaque origin, so only a link refuses data:.
|
|
54
114
|
const SCRIPT_SCHEME_RE = /^(?:javascript|vbscript):/i;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bindBoundAttributes.js","sourceRoot":"","sources":["../../../src/compile/binds/bindBoundAttributes.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C;;;GAGG;AACH,8NAA8N;AAC9N,MAAM,UAAU,mBAAmB,CACjC,IAAgC,EAChC,OAAgC,EAChC,SAAyB;IAEzB,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE;QACtC,sEAAsE;QACtE,6DAA6D;QAC7D,MAAM,KAAK,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC,UAAU,IAAI,OAAO,CAAC;QAEpD,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,EAAE,CAAC;YAC7C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,OAAO,CAAC,CAAC,CAAC;IAC7C,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,CAAC,IAAI,CAAC,+BAA+B,IAAI,EAAE,EAAE,GAAG,CAAC,CAAC;QACzD,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC","sourcesContent":["/** @codex\n{\n \"id\": \"pithy.runtime.attributes\",\n \"title\": \"Bound Attribute Binding System\",\n \"category\": \"runtime\"\n}\n*/\n\nimport { effect } from '@pithyjs/signals';\nimport { nodeExpando } from '../nodeExpando';\n\n/**\n * Binds [attr]=\"expression\" bindings like [class]=\"dynamicClass\"\n * using reactive effect evaluation.\n */\n/** @codexApi {\"parent\":\"pithy.runtime.attributes\",\"name\":\"bindBoundAttributes\",\"stability\":\"stable\",\"signature\":\"(root: Element | DocumentFragment, context: Record<string, unknown>, disposers: (() => void)[]) => void\"} */\nexport function bindBoundAttributes(\n root: Element | DocumentFragment,\n context: Record<string, unknown>,\n disposers: (() => void)[]\n): void {\n root.querySelectorAll('*').forEach(el => {\n // Use element's own __pithyCtx when available (projected slot content\n // carries the parent component's context via this property).\n const elCtx = nodeExpando(el).__pithyCtx || context;\n\n for (const attr of Array.from(el.attributes)) {\n if (!attr.name.startsWith('data-bind-')) continue;\n\n const attrName = attr.name.slice('data-bind-'.length);\n const expr = attr.value;\n\n // Remove data-bind-* attribute after capturing expression to prevent\n // double-processing when component subtrees are inserted into outer DOM\n el.removeAttribute(attr.name);\n\n const stop = effect(() => {\n const value = evaluateExpression(expr, elCtx);\n\n if (typeof value === 'boolean') {\n if (value) {\n el.setAttribute(attrName, '');\n } else {\n el.removeAttribute(attrName);\n }\n } else {\n const text = String(value ?? '');\n if (rejectsScriptSink(el, attrName, text)) {\n el.removeAttribute(attrName);\n } else {\n el.setAttribute(attrName, text);\n }\n }\n });\n\n disposers.push(stop);\n }\n });\n}\n\nconst LINK_SCHEME_RE = /^(?:javascript|vbscript|data):/i;\n// A data: frame, form target or animated href loads in an opaque origin, so only a link refuses data:.\nconst SCRIPT_SCHEME_RE = /^(?:javascript|vbscript):/i;\nconst LINK_ATTRS = ['href', 'xlink:href'];\nconst LINK_SINK = { attrs: LINK_ATTRS, scheme: LINK_SCHEME_RE };\nconst FORM_ACTION_SINK = { attrs: ['formaction'], scheme: SCRIPT_SCHEME_RE };\nconst URL_SINKS = new Map<string, { attrs: readonly string[]; scheme: RegExp }>(\n [\n ['a', LINK_SINK],\n ['area', LINK_SINK],\n ['set', { attrs: ['to'], scheme: SCRIPT_SCHEME_RE }],\n ['animate', { attrs: ['to', 'from', 'values'], scheme: SCRIPT_SCHEME_RE }],\n ['iframe', { attrs: ['src'], scheme: SCRIPT_SCHEME_RE }],\n ['frame', { attrs: ['src'], scheme: SCRIPT_SCHEME_RE }],\n ['form', { attrs: ['action'], scheme: SCRIPT_SCHEME_RE }],\n ['button', FORM_ACTION_SINK],\n ['input', FORM_ACTION_SINK],\n ]\n);\n\nfunction hasScheme(url: string, scheme: RegExp): boolean {\n const compact = [...url].filter(c => c > ' ' && c !== '\\x7f').join('');\n return scheme.test(compact);\n}\n\nexport function rejectsScriptSink(\n el: Element,\n attr: string,\n text: string\n): boolean {\n if (el.localName === 'iframe' && attr === 'srcdoc') {\n if (import.meta.env?.DEV && text !== '') {\n console.warn(\n '[PithyJS] A bound [srcdoc] is rejected: its HTML runs with the page origin. Bind [src] to a data:text/html URL, which loads in an opaque origin.'\n );\n }\n return true;\n }\n const sink = URL_SINKS.get(el.localName);\n if (!sink?.attrs.includes(attr)) return false;\n const urls = attr === 'values' ? text.split(';') : [text];\n if (!urls.some(url => hasScheme(url, sink.scheme))) return false;\n if (import.meta.env?.DEV) {\n console.warn(`[PithyJS] Unsafe [${attr}] rejected: \"${text}\".`);\n }\n return true;\n}\n\nfunction evaluateExpression(\n expr: string,\n context: Record<string, unknown>\n): unknown {\n try {\n // eslint-disable-next-line no-new-func -- args-style template expression evaluation is a core framework feature\n const compiled = new Function(\n ...Object.keys(context),\n `return (${expr})`\n ) as (...args: unknown[]) => unknown;\n return compiled(...Object.values(context));\n } catch (err) {\n console.warn(`[pithy] Failed to evaluate: ${expr}`, err);\n return undefined;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"bindBoundAttributes.js","sourceRoot":"","sources":["../../../src/compile/binds/bindBoundAttributes.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAC1D,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAoB,MAAM,gBAAgB,CAAC;AAE/D;;;GAGG;AACH,8NAA8N;AAC9N,MAAM,UAAU,mBAAmB,CACjC,IAAgC,EAChC,OAAgC,EAChC,SAAyB;IAEzB,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE;QACtC,sEAAsE;QACtE,6DAA6D;QAC7D,MAAM,KAAK,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC,UAAU,IAAI,OAAO,CAAC;QAEpD,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,EAAE,CAAC;YAC7C,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,YAAY,CAAC,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC;gBACnE,SAAS;YAEX,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;YACtD,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC;YAExB,qEAAqE;YACrE,wEAAwE;YACxE,EAAE,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAE9B,MAAM,UAAU,GAAG,QAAQ,KAAK,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;YACjE,M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AAS,CAAC,GAAG,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC;IACzC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,OAAO,KAAK,CAAC;IAC9C,MAAM,IAAI,GAAG,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAC1D,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,SAAS,CAAC,GAAG,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;QAAE,OAAO,KAAK,CAAC;IACjE,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC;QACzB,OAAO,CAAC,IAAI,CAAC,qBAAqB,IAAI,gBAAgB,IAAI,IAAI,CAAC,CAAC;IAClE,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,kBAAkB,CACzB,IAAY,EACZ,OAAgC;IAEhC,IAAI,CAAC;QACH,gHAAgH;QAChH,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAC3B,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,EACvB,WAAW,IAAI,GAAG,CACgB,CAAC;QACrC,OAAO,QAAQ,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;IAC7C,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,CAAC,IAAI,CAAC,+BAA+B,IAAI,EAAE,EAAE,GAAG,CAAC,CAAC;QACzD,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC","sourcesContent":["/** @codex\n{\n \"id\": \"pithy.runtime.attributes\",\n \"title\": \"Bound Attribute Binding System\",\n \"category\": \"runtime\"\n}\n*/\n\nimport { isDeclaredDirective } from './declaredDirective';\nimport { effect } from '@pithyjs/signals';\nimport { nodeExpando, type StyleOwners } from '../nodeExpando';\n\n/**\n * Binds [attr]=\"expression\" bindings like [class]=\"dynamicClass\"\n * using reactive effect evaluation.\n */\n/** @codexApi {\"parent\":\"pithy.runtime.attributes\",\"name\":\"bindBoundAttributes\",\"stability\":\"stable\",\"signature\":\"(root: Element | DocumentFragment, context: Record<string, unknown>, disposers: (() => void)[]) => void\"} */\nexport function bindBoundAttributes(\n root: Element | DocumentFragment,\n context: Record<string, unknown>,\n disposers: (() => void)[]\n): void {\n root.querySelectorAll('*').forEach(el => {\n // Use element's own __pithyCtx when available (projected slot content\n // carries the parent component's context via this property).\n const elCtx = nodeExpando(el).__pithyCtx || context;\n\n for (const attr of Array.from(el.attributes)) {\n if (!attr.name.startsWith('data-bind-') || !isDeclaredDirective(attr))\n continue;\n\n const attrName = attr.name.slice('data-bind-'.length);\n const expr = attr.value;\n\n // Remove data-bind-* attribute after capturing expression to prevent\n // double-processing when component subtrees are inserted into outer DOM\n el.removeAttribute(attr.name);\n\n const writeStyle = attrName === 'style' ? styleWriter(el) : null;\n const stop = effect(() => {\n const value = evaluateExpression(expr, elCtx);\n\n if (writeStyle && typeof value === 'boolean') {\n writeStyle('');\n } else if (typeof value === 'boolean') {\n if (value) {\n el.setAttribute(attrName, '');\n } else {\n el.removeAttribute(attrName);\n }\n } else {\n const text = String(value ?? '');\n if (writeStyle) {\n writeStyle(text);\n } else if (rejectsScriptSink(el, attrName, text)) {\n el.removeAttribute(attrName);\n } else {\n el.setAttribute(attrName, text);\n }\n }\n });\n\n disposers.push(stop);\n }\n });\n}\n\n/**\n * Every binding on one element's `style`. Strings compose in bind order, then\n * each `[style.x]` applies on top, so nothing depends on reading the CSSOM back:\n * a value there cannot show which binding wrote it.\n */\nfunction styleOwners(el: Element): StyleOwners {\n const node = nodeExpando(el);\n return (node.__styleOwners ??= { strings: [], props: new Map() });\n}\n\nfunction renderStyle(el: Element, owners: StyleOwners): void {\n const text = owners.strings.filter(Boolean).join('; ');\n if (text) el.setAttribute('style', text);\n else el.removeAttribute('style');\n const style = (el as Partial<ElementCSSInlineStyle>).style;\n for (const [prop, [value, priority]] of owners.props) {\n style?.setProperty(prop, value, priority);\n }\n}\n\n/** @codexApi {\"parent\":\"pithy.runtime.attributes\",\"name\":\"styleWriter\",\"stability\":\"internal\",\"signature\":\"(el: Element) => (text: string) => void\"} */\nexport function styleWriter(el: Element): (text: string) => void {\n const owners = styleOwners(el);\n if (!owners.strings.length) {\n const style = (el as Partial<ElementCSSInlineStyle>).style;\n for (const prop of owners.props.keys()) style?.removeProperty(prop);\n owners.strings.push(el.getAttribute('style') ?? '');\n }\n const slot = owners.strings.push('') - 1;\n return text => {\n owners.strings[slot] = text;\n renderStyle(el, owners);\n };\n}\n\n/** @codexApi {\"parent\":\"pithy.runtime.attributes\",\"name\":\"setStyleProp\",\"stability\":\"internal\",\"signature\":\"(el: Element, prop: string, value: string | null, priority?: string) => void\"} */\nexport function setStyleProp(\n el: Element,\n prop: string,\n value: string | null,\n priority = ''\n): void {\n const owners = styleOwners(el);\n const style = (el as Partial<ElementCSSInlineStyle>).style;\n owners.props.delete(prop);\n if (value !== null) owners.props.set(prop, [value, priority]);\n if (owners.strings.length) {\n renderStyle(el, owners);\n } else if (value === null) {\n style?.removeProperty(prop);\n } else {\n style?.setProperty(prop, value, priority);\n }\n}\n\nconst LINK_SCHEME_RE = /^(?:javascript|vbscript|data):/i;\n// A data: frame, form target or animated href loads in an opaque origin, so only a link refuses data:.\nconst SCRIPT_SCHEME_RE = /^(?:javascript|vbscript):/i;\nconst LINK_ATTRS = ['href', 'xlink:href'];\nconst LINK_SINK = { attrs: LINK_ATTRS, scheme: LINK_SCHEME_RE };\nconst FORM_ACTION_SINK = { attrs: ['formaction'], scheme: SCRIPT_SCHEME_RE };\nconst URL_SINKS = new Map<string, { attrs: readonly string[]; scheme: RegExp }>(\n [\n ['a', LINK_SINK],\n ['area', LINK_SINK],\n ['set', { attrs: ['to'], scheme: SCRIPT_SCHEME_RE }],\n ['animate', { attrs: ['to', 'from', 'values'], scheme: SCRIPT_SCHEME_RE }],\n ['iframe', { attrs: ['src'], scheme: SCRIPT_SCHEME_RE }],\n ['frame', { attrs: ['src'], scheme: SCRIPT_SCHEME_RE }],\n ['form', { attrs: ['action'], scheme: SCRIPT_SCHEME_RE }],\n ['button', FORM_ACTION_SINK],\n ['input', FORM_ACTION_SINK],\n ]\n);\n\nfunction hasScheme(url: string, scheme: RegExp): boolean {\n const compact = [...url].filter(c => c > ' ' && c !== '\\x7f').join('');\n return scheme.test(compact);\n}\n\nexport function rejectsScriptSink(\n el: Element,\n attr: string,\n text: string\n): boolean {\n if (el.localName === 'iframe' && attr === 'srcdoc') {\n if (import.meta.env?.DEV && text !== '') {\n console.warn(\n '[PithyJS] A bound [srcdoc] is rejected: its HTML runs with the page origin. Bind [src] to a data:text/html URL, which loads in an opaque origin.'\n );\n }\n return true;\n }\n const sink = URL_SINKS.get(el.localName);\n if (!sink?.attrs.includes(attr)) return false;\n const urls = attr === 'values' ? text.split(';') : [text];\n if (!urls.some(url => hasScheme(url, sink.scheme))) return false;\n if (import.meta.env?.DEV) {\n console.warn(`[PithyJS] Unsafe [${attr}] rejected: \"${text}\".`);\n }\n return true;\n}\n\nfunction evaluateExpression(\n expr: string,\n context: Record<string, unknown>\n): unknown {\n try {\n // eslint-disable-next-line no-new-func -- args-style template expression evaluation is a core framework feature\n const compiled = new Function(\n ...Object.keys(context),\n `return (${expr})`\n ) as (...args: unknown[]) => unknown;\n return compiled(...Object.values(context));\n } catch (err) {\n console.warn(`[pithy] Failed to evaluate: ${expr}`, err);\n return undefined;\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bindClassDirectives.d.ts","sourceRoot":"","sources":["../../../src/compile/binds/bindClassDirectives.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;
|
|
1
|
+
{"version":3,"file":"bindClassDirectives.d.ts","sourceRoot":"","sources":["../../../src/compile/binds/bindClassDirectives.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAUF;;;;;;;;;;GAUG;AACH,uNAAuN;AACvN,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,OAAO,GAAG,gBAAgB,EAChC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,SAAS,EAAE,CAAC,MAAM,IAAI,CAAC,EAAE,GACxB,IAAI,CAiFN;AAED,4KAA4K;AAC5K,wBAAgB,eAAe,CAC7B,EAAE,EAAE,OAAO,EACX,MAAM,EAAE,OAAO,EACf,KAAK,EAAE,GAAG,CAAC,MAAM,CAAC,GACjB,IAAI,CA8BN"}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
"category": "runtime"
|
|
6
6
|
}
|
|
7
7
|
*/
|
|
8
|
+
import { declaredValue, isDeclaredDirective } from './declaredDirective.js';
|
|
8
9
|
import { effect } from '@pithyjs/signals';
|
|
9
10
|
import { compileExpression } from '../compileExpression.js';
|
|
10
11
|
import { nodeExpando } from '../nodeExpando.js';
|
|
@@ -37,7 +38,7 @@ export function bindClassDirectives(root, ctx, disposers) {
|
|
|
37
38
|
...Object.keys(expando.__pithyNode?.directives?.classMap ?? {}),
|
|
38
39
|
]);
|
|
39
40
|
const className = declaredName(attr.name.slice(TOGGLE_PREFIX.length), declared);
|
|
40
|
-
if (!className)
|
|
41
|
+
if (!className || !isDeclaredDirective(attr))
|
|
41
42
|
continue;
|
|
42
43
|
const expression = attr.value;
|
|
43
44
|
// Remove directive attribute after capturing to prevent double-processing
|
|
@@ -62,7 +63,7 @@ export function bindClassDirectives(root, ctx, disposers) {
|
|
|
62
63
|
});
|
|
63
64
|
disposers.push(stop);
|
|
64
65
|
}
|
|
65
|
-
const classGroupExpr = el
|
|
66
|
+
const classGroupExpr = declaredValue(el, 'data-classes');
|
|
66
67
|
if (classGroupExpr) {
|
|
67
68
|
// Remove directive attribute after capturing to prevent double-processing
|
|
68
69
|
el.removeAttribute('data-classes');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
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CAAC;SAAM,IAAI,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAChD,KAAK,MAAM,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC;YAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC;IAClE,CAAC;SAAM,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,MAAM,EAAE,CAAC;QAC1C,OAAO,CAAC,IAAI,CACV,uEAAuE,OAAO,MAAM,qCAAqC,CAC1H,CAAC;IACJ,CAAC;IAED,yEAAyE;IACzE,yEAAyE;IACzE,6CAA6C;IAC7C,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;IAChC,KAAK,CAAC,KAAK,EAAE,CAAC;IACd,KAAK,MAAM,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,KAAK,EAAE,CAAC;QAC5B,IAAI,CAAC,EAAE;YAAE,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;aAC3B,IAAI,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC;YACtD,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YACpB,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QACf,CAAC;IACH,CAAC;IACD,KAAK,MAAM,CAAC,IAAI,QAAQ;QAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;YAAE,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AACtE,CAAC","sourcesContent":["/** @codex\n{\n \"id\": \"pithy.runtime.classes\",\n \"title\": \"Class Directive Binding System\",\n \"category\": \"runtime\"\n}\n*/\n\nimport { declaredValue, isDeclaredDirective } from './declaredDirective';\nimport { effect } from '@pithyjs/signals';\nimport { compileExpression } from '../compileExpression';\nimport { nodeExpando } from '../nodeExpando';\nimport { bindStyleProperties, declaredName } from './bindStyleDirectives';\n\nconst TOGGLE_PREFIX = 'data-class-toggle-';\n\n/**\n * Binds class directives like:\n * • [class.active]=\"isActive\"\n * • [classes]=\"{ active: isActive() }\"\n * • [classes]=\"['a', 'b']\"\n * • [classes]=\"'a b'\"\n *\n * @param root - The root node to search for class bindings\n * @param ctx - Context object containing reactive variables and functions\n * @param disposers - Cleanup functions to remove subscriptions\n */\n/** @codexApi {\"parent\":\"pithy.runtime.classes\",\"name\":\"bindClassDirectives\",\"stability\":\"stable\",\"signature\":\"(root: Element | DocumentFragment, ctx: Record<string, unknown>, disposers: (() => void)[]) => void\"} */\nexport function bindClassDirectives(\n root: Element | DocumentFragment,\n ctx: Record<string, unknown>,\n disposers: (() => void)[]\n): void {\n root.querySelectorAll('*').forEach(el => {\n // Use element's own __pithyCtx when available (projected slot content\n // carries the parent component's context via this property).\n const elCtx = nodeExpando(el).__pithyCtx || ctx;\n\n let declared: string[] | undefined;\n for (const attr of Array.from(el.attributes)) {\n if (!attr.name.startsWith(TOGGLE_PREFIX)) continue;\n const expando = nodeExpando(el);\n declared ??= [\n ...(expando.__classToggles ?? []),\n ...Object.keys(expando.__pithyNode?.directives?.classMap ?? {}),\n ];\n const className = declaredName(\n attr.name.slice(TOGGLE_PREFIX.length),\n declared\n );\n if (!className || !isDeclaredDirective(attr)) continue;\n const expression = attr.value;\n\n // Remove directive attribute after capturing to prevent double-processing\n // when component subtrees are inserted into an outer DOM that re-runs binders\n el.removeAttribute(attr.name);\n\n let evaluate: () => unknown;\n\n try {\n evaluate = compileExpression(expression, elCtx);\n } catch (err) {\n console.warn(\n `[PithyJS] ⚠️ Failed to compile [class.${className}]=\"${expression}\"`,\n err\n );\n return;\n }\n\n const stop = effect(() => {\n try {\n const result = evaluate();\n el.classList.toggle(className, !!result);\n } catch {\n /* ignore */\n }\n });\n\n disposers.push(stop);\n }\n\n const classGroupExpr = declaredValue(el, 'data-classes');\n if (classGroupExpr) {\n // Remove directive attribute after capturing to prevent double-processing\n el.removeAttribute('data-classes');\n\n let evaluate: () => unknown;\n\n try {\n evaluate = compileExpression(classGroupExpr, elCtx);\n } catch (err) {\n console.warn(\n `[PithyJS] ⚠️ Failed to compile [classes]=\"${classGroupExpr}\"`,\n err\n );\n return;\n }\n\n const owned = new Set<string>();\n const stop = effect(() => {\n try {\n applyClassGroup(el, evaluate(), owned);\n } catch (err) {\n console.warn('[PithyJS] ⚠️ Failed to evaluate [classes]:', err);\n }\n });\n\n disposers.push(stop);\n }\n\n // After the classes, so the attributes serialise in the build's order.\n bindStyleProperties(el, elCtx, disposers);\n });\n}\n\n/** @codexApi {\"parent\":\"pithy.runtime.classes\",\"name\":\"applyClassGroup\",\"stability\":\"internal\",\"signature\":\"(el: Element, result: unknown, owned: Set<string>) => void\"} */\nexport function applyClassGroup(\n el: Element,\n result: unknown,\n owned: Set<string>\n): void {\n const named = new Map<string, boolean>();\n const name = (classes: string, on: boolean) => {\n for (const c of classes.split(/[\\t\\n\\f\\r ]+/)) if (c) named.set(c, on);\n };\n if (typeof result === 'string') {\n name(result, true);\n } else if (Array.isArray(result)) {\n for (const cls of result) if (typeof cls === 'string') name(cls, true);\n } else if (result && typeof result === 'object') {\n for (const [cls, on] of Object.entries(result)) name(cls, !!on);\n } else if (import.meta.env?.DEV && result) {\n console.warn(\n `[PithyJS] ⚠️ [classes] expects a string, an array or an object, got ${typeof result}; the classes it added are removed.`\n );\n }\n\n // `owned` is this binding's own record, not the element's: a component's\n // root `[classes]` and one forwarded from its tag share the element, and\n // each may only remove the classes it added.\n const previous = new Set(owned);\n owned.clear();\n for (const [c, on] of named) {\n if (!on) el.classList.remove(c);\n else if (previous.has(c) || !el.classList.contains(c)) {\n el.classList.add(c);\n owned.add(c);\n }\n }\n for (const c of previous) if (!named.has(c)) el.classList.remove(c);\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bindComponentsTags.d.ts","sourceRoot":"","sources":["../../../src/compile/binds/bindComponentsTags.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;
|
|
1
|
+
{"version":3,"file":"bindComponentsTags.d.ts","sourceRoot":"","sources":["../../../src/compile/binds/bindComponentsTags.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAgDF,qGAAqG;AACrG,wBAAgB,2BAA2B,IAAI,IAAI,CAElD;AAmKD;;;;;;GAMG;AACH,4NAA4N;AAC5N,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,OAAO,GAAG,gBAAgB,EAChC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,SAAS,EAAE,CAAC,MAAM,IAAI,CAAC,EAAE,GACxB,IAAI,CA8ZN"}
|