@pithyjs/core 0.1.0-beta.2 → 0.1.0-beta.20
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 +224 -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 +86 -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 +39 -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.d.ts.map +1 -1
- package/dist/compile/parseHtml.js +4 -3
- package/dist/compile/parseHtml.js.map +1 -1
- package/dist/compile/pithyTreeToDom.js +2 -1
- 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,8 +5,9 @@
|
|
|
5
5
|
"category": "runtime"
|
|
6
6
|
}
|
|
7
7
|
*/
|
|
8
|
+
import { isDeclaredDirective } from './declaredDirective.js';
|
|
8
9
|
import { effect } from '@pithyjs/signals';
|
|
9
|
-
import { nodeExpando } from '../nodeExpando';
|
|
10
|
+
import { nodeExpando } from '../nodeExpando.js';
|
|
10
11
|
/**
|
|
11
12
|
* Binds [attr]="expression" bindings like [class]="dynamicClass"
|
|
12
13
|
* using reactive effect evaluation.
|
|
@@ -16,42 +17,244 @@ export function bindBoundAttributes(root, context, disposers) {
|
|
|
16
17
|
root.querySelectorAll('*').forEach(el => {
|
|
17
18
|
// Use element's own __pithyCtx when available (projected slot content
|
|
18
19
|
// carries the parent component's context via this property).
|
|
19
|
-
const elCtx = nodeExpando(el).
|
|
20
|
+
const elCtx = nodeExpando(el).__pithyForwardCtx ??
|
|
21
|
+
nodeExpando(el).__pithyCtx ??
|
|
22
|
+
context;
|
|
20
23
|
for (const attr of Array.from(el.attributes)) {
|
|
21
|
-
if (!attr.name.startsWith('data-bind-'))
|
|
24
|
+
if (!attr.name.startsWith('data-bind-') || !isDeclaredDirective(attr))
|
|
22
25
|
continue;
|
|
23
26
|
const attrName = attr.name.slice('data-bind-'.length);
|
|
24
27
|
const expr = attr.value;
|
|
25
28
|
// Remove data-bind-* attribute after capturing expression to prevent
|
|
26
29
|
// double-processing when component subtrees are inserted into outer DOM
|
|
27
30
|
el.removeAttribute(attr.name);
|
|
31
|
+
const writeStyle = attrName === 'style' ? styleWriter(el) : null;
|
|
28
32
|
const stop = effect(() => {
|
|
29
|
-
|
|
30
|
-
if (typeof value === 'boolean') {
|
|
31
|
-
if (value) {
|
|
32
|
-
el.setAttribute(attrName, '');
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
el.removeAttribute(attrName);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
el.setAttribute(attrName, String(value ?? ''));
|
|
40
|
-
}
|
|
33
|
+
writeBound(el, attrName, expr, writeStyle, () => evaluateExpression(expr, elCtx));
|
|
41
34
|
});
|
|
42
35
|
disposers.push(stop);
|
|
43
36
|
}
|
|
44
37
|
});
|
|
45
38
|
}
|
|
46
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Every binding on one element's `style`. Strings compose in bind order, then
|
|
41
|
+
* each `[style.x]` applies on top, so nothing depends on reading the CSSOM back:
|
|
42
|
+
* a value there cannot show which binding wrote it.
|
|
43
|
+
*/
|
|
44
|
+
function styleOwners(el) {
|
|
45
|
+
const node = nodeExpando(el);
|
|
46
|
+
return (node.__styleOwners ?? (node.__styleOwners = { strings: [], props: new Map() }));
|
|
47
|
+
}
|
|
48
|
+
function renderStyle(el, owners) {
|
|
49
|
+
const text = owners.strings.filter(Boolean).join('; ');
|
|
50
|
+
if (text)
|
|
51
|
+
el.setAttribute('style', text);
|
|
52
|
+
else
|
|
53
|
+
el.removeAttribute('style');
|
|
54
|
+
const style = el.style;
|
|
55
|
+
for (const [prop, [value, priority]] of owners.props) {
|
|
56
|
+
style?.setProperty(prop, value, priority);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/** @codexApi {"parent":"pithy.runtime.attributes","name":"styleWriter","stability":"internal","signature":"(el: Element) => (text: string) => void"} */
|
|
60
|
+
export function styleWriter(el) {
|
|
61
|
+
const owners = styleOwners(el);
|
|
62
|
+
if (!owners.strings.length) {
|
|
63
|
+
const style = el.style;
|
|
64
|
+
for (const prop of owners.props.keys())
|
|
65
|
+
style?.removeProperty(prop);
|
|
66
|
+
owners.strings.push(el.getAttribute('style') ?? '');
|
|
67
|
+
}
|
|
68
|
+
const slot = owners.strings.push('') - 1;
|
|
69
|
+
return text => {
|
|
70
|
+
owners.strings[slot] = text;
|
|
71
|
+
renderStyle(el, owners);
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/** @codexApi {"parent":"pithy.runtime.attributes","name":"setStyleProp","stability":"internal","signature":"(el: Element, prop: string, value: string | null, priority?: string) => void"} */
|
|
75
|
+
export function setStyleProp(el, prop, value, priority = '') {
|
|
76
|
+
const owners = styleOwners(el);
|
|
77
|
+
const style = el.style;
|
|
78
|
+
owners.props.delete(prop);
|
|
79
|
+
if (value !== null)
|
|
80
|
+
owners.props.set(prop, [value, priority]);
|
|
81
|
+
if (owners.strings.length) {
|
|
82
|
+
renderStyle(el, owners);
|
|
83
|
+
}
|
|
84
|
+
else if (value === null) {
|
|
85
|
+
style?.removeProperty(prop);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
style?.setProperty(prop, value, priority);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const LINK_SCHEME_RE = /^(?:javascript|vbscript|data):/i;
|
|
92
|
+
// A data: frame, form target or animated href loads in an opaque origin, so only a link refuses data:.
|
|
93
|
+
const SCRIPT_SCHEME_RE = /^(?:javascript|vbscript):/i;
|
|
94
|
+
const LINK_ATTRS = ['href', 'xlink:href'];
|
|
95
|
+
const LINK_SINK = { attrs: LINK_ATTRS, scheme: LINK_SCHEME_RE };
|
|
96
|
+
const FORM_ACTION_SINK = { attrs: ['formaction'], scheme: SCRIPT_SCHEME_RE };
|
|
97
|
+
const URL_SINKS = new Map([
|
|
98
|
+
['a', LINK_SINK],
|
|
99
|
+
['area', LINK_SINK],
|
|
100
|
+
['set', { attrs: ['to'], scheme: SCRIPT_SCHEME_RE }],
|
|
101
|
+
['animate', { attrs: ['to', 'from', 'values'], scheme: SCRIPT_SCHEME_RE }],
|
|
102
|
+
['iframe', { attrs: ['src'], scheme: SCRIPT_SCHEME_RE }],
|
|
103
|
+
['frame', { attrs: ['src'], scheme: SCRIPT_SCHEME_RE }],
|
|
104
|
+
['form', { attrs: ['action'], scheme: SCRIPT_SCHEME_RE }],
|
|
105
|
+
['button', FORM_ACTION_SINK],
|
|
106
|
+
['input', FORM_ACTION_SINK],
|
|
107
|
+
]);
|
|
108
|
+
function hasScheme(url, scheme) {
|
|
109
|
+
const compact = [...url].filter(c => c > ' ' && c !== '\x7f').join('');
|
|
110
|
+
return scheme.test(compact);
|
|
111
|
+
}
|
|
112
|
+
function rejectsScriptSink(el, attr, text) {
|
|
113
|
+
if (el.localName === 'iframe' && attr === 'srcdoc') {
|
|
114
|
+
if (import.meta.env?.DEV && text !== '') {
|
|
115
|
+
console.warn('[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.');
|
|
116
|
+
}
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
const sink = URL_SINKS.get(el.localName);
|
|
120
|
+
if (!sink?.attrs.includes(attr))
|
|
121
|
+
return false;
|
|
122
|
+
const urls = attr === 'values' ? text.split(';') : [text];
|
|
123
|
+
if (!urls.some(url => hasScheme(url, sink.scheme)))
|
|
124
|
+
return false;
|
|
125
|
+
if (import.meta.env?.DEV) {
|
|
126
|
+
console.warn(`[PithyJS] Unsafe [${attr}] rejected: "${text}".`);
|
|
127
|
+
}
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
const TEXT_BOOLEAN_ATTR = /^(?:aria-.+|contenteditable|draggable|spellcheck)$/i;
|
|
131
|
+
const FORM_CONTROLS = new Set(['input', 'select', 'textarea']);
|
|
132
|
+
function syncSelect(select) {
|
|
133
|
+
const bound = nodeExpando(select).__selectValue;
|
|
134
|
+
if (!bound)
|
|
135
|
+
return;
|
|
136
|
+
const picks = select.multiple ? bound.picks : null;
|
|
137
|
+
let found = false;
|
|
138
|
+
for (const option of select.querySelectorAll('option')) {
|
|
139
|
+
const match = picks
|
|
140
|
+
? picks.has(option.value)
|
|
141
|
+
: !found && option.value === bound.text;
|
|
142
|
+
option.toggleAttribute('selected', match);
|
|
143
|
+
if (select.multiple)
|
|
144
|
+
option.selected = match;
|
|
145
|
+
found || (found = match);
|
|
146
|
+
}
|
|
147
|
+
if (!select.multiple)
|
|
148
|
+
select.value = bound.text;
|
|
149
|
+
}
|
|
150
|
+
function syncOption(option) {
|
|
151
|
+
const select = option.closest('select');
|
|
152
|
+
const bound = select && nodeExpando(select).__selectValue;
|
|
153
|
+
if (!select || !bound)
|
|
154
|
+
return;
|
|
155
|
+
if (select.multiple && bound.picks) {
|
|
156
|
+
const match = bound.picks.has(option.value);
|
|
157
|
+
option.toggleAttribute('selected', match);
|
|
158
|
+
option.selected = match;
|
|
159
|
+
}
|
|
160
|
+
else if (option.selected || option.value === bound.text) {
|
|
161
|
+
syncSelect(select);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
function setControlValue(el, text, value) {
|
|
165
|
+
if (el.localName === 'option') {
|
|
166
|
+
syncOption(el);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (!FORM_CONTROLS.has(el.localName))
|
|
170
|
+
return;
|
|
171
|
+
const control = el;
|
|
172
|
+
if (control.type === 'file')
|
|
173
|
+
return;
|
|
174
|
+
if (el.localName === 'select') {
|
|
175
|
+
const picks = Array.isArray(value) ? new Set(value.map(String)) : null;
|
|
176
|
+
nodeExpando(el).__selectValue = { text, picks };
|
|
177
|
+
syncSelect(el);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (el.localName === 'textarea' && el.textContent !== text) {
|
|
181
|
+
el.textContent = text;
|
|
182
|
+
}
|
|
183
|
+
if (control.value !== text)
|
|
184
|
+
control.value = text;
|
|
185
|
+
}
|
|
186
|
+
function writeValue(el, attr, value, writeStyle) {
|
|
187
|
+
if (value == null) {
|
|
188
|
+
if (writeStyle)
|
|
189
|
+
writeStyle('');
|
|
190
|
+
else
|
|
191
|
+
el.removeAttribute(attr);
|
|
192
|
+
if (attr === 'value')
|
|
193
|
+
setControlValue(el, '');
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (typeof value === 'boolean' && !TEXT_BOOLEAN_ATTR.test(attr)) {
|
|
197
|
+
if (writeStyle)
|
|
198
|
+
writeStyle('');
|
|
199
|
+
else if (value)
|
|
200
|
+
el.setAttribute(attr, '');
|
|
201
|
+
else
|
|
202
|
+
el.removeAttribute(attr);
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
const text = String(value);
|
|
206
|
+
if (writeStyle) {
|
|
207
|
+
writeStyle(text);
|
|
208
|
+
}
|
|
209
|
+
else if (rejectsScriptSink(el, attr, text)) {
|
|
210
|
+
el.removeAttribute(attr);
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
el.setAttribute(attr, text);
|
|
214
|
+
if (attr === 'value')
|
|
215
|
+
setControlValue(el, text, value);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/** @codexApi {"parent":"pithy.runtime.attributes","name":"UNCOMPILED","stability":"internal","signature":"Error"} */
|
|
219
|
+
export const UNCOMPILED = new Error('The expression does not compile');
|
|
220
|
+
/** @codexApi {"parent":"pithy.runtime.attributes","name":"writeBound","stability":"internal","signature":"(el: Element, attr: string, expr: string, writeStyle: ((text: string) => void) | null, read: () => unknown) => void"} */
|
|
221
|
+
export function writeBound(el, attr, expr, writeStyle, read) {
|
|
47
222
|
try {
|
|
48
|
-
|
|
49
|
-
const compiled = new Function(...Object.keys(context), `return (${expr})`);
|
|
50
|
-
return compiled(...Object.values(context));
|
|
223
|
+
writeValue(el, attr, read(), writeStyle);
|
|
51
224
|
}
|
|
52
225
|
catch (err) {
|
|
53
|
-
|
|
54
|
-
|
|
226
|
+
if (import.meta.env?.DEV && err !== UNCOMPILED) {
|
|
227
|
+
console.warn(`[pithy] Failed to evaluate: ${expr}`, err);
|
|
228
|
+
}
|
|
229
|
+
removeBound(el, attr, writeStyle);
|
|
230
|
+
}
|
|
231
|
+
if (attr === 'multiple' && el.localName === 'select') {
|
|
232
|
+
syncSelect(el);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
function removeBound(el, attr, writeStyle) {
|
|
236
|
+
try {
|
|
237
|
+
if (writeStyle) {
|
|
238
|
+
writeStyle('');
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
const control = el;
|
|
242
|
+
const kept = control.value;
|
|
243
|
+
el.removeAttribute(attr);
|
|
244
|
+
if (attr === 'value' &&
|
|
245
|
+
FORM_CONTROLS.has(el.localName) &&
|
|
246
|
+
control.value !== kept) {
|
|
247
|
+
control.value = kept;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
catch {
|
|
252
|
+
/* ignore */
|
|
55
253
|
}
|
|
56
254
|
}
|
|
255
|
+
function evaluateExpression(expr, context) {
|
|
256
|
+
// eslint-disable-next-line no-new-func -- args-style template expression evaluation is a core framework feature
|
|
257
|
+
const compiled = new Function(...Object.keys(context), `return (${expr})`);
|
|
258
|
+
return compiled(...Object.values(context));
|
|
259
|
+
}
|
|
57
260
|
//# sourceMappingURL=bindBoundAttributes.js.map
|
|
@@ -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,OAA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,IAAI,CAAC,CAAC;YACzB,IACE,IAAI,KAAK,OAAO;gBAChB,aAAa,CAAC,GAAG,CAAC,EAAE,CAAC,SAAS,CAAC;gBAC/B,OAAO,CAAC,KAAK,KAAK,IAAI,EACtB,CAAC;gBACD,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC;YACvB,CAAC;QACH,CAAC;IACH,CAAC;IAAC,MAAM,CAAC;QACP,YAAY;IACd,CAAC;AACH,CAAC;AAED,SAAS,kBAAkB,CACzB,IAAY,EACZ,OAAgC;IAEhC,gHAAgH;IAChH,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAC3B,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,EACvB,WAAW,IAAI,GAAG,CACgB,CAAC;IACrC,OAAO,QAAQ,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;AAC7C,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 =\n nodeExpando(el).__pithyForwardCtx ??\n nodeExpando(el).__pithyCtx ??\n 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 writeBound(el, attrName, expr, writeStyle, () =>\n evaluateExpression(expr, elCtx)\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\nfunction rejectsScriptSink(el: Element, attr: string, text: string): 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\nconst TEXT_BOOLEAN_ATTR = /^(?:aria-.+|contenteditable|draggable|spellcheck)$/i;\nconst FORM_CONTROLS = new Set(['input', 'select', 'textarea']);\n\nfunction syncSelect(select: HTMLSelectElement): void {\n const bound = nodeExpando(select).__selectValue;\n if (!bound) return;\n const picks = select.multiple ? bound.picks : null;\n let found = false;\n for (const option of select.querySelectorAll('option')) {\n const match: boolean = picks\n ? picks.has(option.value)\n : !found && option.value === bound.text;\n option.toggleAttribute('selected', match);\n if (select.multiple) option.selected = match;\n found ||= match;\n }\n if (!select.multiple) select.value = bound.text;\n}\n\nfunction syncOption(option: HTMLOptionElement): void {\n const select = option.closest('select');\n const bound = select && nodeExpando(select).__selectValue;\n if (!select || !bound) return;\n if (select.multiple && bound.picks) {\n const match = bound.picks.has(option.value);\n option.toggleAttribute('selected', match);\n option.selected = match;\n } else if (option.selected || option.value === bound.text) {\n syncSelect(select);\n }\n}\n\nfunction setControlValue(el: Element, text: string, value?: unknown): void {\n if (el.localName === 'option') {\n syncOption(el as HTMLOptionElement);\n return;\n }\n if (!FORM_CONTROLS.has(el.localName)) return;\n const control = el as HTMLInputElement;\n if (control.type === 'file') return;\n if (el.localName === 'select') {\n const picks = Array.isArray(value) ? new Set(value.map(String)) : null;\n nodeExpando(el).__selectValue = { text, picks };\n syncSelect(el as HTMLSelectElement);\n return;\n }\n if (el.localName === 'textarea' && el.textContent !== text) {\n el.textContent = text;\n }\n if (control.value !== text) control.value = text;\n}\n\nfunction writeValue(\n el: Element,\n attr: string,\n value: unknown,\n writeStyle: ((text: string) => void) | null\n): void {\n if (value == null) {\n if (writeStyle) writeStyle('');\n else el.removeAttribute(attr);\n if (attr === 'value') setControlValue(el, '');\n return;\n }\n if (typeof value === 'boolean' && !TEXT_BOOLEAN_ATTR.test(attr)) {\n if (writeStyle) writeStyle('');\n else if (value) el.setAttribute(attr, '');\n else el.removeAttribute(attr);\n return;\n }\n const text = String(value);\n if (writeStyle) {\n writeStyle(text);\n } else if (rejectsScriptSink(el, attr, text)) {\n el.removeAttribute(attr);\n } else {\n el.setAttribute(attr, text);\n if (attr === 'value') setControlValue(el, text, value);\n }\n}\n\n/** @codexApi {\"parent\":\"pithy.runtime.attributes\",\"name\":\"UNCOMPILED\",\"stability\":\"internal\",\"signature\":\"Error\"} */\nexport const UNCOMPILED = new Error('The expression does not compile');\n\n/** @codexApi {\"parent\":\"pithy.runtime.attributes\",\"name\":\"writeBound\",\"stability\":\"internal\",\"signature\":\"(el: Element, attr: string, expr: string, writeStyle: ((text: string) => void) | null, read: () => unknown) => void\"} */\nexport function writeBound(\n el: Element,\n attr: string,\n expr: string,\n writeStyle: ((text: string) => void) | null,\n read: () => unknown\n): void {\n try {\n writeValue(el, attr, read(), writeStyle);\n } catch (err) {\n if (import.meta.env?.DEV && err !== UNCOMPILED) {\n console.warn(`[pithy] Failed to evaluate: ${expr}`, err);\n }\n removeBound(el, attr, writeStyle);\n }\n if (attr === 'multiple' && el.localName === 'select') {\n syncSelect(el as HTMLSelectElement);\n }\n}\n\nfunction removeBound(\n el: Element,\n attr: string,\n writeStyle: ((text: string) => void) | null\n): void {\n try {\n if (writeStyle) {\n writeStyle('');\n } else {\n const control = el as HTMLInputElement;\n const kept = control.value;\n el.removeAttribute(attr);\n if (\n attr === 'value' &&\n FORM_CONTROLS.has(el.localName) &&\n control.value !== kept\n ) {\n control.value = kept;\n }\n }\n } catch {\n /* ignore */\n }\n}\n\nfunction evaluateExpression(\n expr: string,\n context: Record<string, unknown>\n): unknown {\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}\n"]}
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
* • [class.active]="isActive"
|
|
11
11
|
* • [classes]="{ active: isActive() }"
|
|
12
12
|
* • [classes]="['a', 'b']"
|
|
13
|
+
* • [classes]="'a b'"
|
|
13
14
|
*
|
|
14
15
|
* @param root - The root node to search for class bindings
|
|
15
16
|
* @param ctx - Context object containing reactive variables and functions
|
|
@@ -17,6 +18,6 @@
|
|
|
17
18
|
*/
|
|
18
19
|
/** @codexApi {"parent":"pithy.runtime.classes","name":"bindClassDirectives","stability":"stable","signature":"(root: Element | DocumentFragment, ctx: Record<string, unknown>, disposers: (() => void)[]) => void"} */
|
|
19
20
|
export declare function bindClassDirectives(root: Element | DocumentFragment, ctx: Record<string, unknown>, disposers: (() => void)[]): void;
|
|
20
|
-
/** @codexApi {"parent":"pithy.runtime.classes","name":"applyClassGroup","stability":"internal","signature":"(el: Element, result: unknown) => void"} */
|
|
21
|
-
export declare function applyClassGroup(el: Element, result: unknown): void;
|
|
21
|
+
/** @codexApi {"parent":"pithy.runtime.classes","name":"applyClassGroup","stability":"internal","signature":"(el: Element, result: unknown, owned: Set<string>) => void"} */
|
|
22
|
+
export declare function applyClassGroup(el: Element, result: unknown, owned: Set<string>): void;
|
|
22
23
|
//# sourceMappingURL=bindClassDirectives.d.ts.map
|
|
@@ -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;AAWF;;;;;;;;;;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,CAkFN;AAED,4KAA4K;AAC5K,wBAAgB,eAAe,CAC7B,EAAE,EAAE,OAAO,EACX,MAAM,EAAE,OAAO,EACf,KAAK,EAAE,GAAG,CAAC,MAAM,CAAC,GACjB,IAAI,CA8BN"}
|
|
@@ -5,14 +5,19 @@
|
|
|
5
5
|
"category": "runtime"
|
|
6
6
|
}
|
|
7
7
|
*/
|
|
8
|
+
import { declaredValue, isDeclaredDirective } from './declaredDirective.js';
|
|
8
9
|
import { effect } from '@pithyjs/signals';
|
|
9
|
-
import { compileExpression } from '../compileExpression';
|
|
10
|
-
import { nodeExpando } from '../nodeExpando';
|
|
10
|
+
import { compileExpression } from '../compileExpression.js';
|
|
11
|
+
import { nodeExpando } from '../nodeExpando.js';
|
|
12
|
+
import { classesEvalMessage } from '../evalDiagnostics.js';
|
|
13
|
+
import { bindStyleProperties, declaredName } from './bindStyleDirectives.js';
|
|
14
|
+
const TOGGLE_PREFIX = 'data-class-toggle-';
|
|
11
15
|
/**
|
|
12
16
|
* Binds class directives like:
|
|
13
17
|
* • [class.active]="isActive"
|
|
14
18
|
* • [classes]="{ active: isActive() }"
|
|
15
19
|
* • [classes]="['a', 'b']"
|
|
20
|
+
* • [classes]="'a b'"
|
|
16
21
|
*
|
|
17
22
|
* @param root - The root node to search for class bindings
|
|
18
23
|
* @param ctx - Context object containing reactive variables and functions
|
|
@@ -23,12 +28,19 @@ export function bindClassDirectives(root, ctx, disposers) {
|
|
|
23
28
|
root.querySelectorAll('*').forEach(el => {
|
|
24
29
|
// Use element's own __pithyCtx when available (projected slot content
|
|
25
30
|
// carries the parent component's context via this property).
|
|
26
|
-
const elCtx = nodeExpando(el).__pithyCtx
|
|
27
|
-
|
|
31
|
+
const elCtx = nodeExpando(el).__pithyForwardCtx ?? nodeExpando(el).__pithyCtx ?? ctx;
|
|
32
|
+
let declared;
|
|
28
33
|
for (const attr of Array.from(el.attributes)) {
|
|
29
|
-
if (!attr.name.startsWith(
|
|
34
|
+
if (!attr.name.startsWith(TOGGLE_PREFIX))
|
|
35
|
+
continue;
|
|
36
|
+
const expando = nodeExpando(el);
|
|
37
|
+
declared ?? (declared = [
|
|
38
|
+
...(expando.__classToggles ?? []),
|
|
39
|
+
...Object.keys(expando.__pithyNode?.directives?.classMap ?? {}),
|
|
40
|
+
]);
|
|
41
|
+
const className = declaredName(attr.name.slice(TOGGLE_PREFIX.length), declared);
|
|
42
|
+
if (!className || !isDeclaredDirective(attr))
|
|
30
43
|
continue;
|
|
31
|
-
const className = attr.name.slice('data-class-toggle-'.length);
|
|
32
44
|
const expression = attr.value;
|
|
33
45
|
// Remove directive attribute after capturing to prevent double-processing
|
|
34
46
|
// when component subtrees are inserted into an outer DOM that re-runs binders
|
|
@@ -52,8 +64,7 @@ export function bindClassDirectives(root, ctx, disposers) {
|
|
|
52
64
|
});
|
|
53
65
|
disposers.push(stop);
|
|
54
66
|
}
|
|
55
|
-
|
|
56
|
-
const classGroupExpr = el.getAttribute('data-classes');
|
|
67
|
+
const classGroupExpr = declaredValue(el, 'data-classes');
|
|
57
68
|
if (classGroupExpr) {
|
|
58
69
|
// Remove directive attribute after capturing to prevent double-processing
|
|
59
70
|
el.removeAttribute('data-classes');
|
|
@@ -65,33 +76,59 @@ export function bindClassDirectives(root, ctx, disposers) {
|
|
|
65
76
|
console.warn(`[PithyJS] ⚠️ Failed to compile [classes]="${classGroupExpr}"`, err);
|
|
66
77
|
return;
|
|
67
78
|
}
|
|
79
|
+
const owned = new Set();
|
|
68
80
|
const stop = effect(() => {
|
|
69
81
|
try {
|
|
70
|
-
applyClassGroup(el, evaluate());
|
|
82
|
+
applyClassGroup(el, evaluate(), owned);
|
|
71
83
|
}
|
|
72
84
|
catch (err) {
|
|
73
|
-
console.warn(
|
|
85
|
+
console.warn(classesEvalMessage, err);
|
|
74
86
|
}
|
|
75
87
|
});
|
|
76
88
|
disposers.push(stop);
|
|
77
89
|
}
|
|
90
|
+
// After the classes, so the attributes serialise in the build's order.
|
|
91
|
+
bindStyleProperties(el, elCtx, disposers);
|
|
78
92
|
});
|
|
79
93
|
}
|
|
80
|
-
/** @codexApi {"parent":"pithy.runtime.classes","name":"applyClassGroup","stability":"internal","signature":"(el: Element, result: unknown) => void"} */
|
|
81
|
-
export function applyClassGroup(el, result) {
|
|
82
|
-
|
|
83
|
-
|
|
94
|
+
/** @codexApi {"parent":"pithy.runtime.classes","name":"applyClassGroup","stability":"internal","signature":"(el: Element, result: unknown, owned: Set<string>) => void"} */
|
|
95
|
+
export function applyClassGroup(el, result, owned) {
|
|
96
|
+
const named = new Map();
|
|
97
|
+
const name = (classes, on) => {
|
|
98
|
+
for (const c of classes.split(/[\t\n\f\r ]+/))
|
|
99
|
+
if (c)
|
|
100
|
+
named.set(c, on);
|
|
101
|
+
};
|
|
102
|
+
if (typeof result === 'string') {
|
|
103
|
+
name(result, true);
|
|
104
|
+
}
|
|
105
|
+
else if (Array.isArray(result)) {
|
|
106
|
+
for (const cls of result)
|
|
84
107
|
if (typeof cls === 'string')
|
|
85
|
-
|
|
86
|
-
}
|
|
108
|
+
name(cls, true);
|
|
87
109
|
}
|
|
88
110
|
else if (result && typeof result === 'object') {
|
|
89
|
-
for (const [cls, on] of Object.entries(result))
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
111
|
+
for (const [cls, on] of Object.entries(result))
|
|
112
|
+
name(cls, !!on);
|
|
113
|
+
}
|
|
114
|
+
else if (import.meta.env?.DEV && result) {
|
|
115
|
+
console.warn(`[PithyJS] ⚠️ [classes] expects a string, an array or an object, got ${typeof result}; the classes it added are removed.`);
|
|
116
|
+
}
|
|
117
|
+
// `owned` is this binding's own record, not the element's: a component's
|
|
118
|
+
// root `[classes]` and one forwarded from its tag share the element, and
|
|
119
|
+
// each may only remove the classes it added.
|
|
120
|
+
const previous = new Set(owned);
|
|
121
|
+
owned.clear();
|
|
122
|
+
for (const [c, on] of named) {
|
|
123
|
+
if (!on)
|
|
124
|
+
el.classList.remove(c);
|
|
125
|
+
else if (previous.has(c) || !el.classList.contains(c)) {
|
|
126
|
+
el.classList.add(c);
|
|
127
|
+
owned.add(c);
|
|
94
128
|
}
|
|
95
129
|
}
|
|
130
|
+
for (const c of previous)
|
|
131
|
+
if (!named.has(c))
|
|
132
|
+
el.classList.remove(c);
|
|
96
133
|
}
|
|
97
134
|
//# sourceMappingURL=bindClassDirectives.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bindClassDirectives.js","sourceRoot":"","sources":["../../../src/compile/binds/bindClassDirectives.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"bindClassDirectives.js","sourceRoot":"","sources":["../../../src/compile/binds/bindClassDirectives.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AACzE,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAE1E,MAAM,aAAa,GAAG,oBAAoB,CAAC;AAE3C;;;;;;;;;;GAUG;AACH,uNAAuN;AACvN,MAAM,UAAU,mBAAmB,CACjC,IAAgC,EAChC,GAA4B,EAC5B,SAAyB;IAEzB,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE;QACtC,sEAAsE;QACtE,6DAA6D;QAC7D,MAAM,KAAK,GACT,WAAW,CAAC,EAAE,CAAC,CAAC,iBAAiB,IAAI,WAAW,CAAC,EAAE,CA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AAC,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 { classesEvalMessage } from '../evalDiagnostics';\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 =\n nodeExpando(el).__pithyForwardCtx ?? 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(classesEvalMessage, 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"]}
|
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
"category": "runtime"
|
|
6
6
|
}
|
|
7
7
|
*/
|
|
8
|
+
/** Test-only: forget which tags were warned about, so each render warns as a fresh process would. */
|
|
9
|
+
export declare function __resetComponentTagWarnings(): void;
|
|
8
10
|
/**
|
|
9
11
|
* Replace custom component tags (<Counter/>, <NestedComponent/>) with rendered output.
|
|
10
12
|
* - Skips anything under a pending nested loop host (bind after loop expansion)
|
|
@@ -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;AA2DF,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,CAiaN"}
|