gyosjs 0.2.0 → 0.2.2

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/dist/gyos.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * GyosJS v0.2.0
2
+ * GyosJS v0.2.2
3
3
  * (c) 2026
4
4
  * @license MIT
5
5
  */
@@ -3024,6 +3024,33 @@
3024
3024
  return (num >= minVal && num <= maxVal) || `Must be between ${minVal} and ${maxVal}`;
3025
3025
  });
3026
3026
 
3027
+ const managedForms = new WeakSet();
3028
+ const approvedSubmissions = new WeakSet();
3029
+ // Coordinates the validation listener on a form with the Router's earlier
3030
+ // document-capture listener without exposing state on application elements.
3031
+ function registerValidatedForm(form) {
3032
+ managedForms.add(form);
3033
+ }
3034
+ function unregisterValidatedForm(form) {
3035
+ managedForms.delete(form);
3036
+ approvedSubmissions.delete(form);
3037
+ }
3038
+ function approveNextFormSubmission(form) {
3039
+ approvedSubmissions.add(form);
3040
+ }
3041
+ function cancelApprovedFormSubmission(form) {
3042
+ approvedSubmissions.delete(form);
3043
+ }
3044
+ function shouldDeferToFormValidation(form) {
3045
+ if (!managedForms.has(form))
3046
+ return false;
3047
+ if (approvedSubmissions.has(form)) {
3048
+ approvedSubmissions.delete(form);
3049
+ return false;
3050
+ }
3051
+ return true;
3052
+ }
3053
+
3027
3054
  /**
3028
3055
  * Form Validation Directives
3029
3056
  * Provides g-form, g-validate, and g-errors directives for reactive form validation
@@ -3038,6 +3065,23 @@
3038
3065
  const modelAttr = Array.from(el.attributes).find(attr => attr.name === 'g-model' || attr.name.startsWith('g-model.'));
3039
3066
  return modelAttr?.value || el.getAttribute('name') || el.id;
3040
3067
  }
3068
+ function getControlValue(el, form) {
3069
+ if (!(el instanceof HTMLInputElement))
3070
+ return el.value;
3071
+ if (el.type === 'checkbox')
3072
+ return el.checked ? el.value : '';
3073
+ if (el.type !== 'radio')
3074
+ return el.value;
3075
+ const fieldName = getFieldName(el);
3076
+ const checked = Array.from(form.querySelectorAll('input[type="radio"]')).find(candidate => {
3077
+ if (isInIgnoredTree(candidate) || !candidate.checked)
3078
+ return false;
3079
+ if (el.name && candidate.name === el.name)
3080
+ return true;
3081
+ return getFieldName(candidate) === fieldName;
3082
+ });
3083
+ return checked?.value ?? '';
3084
+ }
3041
3085
  /**
3042
3086
  * Get or create form validation state
3043
3087
  */
@@ -3089,6 +3133,7 @@
3089
3133
  return;
3090
3134
  form.removeAttribute('g-form');
3091
3135
  const state = getFormState(form);
3136
+ registerValidatedForm(form);
3092
3137
  // Create validateAll function
3093
3138
  const validateAll = async () => {
3094
3139
  const validators = formValidators.get(form) || [];
@@ -3122,30 +3167,77 @@
3122
3167
  // Merge signals into existing object
3123
3168
  Object.assign(scope[formName], formStateObj);
3124
3169
  }
3125
- // Prevent default form submission and validate first
3170
+ let replayingSubmission = false;
3171
+ let validatingSubmission = false;
3172
+ let replayScheduled = false;
3173
+ let replayTimer;
3174
+ let active = true;
3175
+ // Hold the first submit while validation runs, then replay a normal submit
3176
+ // so MPA Boost, native navigation, and user submit listeners keep working.
3126
3177
  const submitListener = async (e) => {
3127
- e.preventDefault();
3128
- const isValid = await validateAll();
3129
- if (!isValid)
3178
+ if (replayingSubmission) {
3179
+ replayingSubmission = false;
3180
+ cancelApprovedFormSubmission(form);
3130
3181
  return;
3131
- const submitMethod = form.getAttribute('g-submit');
3132
- // If not defined, submit the form normally
3133
- if (!submitMethod) {
3134
- form.submit();
3182
+ }
3183
+ e.preventDefault();
3184
+ e.stopImmediatePropagation();
3185
+ if (validatingSubmission || replayScheduled)
3135
3186
  return;
3187
+ validatingSubmission = true;
3188
+ try {
3189
+ const isValid = await validateAll();
3190
+ if (!active || !isValid)
3191
+ return;
3192
+ const submitMethod = form.getAttribute('g-submit');
3193
+ // If not defined, replay through the normal submit event pipeline.
3194
+ if (!submitMethod) {
3195
+ const submitter = typeof SubmitEvent !== 'undefined' && e instanceof SubmitEvent && (e.submitter instanceof HTMLButtonElement || e.submitter instanceof HTMLInputElement) ? e.submitter : undefined;
3196
+ // Browsers ignore a re-entrant requestSubmit while the original
3197
+ // submission algorithm is still finishing. Replay in the next task.
3198
+ replayScheduled = true;
3199
+ replayTimer = setTimeout(() => {
3200
+ replayScheduled = false;
3201
+ replayTimer = undefined;
3202
+ if (!active)
3203
+ return;
3204
+ approveNextFormSubmission(form);
3205
+ replayingSubmission = true;
3206
+ try {
3207
+ form.requestSubmit(submitter);
3208
+ }
3209
+ finally {
3210
+ // requestSubmit dispatches synchronously. If native validation or
3211
+ // another capture listener stopped it, do not leak approval state.
3212
+ if (replayingSubmission) {
3213
+ replayingSubmission = false;
3214
+ cancelApprovedFormSubmission(form);
3215
+ }
3216
+ }
3217
+ }, 0);
3218
+ return;
3219
+ }
3220
+ // Trigger custom submit handler if exists
3221
+ const submitHandler = scope[submitMethod];
3222
+ if (typeof submitHandler === 'function') {
3223
+ submitHandler.call(scope);
3224
+ }
3136
3225
  }
3137
- // Trigger custom submit handler if exists
3138
- const submitHandler = scope[submitMethod];
3139
- if (typeof submitHandler === 'function') {
3140
- submitHandler.call(scope);
3226
+ finally {
3227
+ validatingSubmission = false;
3141
3228
  }
3142
3229
  };
3143
- form.addEventListener('submit', submitListener);
3230
+ form.addEventListener('submit', submitListener, true);
3144
3231
  if (!form.__gyos_effects__) {
3145
3232
  form.__gyos_effects__ = [];
3146
3233
  }
3147
3234
  form.__gyos_effects__.push(() => {
3148
- form.removeEventListener('submit', submitListener);
3235
+ active = false;
3236
+ if (replayTimer !== undefined)
3237
+ clearTimeout(replayTimer);
3238
+ replayScheduled = false;
3239
+ form.removeEventListener('submit', submitListener, true);
3240
+ unregisterValidatedForm(form);
3149
3241
  formStates.delete(form);
3150
3242
  formValidators.delete(form);
3151
3243
  });
@@ -3181,7 +3273,7 @@
3181
3273
  state.touched.value[modelAttr] = false;
3182
3274
  // Validation function
3183
3275
  const validateField = async () => {
3184
- const value = el.value;
3276
+ const value = getControlValue(el, form);
3185
3277
  // Get form context for validators that need it (like 'same')
3186
3278
  const formData = {};
3187
3279
  const formInputs = form.querySelectorAll('input, textarea, select');
@@ -3191,7 +3283,7 @@
3191
3283
  if (input instanceof HTMLInputElement || input instanceof HTMLTextAreaElement || input instanceof HTMLSelectElement) {
3192
3284
  const fieldName = getFieldName(input);
3193
3285
  if (fieldName) {
3194
- formData[fieldName] = input.value;
3286
+ formData[fieldName] = getControlValue(input, form);
3195
3287
  }
3196
3288
  }
3197
3289
  });
@@ -3695,6 +3787,9 @@
3695
3787
  if (inputElement.type === 'checkbox') {
3696
3788
  inputElement.checked = !!initialValue;
3697
3789
  }
3790
+ else if (inputElement.type === 'radio') {
3791
+ inputElement.checked = String(unref(initialValue)) === inputElement.value;
3792
+ }
3698
3793
  else {
3699
3794
  inputElement.value = unref(initialValue);
3700
3795
  }
@@ -3717,6 +3812,8 @@
3717
3812
  };
3718
3813
  const eventInput = (e) => {
3719
3814
  const target = e.target;
3815
+ if (target.type === 'radio' && !target.checked)
3816
+ return;
3720
3817
  const value = target.type === 'checkbox' ? target.checked : target.value;
3721
3818
  debouncedUpdate(value);
3722
3819
  };
@@ -3736,6 +3833,9 @@
3736
3833
  if (inputElement.type === 'checkbox') {
3737
3834
  inputElement.checked = !!newValue;
3738
3835
  }
3836
+ else if (inputElement.type === 'radio') {
3837
+ inputElement.checked = String(unref(newValue)) === inputElement.value;
3838
+ }
3739
3839
  else if (inputElement.value !== newValue) {
3740
3840
  inputElement.value = unref(newValue);
3741
3841
  }
@@ -5375,6 +5475,7 @@
5375
5475
  const attrs = node.getAttributeNames();
5376
5476
  return attrs.some(attr => attr.startsWith('g-model'));
5377
5477
  });
5478
+ const radioDefaults = new Map();
5378
5479
  for (const node of walker) {
5379
5480
  if (isInIgnoredTree(node))
5380
5481
  continue;
@@ -5383,13 +5484,34 @@
5383
5484
  if (!modelAttr)
5384
5485
  continue;
5385
5486
  // If prop exists or is nested, skip
5386
- if (modelAttr.value in scope || modelAttr.value.includes('.'))
5487
+ if (modelAttr.value.includes('.'))
5387
5488
  continue;
5388
5489
  const element = node;
5490
+ if (element.type === 'radio') {
5491
+ if (modelAttr.value in scope)
5492
+ continue;
5493
+ if (!radioDefaults.has(modelAttr.value))
5494
+ radioDefaults.set(modelAttr.value, '');
5495
+ if (element.checked) {
5496
+ const value = modelAttr.name.split('.').includes('number')
5497
+ ? Number(element.value)
5498
+ : element.value;
5499
+ radioDefaults.set(modelAttr.value, value);
5500
+ }
5501
+ continue;
5502
+ }
5503
+ if (modelAttr.value in scope)
5504
+ continue;
5389
5505
  const valueAuto = element.type === 'checkbox' ? element.checked : element.value;
5390
5506
  scope[modelAttr.value] = valueAuto;
5391
5507
  DEBUG() && console.log(`[g-model] Auto-bound property "${modelAttr.value}" with initial value:`, valueAuto);
5392
5508
  }
5509
+ for (const [prop, value] of radioDefaults) {
5510
+ if (prop in scope)
5511
+ continue;
5512
+ scope[prop] = value;
5513
+ DEBUG() && console.log(`[g-model] Auto-bound radio property "${prop}" with initial value:`, value);
5514
+ }
5393
5515
  }
5394
5516
  function getPersistKey$1(el, callback) {
5395
5517
  if (!el.hasAttribute('g-scope-persist'))
@@ -7050,6 +7172,16 @@
7050
7172
  function findGlobalOutlet() {
7051
7173
  return document.querySelector('[g-outlet]');
7052
7174
  }
7175
+ function stripInertFallbackContent(root) {
7176
+ root.querySelectorAll('noscript').forEach(element => element.remove());
7177
+ }
7178
+ function parseNavigationDocument(html) {
7179
+ const doc = new DOMParser().parseFromString(html, 'text/html');
7180
+ // DOMParser disables scripting and parses noscript children as active markup.
7181
+ // A boosted page runs with scripting enabled, so those fallbacks must stay inert.
7182
+ stripInertFallbackContent(doc);
7183
+ return doc;
7184
+ }
7053
7185
  function resolveTarget(trigger, targetSelector, targetOutletIndex) {
7054
7186
  if (targetSelector) {
7055
7187
  try {
@@ -7086,7 +7218,7 @@
7086
7218
  }
7087
7219
  function saveSnapshot(url, html) {
7088
7220
  // create HTMLElement by html string to remove scripts and get clean snapshot
7089
- const doc = new DOMParser().parseFromString(html, 'text/html');
7221
+ const doc = parseNavigationDocument(html);
7090
7222
  // Ignore hash for snapshot keys
7091
7223
  if (url.includes('#'))
7092
7224
  url = url.split('#')[0];
@@ -7478,10 +7610,11 @@
7478
7610
  }
7479
7611
  // Parse HTML
7480
7612
  const doc = html
7481
- ? new DOMParser().parseFromString(html, 'text/html')
7613
+ ? parseNavigationDocument(html)
7482
7614
  : document.implementation.createHTMLDocument();
7483
7615
  // Pick source fragment
7484
7616
  const src = opts.historyFragment || pickSourceFragment(doc, target, targetOutletIndex);
7617
+ stripInertFallbackContent(src);
7485
7618
  assertActiveNavigation(transaction);
7486
7619
  // Final URL is response.url because in headers there could be redirects
7487
7620
  // because response.url never include hash, we need to manually merge it
@@ -7758,6 +7891,8 @@
7758
7891
  return;
7759
7892
  if (!(form.hasAttribute('g-boost') || hasGlobalBoost()))
7760
7893
  return;
7894
+ if (shouldDeferToFormValidation(form))
7895
+ return;
7761
7896
  event.preventDefault();
7762
7897
  const action = form.action || window.location.href;
7763
7898
  const method = (form.method || 'GET').toUpperCase();
@@ -7874,7 +8009,7 @@
7874
8009
  onAfterNavigate,
7875
8010
  portalCreate,
7876
8011
  portalDestroy,
7877
- version: '0.2.0'
8012
+ version: '0.2.2'
7878
8013
  };
7879
8014
 
7880
8015
  exports.applyDirective = applyDirective;