@pithyjs/core 0.1.0-beta.2 → 0.1.0-beta.21
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 +46 -8
- package/dist/a11y/focus-trap.d.ts +11 -3
- package/dist/a11y/focus-trap.d.ts.map +1 -1
- package/dist/a11y/focus-trap.js +55 -10
- package/dist/a11y/focus-trap.js.map +1 -1
- package/dist/a11y/index.d.ts +4 -4
- package/dist/a11y/index.js +4 -4
- package/dist/a11y/index.js.map +1 -1
- package/dist/a11y/keyboard-utils.js.map +1 -1
- package/dist/a11y/roving-tabindex.js.map +1 -1
- package/dist/a11y/skip-link.js.map +1 -1
- package/dist/compile/bindSwitchCases.d.ts +8 -3
- package/dist/compile/bindSwitchCases.d.ts.map +1 -1
- package/dist/compile/bindSwitchCases.js +137 -183
- 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 +7 -0
- package/dist/compile/binds/bindAfterProjection.d.ts.map +1 -0
- package/dist/compile/binds/bindAfterProjection.js +15 -0
- package/dist/compile/binds/bindAfterProjection.js.map +1 -0
- package/dist/compile/binds/bindBoundAttributes.d.ts +8 -0
- package/dist/compile/binds/bindBoundAttributes.d.ts.map +1 -1
- package/dist/compile/binds/bindBoundAttributes.js +227 -21
- package/dist/compile/binds/bindBoundAttributes.js.map +1 -1
- package/dist/compile/binds/bindClassDirectives.d.ts +3 -2
- package/dist/compile/binds/bindClassDirectives.d.ts.map +1 -1
- package/dist/compile/binds/bindClassDirectives.js +58 -21
- package/dist/compile/binds/bindClassDirectives.js.map +1 -1
- package/dist/compile/binds/bindComponentsTags.d.ts +2 -0
- package/dist/compile/binds/bindComponentsTags.d.ts.map +1 -1
- package/dist/compile/binds/bindComponentsTags.js +186 -74
- package/dist/compile/binds/bindComponentsTags.js.map +1 -1
- package/dist/compile/binds/bindConditionals.d.ts +2 -2
- package/dist/compile/binds/bindConditionals.d.ts.map +1 -1
- package/dist/compile/binds/bindConditionals.js +22 -5
- package/dist/compile/binds/bindConditionals.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 +21 -19
- package/dist/compile/binds/bindDeferDirectives.js.map +1 -1
- package/dist/compile/binds/bindErrorBoundary.d.ts +61 -0
- package/dist/compile/binds/bindErrorBoundary.d.ts.map +1 -1
- package/dist/compile/binds/bindErrorBoundary.js +272 -16
- 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 +15 -5
- 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 +17 -0
- package/dist/compile/binds/bindStyleDirectives.d.ts.map +1 -0
- package/dist/compile/binds/bindStyleDirectives.js +48 -0
- package/dist/compile/binds/bindStyleDirectives.js.map +1 -0
- package/dist/compile/binds/bindTextInterpolations.d.ts +4 -2
- package/dist/compile/binds/bindTextInterpolations.d.ts.map +1 -1
- package/dist/compile/binds/bindTextInterpolations.js +87 -35
- 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/directiveRegistry.js.map +1 -1
- 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/propNames.d.ts +14 -0
- package/dist/compile/binds/propNames.d.ts.map +1 -0
- package/dist/compile/binds/propNames.js +116 -0
- package/dist/compile/binds/propNames.js.map +1 -0
- 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/compileExpression.js.map +1 -1
- package/dist/compile/compileNodes.d.ts.map +1 -1
- package/dist/compile/compileNodes.js +16 -7
- package/dist/compile/compileNodes.js.map +1 -1
- package/dist/compile/compileTemplate.d.ts +3 -4
- package/dist/compile/compileTemplate.d.ts.map +1 -1
- package/dist/compile/compileTemplate.js +36 -54
- package/dist/compile/compileTemplate.js.map +1 -1
- package/dist/compile/componentResult.js.map +1 -1
- package/dist/compile/contentProjection.d.ts +15 -3
- package/dist/compile/contentProjection.d.ts.map +1 -1
- package/dist/compile/contentProjection.js +90 -9
- 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/evalDiagnostics.d.ts +7 -0
- package/dist/compile/evalDiagnostics.d.ts.map +1 -0
- package/dist/compile/evalDiagnostics.js +8 -0
- package/dist/compile/evalDiagnostics.js.map +1 -0
- package/dist/compile/forLoops/bindForLoops.d.ts +3 -3
- package/dist/compile/forLoops/bindForLoops.d.ts.map +1 -1
- package/dist/compile/forLoops/bindForLoops.js +18 -13
- package/dist/compile/forLoops/bindForLoops.js.map +1 -1
- package/dist/compile/forLoops/createLoopManager.d.ts +3 -3
- package/dist/compile/forLoops/createLoopManager.d.ts.map +1 -1
- package/dist/compile/forLoops/createLoopManager.js +44 -26
- package/dist/compile/forLoops/createLoopManager.js.map +1 -1
- package/dist/compile/forLoops/index.d.ts +5 -5
- package/dist/compile/forLoops/index.js +5 -5
- package/dist/compile/forLoops/index.js.map +1 -1
- package/dist/compile/forLoops/loopHelpers.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/forLoops/loopTraversal.js +2 -2
- package/dist/compile/forLoops/loopTraversal.js.map +1 -1
- package/dist/compile/handleConditionalChain.d.ts +2 -2
- package/dist/compile/handleConditionalChain.d.ts.map +1 -1
- package/dist/compile/handleConditionalChain.js +23 -89
- package/dist/compile/handleConditionalChain.js.map +1 -1
- package/dist/compile/index.d.ts +17 -17
- package/dist/compile/index.js +14 -14
- package/dist/compile/index.js.map +1 -1
- package/dist/compile/injectScopedStyle.js.map +1 -1
- package/dist/compile/nodeExpando.d.ts +28 -0
- package/dist/compile/nodeExpando.d.ts.map +1 -1
- package/dist/compile/nodeExpando.js +12 -0
- package/dist/compile/nodeExpando.js.map +1 -1
- package/dist/compile/parseHtml.js +12 -6
- package/dist/compile/parseHtml.js.map +1 -1
- package/dist/compile/pithyTreeToDom.d.ts.map +1 -1
- package/dist/compile/pithyTreeToDom.js +17 -12
- package/dist/compile/pithyTreeToDom.js.map +1 -1
- package/dist/compile/renderBranch.d.ts +13 -0
- package/dist/compile/renderBranch.d.ts.map +1 -0
- package/dist/compile/renderBranch.js +72 -0
- package/dist/compile/renderBranch.js.map +1 -0
- package/dist/compile/renderChildren.d.ts +12 -2
- package/dist/compile/renderChildren.d.ts.map +1 -1
- package/dist/compile/renderChildren.js +33 -4
- package/dist/compile/renderChildren.js.map +1 -1
- package/dist/compile/ssrCrashes.d.ts +65 -37
- package/dist/compile/ssrCrashes.d.ts.map +1 -1
- package/dist/compile/ssrCrashes.js +344 -39
- package/dist/compile/ssrCrashes.js.map +1 -1
- package/dist/compile/ssrMode.js +1 -1
- package/dist/compile/ssrMode.js.map +1 -1
- package/dist/compile/textValue.js.map +1 -1
- package/dist/compile/trimBlankEdges.d.ts +2 -0
- package/dist/compile/trimBlankEdges.d.ts.map +1 -0
- package/dist/compile/trimBlankEdges.js +15 -0
- package/dist/compile/trimBlankEdges.js.map +1 -0
- package/dist/config/index.js.map +1 -1
- package/dist/index.d.ts +6 -6
- package/dist/index.js +6 -6
- package/dist/index.js.map +1 -1
- package/dist/interfaces/index.d.ts +2 -2
- package/dist/interfaces/index.js +2 -2
- package/dist/interfaces/index.js.map +1 -1
- package/dist/interfaces/ref-signal.interface.js.map +1 -1
- package/dist/interfaces/ref.interface.js.map +1 -1
- package/dist/runtime/applyBindings.d.ts +9 -5
- package/dist/runtime/applyBindings.d.ts.map +1 -1
- package/dist/runtime/applyBindings.js +130 -43
- package/dist/runtime/applyBindings.js.map +1 -1
- package/dist/runtime/bindings/applyComponent.d.ts +2 -2
- package/dist/runtime/bindings/applyComponent.d.ts.map +1 -1
- package/dist/runtime/bindings/applyComponent.js +143 -87
- package/dist/runtime/bindings/applyComponent.js.map +1 -1
- package/dist/runtime/bindings/applyDefer.d.ts +1 -1
- package/dist/runtime/bindings/applyDefer.d.ts.map +1 -1
- package/dist/runtime/bindings/applyDefer.js +16 -15
- package/dist/runtime/bindings/applyDefer.js.map +1 -1
- package/dist/runtime/bindings/applySimple.d.ts +10 -1
- package/dist/runtime/bindings/applySimple.d.ts.map +1 -1
- package/dist/runtime/bindings/applySimple.js +117 -46
- package/dist/runtime/bindings/applySimple.js.map +1 -1
- package/dist/runtime/bindings/applyStructural.d.ts +1 -1
- package/dist/runtime/bindings/applyStructural.d.ts.map +1 -1
- package/dist/runtime/bindings/applyStructural.js +140 -124
- package/dist/runtime/bindings/applyStructural.js.map +1 -1
- package/dist/runtime/bindings/helpers.d.ts +8 -1
- package/dist/runtime/bindings/helpers.d.ts.map +1 -1
- package/dist/runtime/bindings/helpers.js +36 -2
- package/dist/runtime/bindings/helpers.js.map +1 -1
- package/dist/runtime/bindings/index.d.ts +6 -6
- package/dist/runtime/bindings/index.d.ts.map +1 -1
- package/dist/runtime/bindings/index.js +5 -5
- package/dist/runtime/bindings/index.js.map +1 -1
- package/dist/runtime/bindings/types.d.ts +2 -0
- package/dist/runtime/bindings/types.d.ts.map +1 -1
- package/dist/runtime/bindings/types.js.map +1 -1
- package/dist/runtime/bootstrap.d.ts +1 -1
- package/dist/runtime/bootstrap.js +1 -1
- package/dist/runtime/bootstrap.js.map +1 -1
- package/dist/runtime/createContextProxy.js.map +1 -1
- package/dist/runtime/deferHelpers.d.ts +1 -1
- package/dist/runtime/deferHelpers.js.map +1 -1
- package/dist/runtime/html.js +7 -7
- package/dist/runtime/html.js.map +1 -1
- package/dist/runtime/index.d.ts +6 -6
- package/dist/runtime/index.js +6 -6
- package/dist/runtime/index.js.map +1 -1
- package/dist/runtime/lazy.js +1 -1
- package/dist/runtime/lazy.js.map +1 -1
- package/dist/runtime/module-cache.js.map +1 -1
- package/dist/runtime/onDestroy.d.ts +7 -0
- package/dist/runtime/onDestroy.d.ts.map +1 -1
- package/dist/runtime/onDestroy.js +16 -1
- package/dist/runtime/onDestroy.js.map +1 -1
- package/dist/runtime/parseIdentifiersFromTemplate.js +1 -1
- package/dist/runtime/parseIdentifiersFromTemplate.js.map +1 -1
- package/dist/runtime/resource/createAction.d.ts +1 -1
- package/dist/runtime/resource/createAction.js +1 -1
- package/dist/runtime/resource/createAction.js.map +1 -1
- package/dist/runtime/resource/createResource.d.ts +1 -1
- package/dist/runtime/resource/createResource.d.ts.map +1 -1
- package/dist/runtime/resource/createResource.js +3 -4
- package/dist/runtime/resource/createResource.js.map +1 -1
- package/dist/runtime/resource/engine.d.ts +1 -1
- package/dist/runtime/resource/engine.js.map +1 -1
- package/dist/runtime/resource/index.d.ts +3 -3
- package/dist/runtime/resource/index.js +2 -2
- package/dist/runtime/resource/index.js.map +1 -1
- package/dist/runtime/resource/types.js.map +1 -1
- package/dist/types/globals.d.ts +10 -2
- package/dist/types/globals.d.ts.map +1 -1
- package/dist/types/globals.js.map +1 -1
- package/dist/types/index.js.map +1 -1
- package/dist/utilities/index.d.ts +2 -2
- package/dist/utilities/index.js +2 -2
- package/dist/utilities/index.js.map +1 -1
- package/dist/utilities/ref-signal.utility.d.ts +1 -1
- package/dist/utilities/ref-signal.utility.js.map +1 -1
- package/dist/utilities/ref.untility.d.ts +1 -1
- package/dist/utilities/ref.untility.js.map +1 -1
- package/package.json +11 -6
|
@@ -5,20 +5,16 @@
|
|
|
5
5
|
"category": "directive"
|
|
6
6
|
}
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import { bindForLoops } from './forLoops/bindForLoops';
|
|
14
|
-
import { bindSwitchCases } from './bindSwitchCases';
|
|
15
|
-
import { pithyTreeToDom } from './pithyTreeToDom';
|
|
16
|
-
import { isSSRMode } from './ssrMode';
|
|
8
|
+
import { effect } from '@pithyjs/signals';
|
|
9
|
+
import { compileExpression } from './compileExpression.js';
|
|
10
|
+
import { blockDisposer } from './disposeOrder.js';
|
|
11
|
+
import { clearBranch, renderBranch } from './renderBranch.js';
|
|
12
|
+
import { isSSRMode } from './ssrMode.js';
|
|
17
13
|
/**
|
|
18
14
|
* Handles rendering and reactivity for an @if/@else-if/@else chain.
|
|
19
15
|
* Returns a Set of all node IDs that were processed (to avoid double-binding).
|
|
20
16
|
*/
|
|
21
|
-
/** @codexApi {"parent":"pithy.directive.conditional-chain","name":"handleConditionalChain","stability":"stable","signature":"(domChain:
|
|
17
|
+
/** @codexApi {"parent":"pithy.directive.conditional-chain","name":"handleConditionalChain","stability":"stable","signature":"(domChain: Element[], astChain: PithyNode[], ctx: Record<string, unknown>, processedNodeIds?: Set<string>, disposers?: (() => void)[]) => Set<string>"} */
|
|
22
18
|
export function handleConditionalChain(domChain, astChain, ctx, processedNodeIds = new Set(), disposers = []) {
|
|
23
19
|
// Generate a unique ID for this chain for debugging
|
|
24
20
|
const chainId = Math.random().toString(36).substring(2, 8);
|
|
@@ -41,27 +37,11 @@ export function handleConditionalChain(domChain, astChain, ctx, processedNodeIds
|
|
|
41
37
|
// Instead, we rebuild from the AST when needed using pithyTreeToDom.
|
|
42
38
|
// Note: We mark nodes as processed AFTER binding each matched branch,
|
|
43
39
|
// not upfront, so that they can be properly bound first.
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
// Dispose all effects from the previous render
|
|
50
|
-
for (const dispose of currentDisposers) {
|
|
51
|
-
try {
|
|
52
|
-
dispose();
|
|
53
|
-
}
|
|
54
|
-
catch {
|
|
55
|
-
// Ignore disposal errors
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
currentDisposers = [];
|
|
59
|
-
if (currentElement && currentElement.parentNode) {
|
|
60
|
-
currentElement.remove();
|
|
61
|
-
currentElement = null;
|
|
62
|
-
return true;
|
|
63
|
-
}
|
|
64
|
-
return false;
|
|
40
|
+
let current = null;
|
|
41
|
+
function clearCurrentBranch() {
|
|
42
|
+
if (current)
|
|
43
|
+
clearBranch(current);
|
|
44
|
+
current = null;
|
|
65
45
|
}
|
|
66
46
|
// Shared logic for evaluating conditions and rendering the matched branch
|
|
67
47
|
function evaluateAndRender() {
|
|
@@ -95,62 +75,18 @@ export function handleConditionalChain(domChain, astChain, ctx, processedNodeIds
|
|
|
95
75
|
return matchIndex;
|
|
96
76
|
}
|
|
97
77
|
function renderMatchedBranch(matchIndex) {
|
|
98
|
-
// If we found a match, insert the corresponding element
|
|
99
78
|
if (matchIndex < 0 || !placeholder.parentNode)
|
|
100
|
-
return;
|
|
101
|
-
|
|
102
|
-
const matchedAst = astChain[matchIndex];
|
|
103
|
-
// Rebuild the DOM from the AST instead of cloning
|
|
104
|
-
// This ensures structural transformations (e.g., @for -> Comment) are applied
|
|
105
|
-
const freshFragment = pithyTreeToDom([matchedAst], ctx);
|
|
106
|
-
// Extract the first (and should be only) element from the fragment
|
|
107
|
-
const rebuiltElement = freshFragment.firstChild;
|
|
108
|
-
if (!rebuiltElement)
|
|
109
|
-
return;
|
|
110
|
-
// Update our current element reference (set before binding so cleanup can find it)
|
|
111
|
-
currentElement = rebuiltElement;
|
|
112
|
-
// Bind structural directives FIRST (in the same order as compileTemplate)
|
|
113
|
-
// This creates the necessary contexts (e.g., loop item variables) before
|
|
114
|
-
// bindTemplateDirectives tries to use them in text interpolations
|
|
115
|
-
// Store disposers in the outer scope so they can be cleaned up on re-render
|
|
116
|
-
currentDisposers = [];
|
|
117
|
-
// Use a fresh set for this effect run to prevent double-binding within this render
|
|
118
|
-
// But also update the outer processedNodeIds for the caller
|
|
119
|
-
const effectLocalProcessedIds = new Set();
|
|
120
|
-
createRoot(disposeRoot => {
|
|
121
|
-
currentDisposers.push(disposeRoot);
|
|
122
|
-
// 1) Switch/case statements FIRST
|
|
123
|
-
// IMPORTANT: bindSwitchCases must run BEFORE bindConditionals!
|
|
124
|
-
// Switch cases clone their content from DOM - if bindConditionals runs first,
|
|
125
|
-
// it removes @if/@else elements and replaces them with placeholders, so the
|
|
126
|
-
// cloned switch case content would be missing the conditional elements.
|
|
127
|
-
bindSwitchCases(matchedAst.children || [], rebuiltElement, ctx, new Set(), currentDisposers);
|
|
128
|
-
// 2) Nested conditionals
|
|
129
|
-
// Pass currentDisposers so nested conditional effects are cleaned up on re-render
|
|
130
|
-
bindConditionals(matchedAst.children || [], rebuiltElement, ctx, effectLocalProcessedIds, currentDisposers);
|
|
131
|
-
// 3) For loops - creates loop contexts with item/index variables
|
|
132
|
-
// Pass local processed IDs to prevent double-binding within this render
|
|
133
|
-
bindForLoops(matchedAst.children || [], rebuiltElement, ctx, currentDisposers, effectLocalProcessedIds);
|
|
134
|
-
// 4) Now bind non-structural directives (text, events, classes, etc.)
|
|
135
|
-
// Use a temporary fragment for binding (bindTemplateDirectives expects a fragment)
|
|
136
|
-
const tempFragment = document.createDocumentFragment();
|
|
137
|
-
tempFragment.appendChild(rebuiltElement);
|
|
138
|
-
bindTemplateDirectives(tempFragment, ctx, currentDisposers);
|
|
139
|
-
});
|
|
140
|
-
// Insert the element into the DOM after binding
|
|
141
|
-
placeholder.parentNode.insertBefore(rebuiltElement, placeholder.nextSibling);
|
|
142
|
-
// Defer flashUpdate via queueMicrotask to prevent blocking
|
|
143
|
-
if (!isSSRMode()) {
|
|
144
|
-
queueMicrotask(() => flashUpdate(rebuiltElement));
|
|
145
|
-
}
|
|
79
|
+
return null;
|
|
80
|
+
return renderBranch(astChain[matchIndex], ctx, placeholder);
|
|
146
81
|
}
|
|
147
82
|
if (isSSRMode()) {
|
|
148
83
|
// SSR mode: evaluate once synchronously, no reactive effects
|
|
149
84
|
const matchIndex = evaluateAndRender();
|
|
150
|
-
renderMatchedBranch(matchIndex);
|
|
85
|
+
const branch = renderMatchedBranch(matchIndex);
|
|
151
86
|
// Ensure nested directive disposers created during SSR are added
|
|
152
87
|
// to the parent disposers array for proper cleanup on destroy()
|
|
153
|
-
|
|
88
|
+
if (branch)
|
|
89
|
+
disposers.push(...branch.disposers);
|
|
154
90
|
}
|
|
155
91
|
else {
|
|
156
92
|
// Client mode: reactive effect for dynamic updates
|
|
@@ -165,19 +101,17 @@ export function handleConditionalChain(domChain, astChain, ctx, processedNodeIds
|
|
|
165
101
|
}
|
|
166
102
|
// Update the previous match index
|
|
167
103
|
previousMatchIndex = matchIndex;
|
|
168
|
-
// Now clear the current
|
|
169
|
-
|
|
170
|
-
renderMatchedBranch(matchIndex);
|
|
104
|
+
// Now clear the current branch (only if we're about to rebuild)
|
|
105
|
+
clearCurrentBranch();
|
|
106
|
+
current = renderMatchedBranch(matchIndex);
|
|
171
107
|
// Don't return cleanup here - we handle cleanup explicitly:
|
|
172
|
-
// 1. When match changes:
|
|
173
|
-
// 2. When effect stops:
|
|
108
|
+
// 1. When match changes: clearCurrentBranch() is called above
|
|
109
|
+
// 2. When effect stops: clearCurrentBranch is in disposers array
|
|
174
110
|
// Returning cleanup would cause it to run on EVERY re-run, even when match is same
|
|
175
111
|
});
|
|
176
|
-
//
|
|
177
|
-
// Note: We add clearCurrentElement explicitly because the effect may have
|
|
112
|
+
// Note: We add clearCurrentBranch explicitly because the effect may have
|
|
178
113
|
// returned undefined on its last run (when match didn't change)
|
|
179
|
-
disposers.push(
|
|
180
|
-
disposers.push(stopEffect);
|
|
114
|
+
disposers.push(blockDisposer(stopEffect, clearCurrentBranch, placeholder));
|
|
181
115
|
}
|
|
182
116
|
return processedNodeIds;
|
|
183
117
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleConditionalChain.js","sourceRoot":"","sources":["../../src/compile/handleConditionalChain.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"handleConditionalChain.js","sourceRoot":"","sources":["../../src/compile/handleConditionalChain.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAExD,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,YAAY,EAAe,MAAM,gBAAgB,CAAC;AACxE,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC;;;GAGG;AACH,wRAAwR;AACxR,MAAM,UAAU,sBAAsB,CACpC,QAAmB,EACnB,QAAqB,EACrB,GAA4B,EAC5B,mBAAgC,IAAI,GAAG,EAAE,EACzC,YAA4B,EAAE;IAE9B,oDAAoD;IACpD,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE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wE;QACxE,MAAM,UAAU,GAAG,MAAM,CAAC,GAAG,EAAE;YAC7B,MAAM,UAAU,GAAG,iBAAiB,EAAE,CAAC;YAEvC,4CAA4C;YAC5C,IAAI,UAAU,KAAK,kBAAkB,EAAE,CAAC;gBACtC,OAAO;YACT,CAAC;YAED,kCAAkC;YAClC,kBAAkB,GAAG,UAAU,CAAC;YAEhC,gEAAgE;YAChE,kBAAkB,EAAE,CAAC;YACrB,OAAO,GAAG,mBAAmB,CAAC,UAAU,CAAC,CAAC;YAE1C,4DAA4D;YAC5D,8DAA8D;YAC9D,iEAAiE;YACjE,mFAAmF;QACrF,CAAC,CAAC,CAAC;QAEH,yEAAyE;QACzE,gEAAgE;QAChE,SAAS,CAAC,IAAI,CAAC,aAAa,CAAC,UAAU,EAAE,kBAAkB,EAAE,WAAW,CAAC,CAAC,CAAC;IAC7E,CAAC;IAED,OAAO,gBAAgB,CAAC;AAC1B,CAAC","sourcesContent":["/** @codex\n{\n \"id\": \"pithy.directive.conditional-chain\",\n \"title\": \"Conditional Chain Handler\",\n \"category\": \"directive\"\n}\n*/\n\nimport { effect } from '@pithyjs/signals';\nimport { compileExpression } from './compileExpression';\nimport type { PithyNode } from '@pithyjs/compiler';\nimport { blockDisposer } from './disposeOrder';\nimport { clearBranch, renderBranch, type Branch } from './renderBranch';\nimport { isSSRMode } from './ssrMode';\n\n/**\n * Handles rendering and reactivity for an @if/@else-if/@else chain.\n * Returns a Set of all node IDs that were processed (to avoid double-binding).\n */\n/** @codexApi {\"parent\":\"pithy.directive.conditional-chain\",\"name\":\"handleConditionalChain\",\"stability\":\"stable\",\"signature\":\"(domChain: Element[], astChain: PithyNode[], ctx: Record<string, unknown>, processedNodeIds?: Set<string>, disposers?: (() => void)[]) => Set<string>\"} */\nexport function handleConditionalChain(\n domChain: Element[],\n astChain: PithyNode[],\n ctx: Record<string, unknown>,\n processedNodeIds: Set<string> = new Set(),\n disposers: (() => void)[] = []\n): Set<string> {\n // Generate a unique ID for this chain for debugging\n const chainId = Math.random().toString(36).substring(2, 8);\n\n // Create a placeholder comment to mark where our conditional content should go\n const placeholder = document.createComment(`conditional-chain-${chainId}`);\n\n // Insert the placeholder before the first element in the chain\n const firstElement = domChain[0];\n const parent = firstElement.parentNode;\n\n if (!parent) {\n console.error(`[Chain-${chainId}] No parent found for first element`);\n return processedNodeIds;\n }\n\n parent.insertBefore(placeholder, firstElement);\n\n // Remove all original elements from the DOM immediately\n for (const el of domChain) {\n el.remove();\n }\n\n // Note: We don't clone the original DOM elements because cloning loses\n // the structural transformations (e.g., @for loops need Comment anchors).\n // Instead, we rebuild from the AST when needed using pithyTreeToDom.\n\n // Note: We mark nodes as processed AFTER binding each matched branch,\n // not upfront, so that they can be properly bound first.\n\n let current: Branch | null = null;\n\n function clearCurrentBranch() {\n if (current) clearBranch(current);\n current = null;\n }\n\n // Shared logic for evaluating conditions and rendering the matched branch\n function evaluateAndRender() {\n // Find the first matching condition\n let matchIndex = -1;\n\n for (let i = 0; i < astChain.length; i++) {\n const node = astChain[i];\n\n if (node.directives?.if !== undefined) {\n // This is an @if node\n const fn = compileExpression(node.directives.if, ctx, false);\n const result = fn();\n if (result) {\n matchIndex = i;\n break;\n }\n } else if (node.directives?.elseIf !== undefined) {\n // This is an @else-if node\n const fn = compileExpression(node.directives.elseIf, ctx, false);\n if (fn()) {\n matchIndex = i;\n break;\n }\n } else if (node.directives?.else !== undefined) {\n // This is an @else node, which always matches if we reach it\n matchIndex = i;\n break;\n }\n }\n\n return matchIndex;\n }\n\n function renderMatchedBranch(matchIndex: number): Branch | null {\n if (matchIndex < 0 || !placeholder.parentNode) return null;\n return renderBranch(astChain[matchIndex], ctx, placeholder);\n }\n\n if (isSSRMode()) {\n // SSR mode: evaluate once synchronously, no reactive effects\n const matchIndex = evaluateAndRender();\n const branch = renderMatchedBranch(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 for disposal\n const stopEffect = effect(() => {\n const matchIndex = evaluateAndRender();\n\n // Skip re-insertion if match hasn't changed\n if (matchIndex === previousMatchIndex) {\n return;\n }\n\n // Update the previous match index\n previousMatchIndex = matchIndex;\n\n // Now clear the current branch (only if we're about to rebuild)\n clearCurrentBranch();\n current = renderMatchedBranch(matchIndex);\n\n // Don't return cleanup here - we handle cleanup explicitly:\n // 1. When match changes: clearCurrentBranch() is called above\n // 2. When effect stops: clearCurrentBranch is in disposers array\n // Returning cleanup would cause it to run on EVERY re-run, even when match is same\n });\n\n // Note: We add clearCurrentBranch explicitly because the effect may have\n // returned undefined on its last run (when match didn't change)\n disposers.push(blockDisposer(stopEffect, clearCurrentBranch, placeholder));\n }\n\n return processedNodeIds;\n}\n"]}
|
package/dist/compile/index.d.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
export { compileTemplate } from './compileTemplate';
|
|
2
|
-
export { compileNodes } from './compileNodes';
|
|
3
|
-
export { renderChildren, captureChildren, readChildPayload, type ChildPayload, } from './renderChildren';
|
|
4
|
-
export { injectScopedStyle } from './injectScopedStyle';
|
|
5
|
-
export { bindTextInterpolations } from './binds/bindTextInterpolations';
|
|
6
|
-
export { bindEventHandlers } from './binds/bindEventHandlers';
|
|
7
|
-
export { bindClassDirectives } from './binds/bindClassDirectives';
|
|
8
|
-
export { bindComponentTags } from './binds/bindComponentsTags';
|
|
9
|
-
export { bindForLoops, createLoopManager, type ChildFactory, type LoopManager, } from './forLoops';
|
|
10
|
-
export { bindTemplateDirectives } from './compileTemplate';
|
|
11
|
-
export type { SlotMap } from './contentProjection';
|
|
12
|
-
export { isSSRMode } from './ssrMode';
|
|
13
|
-
export { registerDirectiveBinder, unregisterDirectiveBinder, executeCustomDirectiveBinders, type DirectiveBinder, } from './binds/directiveRegistry';
|
|
14
|
-
export { boundaryForElement, setBoundaryError, clearBoundaryError, registerBoundary, } from './binds/bindErrorBoundary';
|
|
15
|
-
export type { BoundaryInfo } from './binds/bindErrorBoundary';
|
|
16
|
-
export { collectUnhandledSSRRenderCrashes, resetSSRRenderCrashes, } from './ssrCrashes';
|
|
17
|
-
export type { SSRRenderCrash } from './ssrCrashes';
|
|
1
|
+
export { compileTemplate } from './compileTemplate.js';
|
|
2
|
+
export { compileNodes } from './compileNodes.js';
|
|
3
|
+
export { renderChildren, captureChildren, readChildPayload, type ChildPayload, } from './renderChildren.js';
|
|
4
|
+
export { injectScopedStyle } from './injectScopedStyle.js';
|
|
5
|
+
export { bindTextInterpolations } from './binds/bindTextInterpolations.js';
|
|
6
|
+
export { bindEventHandlers } from './binds/bindEventHandlers.js';
|
|
7
|
+
export { bindClassDirectives } from './binds/bindClassDirectives.js';
|
|
8
|
+
export { bindComponentTags } from './binds/bindComponentsTags.js';
|
|
9
|
+
export { bindForLoops, createLoopManager, type ChildFactory, type LoopManager, } from './forLoops/index.js';
|
|
10
|
+
export { bindTemplateDirectives } from './compileTemplate.js';
|
|
11
|
+
export type { SlotMap } from './contentProjection.js';
|
|
12
|
+
export { isSSRMode } from './ssrMode.js';
|
|
13
|
+
export { registerDirectiveBinder, unregisterDirectiveBinder, executeCustomDirectiveBinders, type DirectiveBinder, } from './binds/directiveRegistry.js';
|
|
14
|
+
export { boundaryForElement, setBoundaryError, clearBoundaryError, registerBoundary, } from './binds/bindErrorBoundary.js';
|
|
15
|
+
export type { BoundaryInfo } from './binds/bindErrorBoundary.js';
|
|
16
|
+
export { collectUnhandledSSRRenderCrashes, resetSSRRenderCrashes, } from './ssrCrashes.js';
|
|
17
|
+
export type { SSRRenderCrash } from './ssrCrashes.js';
|
|
18
18
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/compile/index.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
export { compileTemplate } from './compileTemplate';
|
|
2
|
-
export { compileNodes } from './compileNodes';
|
|
3
|
-
export { renderChildren, captureChildren, readChildPayload, } from './renderChildren';
|
|
1
|
+
export { compileTemplate } from './compileTemplate.js';
|
|
2
|
+
export { compileNodes } from './compileNodes.js';
|
|
3
|
+
export { renderChildren, captureChildren, readChildPayload, } from './renderChildren.js';
|
|
4
4
|
// parseHtml is NOT re-exported from this barrel to prevent parse5 from
|
|
5
5
|
// being pulled into production bundles. Use the dedicated subpath export:
|
|
6
6
|
// import { loadJITCompiler } from '@pithyjs/core/compile/parseHtml'
|
|
7
|
-
export { injectScopedStyle } from './injectScopedStyle';
|
|
7
|
+
export { injectScopedStyle } from './injectScopedStyle.js';
|
|
8
8
|
// Only export these if useful for advanced users / SSR:
|
|
9
|
-
export { bindTextInterpolations } from './binds/bindTextInterpolations';
|
|
10
|
-
export { bindEventHandlers } from './binds/bindEventHandlers';
|
|
11
|
-
export { bindClassDirectives } from './binds/bindClassDirectives';
|
|
12
|
-
export { bindComponentTags } from './binds/bindComponentsTags';
|
|
13
|
-
export { bindForLoops, createLoopManager, } from './forLoops';
|
|
14
|
-
export { bindTemplateDirectives } from './compileTemplate';
|
|
9
|
+
export { bindTextInterpolations } from './binds/bindTextInterpolations.js';
|
|
10
|
+
export { bindEventHandlers } from './binds/bindEventHandlers.js';
|
|
11
|
+
export { bindClassDirectives } from './binds/bindClassDirectives.js';
|
|
12
|
+
export { bindComponentTags } from './binds/bindComponentsTags.js';
|
|
13
|
+
export { bindForLoops, createLoopManager, } from './forLoops/index.js';
|
|
14
|
+
export { bindTemplateDirectives } from './compileTemplate.js';
|
|
15
15
|
// The explicit static-render signal. `@pithyjs/ssr`'s setupEnvironment() copies
|
|
16
16
|
// a happy-dom window onto globalThis — IntersectionObserver, requestAnimationFrame
|
|
17
17
|
// and matchMedia included — so no `typeof` feature check can distinguish a server
|
|
18
18
|
// render from a browser. Packages that must branch on it (e.g. @pithyjs/ui-kit's
|
|
19
19
|
// section entrance animations, BUG-010) read this rather than hand-rolling their
|
|
20
20
|
// own derivation of the underlying `__PITHY_SSR__` global.
|
|
21
|
-
export { isSSRMode } from './ssrMode';
|
|
21
|
+
export { isSSRMode } from './ssrMode.js';
|
|
22
22
|
// Custom directive registry - allows external packages (router, etc.) to register binders
|
|
23
|
-
export { registerDirectiveBinder, unregisterDirectiveBinder, executeCustomDirectiveBinders, } from './binds/directiveRegistry';
|
|
23
|
+
export { registerDirectiveBinder, unregisterDirectiveBinder, executeCustomDirectiveBinders, } from './binds/directiveRegistry.js';
|
|
24
24
|
// Error boundary functions for router integration:
|
|
25
|
-
export { boundaryForElement, setBoundaryError, clearBoundaryError, registerBoundary, } from './binds/bindErrorBoundary';
|
|
25
|
+
export { boundaryForElement, setBoundaryError, clearBoundaryError, registerBoundary, } from './binds/bindErrorBoundary.js';
|
|
26
26
|
// Unhandled component crashes recorded during a server render. Consumed by
|
|
27
27
|
// @pithyjs/ssr's renderToString so a crash fails its route (BUG-018).
|
|
28
28
|
//
|
|
@@ -31,5 +31,5 @@ export { boundaryForElement, setBoundaryError, clearBoundaryError, registerBound
|
|
|
31
31
|
// binders inside this package, so they are annotated `internal` rather than
|
|
32
32
|
// exported here — advertising them as stable in the generated API table while
|
|
33
33
|
// leaving them unimportable is worse than either alternative.
|
|
34
|
-
export { collectUnhandledSSRRenderCrashes, resetSSRRenderCrashes, } from './ssrCrashes';
|
|
34
|
+
export { collectUnhandledSSRRenderCrashes, resetSSRRenderCrashes, } from './ssrCrashes.js';
|
|
35
35
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/compile/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EACL,cAAc,EACd,eAAe,EACf,gBAAgB,GAEjB,MAAM,kBAAkB,CAAC;AAC1B,uEAAuE;AACvE,0EAA0E;AAC1E,sEAAsE;AACtE,O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AAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAExD,wDAAwD;AACxD,OAAO,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AACxE,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAC/D,OAAO,EACL,YAAY,EACZ,iBAAiB,GAGlB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAK3D,gFAAgF;AAChF,mFAAmF;AACnF,kFAAkF;AAClF,iFAAiF;AACjF,iFAAiF;AACjF,2DAA2D;AAC3D,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,0FAA0F;AAC1F,OAAO,EACL,uBAAuB,EACvB,yBAAyB,EACzB,6BAA6B,GAE9B,MAAM,2BAA2B,CAAC;AAEnC,mDAAmD;AACnD,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,2BAA2B,CAAC;AAGnC,2EAA2E;AAC3E,sEAAsE;AACtE,EAAE;AACF,0EAA0E;AAC1E,8EAA8E;AAC9E,4EAA4E;AAC5E,8EAA8E;AAC9E,8DAA8D;AAC9D,OAAO,EACL,gCAAgC,EAChC,qBAAqB,GACtB,MAAM,cAAc,CAAC","sourcesContent":["export { compileTemplate } from './compileTemplate';\nexport { compileNodes } from './compileNodes';\nexport {\n renderChildren,\n captureChildren,\n readChildPayload,\n type ChildPayload,\n} from './renderChildren';\n// parseHtml is NOT re-exported from this barrel to prevent parse5 from\n// being pulled into production bundles. Use the dedicated subpath export:\n// import { loadJITCompiler } from '@pithyjs/core/compile/parseHtml'\nexport { injectScopedStyle } from './injectScopedStyle';\n\n// Only export these if useful for advanced users / SSR:\nexport { bindTextInterpolations } from './binds/bindTextInterpolations';\nexport { bindEventHandlers } from './binds/bindEventHandlers';\nexport { bindClassDirectives } from './binds/bindClassDirectives';\nexport { bindComponentTags } from './binds/bindComponentsTags';\nexport {\n bindForLoops,\n createLoopManager,\n type ChildFactory,\n type LoopManager,\n} from './forLoops';\nexport { bindTemplateDirectives } from './compileTemplate';\n\n// Content projection types (stable — part of the component authoring API)\nexport type { SlotMap } from './contentProjection';\n\n// The explicit static-render signal. `@pithyjs/ssr`'s setupEnvironment() copies\n// a happy-dom window onto globalThis — IntersectionObserver, requestAnimationFrame\n// and matchMedia included — so no `typeof` feature check can distinguish a server\n// render from a browser. Packages that must branch on it (e.g. @pithyjs/ui-kit's\n// section entrance animations, BUG-010) read this rather than hand-rolling their\n// own derivation of the underlying `__PITHY_SSR__` global.\nexport { isSSRMode } from './ssrMode';\n\n// Custom directive registry - allows external packages (router, etc.) to register binders\nexport {\n registerDirectiveBinder,\n unregisterDirectiveBinder,\n executeCustomDirectiveBinders,\n type DirectiveBinder,\n} from './binds/directiveRegistry';\n\n// Error boundary functions for router integration:\nexport {\n boundaryForElement,\n setBoundaryError,\n clearBoundaryError,\n registerBoundary,\n} from './binds/bindErrorBoundary';\nexport type { BoundaryInfo } from './binds/bindErrorBoundary';\n\n// Unhandled component crashes recorded during a server render. Consumed by\n// @pithyjs/ssr's renderToString so a crash fails its route (BUG-018).\n//\n// Only the two read/lifecycle APIs are public. `recordSSRRenderCrash` and\n// `clearSSRRenderCrash` mutate the registry and are called exclusively by the\n// binders inside this package, so they are annotated `internal` rather than\n// exported here — advertising them as stable in the generated API table while\n// leaving them unimportable is worse than either alternative.\nexport {\n collectUnhandledSSRRenderCrashes,\n resetSSRRenderCrashes,\n} from './ssrCrashes';\nexport type { SSRRenderCrash } from './ssrCrashes';\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectScopedStyle.js","sourceRoot":"","sources":["../../src/compile/injectScopedStyle.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF;;GAEG;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AC5D,MAAM,SAAS,GAAG,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IAEzC,IAAI,cAAc,CAAC,GAAG,CAAC,SAAS,CAAC;QAAE,OAAO,CAAC,+BAA+B;IAC1E,cAAc,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAE9B,MAAM,KAAK,GAAG,QAAQ,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IAC9C,KAAK,CAAC,WAAW,GAAG,SAAS,CAAC;IAC9B,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;AACnC,CAAC;AAED;;;;;;;;;;;GAWG;AACH,wJAAwJ;AACxJ,SAAS,QAAQ,CAAC,GAAW,EAAE,OAAe;IAC5C,OAAO,GAAG,CAAC,OAAO,CAAC,yBAAyB,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE,QAAQ,EAAE,EAAE;QACjE,MAAM,MAAM,GAAG,QAAQ;aACpB,KAAK,CAAC,GAAG,CAAC;aACV,GAAG,CAAC,CAAC,CAAS,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,gBAAgB,OAAO,IAAI,CAAC;aAC1D,IAAI,CAAC,IAAI,CAAC,CAAC;QACd,OAAO,GAAG,GAAG,IAAI,MAAM,IAAI,CAAC;IAC9B,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["/** @codex\n{\n \"id\": \"pithy.compiler.scoped-styles\",\n \"title\": \"Scoped CSS Style Injector\",\n \"category\": \"compiler\"\n}\n*/\n\n/**\n * A Set to track injected styles and avoid duplicate <style> tags.\n */\nconst injectedStyles = new Set<string>();\n\n/**\n * Rewrites CSS selectors to scope them with a unique attribute (e.g., `[data-scope-xxxx]`) and injects them into the DOM.\n *\n * @param css - Raw CSS string from the component\n * @param scopeId - Unique scope ID used to prefix selectors (e.g., `pithy-abc123`)\n */\n/** @codexApi {\"parent\":\"pithy.compiler.scoped-styles\",\"name\":\"injectScopedStyle\",\"stability\":\"stable\",\"signature\":\"(css: string, scopeId: string) => void\"} */\nexport function injectScopedStyle(css: string, scopeId: string): void {\n const scopedCss = scopeCss(css, scopeId);\n\n if (injectedStyles.has(scopedCss)) return; // Avoid reinjecting same style\n injectedStyles.add(scopedCss);\n\n const style = document.createElement('style');\n style.textContent = scopedCss;\n document.head.appendChild(style);\n}\n\n/**\n * Prefixes all top-level CSS selectors with a scoped attribute selector.\n * This ensures styles apply only to elements within the component.\n *\n * Example:\n * Input: \"p { color: red; }\", scopeId = \"abc123\"\n * Output: \"p[data-scope=\"abc123\"] { color: red; }\"\n *\n * @param css - Raw CSS string\n * @param scopeId - Unique ID used as a data-scope attribute value\n * @returns Scoped CSS string\n */\n/** @codexApi {\"parent\":\"pithy.compiler.scoped-styles\",\"name\":\"scopeCss\",\"stability\":\"stable\",\"signature\":\"(css: string, scopeId: string) => string\"} */\nfunction scopeCss(css: string, scopeId: string): string {\n return css.replace(/(^|\\})\\s*([^{}]+)\\s*\\{/g, (_, sep, selector) => {\n const scoped = selector\n .split(',')\n .map((s: string) => `${s.trim()}[data-scope=\"${scopeId}\"]`)\n .join(', ');\n return `${sep} ${scoped} {`;\n });\n}\n"]}
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import type { PithyNode } from '@pithyjs/compiler';
|
|
2
|
+
export interface StyleOwners {
|
|
3
|
+
strings: string[];
|
|
4
|
+
props: Map<string, [value: string, priority: string]>;
|
|
5
|
+
}
|
|
2
6
|
export interface NodeExpando {
|
|
3
7
|
__pithyCtx?: Record<string, unknown>;
|
|
4
8
|
__pithyNode?: PithyNode;
|
|
@@ -7,6 +11,30 @@ export interface NodeExpando {
|
|
|
7
11
|
__fallbackFactory?: () => DocumentFragment;
|
|
8
12
|
__nested?: boolean;
|
|
9
13
|
__pithySkipText?: boolean;
|
|
14
|
+
__pithyComponentDom?: boolean;
|
|
15
|
+
/** A build's component placeholder: renders it through the AOT binder. */
|
|
16
|
+
/** Renders a build's placeholder; returns the nodes that replaced it. */
|
|
17
|
+
__pithyAotMount?: (disposers: (() => void)[]) => Node[];
|
|
18
|
+
__styleProps?: string[];
|
|
19
|
+
__styleOwners?: StyleOwners;
|
|
20
|
+
__selectValue?: {
|
|
21
|
+
text: string;
|
|
22
|
+
picks: Set<string> | null;
|
|
23
|
+
};
|
|
24
|
+
__classToggles?: string[];
|
|
25
|
+
__forwardedDirectives?: Map<string, string>;
|
|
26
|
+
/** A component root's forwarded bindings evaluate in the tag's template. */
|
|
27
|
+
__pithyForwardCtx?: Record<string, unknown>;
|
|
28
|
+
/** The attributes the tag that set `__pithyForwardCtx` forwarded. */
|
|
29
|
+
__pithyForwardAttrs?: Set<string>;
|
|
30
|
+
/** Identifies that forwarding, so two tags in one scope stay two bindings. */
|
|
31
|
+
__pithyForwardId?: number;
|
|
32
|
+
/** The boundary element a slot rendered this node for, wherever a portal moves it. */
|
|
33
|
+
__pithySlotOwner?: Element;
|
|
10
34
|
}
|
|
35
|
+
/** Records that `ctx`'s tag forwarded `names` onto `root`. */
|
|
36
|
+
export declare function markForwardTarget(root: Element, ctx: Record<string, unknown>, names: Set<string>): void;
|
|
37
|
+
/** Keys a forwarded event by the forwarding that put it on `el`. */
|
|
38
|
+
export declare function forwardEventKey(el: Element, key: string): string;
|
|
11
39
|
export declare function nodeExpando<T extends Node>(node: T): T & NodeExpando;
|
|
12
40
|
//# sourceMappingURL=nodeExpando.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nodeExpando.d.ts","sourceRoot":"","sources":["../../src/compile/nodeExpando.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAEnD,MAAM,WAAW,WAAW;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrC,WAAW,CAAC,EAAE,SAAS,CAAC;IACxB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CA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,EAAE,MAAM,CAAC,CAAC;IAC5C,4EAA4E;IAC5E,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC5C,qEAAqE;IACrE,mBAAmB,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IAClC,8EAA8E;IAC9E,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,sFAAsF;IACtF,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B;AAID,8DAA8D;AAC9D,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,OAAO,EACb,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,KAAK,EAAE,GAAG,CAAC,MAAM,CAAC,GACjB,IAAI,CAKN;AAED,oEAAoE;AACpE,wBAAgB,eAAe,CAAC,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAEhE;AAED,wBAAgB,WAAW,CAAC,CAAC,SAAS,IAAI,EAAE,IAAI,EAAE,CAAC,GAAG,CAAC,GAAG,WAAW,CAEpE"}
|
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
let nextForwardId = 0;
|
|
2
|
+
/** Records that `ctx`'s tag forwarded `names` onto `root`. */
|
|
3
|
+
export function markForwardTarget(root, ctx, names) {
|
|
4
|
+
const x = nodeExpando(root);
|
|
5
|
+
x.__pithyForwardCtx = ctx;
|
|
6
|
+
x.__pithyForwardAttrs = names;
|
|
7
|
+
x.__pithyForwardId = ++nextForwardId;
|
|
8
|
+
}
|
|
9
|
+
/** Keys a forwarded event by the forwarding that put it on `el`. */
|
|
10
|
+
export function forwardEventKey(el, key) {
|
|
11
|
+
return `${nodeExpando(el).__pithyForwardId}|${key}`;
|
|
12
|
+
}
|
|
1
13
|
export function nodeExpando(node) {
|
|
2
14
|
return node;
|
|
3
15
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nodeExpando.js","sourceRoot":"","sources":["../../src/compile/nodeExpando.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"nodeExpando.js","sourceRoot":"","sources":["../../src/compile/nodeExpando.ts"],"names":[],"mappings":"AAkCA,IAAI,aAAa,GAAG,CAAC,CAAC;AAEtB,8DAA8D;AAC9D,MAAM,UAAU,iBAAiB,CAC/B,IAAa,EACb,GAA4B,EAC5B,KAAkB;IAElB,MAAM,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IAC5B,CAAC,CAAC,iBAAiB,GAAG,GAAG,CAAC;IAC1B,CAAC,CAAC,mBAAmB,GAAG,KAAK,CAAC;IAC9B,CAAC,CAAC,gBAAgB,GAAG,EAAE,aAAa,CAAC;AACvC,CAAC;AAED,oEAAoE;AACpE,MAAM,UAAU,eAAe,CAAC,EAAW,EAAE,GAAW;IACtD,OAAO,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC,gBAAgB,IAAI,GAAG,EAAE,CAAC;AACtD,CAAC;AAED,MAAM,UAAU,WAAW,CAAiB,IAAO;IACjD,OAAO,IAAuB,CAAC;AACjC,CAAC","sourcesContent":["import type { PithyNode } from '@pithyjs/compiler';\n\nexport interface StyleOwners {\n strings: string[];\n props: Map<string, [value: string, priority: string]>;\n}\n\nexport interface NodeExpando {\n __pithyCtx?: Record<string, unknown>;\n __pithyNode?: PithyNode;\n __nodeId?: string;\n __slotName?: string;\n __fallbackFactory?: () => DocumentFragment;\n __nested?: boolean;\n __pithySkipText?: boolean;\n __pithyComponentDom?: boolean;\n /** A build's component placeholder: renders it through the AOT binder. */\n /** Renders a build's placeholder; returns the nodes that replaced it. */\n __pithyAotMount?: (disposers: (() => void)[]) => Node[];\n __styleProps?: string[];\n __styleOwners?: StyleOwners;\n __selectValue?: { text: string; picks: Set<string> | null };\n __classToggles?: string[];\n __forwardedDirectives?: Map<string, string>;\n /** A component root's forwarded bindings evaluate in the tag's template. */\n __pithyForwardCtx?: Record<string, unknown>;\n /** The attributes the tag that set `__pithyForwardCtx` forwarded. */\n __pithyForwardAttrs?: Set<string>;\n /** Identifies that forwarding, so two tags in one scope stay two bindings. */\n __pithyForwardId?: number;\n /** The boundary element a slot rendered this node for, wherever a portal moves it. */\n __pithySlotOwner?: Element;\n}\n\nlet nextForwardId = 0;\n\n/** Records that `ctx`'s tag forwarded `names` onto `root`. */\nexport function markForwardTarget(\n root: Element,\n ctx: Record<string, unknown>,\n names: Set<string>\n): void {\n const x = nodeExpando(root);\n x.__pithyForwardCtx = ctx;\n x.__pithyForwardAttrs = names;\n x.__pithyForwardId = ++nextForwardId;\n}\n\n/** Keys a forwarded event by the forwarding that put it on `el`. */\nexport function forwardEventKey(el: Element, key: string): string {\n return `${nodeExpando(el).__pithyForwardId}|${key}`;\n}\n\nexport function nodeExpando<T extends Node>(node: T): T & NodeExpando {\n return node as T & NodeExpando;\n}\n"]}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
}
|
|
7
7
|
*/
|
|
8
8
|
import { parse5ToPithyTree, escapeComponentTags, extractOriginalTagNames, extractOriginalAttributeNames, } from '@pithyjs/compiler';
|
|
9
|
-
import { pithyTreeToDom } from './pithyTreeToDom';
|
|
9
|
+
import { pithyTreeToDom } from './pithyTreeToDom.js';
|
|
10
10
|
let _parseFragment = null;
|
|
11
11
|
let _loadPromise = null;
|
|
12
12
|
/**
|
|
@@ -122,14 +122,12 @@ export function parseHtml(html, scopeId, context, importedIdentifiers = new Set(
|
|
|
122
122
|
const tagNameCasingMap = extractOriginalTagNames(escapedHtml);
|
|
123
123
|
const attributeNameCasingMap = extractOriginalAttributeNames(escapedHtml);
|
|
124
124
|
const pithyNodes = parsed.childNodes
|
|
125
|
-
.map((n) => parse5ToPithyTree(n, importedIdentifiers, tagNameCasingMap, undefined, attributeNameCasingMap))
|
|
125
|
+
.map((n) => parse5ToPithyTree(n, importedIdentifiers, tagNameCasingMap, undefined, attributeNameCasingMap, undefined, escapedHtml))
|
|
126
126
|
.filter((n) => n != null);
|
|
127
127
|
// 3. Compile DOM tree from PithyNodes
|
|
128
128
|
const fragment = pithyTreeToDom(pithyNodes, context);
|
|
129
129
|
// 4. Add data-scope attribute to all elements
|
|
130
|
-
fragment
|
|
131
|
-
el.setAttribute(`data-scope-${scopeId}`, '');
|
|
132
|
-
});
|
|
130
|
+
addScope(fragment, scopeId);
|
|
133
131
|
return {
|
|
134
132
|
fragment,
|
|
135
133
|
pithyNodes,
|
|
@@ -152,7 +150,8 @@ function preprocessSelfClosingComponents(html, importedIdentifiers) {
|
|
|
152
150
|
// Matches: <ComponentName /> or <ComponentName/>
|
|
153
151
|
// IMPORTANT: Use 'g' flag only (NOT 'gi') - case-sensitive matching is required
|
|
154
152
|
// to avoid converting native elements like <input /> to <Input></Input>
|
|
155
|
-
|
|
153
|
+
// Quoted values may hold `>` (an arrow-function prop), so skip them whole.
|
|
154
|
+
const selfClosingRegex = new RegExp(`<${componentName}(\\s(?:[^>"']|"[^"]*"|'[^']*')*?)?\\s*\\/\\s*>`, 'g');
|
|
156
155
|
// Replace with regular opening and closing tags
|
|
157
156
|
processedHtml = processedHtml.replace(selfClosingRegex, (match, attributes) => {
|
|
158
157
|
const attrs = attributes || '';
|
|
@@ -161,4 +160,11 @@ function preprocessSelfClosingComponents(html, importedIdentifiers) {
|
|
|
161
160
|
}
|
|
162
161
|
return processedHtml;
|
|
163
162
|
}
|
|
163
|
+
function addScope(root, scopeId) {
|
|
164
|
+
root.querySelectorAll?.('*').forEach(el => {
|
|
165
|
+
el.setAttribute(`data-scope-${scopeId}`, '');
|
|
166
|
+
if (el.localName === 'template' && 'content' in el)
|
|
167
|
+
addScope(el.content, scopeId);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
164
170
|
//# sourceMappingURL=parseHtml.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parseHtml.js","sourceRoot":"","sources":["../../src/compile/parseHtml.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,u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A+B,CACtC,IAAY,EACZ,mBAAgC;IAEhC,IAAI,aAAa,GAAG,IAAI,CAAC;IAEzB,mEAAmE;IACnE,KAAK,MAAM,aAAa,IAAI,mBAAmB,EAAE,CAAC;QAChD,oDAAoD;QACpD,iDAAiD;QACjD,gFAAgF;QAChF,wEAAwE;QACxE,2EAA2E;QAC3E,MAAM,gBAAgB,GAAG,IAAI,MAAM,CACjC,IAAI,aAAa,gDAAgD,EACjE,GAAG,CACJ,CAAC;QAEF,gDAAgD;QAChD,aAAa,GAAG,aAAa,CAAC,OAAO,CACnC,gBAAgB,EAChB,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE;YACpB,MAAM,KAAK,GAAG,UAAU,IAAI,EAAE,CAAC;YAC/B,OAAO,IAAI,aAAa,GAAG,KAAK,MAAM,aAAa,GAAG,CAAC;QACzD,CAAC,CACF,CAAC;IACJ,CAAC;IAED,OAAO,aAAa,CAAC;AACvB,CAAC;AAED,SAAS,QAAQ,CAAC,IAAgB,EAAE,OAAe;IACjD,IAAI,CAAC,gBAAgB,EAAE,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE;QACxC,EAAE,CAAC,YAAY,CAAC,cAAc,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC;QAC7C,IAAI,EAAE,CAAC,SAAS,KAAK,UAAU,IAAI,SAAS,IAAI,EAAE;YAChD,QAAQ,CAAE,EAA0B,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC3D,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["/** @codex\n{\n \"id\": \"pithy.compiler.html-parser\",\n \"title\": \"HTML Template Parser\",\n \"category\": \"compiler\"\n}\n*/\n\nimport {\n parse5ToPithyTree,\n escapeComponentTags,\n extractOriginalTagNames,\n extractOriginalAttributeNames,\n type PithyNode,\n} from '@pithyjs/compiler';\nimport { pithyTreeToDom } from './pithyTreeToDom';\n\n// ─── Dynamic parse5 loading ──────────────────────────────────────────────────\n// parse5 is no longer statically imported. It is loaded on demand via\n// loadJITCompiler() and cached in this module-level variable.\n// This ensures parse5 (45.6 KB gzip) is tree-shaken from production bundles\n// when all templates are AOT-compiled.\n\n/** Structural type matching parse5's parseFragment — avoids leaking parse5 into .d.ts */\ntype ParseFragmentFn = (\n html: string,\n options?: { sourceCodeLocationInfo?: boolean }\n) => unknown;\n\nlet _parseFragment: ParseFragmentFn | null = null;\nlet _loadPromise: Promise<void> | null = null;\n\n/**\n * Dynamically import parse5 and cache the parseFragment function.\n * Must be called before using html() for runtime template compilation.\n *\n * If all templates are AOT-compiled, this function never needs to be called\n * and parse5 will be completely absent from the production bundle.\n *\n * @returns Promise that resolves when parse5 is ready\n */\n/** @codexApi {\"parent\":\"pithy.compiler.html-parser\",\"name\":\"loadJITCompiler\",\"stability\":\"stable\",\"signature\":\"() => Promise<void>\"} */\nexport function loadJITCompiler(): Promise<void> {\n if (_parseFragment) return Promise.resolve(); // Already loaded\n if (_loadPromise) return _loadPromise; // Loading in progress — reuse same promise\n\n _loadPromise = import('parse5')\n .then(mod => {\n _parseFragment = mod.parseFragment;\n _loadPromise = null;\n })\n .catch(err => {\n // Reset so a retry can be attempted (e.g., after installing parse5)\n _loadPromise = null;\n // Manual `.cause` assignment instead of `new Error(msg, { cause })`\n // because the project targets ES2020 which doesn't support Error cause.\n const loadErr = new Error(\n 'Failed to load parse5. Install it (`pnpm add parse5`) or enable AOT compilation to avoid the runtime compiler.'\n );\n (loadErr as Error & { cause?: unknown }).cause = err;\n throw loadErr;\n });\n\n return _loadPromise;\n}\n\n/**\n * Check if the JIT compiler (parse5) has been loaded.\n * Used internally by parseHtml to provide a clear error message.\n * @returns `true` if parse5 parseFragment is cached\n */\n/** @codexApi {\"parent\":\"pithy.compiler.html-parser\",\"name\":\"isJITLoaded\",\"stability\":\"internal\",\"signature\":\"() => boolean\"} */\nexport function isJITLoaded(): boolean {\n return _parseFragment !== null;\n}\n\n/**\n * Get the cached parseFragment function, throwing if not loaded.\n * @internal\n */\nfunction getParseFragment(): ParseFragmentFn {\n if (!_parseFragment) {\n throw new Error(\n 'Runtime template compiler not loaded. Enable AOT compilation (recommended) ' +\n 'or call loadJITCompiler() at startup.'\n );\n }\n return _parseFragment;\n}\n\n/**\n * Reset JIT compiler state. For testing only.\n * @internal\n */\n/** @codexApi {\"parent\":\"pithy.compiler.html-parser\",\"name\":\"resetJITState\",\"stability\":\"internal\",\"signature\":\"() => void\"} */\nexport function resetJITState(): void {\n _parseFragment = null;\n _loadPromise = null;\n}\n\n/**\n * Pre-load parse5 synchronously for environments where it's already available\n * (e.g., Node.js test environments, SSR). This is a convenience for the\n * transition period — production code should use loadJITCompiler().\n * @internal\n */\n/** @codexApi {\"parent\":\"pithy.compiler.html-parser\",\"name\":\"setParseFragment\",\"stability\":\"internal\",\"signature\":\"(fn: ParseFragmentFn) => void\"} */\nexport function setParseFragment(fn: ParseFragmentFn): void {\n _parseFragment = fn;\n}\n\n/**\n * Return the cached parseFragment function (or null if not loaded).\n * Used by parseIdentifiersFromTemplate to reuse the same parse5 instance\n * loaded via loadJITCompiler(), avoiding a separate dynamic import.\n * @internal\n */\n/** @codexApi {\"parent\":\"pithy.compiler.html-parser\",\"name\":\"getParseFragmentFn\",\"stability\":\"internal\",\"signature\":\"() => ParseFragmentFn | null\"} */\nexport function getParseFragmentFn(): ParseFragmentFn | null {\n return _parseFragment;\n}\n\n// ─── Main API ────────────────────────────────────────────────────────────────\n\n/**\n * Parse an HTML template string into a DOM fragment with PithyNode metadata.\n * Requires parse5 to be loaded first via `loadJITCompiler()` or `setParseFragment()`.\n * Throws if the runtime compiler has not been loaded.\n *\n * @param html - Raw HTML template string\n * @param scopeId - Unique scope identifier for style scoping\n * @param context - Reactive context for expression evaluation\n * @param importedIdentifiers - Set of imported component names for self-closing tag handling\n * @returns Object with `fragment` (live DOM) and `pithyNodes` (AST metadata)\n */\n/** @codexApi {\"parent\":\"pithy.compiler.html-parser\",\"name\":\"parseHtml\",\"stability\":\"stable\",\"signature\":\"(html: string, scopeId: string, context: Record<string, unknown>, importedIdentifiers?: Set<string>) => { fragment: DocumentFragment; pithyNodes: PithyNode[] }\"} */\nexport function parseHtml(\n html: string,\n scopeId: string,\n context: Record<string, unknown>,\n importedIdentifiers: Set<string> = new Set()\n): {\n fragment: DocumentFragment;\n pithyNodes: PithyNode[];\n} {\n const parseFragment = getParseFragment();\n\n // 1. Pre-process HTML to convert self-closing component tags to regular tags\n const processedHtml = preprocessSelfClosingComponents(\n html,\n importedIdentifiers\n );\n\n // 2. Escape PascalCase component tags so parse5 doesn't enter special\n // insertion modes (e.g., \"in select\", \"in table\") that corrupt the tree.\n const escapedHtml = escapeComponentTags(processedHtml);\n\n // 3. Parse HTML into parse5 tree\n const parsed = parseFragment(escapedHtml, {\n sourceCodeLocationInfo: true,\n }) as {\n childNodes: unknown[];\n };\n\n // 4. Restore casing and tag type via your parser\n // IMPORTANT: Use escapedHtml (not raw html) because parse5 reports\n // sourceCodeLocation offsets based on escapedHtml. If we used raw html,\n // self-closing tag expansion (e.g. <Foo/> → <Foo></Foo>) and escape\n // prefixing would shift all subsequent offsets, causing casing map\n // lookups to miss for components that appear after other components.\n const tagNameCasingMap = extractOriginalTagNames(escapedHtml);\n const attributeNameCasingMap = extractOriginalAttributeNames(escapedHtml);\n\n const pithyNodes = parsed.childNodes\n .map((n: unknown) =>\n parse5ToPithyTree(\n n as Parameters<typeof parse5ToPithyTree>[0],\n importedIdentifiers,\n tagNameCasingMap,\n undefined,\n attributeNameCasingMap,\n undefined,\n escapedHtml\n )\n )\n .filter((n): n is PithyNode => n != null);\n\n // 3. Compile DOM tree from PithyNodes\n const fragment = pithyTreeToDom(pithyNodes, context);\n\n // 4. Add data-scope attribute to all elements\n addScope(fragment, scopeId);\n\n return {\n fragment,\n pithyNodes,\n };\n}\n\n/**\n * Pre-processes HTML to convert self-closing component tags to regular tags.\n * This works around parse5's limitation with self-closing custom elements.\n *\n * @param html - The HTML template string\n * @param importedIdentifiers - Set of imported component names\n * @returns Processed HTML with self-closing components converted to regular tags\n */\n/** @codexApi {\"parent\":\"pithy.compiler.html-parser\",\"name\":\"preprocessSelfClosingComponents\",\"stability\":\"stable\",\"signature\":\"(html: string, importedIdentifiers: Set<string>) => string\"} */\nfunction preprocessSelfClosingComponents(\n html: string,\n importedIdentifiers: Set<string>\n): string {\n let processedHtml = html;\n\n // Convert each imported component from self-closing to regular tag\n for (const componentName of importedIdentifiers) {\n // Create regex to match self-closing component tags\n // Matches: <ComponentName /> or <ComponentName/>\n // IMPORTANT: Use 'g' flag only (NOT 'gi') - case-sensitive matching is required\n // to avoid converting native elements like <input /> to <Input></Input>\n // Quoted values may hold `>` (an arrow-function prop), so skip them whole.\n const selfClosingRegex = new RegExp(\n `<${componentName}(\\\\s(?:[^>\"']|\"[^\"]*\"|'[^']*')*?)?\\\\s*\\\\/\\\\s*>`,\n 'g'\n );\n\n // Replace with regular opening and closing tags\n processedHtml = processedHtml.replace(\n selfClosingRegex,\n (match, attributes) => {\n const attrs = attributes || '';\n return `<${componentName}${attrs}></${componentName}>`;\n }\n );\n }\n\n return processedHtml;\n}\n\nfunction addScope(root: ParentNode, scopeId: string): void {\n root.querySelectorAll?.('*').forEach(el => {\n el.setAttribute(`data-scope-${scopeId}`, '');\n if (el.localName === 'template' && 'content' in el)\n addScope((el as HTMLTemplateElement).content, scopeId);\n });\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pithyTreeToDom.d.ts","sourceRoot":"","sources":["../../src/compile/pithyTreeToDom.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,OAAO,
|
|
1
|
+
{"version":3,"file":"pithyTreeToDom.d.ts","sourceRoot":"","sources":["../../src/compile/pithyTreeToDom.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,OAAO,EAKL,KAAK,SAAS,EACf,MAAM,mBAAmB,CAAC;AAG3B;;;;GAIG;AACH,uLAAuL;AACvL,wBAAgB,cAAc,CAC5B,KAAK,EAAE,SAAS,EAAE,EAClB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC/B,gBAAgB,CAWlB"}
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
"category": "compiler"
|
|
6
6
|
}
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
9
|
-
import { nodeExpando } from './nodeExpando';
|
|
8
|
+
import { elementTagName, isSvgNode, SVG_NS, forEachDirectiveAttr, } from '@pithyjs/compiler';
|
|
9
|
+
import { nodeExpando } from './nodeExpando.js';
|
|
10
10
|
/**
|
|
11
11
|
* Converts a list of PithyNodes into a DocumentFragment of real DOM nodes.
|
|
12
12
|
* Component tags (isComponent: true) are rendered as placeholder elements.
|
|
@@ -24,10 +24,11 @@ export function pithyTreeToDom(nodes, context) {
|
|
|
24
24
|
return fragment;
|
|
25
25
|
}
|
|
26
26
|
/** @codexApi {"parent":"pithy.compiler.dom-tree","name":"createDomNodeFromPithy","stability":"stable","signature":"(node: PithyNode, context: Record<string, any>) => Node | null"} */
|
|
27
|
-
function createDomNodeFromPithy(node, context) {
|
|
27
|
+
function createDomNodeFromPithy(node, context, doc = document) {
|
|
28
28
|
if (node.type === 'text') {
|
|
29
|
-
const textNode =
|
|
29
|
+
const textNode = doc.createTextNode(node.textContent ?? '');
|
|
30
30
|
nodeExpando(textNode).__pithyCtx = context;
|
|
31
|
+
nodeExpando(textNode).__pithyNode = node;
|
|
31
32
|
return textNode;
|
|
32
33
|
}
|
|
33
34
|
if (node.type === 'slot') {
|
|
@@ -49,11 +50,10 @@ function createDomNodeFromPithy(node, context) {
|
|
|
49
50
|
}
|
|
50
51
|
if (node.type === 'element') {
|
|
51
52
|
// For components, create placeholder element (to be upgraded later)
|
|
52
|
-
const tag = node
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
: document.createElement(tag);
|
|
53
|
+
const tag = elementTagName(node);
|
|
54
|
+
const el = isSvgNode(node)
|
|
55
|
+
? doc.createElementNS(SVG_NS, tag)
|
|
56
|
+
: doc.createElement(tag);
|
|
57
57
|
nodeExpando(el).__pithyCtx = context;
|
|
58
58
|
// Store the PithyNode data for component binding to access:
|
|
59
59
|
// - isComponent flag (to distinguish native elements from components)
|
|
@@ -105,14 +105,19 @@ function createDomNodeFromPithy(node, context) {
|
|
|
105
105
|
// Preserve existing data-pid from the original HTML
|
|
106
106
|
el.setAttribute('data-pid', node.attrs['data-pid']);
|
|
107
107
|
}
|
|
108
|
-
|
|
108
|
+
const template = isTemplate(el);
|
|
109
|
+
const parent = template ? el.content : el;
|
|
110
|
+
const childDoc = template ? el.content.ownerDocument : doc;
|
|
109
111
|
for (const child of node.children ?? []) {
|
|
110
|
-
const childDom = createDomNodeFromPithy(child, context);
|
|
112
|
+
const childDom = createDomNodeFromPithy(child, context, childDoc);
|
|
111
113
|
if (childDom)
|
|
112
|
-
|
|
114
|
+
parent.appendChild(childDom);
|
|
113
115
|
}
|
|
114
116
|
return el;
|
|
115
117
|
}
|
|
116
118
|
return null;
|
|
117
119
|
}
|
|
120
|
+
function isTemplate(el) {
|
|
121
|
+
return el.localName === 'template' && el.namespaceURI !== SVG_NS;
|
|
122
|
+
}
|
|
118
123
|
//# sourceMappingURL=pithyTreeToDom.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pithyTreeToDom.js","sourceRoot":"","sources":["../../src/compile/pithyTreeToDom.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,OAAO,EACL,
|
|
1
|
+
{"version":3,"file":"pithyTreeToDom.js","sourceRoot":"","sources":["../../src/compile/pithyTreeToDom.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,OAAO,EACL,cAAc,EACd,SAAS,EACT,MAAM,EACN,oBAAoB,GAErB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C;;;;GAIG;AACH,uLAAuL;AACvL,MAAM,UAAU,cAAc,CAC5B,KAAkB,EAClB,OAAgC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AoD;YACpD,EAAE,CAAC,YAAY,CAAC,UAAU,EAAE,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC;QACtD,CAAC;QAED,MAAM,QAAQ,GAAG,UAAU,CAAC,EAAE,CAAC,CAAC;QAChC,MAAM,MAAM,GAAG,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;QAC1C,MAAM,QAAQ,GAAG,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,GAAG,CAAC;QAC3D,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,QAAQ,IAAI,EAAE,EAAE,CAAC;YACxC,MAAM,QAAQ,GAAG,sBAAsB,CAAC,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;YAClE,IAAI,QAAQ;gBAAE,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;QAC7C,CAAC;QAED,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,UAAU,CAAC,EAAW;IAC7B,OAAO,EAAE,CAAC,SAAS,KAAK,UAAU,IAAI,EAAE,CAAC,YAAY,KAAK,MAAM,CAAC;AACnE,CAAC","sourcesContent":["/** @codex\n{\n \"id\": \"pithy.compiler.dom-tree\",\n \"title\": \"PithyNode to DOM Tree Converter\",\n \"category\": \"compiler\"\n}\n*/\n\nimport {\n elementTagName,\n isSvgNode,\n SVG_NS,\n forEachDirectiveAttr,\n type PithyNode,\n} from '@pithyjs/compiler';\nimport { nodeExpando } from './nodeExpando';\n\n/**\n * Converts a list of PithyNodes into a DocumentFragment of real DOM nodes.\n * Component tags (isComponent: true) are rendered as placeholder elements.\n * No bindings or reactivity are applied here — those are handled in bind*() functions.\n */\n/** @codexApi {\"parent\":\"pithy.compiler.dom-tree\",\"name\":\"pithyTreeToDom\",\"stability\":\"stable\",\"signature\":\"(nodes: PithyNode[], context: Record<string, any>) => DocumentFragment\"} */\nexport function pithyTreeToDom(\n nodes: PithyNode[],\n context: Record<string, unknown>\n): DocumentFragment {\n const fragment = document.createDocumentFragment();\n\n for (const node of nodes) {\n const domNode = createDomNodeFromPithy(node, context);\n if (domNode) {\n fragment.appendChild(domNode);\n }\n }\n\n return fragment;\n}\n\n/** @codexApi {\"parent\":\"pithy.compiler.dom-tree\",\"name\":\"createDomNodeFromPithy\",\"stability\":\"stable\",\"signature\":\"(node: PithyNode, context: Record<string, any>) => Node | null\"} */\nfunction createDomNodeFromPithy(\n node: PithyNode,\n context: Record<string, unknown>,\n doc: Document = document\n): Node | null {\n if (node.type === 'text') {\n const textNode = doc.createTextNode(node.textContent ?? '');\n nodeExpando(textNode).__pithyCtx = context;\n nodeExpando(textNode).__pithyNode = node;\n return textNode;\n }\n\n if (node.type === 'slot') {\n // Create comment anchor for slot\n const slotName = node.slotName || '';\n const anchor = document.createComment(\n `slot:${slotName} pid=\"${node.nodeId}\"`\n );\n\n // Store slot metadata for runtime stitching\n nodeExpando(anchor).__pithyCtx = context;\n nodeExpando(anchor).__nodeId = node.nodeId;\n nodeExpando(anchor).__slotName = slotName;\n nodeExpando(anchor).__pithyNode = node;\n\n // Create fallback fragment factory if fallback content exists\n if (node.fallbackChildren && node.fallbackChildren.length > 0) {\n nodeExpando(anchor).__fallbackFactory = () => {\n return pithyTreeToDom(node.fallbackChildren!, context);\n };\n }\n\n return anchor;\n }\n\n if (node.type === 'element') {\n // For components, create placeholder element (to be upgraded later)\n const tag = elementTagName(node);\n\n const el = isSvgNode(node)\n ? doc.createElementNS(SVG_NS, tag)\n : doc.createElement(tag);\n\n nodeExpando(el).__pithyCtx = context;\n\n // Store the PithyNode data for component binding to access:\n // - isComponent flag (to distinguish native elements from components)\n // - original attribute names (for prop name normalization)\n nodeExpando(el).__pithyNode = node;\n\n // Set static attributes\n for (const [name, value] of Object.entries(node.attrs ?? {})) {\n if (name.startsWith('@') || name.startsWith('[')) {\n continue; // Skip PithyJS directive attributes — handled in bind* functions\n }\n // Use original attribute name casing (important for SVG attributes like viewBox)\n const originalName = node.originalAttrs?.[name] || name;\n el.setAttribute(originalName, value);\n }\n\n // @for loop - REPLACE element with anchor\n if (node.directives?.for) {\n // Create a comment node as placeholder\n const placeholder = document.createComment(\n `Placeholder for loop items pid=\"${node.nodeId}\"`\n );\n nodeExpando(placeholder).__pithyCtx = context;\n\n // Store the nodeId on the comment for bindForLoops to find it\n if (node.nodeId) {\n nodeExpando(placeholder).__nodeId = node.nodeId;\n // Loop placeholders are comment nodes — discovered via __nodeId walk, not querySelectorAll (data-pid is for element nodes only)\n }\n\n // Store the original PithyNode structure for nested processing\n nodeExpando(placeholder).__pithyNode = node;\n\n return placeholder;\n }\n\n // Apply directive data-* attributes for runtime binders.\n // Uses the shared mapping from @pithyjs/compiler to stay in sync with AOT codegen.\n if (node.directives) {\n forEachDirectiveAttr(node.directives, (attr, value) => {\n el.setAttribute(attr, value);\n });\n\n // Preserve `slot` as a standard HTML attribute.\n // forEachDirectiveAttr intentionally skips slot (it's consumed by content\n // projection via directives.slot), but downstream binders like\n // bindDeferDirectives need it as a DOM attribute for querySelector lookups\n // (e.g., '[slot=\"placeholder\"]', '[slot=\"error\"]').\n if (node.directives.slot && !el.hasAttribute('slot')) {\n el.setAttribute('slot', node.directives.slot);\n }\n }\n\n // Optional: add dev/debug node ID\n if (node.nodeId) {\n el.setAttribute('data-pid', node.nodeId);\n } else if (node.attrs?.['data-pid']) {\n // Preserve existing data-pid from the original HTML\n el.setAttribute('data-pid', node.attrs['data-pid']);\n }\n\n const template = isTemplate(el);\n const parent = template ? el.content : el;\n const childDoc = template ? el.content.ownerDocument : doc;\n for (const child of node.children ?? []) {\n const childDom = createDomNodeFromPithy(child, context, childDoc);\n if (childDom) parent.appendChild(childDom);\n }\n\n return el;\n }\n\n return null;\n}\n\nfunction isTemplate(el: Element): el is HTMLTemplateElement {\n return el.localName === 'template' && el.namespaceURI !== SVG_NS;\n}\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { PithyNode } from '@pithyjs/compiler';
|
|
2
|
+
export interface Branch {
|
|
3
|
+
nodes: Node[];
|
|
4
|
+
disposers: (() => void)[];
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Builds the DOM for one `@if` or `@case` branch from its AST, binds it, and
|
|
8
|
+
* inserts it after `anchor`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function renderBranch(ast: PithyNode, ctx: Record<string, unknown>, anchor: Node): Branch;
|
|
11
|
+
/** Disposes a branch's effects, last registered first, then removes its nodes. */
|
|
12
|
+
export declare function clearBranch(branch: Branch): void;
|
|
13
|
+
//# sourceMappingURL=renderBranch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"renderBranch.d.ts","sourceRoot":"","sources":["../../src/compile/renderBranch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAanD,MAAM,WAAW,MAAM;IACrB,KAAK,EAAE,IAAI,EAAE,CAAC;IACd,SAAS,EAAE,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC;CAC3B;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,GAAG,EAAE,SAAS,EACd,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,MAAM,EAAE,IAAI,GACX,MAAM,CAER;AA+CD,kFAAkF;AAClF,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAahD"}
|