gyosjs 0.5.3 → 0.5.4
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 +1 -1
- package/dist/core/composables.d.ts +3 -1
- package/dist/core/composables.d.ts.map +1 -1
- package/dist/core/portal.d.ts.map +1 -1
- package/dist/core/reactive.d.ts.map +1 -1
- package/dist/core/router/router.d.ts.map +1 -1
- package/dist/core/router/scroll.d.ts.map +1 -1
- package/dist/core/router/swap.d.ts.map +1 -1
- package/dist/core/scope-registry.d.ts.map +1 -1
- package/dist/form/form-validation.d.ts.map +1 -1
- package/dist/gyos.auto.esm.js +412 -212
- package/dist/gyos.auto.esm.js.map +1 -1
- package/dist/gyos.auto.js +412 -212
- package/dist/gyos.auto.js.map +1 -1
- package/dist/gyos.auto.min.js +2 -2
- package/dist/gyos.csp.auto.esm.js +428 -228
- package/dist/gyos.csp.auto.esm.js.map +1 -1
- package/dist/gyos.csp.auto.js +428 -228
- package/dist/gyos.csp.auto.js.map +1 -1
- package/dist/gyos.csp.auto.min.js +2 -2
- package/dist/gyos.csp.esm.js +428 -228
- package/dist/gyos.csp.esm.js.map +1 -1
- package/dist/gyos.csp.js +428 -228
- package/dist/gyos.csp.js.map +1 -1
- package/dist/gyos.csp.min.js +2 -2
- package/dist/gyos.esm.js +412 -212
- package/dist/gyos.esm.js.map +1 -1
- package/dist/gyos.js +412 -212
- package/dist/gyos.js.map +1 -1
- package/dist/gyos.min.js +2 -2
- package/dist/reactivity/signal.d.ts.map +1 -1
- package/dist/runtime/csp-evaluator.d.ts.map +1 -1
- package/dist/template/bindings.d.ts.map +1 -1
- package/dist/template/cleanup.d.ts +1 -0
- package/dist/template/cleanup.d.ts.map +1 -1
- package/dist/template/directives.d.ts.map +1 -1
- package/dist/template/effect-queue.d.ts +1 -1
- package/dist/template/effect-queue.d.ts.map +1 -1
- package/dist/template/events.d.ts.map +1 -1
- package/dist/template/process.d.ts.map +1 -1
- package/dist/template/structurals/structural.d.ts.map +1 -1
- package/dist/utils/helpers.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/gyos.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* GyosJS v0.5.
|
|
2
|
+
* GyosJS v0.5.4
|
|
3
3
|
* (c) 2026
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
@@ -122,25 +122,30 @@
|
|
|
122
122
|
}
|
|
123
123
|
const cloned = {};
|
|
124
124
|
for (const key in obj) {
|
|
125
|
-
if (
|
|
125
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
126
126
|
cloned[key] = clone(obj[key]);
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
129
|
return cloned;
|
|
130
130
|
}
|
|
131
131
|
function deepEqual(a, b) {
|
|
132
|
-
if (a
|
|
132
|
+
if (Object.is(a, b))
|
|
133
133
|
return true;
|
|
134
134
|
if (typeof a !== 'object' || typeof b !== 'object' || a == null || b == null) {
|
|
135
135
|
return false;
|
|
136
136
|
}
|
|
137
|
+
if (a instanceof Date || b instanceof Date) {
|
|
138
|
+
return a instanceof Date && b instanceof Date && Object.is(a.getTime(), b.getTime());
|
|
139
|
+
}
|
|
140
|
+
if (Array.isArray(a) !== Array.isArray(b))
|
|
141
|
+
return false;
|
|
137
142
|
const keysA = Object.keys(a);
|
|
138
143
|
const keysB = Object.keys(b);
|
|
139
144
|
if (keysA.length !== keysB.length) {
|
|
140
145
|
return false;
|
|
141
146
|
}
|
|
142
147
|
for (const key of keysA) {
|
|
143
|
-
if (!deepEqual(a[key], b[key])) {
|
|
148
|
+
if (!Object.prototype.hasOwnProperty.call(b, key) || !deepEqual(a[key], b[key])) {
|
|
144
149
|
return false;
|
|
145
150
|
}
|
|
146
151
|
}
|
|
@@ -676,8 +681,11 @@
|
|
|
676
681
|
execute.__dependencies = dependencies;
|
|
677
682
|
execute.__versions = versions;
|
|
678
683
|
try {
|
|
679
|
-
|
|
680
|
-
|
|
684
|
+
// The previous cleanup can dispose this effect before the next run.
|
|
685
|
+
if (active) {
|
|
686
|
+
const nextCleanup = fn();
|
|
687
|
+
cleanup = typeof nextCleanup === 'function' ? nextCleanup : undefined;
|
|
688
|
+
}
|
|
681
689
|
}
|
|
682
690
|
catch (error) {
|
|
683
691
|
console.error('[Effect] Error in effect:', error);
|
|
@@ -686,6 +694,25 @@
|
|
|
686
694
|
finally {
|
|
687
695
|
initialized = true;
|
|
688
696
|
currentEffect = prevEffect;
|
|
697
|
+
// stop() may have been called inside fn, before its cleanup was returned
|
|
698
|
+
// or before later reads registered more dependencies.
|
|
699
|
+
if (!active) {
|
|
700
|
+
dependencies.forEach(dependency => dependency.delete(execute));
|
|
701
|
+
dependencies.clear();
|
|
702
|
+
versions.clear();
|
|
703
|
+
effectQueue.delete(execute);
|
|
704
|
+
batchedEffects.delete(execute);
|
|
705
|
+
const finalCleanup = cleanup;
|
|
706
|
+
cleanup = undefined;
|
|
707
|
+
if (finalCleanup) {
|
|
708
|
+
try {
|
|
709
|
+
untrack(finalCleanup);
|
|
710
|
+
}
|
|
711
|
+
catch (error) {
|
|
712
|
+
console.error('[Effect] Error in final cleanup:', error);
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
}
|
|
689
716
|
effectDepth--;
|
|
690
717
|
runningEffects.delete(execute);
|
|
691
718
|
// Flush queue when we're back to top level
|
|
@@ -710,8 +737,10 @@
|
|
|
710
737
|
dependencies.clear();
|
|
711
738
|
versions.clear();
|
|
712
739
|
if (cleanup) {
|
|
740
|
+
const finalCleanup = cleanup;
|
|
741
|
+
cleanup = undefined;
|
|
713
742
|
try {
|
|
714
|
-
untrack(
|
|
743
|
+
untrack(finalCleanup);
|
|
715
744
|
}
|
|
716
745
|
catch (error) {
|
|
717
746
|
console.error('[Effect] Error in final cleanup:', error);
|
|
@@ -1439,11 +1468,6 @@
|
|
|
1439
1468
|
* @returns Evaluated value
|
|
1440
1469
|
*/
|
|
1441
1470
|
function evaluateSimpleExpression(expr, scope) {
|
|
1442
|
-
// Handle string literals
|
|
1443
|
-
if ((expr.startsWith("'") && expr.endsWith("'")) ||
|
|
1444
|
-
(expr.startsWith('"') && expr.endsWith('"'))) {
|
|
1445
|
-
return expr.slice(1, -1);
|
|
1446
|
-
}
|
|
1447
1471
|
// Handle numbers
|
|
1448
1472
|
if (!isNaN(Number(expr))) {
|
|
1449
1473
|
return Number(expr);
|
|
@@ -1490,6 +1514,8 @@
|
|
|
1490
1514
|
let instance = mountedScopes.get(el);
|
|
1491
1515
|
if (!instance)
|
|
1492
1516
|
return;
|
|
1517
|
+
// A hook may call cleanup again; each mount is disposed at most once.
|
|
1518
|
+
mountedScopes.delete(el);
|
|
1493
1519
|
// Cleanup all effects
|
|
1494
1520
|
if (instance.__gyos_effects__) {
|
|
1495
1521
|
instance.__gyos_effects__.forEach((cleanup) => {
|
|
@@ -1504,7 +1530,12 @@
|
|
|
1504
1530
|
}
|
|
1505
1531
|
// Call lifecycle hook
|
|
1506
1532
|
if (instance.onUnmount) {
|
|
1507
|
-
|
|
1533
|
+
try {
|
|
1534
|
+
instance.onUnmount.call(instance);
|
|
1535
|
+
}
|
|
1536
|
+
catch (error) {
|
|
1537
|
+
console.error('[Component] Error in onUnmount:', error);
|
|
1538
|
+
}
|
|
1508
1539
|
}
|
|
1509
1540
|
// Clear reference on element
|
|
1510
1541
|
el.__gyos_scope__ = null;
|
|
@@ -1541,6 +1572,9 @@
|
|
|
1541
1572
|
return effectQueueStack[effectQueueStack.length - 1];
|
|
1542
1573
|
}
|
|
1543
1574
|
function attachDispose(target, runner) {
|
|
1575
|
+
// An earlier queued content binding can replace this subtree during flush.
|
|
1576
|
+
if (!target.isConnected)
|
|
1577
|
+
return;
|
|
1544
1578
|
const dispose = effect(runner);
|
|
1545
1579
|
if (!target.__gyos_effects__) {
|
|
1546
1580
|
target.__gyos_effects__ = [];
|
|
@@ -2460,6 +2494,125 @@
|
|
|
2460
2494
|
return transitionManager.leave(element, transitionConfig, removeElement);
|
|
2461
2495
|
}
|
|
2462
2496
|
|
|
2497
|
+
/**
|
|
2498
|
+
* Portal/Teleport System - Render content in different DOM location
|
|
2499
|
+
* Useful for modals, tooltips, notifications that need to break out of parent containers
|
|
2500
|
+
*
|
|
2501
|
+
* @example
|
|
2502
|
+
* // Portal to body (for modals)
|
|
2503
|
+
* <div g-portal="body">
|
|
2504
|
+
* <div class="modal">Modal content</div>
|
|
2505
|
+
* </div>
|
|
2506
|
+
*
|
|
2507
|
+
* @example
|
|
2508
|
+
* // Portal to specific element
|
|
2509
|
+
* <div g-portal="#notifications">
|
|
2510
|
+
* <div class="toast">Toast message</div>
|
|
2511
|
+
* </div>
|
|
2512
|
+
*/
|
|
2513
|
+
/**
|
|
2514
|
+
* Create a portal - Move element to target location
|
|
2515
|
+
*
|
|
2516
|
+
* @param sourceEl - Element to teleport
|
|
2517
|
+
* @param targetSelector - CSS selector for destination
|
|
2518
|
+
*
|
|
2519
|
+
* @example
|
|
2520
|
+
* portalCreate(modalEl, 'body');
|
|
2521
|
+
* portalCreate(tooltipEl, '#tooltip-container');
|
|
2522
|
+
*/
|
|
2523
|
+
function portalCreate(sourceEl, targetSelector) {
|
|
2524
|
+
if (!sourceEl)
|
|
2525
|
+
return;
|
|
2526
|
+
const target = document.querySelector(targetSelector);
|
|
2527
|
+
if (!target) {
|
|
2528
|
+
console.warn(`[GyosJS] Portal target "${targetSelector}" not found`);
|
|
2529
|
+
return;
|
|
2530
|
+
}
|
|
2531
|
+
// Create placeholder comment to mark original position
|
|
2532
|
+
const placeholder = document.createComment('g-portal');
|
|
2533
|
+
sourceEl.parentNode?.insertBefore(placeholder, sourceEl);
|
|
2534
|
+
// Store original parent for restoration
|
|
2535
|
+
sourceEl.__portal_placeholder__ = placeholder;
|
|
2536
|
+
sourceEl.__portal_target__ = target;
|
|
2537
|
+
placeholder.__gyos_portal__ = sourceEl;
|
|
2538
|
+
// Move the entire element to target
|
|
2539
|
+
target.appendChild(sourceEl);
|
|
2540
|
+
DEBUG() && console.log('[Portal] Created - moved element to', targetSelector);
|
|
2541
|
+
}
|
|
2542
|
+
/**
|
|
2543
|
+
* Destroy a portal - Restore element to original position
|
|
2544
|
+
*
|
|
2545
|
+
* @param sourceEl - Element to restore
|
|
2546
|
+
*
|
|
2547
|
+
* @example
|
|
2548
|
+
* portalDestroy(modalEl); // Returns modal to original location
|
|
2549
|
+
*/
|
|
2550
|
+
function portalDestroy(sourceEl) {
|
|
2551
|
+
if (!sourceEl)
|
|
2552
|
+
return;
|
|
2553
|
+
const placeholder = sourceEl.__portal_placeholder__;
|
|
2554
|
+
if (placeholder && placeholder.parentNode) {
|
|
2555
|
+
delete placeholder.__gyos_portal__;
|
|
2556
|
+
// Move element back to original position
|
|
2557
|
+
placeholder.parentNode.insertBefore(sourceEl, placeholder);
|
|
2558
|
+
placeholder.remove();
|
|
2559
|
+
delete sourceEl.__portal_placeholder__;
|
|
2560
|
+
delete sourceEl.__portal_target__;
|
|
2561
|
+
DEBUG() && console.log('[Portal] Destroyed - restored element to original position');
|
|
2562
|
+
}
|
|
2563
|
+
}
|
|
2564
|
+
|
|
2565
|
+
/**
|
|
2566
|
+
* Cleanup utilities for disposing effects and event listeners
|
|
2567
|
+
*/
|
|
2568
|
+
/**
|
|
2569
|
+
* Dispose all effects attached to an element and its descendants
|
|
2570
|
+
* Handles both element effects (__gyos_effects__) and component lifecycle
|
|
2571
|
+
*/
|
|
2572
|
+
function disposeEffects(element = document.body, removeElement = false) {
|
|
2573
|
+
if (!element)
|
|
2574
|
+
return;
|
|
2575
|
+
disposeNode(element);
|
|
2576
|
+
if (removeElement)
|
|
2577
|
+
element.remove();
|
|
2578
|
+
}
|
|
2579
|
+
function disposeNode(element, removeElement = false) {
|
|
2580
|
+
untrack(() => disposeNodeInternal(element, removeElement));
|
|
2581
|
+
}
|
|
2582
|
+
function disposeNodeInternal(element, removeElement = false) {
|
|
2583
|
+
const portal = element.__gyos_portal__;
|
|
2584
|
+
if (portal) {
|
|
2585
|
+
delete element.__gyos_portal__;
|
|
2586
|
+
disposeNode(portal, removeElement);
|
|
2587
|
+
}
|
|
2588
|
+
// Include structural comment anchors. Snapshot before hooks mutate the tree.
|
|
2589
|
+
const children = Array.from(element.childNodes);
|
|
2590
|
+
const effects = element.__gyos_effects__;
|
|
2591
|
+
// Release ownership before callbacks to make reentrant cleanup safe.
|
|
2592
|
+
element.__gyos_effects__ = null;
|
|
2593
|
+
// Unmount scope if element has one (calls onUnmount + cleans instance.__gyos_effects__)
|
|
2594
|
+
if (element.hasAttribute?.('g-scope') || element.__gyos_scope__) {
|
|
2595
|
+
unmountScope(element);
|
|
2596
|
+
}
|
|
2597
|
+
// Dispose effects on this element (template effects like g-show, :class, {text})
|
|
2598
|
+
if (effects) {
|
|
2599
|
+
effects.forEach((dispose) => {
|
|
2600
|
+
try {
|
|
2601
|
+
dispose();
|
|
2602
|
+
}
|
|
2603
|
+
catch (error) {
|
|
2604
|
+
console.error('[GyosJS] Error disposing effect:', error);
|
|
2605
|
+
}
|
|
2606
|
+
});
|
|
2607
|
+
}
|
|
2608
|
+
// Recursively dispose effects on all descendants
|
|
2609
|
+
children.forEach(child => disposeNode(child));
|
|
2610
|
+
// Restore a portal to its placeholder before removing the owning tree.
|
|
2611
|
+
portalDestroy(element);
|
|
2612
|
+
if (removeElement)
|
|
2613
|
+
element.parentNode?.removeChild(element);
|
|
2614
|
+
}
|
|
2615
|
+
|
|
2463
2616
|
/**
|
|
2464
2617
|
* Directive Processing
|
|
2465
2618
|
* Handles built-in directives (g-show, g-html, g-text) and custom directives
|
|
@@ -2521,6 +2674,7 @@
|
|
|
2521
2674
|
const expr = element.getAttribute('g-html');
|
|
2522
2675
|
queueReactiveEffect(element, () => {
|
|
2523
2676
|
const value = evaluateExpression(expr, scope);
|
|
2677
|
+
Array.from(element.childNodes).forEach(child => disposeNode(child));
|
|
2524
2678
|
element.innerHTML = value;
|
|
2525
2679
|
});
|
|
2526
2680
|
}
|
|
@@ -2529,6 +2683,7 @@
|
|
|
2529
2683
|
const expr = element.getAttribute('g-text');
|
|
2530
2684
|
queueReactiveEffect(element, () => {
|
|
2531
2685
|
const value = evaluateExpression(expr, scope);
|
|
2686
|
+
Array.from(element.childNodes).forEach(child => disposeNode(child));
|
|
2532
2687
|
element.textContent = value;
|
|
2533
2688
|
});
|
|
2534
2689
|
}
|
|
@@ -2606,7 +2761,7 @@
|
|
|
2606
2761
|
* @param scope - Scope object for evaluation
|
|
2607
2762
|
*/
|
|
2608
2763
|
function processDirectivesStatic(el, scope) {
|
|
2609
|
-
const directives =
|
|
2764
|
+
const directives = [el, ...el.querySelectorAll('[g-show], [g-html], [g-text]')]
|
|
2610
2765
|
.filter(element => !isInIgnoredTree(element));
|
|
2611
2766
|
directives.forEach(elem => {
|
|
2612
2767
|
const element = elem;
|
|
@@ -2683,7 +2838,9 @@
|
|
|
2683
2838
|
return values;
|
|
2684
2839
|
}
|
|
2685
2840
|
function normalizeStyleProperty(property) {
|
|
2686
|
-
if (property.startsWith('--')
|
|
2841
|
+
if (property.startsWith('--'))
|
|
2842
|
+
return property;
|
|
2843
|
+
if (property.includes('-'))
|
|
2687
2844
|
return property.toLowerCase();
|
|
2688
2845
|
return property.replace(/[A-Z]/g, match => `-${match.toLowerCase()}`);
|
|
2689
2846
|
}
|
|
@@ -2862,72 +3019,6 @@
|
|
|
2862
3019
|
}
|
|
2863
3020
|
}
|
|
2864
3021
|
|
|
2865
|
-
/**
|
|
2866
|
-
* Portal/Teleport System - Render content in different DOM location
|
|
2867
|
-
* Useful for modals, tooltips, notifications that need to break out of parent containers
|
|
2868
|
-
*
|
|
2869
|
-
* @example
|
|
2870
|
-
* // Portal to body (for modals)
|
|
2871
|
-
* <div g-portal="body">
|
|
2872
|
-
* <div class="modal">Modal content</div>
|
|
2873
|
-
* </div>
|
|
2874
|
-
*
|
|
2875
|
-
* @example
|
|
2876
|
-
* // Portal to specific element
|
|
2877
|
-
* <div g-portal="#notifications">
|
|
2878
|
-
* <div class="toast">Toast message</div>
|
|
2879
|
-
* </div>
|
|
2880
|
-
*/
|
|
2881
|
-
/**
|
|
2882
|
-
* Create a portal - Move element to target location
|
|
2883
|
-
*
|
|
2884
|
-
* @param sourceEl - Element to teleport
|
|
2885
|
-
* @param targetSelector - CSS selector for destination
|
|
2886
|
-
*
|
|
2887
|
-
* @example
|
|
2888
|
-
* portalCreate(modalEl, 'body');
|
|
2889
|
-
* portalCreate(tooltipEl, '#tooltip-container');
|
|
2890
|
-
*/
|
|
2891
|
-
function portalCreate(sourceEl, targetSelector) {
|
|
2892
|
-
if (!sourceEl)
|
|
2893
|
-
return;
|
|
2894
|
-
const target = document.querySelector(targetSelector);
|
|
2895
|
-
if (!target) {
|
|
2896
|
-
console.warn(`[GyosJS] Portal target "${targetSelector}" not found`);
|
|
2897
|
-
return;
|
|
2898
|
-
}
|
|
2899
|
-
// Create placeholder comment to mark original position
|
|
2900
|
-
const placeholder = document.createComment('g-portal');
|
|
2901
|
-
sourceEl.parentNode?.insertBefore(placeholder, sourceEl);
|
|
2902
|
-
// Store original parent for restoration
|
|
2903
|
-
sourceEl.__portal_placeholder__ = placeholder;
|
|
2904
|
-
sourceEl.__portal_target__ = target;
|
|
2905
|
-
// Move the entire element to target
|
|
2906
|
-
target.appendChild(sourceEl);
|
|
2907
|
-
DEBUG() && console.log('[Portal] Created - moved element to', targetSelector);
|
|
2908
|
-
}
|
|
2909
|
-
/**
|
|
2910
|
-
* Destroy a portal - Restore element to original position
|
|
2911
|
-
*
|
|
2912
|
-
* @param sourceEl - Element to restore
|
|
2913
|
-
*
|
|
2914
|
-
* @example
|
|
2915
|
-
* portalDestroy(modalEl); // Returns modal to original location
|
|
2916
|
-
*/
|
|
2917
|
-
function portalDestroy(sourceEl) {
|
|
2918
|
-
if (!sourceEl)
|
|
2919
|
-
return;
|
|
2920
|
-
const placeholder = sourceEl.__portal_placeholder__;
|
|
2921
|
-
if (placeholder && placeholder.parentNode) {
|
|
2922
|
-
// Move element back to original position
|
|
2923
|
-
placeholder.parentNode.insertBefore(sourceEl, placeholder);
|
|
2924
|
-
placeholder.remove();
|
|
2925
|
-
delete sourceEl.__portal_placeholder__;
|
|
2926
|
-
delete sourceEl.__portal_target__;
|
|
2927
|
-
DEBUG() && console.log('[Portal] Destroyed - restored element to original position');
|
|
2928
|
-
}
|
|
2929
|
-
}
|
|
2930
|
-
|
|
2931
3022
|
/**
|
|
2932
3023
|
* Validator registry
|
|
2933
3024
|
*/
|
|
@@ -3113,7 +3204,7 @@
|
|
|
3113
3204
|
* @example "min(18)"
|
|
3114
3205
|
*/
|
|
3115
3206
|
validator('min', (value, min) => {
|
|
3116
|
-
if (
|
|
3207
|
+
if (value == null || value === '')
|
|
3117
3208
|
return true;
|
|
3118
3209
|
const minVal = parseFloat(min);
|
|
3119
3210
|
return parseFloat(value) >= minVal || `Minimum value is ${minVal}`;
|
|
@@ -3124,7 +3215,7 @@
|
|
|
3124
3215
|
* @example "max(100)"
|
|
3125
3216
|
*/
|
|
3126
3217
|
validator('max', (value, max) => {
|
|
3127
|
-
if (
|
|
3218
|
+
if (value == null || value === '')
|
|
3128
3219
|
return true;
|
|
3129
3220
|
const maxVal = parseFloat(max);
|
|
3130
3221
|
return parseFloat(value) <= maxVal || `Maximum value is ${maxVal}`;
|
|
@@ -3145,9 +3236,9 @@
|
|
|
3145
3236
|
* @example "integer"
|
|
3146
3237
|
*/
|
|
3147
3238
|
validator('integer', (value) => {
|
|
3148
|
-
if (
|
|
3239
|
+
if (value == null || value === '')
|
|
3149
3240
|
return true;
|
|
3150
|
-
return Number.isInteger(
|
|
3241
|
+
return (String(value).trim() !== '' && Number.isInteger(Number(value))) || 'Must be an integer';
|
|
3151
3242
|
});
|
|
3152
3243
|
/**
|
|
3153
3244
|
* Numeric - Must contain only digits
|
|
@@ -3260,7 +3351,7 @@
|
|
|
3260
3351
|
return 'Invalid date format (use YYYY-MM-DD)';
|
|
3261
3352
|
}
|
|
3262
3353
|
const date = new Date(value);
|
|
3263
|
-
return !isNaN(date.getTime()) || 'Invalid date';
|
|
3354
|
+
return (!isNaN(date.getTime()) && date.toISOString().slice(0, 10) === value) || 'Invalid date';
|
|
3264
3355
|
});
|
|
3265
3356
|
/**
|
|
3266
3357
|
* Before - Date must be before specified date
|
|
@@ -3340,7 +3431,7 @@
|
|
|
3340
3431
|
* @example "between(18,65)"
|
|
3341
3432
|
*/
|
|
3342
3433
|
validator('between', (value, min, max) => {
|
|
3343
|
-
if (
|
|
3434
|
+
if (value == null || value === '')
|
|
3344
3435
|
return true;
|
|
3345
3436
|
const num = parseFloat(value);
|
|
3346
3437
|
const minVal = parseFloat(min);
|
|
@@ -3594,8 +3685,16 @@
|
|
|
3594
3685
|
// Initialize error state - direct mutation
|
|
3595
3686
|
state.errors.value[modelAttr] = null;
|
|
3596
3687
|
state.touched.value[modelAttr] = false;
|
|
3597
|
-
|
|
3598
|
-
|
|
3688
|
+
let active = true;
|
|
3689
|
+
let generation = 0;
|
|
3690
|
+
let timeout;
|
|
3691
|
+
let latestValidation = Promise.resolve();
|
|
3692
|
+
// Each control publishes only its newest result. validateAll also waits for
|
|
3693
|
+
// a newer validation that superseded the request it originally started.
|
|
3694
|
+
const runValidation = async () => {
|
|
3695
|
+
if (!active)
|
|
3696
|
+
return;
|
|
3697
|
+
const request = ++generation;
|
|
3599
3698
|
const value = getControlValue(el, form);
|
|
3600
3699
|
// Get form context for validators that need it (like 'same')
|
|
3601
3700
|
const formData = {};
|
|
@@ -3610,7 +3709,26 @@
|
|
|
3610
3709
|
}
|
|
3611
3710
|
}
|
|
3612
3711
|
});
|
|
3613
|
-
|
|
3712
|
+
let error;
|
|
3713
|
+
try {
|
|
3714
|
+
error = await validate(value, rules, { form: formData, field: modelAttr });
|
|
3715
|
+
}
|
|
3716
|
+
catch (cause) {
|
|
3717
|
+
console.error('[GyosJS] Field validation failed:', cause);
|
|
3718
|
+
error = 'Validation failed. Please try again.';
|
|
3719
|
+
}
|
|
3720
|
+
if (!active)
|
|
3721
|
+
return;
|
|
3722
|
+
if (request !== generation) {
|
|
3723
|
+
await latestValidation;
|
|
3724
|
+
return;
|
|
3725
|
+
}
|
|
3726
|
+
// Input may have changed while its debounce timer has not fired yet.
|
|
3727
|
+
// A submit must validate the current value before publishing approval.
|
|
3728
|
+
if (value !== getControlValue(el, form)) {
|
|
3729
|
+
await validateField();
|
|
3730
|
+
return;
|
|
3731
|
+
}
|
|
3614
3732
|
// Update errors - only if changed
|
|
3615
3733
|
const currentErrors = state.errors.value;
|
|
3616
3734
|
if (currentErrors[modelAttr] !== error) {
|
|
@@ -3620,6 +3738,12 @@
|
|
|
3620
3738
|
updateFormState(state);
|
|
3621
3739
|
}
|
|
3622
3740
|
};
|
|
3741
|
+
const validateField = () => {
|
|
3742
|
+
clearTimeout(timeout);
|
|
3743
|
+
timeout = undefined;
|
|
3744
|
+
latestValidation = runValidation();
|
|
3745
|
+
return latestValidation;
|
|
3746
|
+
};
|
|
3623
3747
|
// Register validator for validateAll
|
|
3624
3748
|
if (!formValidators.has(form)) {
|
|
3625
3749
|
formValidators.set(form, []);
|
|
@@ -3637,7 +3761,6 @@
|
|
|
3637
3761
|
};
|
|
3638
3762
|
el.addEventListener('blur', blurListener);
|
|
3639
3763
|
// Validate on input (with debounce)
|
|
3640
|
-
let timeout;
|
|
3641
3764
|
const inputListener = () => {
|
|
3642
3765
|
clearTimeout(timeout);
|
|
3643
3766
|
timeout = setTimeout(() => {
|
|
@@ -3649,6 +3772,24 @@
|
|
|
3649
3772
|
el.__gyos_effects__ = [];
|
|
3650
3773
|
}
|
|
3651
3774
|
el.__gyos_effects__.push(() => {
|
|
3775
|
+
active = false;
|
|
3776
|
+
generation++;
|
|
3777
|
+
delete el[validationFieldKey];
|
|
3778
|
+
const validators = formValidators.get(form);
|
|
3779
|
+
const index = validators?.indexOf(validateField) ?? -1;
|
|
3780
|
+
if (index !== -1)
|
|
3781
|
+
validators.splice(index, 1);
|
|
3782
|
+
const sharesField = Array.from(form.querySelectorAll('input, textarea, select'))
|
|
3783
|
+
.some(control => control[validationFieldKey] === modelAttr);
|
|
3784
|
+
if (!sharesField) {
|
|
3785
|
+
const errors = { ...state.errors.peek };
|
|
3786
|
+
delete errors[modelAttr];
|
|
3787
|
+
const touched = { ...state.touched.peek };
|
|
3788
|
+
delete touched[modelAttr];
|
|
3789
|
+
state.errors.value = errors;
|
|
3790
|
+
state.touched.value = touched;
|
|
3791
|
+
updateFormState(state);
|
|
3792
|
+
}
|
|
3652
3793
|
el.removeEventListener('blur', blurListener);
|
|
3653
3794
|
el.removeEventListener('input', inputListener);
|
|
3654
3795
|
clearTimeout(timeout);
|
|
@@ -3721,7 +3862,9 @@
|
|
|
3721
3862
|
processFormDirective(root, scope);
|
|
3722
3863
|
}
|
|
3723
3864
|
// Then process g-validate
|
|
3724
|
-
root.querySelectorAll('[g-validate]').forEach(el => {
|
|
3865
|
+
[root, ...root.querySelectorAll('[g-validate]')].forEach(el => {
|
|
3866
|
+
if (!el.hasAttribute('g-validate'))
|
|
3867
|
+
return;
|
|
3725
3868
|
if (isInIgnoredTree(el))
|
|
3726
3869
|
return;
|
|
3727
3870
|
if (el instanceof HTMLElement) {
|
|
@@ -3729,7 +3872,9 @@
|
|
|
3729
3872
|
}
|
|
3730
3873
|
});
|
|
3731
3874
|
// Finally process g-errors
|
|
3732
|
-
root.querySelectorAll('[g-errors]').forEach(el => {
|
|
3875
|
+
[root, ...root.querySelectorAll('[g-errors]')].forEach(el => {
|
|
3876
|
+
if (!el.hasAttribute('g-errors'))
|
|
3877
|
+
return;
|
|
3733
3878
|
if (isInIgnoredTree(el))
|
|
3734
3879
|
return;
|
|
3735
3880
|
if (el instanceof HTMLElement) {
|
|
@@ -3760,12 +3905,13 @@
|
|
|
3760
3905
|
/**
|
|
3761
3906
|
* Execute handler with modifiers (prevent, stop)
|
|
3762
3907
|
*/
|
|
3763
|
-
function
|
|
3764
|
-
// Handle prevent/stop modifiers
|
|
3908
|
+
function applyEventModifiers(e, modifiers) {
|
|
3765
3909
|
if (modifiers.includes('prevent'))
|
|
3766
3910
|
e.preventDefault();
|
|
3767
3911
|
if (modifiers.includes('stop'))
|
|
3768
3912
|
e.stopPropagation();
|
|
3913
|
+
}
|
|
3914
|
+
function executeHandler(handler, scope, e) {
|
|
3769
3915
|
try {
|
|
3770
3916
|
expressionRuntime().execute(handler, scope, e);
|
|
3771
3917
|
}
|
|
@@ -3855,10 +4001,11 @@
|
|
|
3855
4001
|
if (target instanceof Element && target.closest('[g-ignore-outside-click]'))
|
|
3856
4002
|
return;
|
|
3857
4003
|
if (element !== target && !element.contains(target)) {
|
|
4004
|
+
applyEventModifiers(e, modifiers);
|
|
3858
4005
|
const currentScope = getScopeFromElement(element) || scope;
|
|
3859
4006
|
if (modifiers.includes('once'))
|
|
3860
4007
|
cleanup();
|
|
3861
|
-
executeHandler(handler, currentScope, e
|
|
4008
|
+
executeHandler(handler, currentScope, e);
|
|
3862
4009
|
}
|
|
3863
4010
|
};
|
|
3864
4011
|
// Cleanup function
|
|
@@ -3935,13 +4082,14 @@
|
|
|
3935
4082
|
return; // Key doesn't match, skip
|
|
3936
4083
|
}
|
|
3937
4084
|
// Execute handler with optional debounce
|
|
4085
|
+
applyEventModifiers(e, modifiers);
|
|
3938
4086
|
const execute = () => {
|
|
3939
4087
|
if (modifiers.includes('once'))
|
|
3940
4088
|
cleanup();
|
|
3941
4089
|
// Batch in case handler mutates multiple signals
|
|
3942
4090
|
batch(() => {
|
|
3943
4091
|
const currentScope = getScopeFromElement(element) || scope;
|
|
3944
|
-
executeHandler(handler, currentScope, e
|
|
4092
|
+
executeHandler(handler, currentScope, e);
|
|
3945
4093
|
});
|
|
3946
4094
|
};
|
|
3947
4095
|
if (debounceMs > 0) {
|
|
@@ -4191,60 +4339,6 @@
|
|
|
4191
4339
|
element.removeAttribute(modelAttr.name);
|
|
4192
4340
|
}
|
|
4193
4341
|
|
|
4194
|
-
/**
|
|
4195
|
-
* Cleanup utilities for disposing effects and event listeners
|
|
4196
|
-
*/
|
|
4197
|
-
/**
|
|
4198
|
-
* Dispose all effects attached to an element and its descendants
|
|
4199
|
-
* Handles both element effects (__gyos_effects__) and component lifecycle
|
|
4200
|
-
*/
|
|
4201
|
-
function disposeEffects(element = document.body, removeElement = false) {
|
|
4202
|
-
if (!element)
|
|
4203
|
-
return;
|
|
4204
|
-
// Unmount scope if element has one (calls onUnmount + cleans instance.__gyos_effects__)
|
|
4205
|
-
if (element.hasAttribute?.('g-scope') || element.__gyos_scope__) {
|
|
4206
|
-
unmountScope(element);
|
|
4207
|
-
}
|
|
4208
|
-
// Dispose effects on this element (template effects like g-show, :class, {text})
|
|
4209
|
-
if (element.__gyos_effects__) {
|
|
4210
|
-
const effects = element.__gyos_effects__;
|
|
4211
|
-
effects.forEach((dispose) => {
|
|
4212
|
-
try {
|
|
4213
|
-
dispose();
|
|
4214
|
-
}
|
|
4215
|
-
catch (error) {
|
|
4216
|
-
console.error('[GyosJS] Error disposing effect:', error);
|
|
4217
|
-
}
|
|
4218
|
-
});
|
|
4219
|
-
element.__gyos_effects__ = null;
|
|
4220
|
-
}
|
|
4221
|
-
// Recursively dispose effects on all descendants
|
|
4222
|
-
const descendants = element.querySelectorAll('*');
|
|
4223
|
-
descendants.forEach((child) => {
|
|
4224
|
-
// Unmount child scopes
|
|
4225
|
-
if (child.hasAttribute?.('g-scope') || child.__gyos_scope__) {
|
|
4226
|
-
unmountScope(child);
|
|
4227
|
-
}
|
|
4228
|
-
// Dispose child template effects
|
|
4229
|
-
if (child.__gyos_effects__) {
|
|
4230
|
-
const effects = child.__gyos_effects__;
|
|
4231
|
-
effects.forEach((dispose) => {
|
|
4232
|
-
try {
|
|
4233
|
-
dispose();
|
|
4234
|
-
}
|
|
4235
|
-
catch (error) {
|
|
4236
|
-
console.error('[GyosJS] Error disposing child effect:', error);
|
|
4237
|
-
}
|
|
4238
|
-
});
|
|
4239
|
-
child.__gyos_effects__ = null;
|
|
4240
|
-
}
|
|
4241
|
-
});
|
|
4242
|
-
// portal cleanup could be added here if needed
|
|
4243
|
-
portalDestroy(element);
|
|
4244
|
-
if (removeElement)
|
|
4245
|
-
element.remove();
|
|
4246
|
-
}
|
|
4247
|
-
|
|
4248
4342
|
const storeCache = new WeakMap();
|
|
4249
4343
|
const reactiveProxies = new WeakSet();
|
|
4250
4344
|
const fieldSignals = new WeakMap();
|
|
@@ -4259,7 +4353,7 @@
|
|
|
4259
4353
|
if (visited.has(ver))
|
|
4260
4354
|
return;
|
|
4261
4355
|
visited.add(ver);
|
|
4262
|
-
ver.value
|
|
4356
|
+
ver.update(value => value + 1);
|
|
4263
4357
|
const parents = versionParents.get(ver);
|
|
4264
4358
|
if (parents) {
|
|
4265
4359
|
parents.forEach(parentVer => bumpSignal(parentVer));
|
|
@@ -4297,7 +4391,7 @@
|
|
|
4297
4391
|
}
|
|
4298
4392
|
const STORE_MARKER = Symbol.for('gyos.store');
|
|
4299
4393
|
function markStore(obj) {
|
|
4300
|
-
if (
|
|
4394
|
+
if (Object.prototype.hasOwnProperty.call(obj, STORE_MARKER))
|
|
4301
4395
|
return obj;
|
|
4302
4396
|
Reflect.defineProperty(obj, STORE_MARKER, {
|
|
4303
4397
|
value: true,
|
|
@@ -4311,7 +4405,7 @@
|
|
|
4311
4405
|
// markRaw: đánh dấu object để reactive() bỏ qua, không tạo proxy
|
|
4312
4406
|
const RAW_MARKER = Symbol.for('gyos.raw');
|
|
4313
4407
|
function markRaw(obj) {
|
|
4314
|
-
if (
|
|
4408
|
+
if (Object.prototype.hasOwnProperty.call(obj, RAW_MARKER))
|
|
4315
4409
|
return obj;
|
|
4316
4410
|
Reflect.defineProperty(obj, RAW_MARKER, {
|
|
4317
4411
|
value: true,
|
|
@@ -4325,7 +4419,7 @@
|
|
|
4325
4419
|
// shallow: chỉ reactive "lớp ngoài", không reactive sâu bên trong
|
|
4326
4420
|
const SHALLOW_MARKER = Symbol.for('gyos.shallow');
|
|
4327
4421
|
function shallow(obj) {
|
|
4328
|
-
if (
|
|
4422
|
+
if (Object.prototype.hasOwnProperty.call(obj, SHALLOW_MARKER))
|
|
4329
4423
|
return obj;
|
|
4330
4424
|
Reflect.defineProperty(obj, SHALLOW_MARKER, {
|
|
4331
4425
|
value: true,
|
|
@@ -4359,6 +4453,25 @@
|
|
|
4359
4453
|
}
|
|
4360
4454
|
versionParentsSet.add(parentVersion);
|
|
4361
4455
|
}
|
|
4456
|
+
// Array [[DefineOwnProperty]] can change length or remove indices without
|
|
4457
|
+
// entering their individual Proxy traps. Keep already observed fields current.
|
|
4458
|
+
function syncArrayFields(target, previousLength) {
|
|
4459
|
+
if (previousLength === undefined)
|
|
4460
|
+
return;
|
|
4461
|
+
const array = target;
|
|
4462
|
+
if (array.length === previousLength)
|
|
4463
|
+
return;
|
|
4464
|
+
const signals = fieldSignals.get(target);
|
|
4465
|
+
signals?.get('length')?.update(() => array.length);
|
|
4466
|
+
if (array.length < previousLength) {
|
|
4467
|
+
signals?.forEach((sig, key) => {
|
|
4468
|
+
if (typeof key === 'string' && /^(0|[1-9]\d*)$/.test(key)
|
|
4469
|
+
&& Number(key) >= array.length && Number(key) < previousLength) {
|
|
4470
|
+
sig.update(() => Reflect.get(target, key));
|
|
4471
|
+
}
|
|
4472
|
+
});
|
|
4473
|
+
}
|
|
4474
|
+
}
|
|
4362
4475
|
function reactive(raw) {
|
|
4363
4476
|
if (!raw || typeof raw !== "object" || isBuiltInObject(raw)) {
|
|
4364
4477
|
return raw;
|
|
@@ -4396,6 +4509,8 @@
|
|
|
4396
4509
|
return value;
|
|
4397
4510
|
}
|
|
4398
4511
|
if (isStoreObject(value)) {
|
|
4512
|
+
if (hasCurrentEffect())
|
|
4513
|
+
getFieldSignal(target, key, value).value;
|
|
4399
4514
|
return value;
|
|
4400
4515
|
}
|
|
4401
4516
|
// Check if this is a getter property (computed property)
|
|
@@ -4472,10 +4587,6 @@
|
|
|
4472
4587
|
}
|
|
4473
4588
|
// capture old value BEFORE write to detect real changes
|
|
4474
4589
|
const oldValue = target[key];
|
|
4475
|
-
// VALUE LÀ STORE → set thẳng, KHÔNG tạo signal, KHÔNG trigger
|
|
4476
|
-
if (isStoreObject(value)) {
|
|
4477
|
-
return Reflect.set(target, key, value, receiver);
|
|
4478
|
-
}
|
|
4479
4590
|
const ok = Reflect.set(target, key, value, receiver);
|
|
4480
4591
|
if (!ok)
|
|
4481
4592
|
return false;
|
|
@@ -4517,7 +4628,9 @@
|
|
|
4517
4628
|
const oldDesc = Object.getOwnPropertyDescriptor(target, key);
|
|
4518
4629
|
const oldValue = oldDesc && "value" in oldDesc ? oldDesc.value : undefined;
|
|
4519
4630
|
const newValue = "value" in descriptor ? descriptor.value : oldValue;
|
|
4631
|
+
const previousLength = Array.isArray(target) ? target.length : undefined;
|
|
4520
4632
|
const ok = Reflect.defineProperty(target, key, descriptor);
|
|
4633
|
+
syncArrayFields(target, previousLength);
|
|
4521
4634
|
if (!ok)
|
|
4522
4635
|
return false;
|
|
4523
4636
|
if ('value' in descriptor) {
|
|
@@ -4543,7 +4656,7 @@
|
|
|
4543
4656
|
// tạo 1 version signal cho root scope
|
|
4544
4657
|
const version = ensureVersion(source);
|
|
4545
4658
|
const proxy = reactive(source);
|
|
4546
|
-
if (!
|
|
4659
|
+
if (!Object.prototype.hasOwnProperty.call(proxy, SCOPE_VERSION)) {
|
|
4547
4660
|
Object.defineProperty(proxy, SCOPE_VERSION, {
|
|
4548
4661
|
value: version,
|
|
4549
4662
|
enumerable: false,
|
|
@@ -4665,6 +4778,20 @@
|
|
|
4665
4778
|
function setRootScope(element, scope) {
|
|
4666
4779
|
element.__gyos_scope__ = scope;
|
|
4667
4780
|
}
|
|
4781
|
+
function inheritScope(child, parent) {
|
|
4782
|
+
attachParentScope(child, parent);
|
|
4783
|
+
for (const key in parent) {
|
|
4784
|
+
if (!(key in child)) {
|
|
4785
|
+
Object.defineProperty(child, key, {
|
|
4786
|
+
get: () => parent[key],
|
|
4787
|
+
set: value => { parent[key] = value; },
|
|
4788
|
+
enumerable: true,
|
|
4789
|
+
configurable: true,
|
|
4790
|
+
});
|
|
4791
|
+
}
|
|
4792
|
+
}
|
|
4793
|
+
return child;
|
|
4794
|
+
}
|
|
4668
4795
|
/**
|
|
4669
4796
|
* Create child scope for *for loops
|
|
4670
4797
|
* Inherits parent scope properties via getters
|
|
@@ -4735,13 +4862,15 @@
|
|
|
4735
4862
|
template.removeAttribute('g-key');
|
|
4736
4863
|
const placeholder = document.createComment('*for');
|
|
4737
4864
|
element.parentNode?.replaceChild(placeholder, element);
|
|
4865
|
+
const end = document.createComment('/*for');
|
|
4866
|
+
placeholder.parentNode?.insertBefore(end, placeholder.nextSibling);
|
|
4738
4867
|
// Store previous items for key-based diffing
|
|
4739
4868
|
// WARNING: Use primitive keys (id, string, number) to avoid memory leaks
|
|
4740
4869
|
// Object keys will prevent garbage collection
|
|
4741
4870
|
let prevItemsMap = new Map();
|
|
4742
4871
|
let prevListRef = null;
|
|
4743
4872
|
let prevVersionValue = undefined;
|
|
4744
|
-
queueReactiveEffect(
|
|
4873
|
+
queueReactiveEffect(placeholder, () => {
|
|
4745
4874
|
// Parse: "item in items" or "item, index in items"
|
|
4746
4875
|
const match = expr.match(/(.+?)\s+in\s+(.+)/);
|
|
4747
4876
|
if (!match)
|
|
@@ -4768,7 +4897,7 @@
|
|
|
4768
4897
|
}
|
|
4769
4898
|
else {
|
|
4770
4899
|
// Simple re-render (no diffing)
|
|
4771
|
-
processForSimple(placeholder, template, list, itemVar, currentScope);
|
|
4900
|
+
processForSimple(placeholder, end, template, list, itemVar, currentScope);
|
|
4772
4901
|
}
|
|
4773
4902
|
});
|
|
4774
4903
|
});
|
|
@@ -4875,16 +5004,19 @@
|
|
|
4875
5004
|
/**
|
|
4876
5005
|
* Process *for without keys (simple re-render)
|
|
4877
5006
|
*/
|
|
4878
|
-
function processForSimple(placeholder, template, list, itemVar, currentScope) {
|
|
5007
|
+
function processForSimple(placeholder, end, template, list, itemVar, currentScope) {
|
|
4879
5008
|
// Remove all existing items
|
|
4880
5009
|
let next = placeholder.nextSibling;
|
|
4881
|
-
while (next && next
|
|
5010
|
+
while (next && next !== end) {
|
|
4882
5011
|
const toRemove = next;
|
|
4883
5012
|
next = next.nextSibling;
|
|
4884
5013
|
if (toRemove instanceof Element) {
|
|
4885
5014
|
unmountChildScopes(toRemove);
|
|
4886
5015
|
disposeEffects(toRemove, true);
|
|
4887
5016
|
}
|
|
5017
|
+
else {
|
|
5018
|
+
disposeNode(toRemove, true);
|
|
5019
|
+
}
|
|
4888
5020
|
}
|
|
4889
5021
|
// Render new items
|
|
4890
5022
|
// Track last inserted node to maintain order
|
|
@@ -4977,7 +5109,7 @@
|
|
|
4977
5109
|
element.parentNode?.replaceChild(placeholder, element);
|
|
4978
5110
|
let currentElement = null;
|
|
4979
5111
|
let currentIndex = -1;
|
|
4980
|
-
queueReactiveEffect(
|
|
5112
|
+
queueReactiveEffect(placeholder, () => {
|
|
4981
5113
|
// Find first truthy condition
|
|
4982
5114
|
let matchedIndex = -1;
|
|
4983
5115
|
for (let i = 0; i < templates.length; i++) {
|
|
@@ -5103,14 +5235,13 @@
|
|
|
5103
5235
|
const switchValue = evaluateExpression(switchExpr, currentScope, false);
|
|
5104
5236
|
// Find matching case
|
|
5105
5237
|
let matchedIndex = -1;
|
|
5238
|
+
let defaultIndex = -1;
|
|
5106
5239
|
for (let i = 0; i < templates.length; i++) {
|
|
5107
5240
|
const { value } = templates[i];
|
|
5108
5241
|
if (value === null) {
|
|
5109
|
-
|
|
5110
|
-
|
|
5111
|
-
|
|
5112
|
-
}
|
|
5113
|
-
break;
|
|
5242
|
+
if (defaultIndex === -1)
|
|
5243
|
+
defaultIndex = i;
|
|
5244
|
+
continue;
|
|
5114
5245
|
}
|
|
5115
5246
|
const caseValue = evaluateExpression(value, currentScope, false);
|
|
5116
5247
|
if (switchValue === caseValue) {
|
|
@@ -5118,6 +5249,8 @@
|
|
|
5118
5249
|
break;
|
|
5119
5250
|
}
|
|
5120
5251
|
}
|
|
5252
|
+
if (matchedIndex === -1)
|
|
5253
|
+
matchedIndex = defaultIndex;
|
|
5121
5254
|
// If same case, do nothing
|
|
5122
5255
|
if (matchedIndex === currentIndex)
|
|
5123
5256
|
return;
|
|
@@ -5236,6 +5369,7 @@
|
|
|
5236
5369
|
let requestVersion = 0;
|
|
5237
5370
|
// Helper to render state
|
|
5238
5371
|
const renderState = (state, data) => {
|
|
5372
|
+
currentState = state;
|
|
5239
5373
|
// Remove old element
|
|
5240
5374
|
if (currentElement) {
|
|
5241
5375
|
const elemToRemove = currentElement;
|
|
@@ -5261,20 +5395,19 @@
|
|
|
5261
5395
|
else if (state === 'fulfilled' && thenTemplate) {
|
|
5262
5396
|
template = thenTemplate.template;
|
|
5263
5397
|
// Create child scope with data if varName provided
|
|
5264
|
-
if (thenTemplate.varName
|
|
5265
|
-
scope = {
|
|
5398
|
+
if (thenTemplate.varName) {
|
|
5399
|
+
scope = inheritScope({ [thenTemplate.varName]: data }, currentScope);
|
|
5266
5400
|
}
|
|
5267
5401
|
}
|
|
5268
5402
|
else if (state === 'rejected' && catchTemplate) {
|
|
5269
5403
|
template = catchTemplate.template;
|
|
5270
5404
|
// Create child scope with error if varName provided
|
|
5271
|
-
if (catchTemplate.varName
|
|
5272
|
-
scope = {
|
|
5405
|
+
if (catchTemplate.varName) {
|
|
5406
|
+
scope = inheritScope({ [catchTemplate.varName]: data }, currentScope);
|
|
5273
5407
|
}
|
|
5274
5408
|
}
|
|
5275
5409
|
if (template) {
|
|
5276
5410
|
currentElement = getCachedTemplate(template);
|
|
5277
|
-
currentState = state;
|
|
5278
5411
|
setRootScope(currentElement, scope);
|
|
5279
5412
|
// Apply transition initial state
|
|
5280
5413
|
const elementsWithTransition = findElementsWithTransition(currentElement);
|
|
@@ -5302,8 +5435,6 @@
|
|
|
5302
5435
|
}
|
|
5303
5436
|
}
|
|
5304
5437
|
};
|
|
5305
|
-
// Initial state: pending
|
|
5306
|
-
renderState('pending');
|
|
5307
5438
|
// Track promise expression
|
|
5308
5439
|
queueReactiveEffect(element, () => {
|
|
5309
5440
|
const promiseOrValue = evaluateExpression(awaitExpr, currentScope, false);
|
|
@@ -5327,6 +5458,8 @@
|
|
|
5327
5458
|
renderState('rejected', error);
|
|
5328
5459
|
}
|
|
5329
5460
|
});
|
|
5461
|
+
// Both reruns and disposal invalidate callbacks from this request.
|
|
5462
|
+
return () => { requestVersion++; };
|
|
5330
5463
|
});
|
|
5331
5464
|
}
|
|
5332
5465
|
|
|
@@ -5394,6 +5527,9 @@
|
|
|
5394
5527
|
structuralElements.sort((a, b) => a.depth - b.depth);
|
|
5395
5528
|
// Process each structural directive
|
|
5396
5529
|
structuralElements.forEach(({ element, type }) => {
|
|
5530
|
+
// Parent directives parse their clones, not the detached original tree.
|
|
5531
|
+
if (!root.contains(element) || elementBelongsToAnotherScope(element, scope))
|
|
5532
|
+
return;
|
|
5397
5533
|
if (type === 'for') {
|
|
5398
5534
|
processForDirective(element, scope);
|
|
5399
5535
|
}
|
|
@@ -5429,6 +5565,10 @@
|
|
|
5429
5565
|
function processParse(element, scope, fromStructural = false) {
|
|
5430
5566
|
if (isInIgnoredTree(element))
|
|
5431
5567
|
return;
|
|
5568
|
+
if (fromStructural && element.hasAttribute('g-scope') && mountFunction) {
|
|
5569
|
+
mountFunction(element);
|
|
5570
|
+
return;
|
|
5571
|
+
}
|
|
5432
5572
|
beginEffectCollection();
|
|
5433
5573
|
const allElements = [
|
|
5434
5574
|
element,
|
|
@@ -5450,7 +5590,7 @@
|
|
|
5450
5590
|
return;
|
|
5451
5591
|
if (el.attributes.length === 0)
|
|
5452
5592
|
return; // Skip if no attributes
|
|
5453
|
-
if (elementBelongsToAnotherScope(el, scope)
|
|
5593
|
+
if (elementBelongsToAnotherScope(el, scope)) {
|
|
5454
5594
|
// (fromStructural allows processing newly created elements inside structurals)
|
|
5455
5595
|
return; // Skip if inside mounted scope
|
|
5456
5596
|
}
|
|
@@ -5458,8 +5598,9 @@
|
|
|
5458
5598
|
if (el.hasAttribute('g-static')) {
|
|
5459
5599
|
// Pre-process g-static elements (mark and render them before reactive processing)
|
|
5460
5600
|
processStaticElements(el, scope);
|
|
5601
|
+
return;
|
|
5461
5602
|
}
|
|
5462
|
-
if (el.hasAttribute('g-form')) {
|
|
5603
|
+
if (el.hasAttribute('g-form') || el.hasAttribute('g-validate') || el.hasAttribute('g-errors')) {
|
|
5463
5604
|
// Process form validation (g-form, g-validate, g-errors)
|
|
5464
5605
|
processFormValidation(el, scope);
|
|
5465
5606
|
}
|
|
@@ -5809,7 +5950,7 @@
|
|
|
5809
5950
|
const allElements = [...elements, ...elementsNotScope].filter(el => !isInIgnoredTree(el));
|
|
5810
5951
|
allElements.sort((a, b) => getDepth(b) - getDepth(a));
|
|
5811
5952
|
// Filter out elements inside structural directives - they will be mounted by structural processor
|
|
5812
|
-
allElements.forEach(el => !hasStructuralParent(el) && mount(el));
|
|
5953
|
+
allElements.forEach(el => !el.hasAttribute('*if') && !el.hasAttribute('*for') && !hasStructuralParent(el) && mount(el));
|
|
5813
5954
|
diagnoseUnscopedReveals(document.body);
|
|
5814
5955
|
}
|
|
5815
5956
|
function mountTree(el) {
|
|
@@ -5820,7 +5961,7 @@
|
|
|
5820
5961
|
.filter(element => element.hasAttribute('g-scope') || hasAutoScope(element));
|
|
5821
5962
|
// Child scopes own their subtree before an existing parent scope parses the insertion.
|
|
5822
5963
|
candidates.sort((a, b) => getDepth(b) - getDepth(a));
|
|
5823
|
-
candidates.forEach(element => !hasStructuralParent(element) && mount(element));
|
|
5964
|
+
candidates.forEach(element => !element.hasAttribute('*if') && !element.hasAttribute('*for') && !hasStructuralParent(element) && mount(element));
|
|
5824
5965
|
diagnoseUnscopedReveals(el);
|
|
5825
5966
|
if (getMountedScope(el))
|
|
5826
5967
|
return;
|
|
@@ -5849,9 +5990,17 @@
|
|
|
5849
5990
|
/**
|
|
5850
5991
|
* Parse inline scope definition from HTML attribute
|
|
5851
5992
|
*/
|
|
5993
|
+
function decodeMarkupExpression(value) {
|
|
5994
|
+
try {
|
|
5995
|
+
return decodeURIComponent(value);
|
|
5996
|
+
}
|
|
5997
|
+
catch {
|
|
5998
|
+
return value;
|
|
5999
|
+
} // Plain expressions may contain a literal % or modulo.
|
|
6000
|
+
}
|
|
5852
6001
|
function parseInlineScope(scopeStr) {
|
|
5853
6002
|
// If scopeStr is encode urld, decode it first
|
|
5854
|
-
scopeStr =
|
|
6003
|
+
scopeStr = decodeMarkupExpression(scopeStr);
|
|
5855
6004
|
try {
|
|
5856
6005
|
// Check if it's an inline object definition (starts with '{')
|
|
5857
6006
|
const trimmed = scopeStr.trim();
|
|
@@ -5918,7 +6067,7 @@
|
|
|
5918
6067
|
// gm-method-name:args:arg2="expression"
|
|
5919
6068
|
if (attr.name.match(/^gm-.+/)) {
|
|
5920
6069
|
// If expr is encoded url, decode it first
|
|
5921
|
-
const expr =
|
|
6070
|
+
const expr = decodeMarkupExpression(attr.value.trim());
|
|
5922
6071
|
if (forbidden.some(f => expr.includes(f))) {
|
|
5923
6072
|
DEBUG() && console.log("[GyosJS] Scope mount error: Forbidden expression:", expr);
|
|
5924
6073
|
}
|
|
@@ -6029,15 +6178,11 @@
|
|
|
6029
6178
|
* Mount a specific scope
|
|
6030
6179
|
*/
|
|
6031
6180
|
function mountScope(el, scopeAttr) {
|
|
6032
|
-
const definition = handleDefinition(el, scopeAttr);
|
|
6033
6181
|
// Check if already mounted
|
|
6034
6182
|
if (getMountedScope(el)) {
|
|
6035
6183
|
return;
|
|
6036
6184
|
}
|
|
6037
|
-
|
|
6038
|
-
if (el.hasAttribute('g-static')) {
|
|
6039
|
-
return;
|
|
6040
|
-
}
|
|
6185
|
+
const definition = handleDefinition(el, scopeAttr);
|
|
6041
6186
|
// Check hydration strategy
|
|
6042
6187
|
const hydrationStrategy = getHydrationStrategy(el);
|
|
6043
6188
|
if (hydrationStrategy) {
|
|
@@ -6102,13 +6247,21 @@
|
|
|
6102
6247
|
// Parse and bind template (portals will be processed inside *if)
|
|
6103
6248
|
// Pass onMount callback to be called after parsing (or after hydration triggers)
|
|
6104
6249
|
parseTemplate(el, instance, () => {
|
|
6250
|
+
// The hook may inspect or clean up its own mount. Register before calling
|
|
6251
|
+
// user code, and do not register again if the hook disposes the scope.
|
|
6252
|
+
registerMountedScope(el, instance);
|
|
6105
6253
|
// Call lifecycle hook after template is parsed
|
|
6106
6254
|
if (instance.onMount) {
|
|
6107
|
-
|
|
6255
|
+
try {
|
|
6256
|
+
Promise.resolve(instance.onMount.call(instance)).catch(console.error);
|
|
6257
|
+
}
|
|
6258
|
+
catch (error) {
|
|
6259
|
+
console.error('[Component] Error in onMount:', error);
|
|
6260
|
+
}
|
|
6108
6261
|
}
|
|
6109
6262
|
});
|
|
6110
|
-
|
|
6111
|
-
|
|
6263
|
+
if (getMountedScope(el) !== instance)
|
|
6264
|
+
return;
|
|
6112
6265
|
// Cache instance if g-scope-persist is set (with interpolation support)
|
|
6113
6266
|
getPersistKey$1(el, persistKey => {
|
|
6114
6267
|
if (!scopeCached.has(persistKey)) {
|
|
@@ -6124,7 +6277,7 @@
|
|
|
6124
6277
|
const events = new Map();
|
|
6125
6278
|
// Find all refs within this scope only
|
|
6126
6279
|
el.querySelectorAll('[g-ref]').forEach(refEl => {
|
|
6127
|
-
if (isInIgnoredTree(refEl))
|
|
6280
|
+
if (isInIgnoredTree(refEl) || (refEl.closest('[g-scope]') || el) !== el)
|
|
6128
6281
|
return;
|
|
6129
6282
|
const refName = refEl.getAttribute('g-ref');
|
|
6130
6283
|
if (refName && !hasUnsafePropertyPath(refName)) {
|
|
@@ -6174,13 +6327,16 @@
|
|
|
6174
6327
|
}
|
|
6175
6328
|
const { immediate = false, deep = false, debounce = 0 } = options;
|
|
6176
6329
|
let timeoutId;
|
|
6330
|
+
let active = true;
|
|
6177
6331
|
if (debounce > 0) {
|
|
6178
6332
|
// Wrap handler with debounce
|
|
6179
6333
|
const originalHandler = handler;
|
|
6180
6334
|
handler = (...args) => {
|
|
6181
6335
|
clearTimeout(timeoutId);
|
|
6182
6336
|
timeoutId = setTimeout(() => {
|
|
6183
|
-
|
|
6337
|
+
timeoutId = undefined;
|
|
6338
|
+
if (active)
|
|
6339
|
+
untrack(() => originalHandler.call(instance, ...args));
|
|
6184
6340
|
}, debounce);
|
|
6185
6341
|
};
|
|
6186
6342
|
}
|
|
@@ -6209,28 +6365,34 @@
|
|
|
6209
6365
|
initialized = true;
|
|
6210
6366
|
// Call immediately if requested
|
|
6211
6367
|
if (immediate) {
|
|
6212
|
-
handler.call(instance, clonedValue, undefined);
|
|
6368
|
+
untrack(() => handler.call(instance, clonedValue, undefined));
|
|
6213
6369
|
}
|
|
6214
6370
|
return;
|
|
6215
6371
|
}
|
|
6216
6372
|
// Check if value actually changed
|
|
6217
6373
|
const changed = deep
|
|
6218
6374
|
? !deepEqual(oldValue, clonedValue)
|
|
6219
|
-
: oldValue
|
|
6375
|
+
: !Object.is(oldValue, clonedValue);
|
|
6220
6376
|
if (changed) {
|
|
6221
|
-
handler.call(instance, clonedValue, oldValue);
|
|
6377
|
+
untrack(() => handler.call(instance, clonedValue, oldValue));
|
|
6222
6378
|
oldValue = deep && typeof clonedValue === 'object'
|
|
6223
6379
|
? clone(clonedValue)
|
|
6224
6380
|
: clonedValue;
|
|
6225
6381
|
}
|
|
6226
6382
|
});
|
|
6383
|
+
const stopWatching = () => {
|
|
6384
|
+
active = false;
|
|
6385
|
+
clearTimeout(timeoutId);
|
|
6386
|
+
timeoutId = undefined;
|
|
6387
|
+
watchEffect();
|
|
6388
|
+
};
|
|
6227
6389
|
// Store cleanup for unmount
|
|
6228
6390
|
if (!instance.__gyos_effects__) {
|
|
6229
6391
|
instance.__gyos_effects__ = [];
|
|
6230
6392
|
}
|
|
6231
|
-
instance.__gyos_effects__.push(
|
|
6393
|
+
instance.__gyos_effects__.push(stopWatching);
|
|
6232
6394
|
// Return unwatch function
|
|
6233
|
-
return
|
|
6395
|
+
return stopWatching;
|
|
6234
6396
|
},
|
|
6235
6397
|
$effect: (fn) => {
|
|
6236
6398
|
// Shorthand for effect with auto cleanup on unmount
|
|
@@ -6438,8 +6600,8 @@
|
|
|
6438
6600
|
*/
|
|
6439
6601
|
function once(event, handler) {
|
|
6440
6602
|
const wrapper = (...args) => {
|
|
6441
|
-
handler(...args);
|
|
6442
6603
|
off(event, wrapper);
|
|
6604
|
+
handler(...args);
|
|
6443
6605
|
};
|
|
6444
6606
|
return on(event, wrapper);
|
|
6445
6607
|
}
|
|
@@ -6477,23 +6639,34 @@
|
|
|
6477
6639
|
const data = signal(null);
|
|
6478
6640
|
const loading = signal(true);
|
|
6479
6641
|
const error = signal(null);
|
|
6642
|
+
let generation = 0;
|
|
6643
|
+
let active = true;
|
|
6480
6644
|
const fetchData = async () => {
|
|
6645
|
+
if (!active)
|
|
6646
|
+
return;
|
|
6647
|
+
const requestId = ++generation;
|
|
6481
6648
|
loading.value = true;
|
|
6482
6649
|
error.value = null;
|
|
6483
6650
|
data.value = null;
|
|
6484
6651
|
try {
|
|
6485
6652
|
const request = typeof url === 'function' ? await url() : url;
|
|
6653
|
+
if (requestId !== generation)
|
|
6654
|
+
return;
|
|
6486
6655
|
const response = typeof request === 'string' ? await fetch(request) : request;
|
|
6487
6656
|
if (!response.ok) {
|
|
6488
6657
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
6489
6658
|
}
|
|
6490
|
-
|
|
6659
|
+
const result = await response.json();
|
|
6660
|
+
if (requestId === generation)
|
|
6661
|
+
data.value = result;
|
|
6491
6662
|
}
|
|
6492
6663
|
catch (e) {
|
|
6493
|
-
|
|
6664
|
+
if (requestId === generation)
|
|
6665
|
+
error.value = e;
|
|
6494
6666
|
}
|
|
6495
6667
|
finally {
|
|
6496
|
-
|
|
6668
|
+
if (requestId === generation)
|
|
6669
|
+
loading.value = false;
|
|
6497
6670
|
}
|
|
6498
6671
|
};
|
|
6499
6672
|
return {
|
|
@@ -6501,7 +6674,8 @@
|
|
|
6501
6674
|
loading,
|
|
6502
6675
|
error,
|
|
6503
6676
|
refetch: fetchData,
|
|
6504
|
-
onMount: fetchData
|
|
6677
|
+
onMount: () => { active = true; return fetchData(); },
|
|
6678
|
+
onUnmount: () => { active = false; generation++; loading.value = false; }
|
|
6505
6679
|
};
|
|
6506
6680
|
}
|
|
6507
6681
|
/**
|
|
@@ -6759,17 +6933,26 @@
|
|
|
6759
6933
|
const data = signal(null);
|
|
6760
6934
|
const loading = signal(false);
|
|
6761
6935
|
const error = signal(null);
|
|
6936
|
+
let generation = 0;
|
|
6937
|
+
let active = true;
|
|
6762
6938
|
const execute = async () => {
|
|
6939
|
+
if (!active)
|
|
6940
|
+
return;
|
|
6941
|
+
const requestId = ++generation;
|
|
6763
6942
|
loading.value = true;
|
|
6764
6943
|
error.value = null;
|
|
6765
6944
|
try {
|
|
6766
|
-
|
|
6945
|
+
const result = await asyncFn();
|
|
6946
|
+
if (requestId === generation)
|
|
6947
|
+
data.value = result;
|
|
6767
6948
|
}
|
|
6768
6949
|
catch (e) {
|
|
6769
|
-
|
|
6950
|
+
if (requestId === generation)
|
|
6951
|
+
error.value = e;
|
|
6770
6952
|
}
|
|
6771
6953
|
finally {
|
|
6772
|
-
|
|
6954
|
+
if (requestId === generation)
|
|
6955
|
+
loading.value = false;
|
|
6773
6956
|
}
|
|
6774
6957
|
};
|
|
6775
6958
|
return {
|
|
@@ -6778,9 +6961,10 @@
|
|
|
6778
6961
|
error,
|
|
6779
6962
|
execute,
|
|
6780
6963
|
onMount: () => {
|
|
6781
|
-
|
|
6782
|
-
|
|
6783
|
-
}
|
|
6964
|
+
active = true;
|
|
6965
|
+
return immediate ? execute() : undefined;
|
|
6966
|
+
},
|
|
6967
|
+
onUnmount: () => { active = false; generation++; loading.value = false; }
|
|
6784
6968
|
};
|
|
6785
6969
|
}
|
|
6786
6970
|
|
|
@@ -7254,7 +7438,10 @@
|
|
|
7254
7438
|
Array.from(source.attributes).forEach(attribute => {
|
|
7255
7439
|
sourceAttributes.add(attribute.name);
|
|
7256
7440
|
if (target.getAttribute(attribute.name) !== attribute.value) {
|
|
7257
|
-
|
|
7441
|
+
// HTML permits directive names such as @click and *if, while older
|
|
7442
|
+
// DOM implementations apply XML name validation in setAttribute.
|
|
7443
|
+
// Transfer the already-parsed Attr without validating its name again.
|
|
7444
|
+
target.setAttributeNodeNS(target.ownerDocument.importNode(attribute, true));
|
|
7258
7445
|
}
|
|
7259
7446
|
});
|
|
7260
7447
|
Array.from(target.attributes).forEach(attribute => {
|
|
@@ -7538,7 +7725,14 @@
|
|
|
7538
7725
|
// Hash scrolling
|
|
7539
7726
|
const hash = new URL(url).hash;
|
|
7540
7727
|
if (hash) {
|
|
7541
|
-
|
|
7728
|
+
// A URL fragment identifies an id, not a CSS selector. Numeric ids,
|
|
7729
|
+
// punctuation and percent-encoded Unicode must work without escaping.
|
|
7730
|
+
let id = hash.slice(1);
|
|
7731
|
+
try {
|
|
7732
|
+
id = decodeURIComponent(id);
|
|
7733
|
+
}
|
|
7734
|
+
catch { /* Keep malformed fragments literal. */ }
|
|
7735
|
+
const el = document.getElementById(id);
|
|
7542
7736
|
if (el) {
|
|
7543
7737
|
el.scrollIntoView({ behavior: 'smooth' });
|
|
7544
7738
|
return;
|
|
@@ -8118,6 +8312,12 @@
|
|
|
8118
8312
|
return fragment?.cloneNode(true);
|
|
8119
8313
|
}
|
|
8120
8314
|
function cloneHistoryTarget(target) {
|
|
8315
|
+
// Mounted templates have already consumed event/model attributes and text
|
|
8316
|
+
// expressions. A DOM clone cannot recreate their observers. Restore these
|
|
8317
|
+
// targets from response HTML (snapshot or fetch) instead of caching inert UI.
|
|
8318
|
+
if (getScopeFromElement(target) || Array.from(target.querySelectorAll('*'))
|
|
8319
|
+
.some(element => element.__gyos_scope__))
|
|
8320
|
+
return null;
|
|
8121
8321
|
const clone = target.cloneNode(true);
|
|
8122
8322
|
clone.querySelectorAll('.gyos-target-spinner').forEach(spinner => spinner.remove());
|
|
8123
8323
|
return clone;
|
|
@@ -8975,7 +9175,7 @@
|
|
|
8975
9175
|
portalCreate,
|
|
8976
9176
|
portalDestroy,
|
|
8977
9177
|
setCspNonce,
|
|
8978
|
-
version: '0.5.
|
|
9178
|
+
version: '0.5.4'
|
|
8979
9179
|
};
|
|
8980
9180
|
|
|
8981
9181
|
setExpressionRuntime(standardExpressionRuntime);
|