gyosjs 0.4.1 → 0.5.0

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.
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * GyosJS v0.4.1
2
+ * GyosJS v0.5.0
3
3
  * (c) 2026
4
4
  * @license MIT
5
5
  */
@@ -5636,6 +5636,21 @@
5636
5636
  * Registry of all scopes
5637
5637
  */
5638
5638
  const scopeRegistry = new Map();
5639
+ const warnedReveals = new WeakSet();
5640
+ function diagnoseUnscopedReveals(root) {
5641
+ if (!DEBUG())
5642
+ return;
5643
+ const elements = [...root.querySelectorAll('[g-reveal]')];
5644
+ if (root instanceof Element && root.hasAttribute('g-reveal'))
5645
+ elements.unshift(root);
5646
+ for (const element of elements) {
5647
+ if (warnedReveals.has(element) || isInIgnoredTree(element)
5648
+ || element.closest('[g-scope]') || getScopeFromElement(element))
5649
+ continue;
5650
+ warnedReveals.add(element);
5651
+ console.warn('[Gyos] g-reveal has no owning scope. Add g-scope to its container; global reveal CSS may otherwise hide this content.', element);
5652
+ }
5653
+ }
5639
5654
  /**
5640
5655
  * Register a scope
5641
5656
  */
@@ -5683,6 +5698,7 @@
5683
5698
  allElements.sort((a, b) => getDepth(b) - getDepth(a));
5684
5699
  // Filter out elements inside structural directives - they will be mounted by structural processor
5685
5700
  allElements.forEach(el => !hasStructuralParent(el) && mount(el));
5701
+ diagnoseUnscopedReveals(document.body);
5686
5702
  }
5687
5703
  function mountTree(el) {
5688
5704
  if (isInIgnoredTree(el) || !document.body.contains(el))
@@ -5693,6 +5709,7 @@
5693
5709
  // Child scopes own their subtree before an existing parent scope parses the insertion.
5694
5710
  candidates.sort((a, b) => getDepth(b) - getDepth(a));
5695
5711
  candidates.forEach(element => !hasStructuralParent(element) && mount(element));
5712
+ diagnoseUnscopedReveals(el);
5696
5713
  if (getMountedScope(el))
5697
5714
  return;
5698
5715
  const ownerScope = getScopeFromElement(el);
@@ -5711,7 +5728,7 @@
5711
5728
  return;
5712
5729
  // Check if element removed but still has g-scope (possible in some edge cases)
5713
5730
  getAllMountedScopes().forEach((_, mountedEl) => {
5714
- if (!document.body.contains(mountedEl)) {
5731
+ if (!mountedEl.isConnected) {
5715
5732
  disposeEffects(mountedEl);
5716
5733
  }
5717
5734
  });
@@ -7037,7 +7054,8 @@
7037
7054
  el.dataset.gyosPersistId = key;
7038
7055
  return key;
7039
7056
  }
7040
- // Hidden parking lot keeps persisted nodes alive (e.g. audio keeps playing)
7057
+ // Keep the parking host outside body: body itself may be the swap target.
7058
+ // A disconnected parking host makes browsers pause otherwise persisted media.
7041
7059
  const persistParking = typeof document !== 'undefined'
7042
7060
  ? (() => {
7043
7061
  const park = document.createElement('div');
@@ -7046,12 +7064,7 @@
7046
7064
  park.style.top = '-99999px';
7047
7065
  park.style.width = '0';
7048
7066
  park.style.height = '0';
7049
- if (document.readyState === 'loading') {
7050
- document.addEventListener('DOMContentLoaded', () => document.body.appendChild(park));
7051
- }
7052
- else {
7053
- document.body.appendChild(park);
7054
- }
7067
+ document.documentElement.appendChild(park);
7055
7068
  return park;
7056
7069
  })()
7057
7070
  : null;
@@ -7062,8 +7075,8 @@
7062
7075
  return elements;
7063
7076
  }
7064
7077
  function ensureParkingIsConnected() {
7065
- if (persistParking && document.body && !persistParking.isConnected) {
7066
- document.body.appendChild(persistParking);
7078
+ if (persistParking && persistParking.parentElement !== document.documentElement) {
7079
+ document.documentElement.appendChild(persistParking);
7067
7080
  }
7068
7081
  }
7069
7082
  function detachPersist(root = document) {
@@ -7081,12 +7094,18 @@
7081
7094
  });
7082
7095
  }
7083
7096
  function mergePersistIntoLive(root) {
7097
+ // Return the host to the current body before scope mounting/cleanup inspects it.
7098
+ if (persistParking && persistParking.parentElement !== document.body) {
7099
+ document.body.appendChild(persistParking);
7100
+ }
7084
7101
  if (!persistEntries.size)
7085
7102
  return;
7086
7103
  // If placeholders lack ids but we only have one persist entry, assign its key for convenience
7087
7104
  if (persistEntries.size === 1) {
7088
7105
  const loneKey = Array.from(persistEntries.keys())[0];
7089
7106
  elementsIn(root).forEach(el => {
7107
+ if (persistParking?.contains(el))
7108
+ return;
7090
7109
  if (!getPersistKey(el, false)) {
7091
7110
  el.dataset.gyosPersistId = loneKey;
7092
7111
  }
@@ -7094,6 +7113,8 @@
7094
7113
  }
7095
7114
  // Element placeholders
7096
7115
  elementsIn(root).forEach(placeholder => {
7116
+ if (persistParking?.contains(placeholder))
7117
+ return;
7097
7118
  const key = getPersistKey(placeholder, false);
7098
7119
  if (!key)
7099
7120
  return;
@@ -7111,6 +7132,8 @@
7111
7132
  const toReplace = [];
7112
7133
  while (walker.nextNode()) {
7113
7134
  const comment = walker.currentNode;
7135
+ if (persistParking?.contains(comment))
7136
+ continue;
7114
7137
  const match = comment.data.match(/g-persist[:=]\s*([A-Za-z0-9_\-:.]+)/);
7115
7138
  if (match) {
7116
7139
  toReplace.push({ node: comment, key: match[1] });
@@ -7768,6 +7791,36 @@
7768
7791
  * Implements attribute-driven routing, snapshot caching, and persistent islands.
7769
7792
  */
7770
7793
  const submittingForms = new WeakSet();
7794
+ async function visit(url, options, fresh) {
7795
+ const destination = new URL(url, document.baseURI);
7796
+ if (!['http:', 'https:'].includes(destination.protocol) || destination.origin !== location.origin || destination.username || destination.password) {
7797
+ throw new TypeError('Gyos.navigate requires a same-origin HTTP(S) URL without credentials.');
7798
+ }
7799
+ if (options.target !== undefined && !(document.querySelector(options.target) instanceof HTMLElement)) {
7800
+ throw new TypeError('Gyos.navigate target does not exist.');
7801
+ }
7802
+ if (!document.querySelector('[g-boost]')) {
7803
+ throw new TypeError('Gyos.navigate requires a g-boost layout.');
7804
+ }
7805
+ startRouter();
7806
+ let result;
7807
+ await navigateRequest({
7808
+ url: destination.href, method: 'GET',
7809
+ changeState: options.history !== 'none', replace: options.history === 'replace',
7810
+ targetSelector: options.target, strictTarget: options.target !== undefined, swapMode: options.swap,
7811
+ currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, fresh,
7812
+ onEnd: context => { result = context; }
7813
+ });
7814
+ return result;
7815
+ }
7816
+ /** Navigate through the boost pipeline. Resolves after navigation-end hooks. */
7817
+ function navigate(url, options = {}) {
7818
+ return visit(url, options, false);
7819
+ }
7820
+ /** Fetch the current URL again, preserving history and scroll by default. */
7821
+ function refresh(options = {}) {
7822
+ return visit(location.href, { history: 'none', scroll: false, ...options }, true);
7823
+ }
7771
7824
  const snapshots = new Map();
7772
7825
  const preloadCache = new Map();
7773
7826
  const preloadControllers = new Map();
@@ -8154,12 +8207,27 @@
8154
8207
  }
8155
8208
  console.error(`[GyosRouter] ${method} navigation could not be boosted; the request will not be replayed.`);
8156
8209
  }
8157
- async function navigate(opts) {
8210
+ async function navigateRequest(opts) {
8211
+ const finishUnstarted = () => {
8212
+ opts.onEnd?.({ from: currentUrl, url: opts.url, method: opts.method,
8213
+ trigger: opts.trigger || null, target: null, form: null, popstate: false,
8214
+ signal: opts.signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
8215
+ };
8216
+ if (opts.signal?.aborted) {
8217
+ finishUnstarted();
8218
+ return;
8219
+ }
8158
8220
  while (activeNavigation) {
8159
8221
  const previous = activeNavigation;
8160
8222
  if (previous.committing) {
8161
8223
  log('[GyosRouter] Waiting for previous DOM commit');
8162
- await previous.done;
8224
+ try {
8225
+ await (opts.signal ? waitForNavigation(previous.done, opts.signal) : previous.done);
8226
+ }
8227
+ catch {
8228
+ finishUnstarted();
8229
+ return;
8230
+ }
8163
8231
  continue;
8164
8232
  }
8165
8233
  log('[GyosRouter] Cancelling previous navigation');
@@ -8178,6 +8246,10 @@
8178
8246
  resolveDone
8179
8247
  };
8180
8248
  activeNavigation = transaction;
8249
+ const abort = () => cancelNavigation(transaction);
8250
+ opts.signal?.addEventListener('abort', abort, { once: true });
8251
+ if (opts.signal?.aborted)
8252
+ abort();
8181
8253
  const currentBaseUrl = document.baseURI;
8182
8254
  rememberResourceUrls(document, currentBaseUrl);
8183
8255
  const context = {
@@ -8201,6 +8273,7 @@
8201
8273
  }
8202
8274
  };
8203
8275
  try {
8276
+ assertActiveNavigation(transaction);
8204
8277
  if (formEvent)
8205
8278
  document.dispatchEvent(new CustomEvent('gyos:form-start', { detail: { ...formEvent } }));
8206
8279
  runNavigationHooks(beforeNavigate, opts.url, context);
@@ -8213,6 +8286,10 @@
8213
8286
  saveScrollPosition();
8214
8287
  // Pick target element to swap
8215
8288
  let target = resolveTarget(opts.trigger, opts.targetSelector, opts.targetOutletIndex);
8289
+ if (opts.strictTarget && (!opts.targetSelector || !target?.matches(opts.targetSelector))) {
8290
+ context.outcome = 'invalid-response';
8291
+ return; // A queued explicit target disappeared; never broaden the swap.
8292
+ }
8216
8293
  context.target = target;
8217
8294
  if (!target) {
8218
8295
  if (isActiveNavigation(transaction))
@@ -8257,7 +8334,12 @@
8257
8334
  else {
8258
8335
  // Check preload cache if GET
8259
8336
  const preloadKey = canonicalUrl(finalUrl, true);
8260
- if (opts.method.toUpperCase() === 'GET' && preloadCache.has(preloadKey)) {
8337
+ if (opts.fresh) {
8338
+ preloadControllers.get(preloadKey)?.abort();
8339
+ preloadControllers.delete(preloadKey);
8340
+ preloadCache.delete(preloadKey);
8341
+ }
8342
+ if (!opts.fresh && opts.method.toUpperCase() === 'GET' && preloadCache.has(preloadKey)) {
8261
8343
  log('[GyosRouter] using preload cache for', finalUrl);
8262
8344
  try {
8263
8345
  response = await waitForNavigation(preloadCache.get(preloadKey), transaction.controller.signal);
@@ -8465,7 +8547,10 @@
8465
8547
  finalizeCommit();
8466
8548
  }
8467
8549
  // Handle scroll after mount scopes
8468
- if (changesState) {
8550
+ if (opts.scroll === false) {
8551
+ // Explicit refresh/navigation policy keeps the current viewport.
8552
+ }
8553
+ else if (changesState) {
8469
8554
  handleScroll(targetUrl, opts.trigger);
8470
8555
  }
8471
8556
  else if (opts.popstate) {
@@ -8530,6 +8615,7 @@
8530
8615
  activeNavigation = null;
8531
8616
  progressBar?.complete();
8532
8617
  }
8618
+ opts.signal?.removeEventListener('abort', abort);
8533
8619
  transaction.resolveDone();
8534
8620
  if (formEvent)
8535
8621
  submittingForms.delete(formEvent.form);
@@ -8540,6 +8626,7 @@
8540
8626
  detail: { ...formEvent, url, status, outcome, committed, fallback }
8541
8627
  }));
8542
8628
  }
8629
+ opts.onEnd?.({ ...context });
8543
8630
  }
8544
8631
  }
8545
8632
  function shouldBoost(el) {
@@ -8570,7 +8657,7 @@
8570
8657
  const scope = getScopeFromElement(element);
8571
8658
  const jsonData = evaluateExpression(decodeURIComponent(element.getAttribute(paramsAttr) || ''), scope, false);
8572
8659
  // The default changeState is false, as this is commonly used for: load more, infinite scroll, etc.
8573
- navigate({
8660
+ navigateRequest({
8574
8661
  url: parsedUrl.toString(), method, trigger: element, jsonData, changeState: false
8575
8662
  });
8576
8663
  }
@@ -8619,7 +8706,7 @@
8619
8706
  return;
8620
8707
  event.preventDefault();
8621
8708
  // The default changeState is true, as this is commonly used for: navigation, etc.
8622
- navigate({ url: url.toString(), method: 'GET', trigger: link, changeState: true });
8709
+ navigateRequest({ url: url.toString(), method: 'GET', trigger: link, changeState: true });
8623
8710
  }
8624
8711
  function onMouseOverCapture(event) {
8625
8712
  const target = event.target;
@@ -8675,7 +8762,7 @@
8675
8762
  return;
8676
8763
  const formData = new FormData(form, submitter);
8677
8764
  submittingForms.add(form);
8678
- void navigate({ url: url.toString(), method, trigger: form, form, formData, formEncoding, changeState: method === 'GET' });
8765
+ void navigateRequest({ url: url.toString(), method, trigger: form, form, formData, formEncoding, changeState: method === 'GET' });
8679
8766
  }
8680
8767
  function onPopState(event) {
8681
8768
  const url = window.location.href;
@@ -8693,14 +8780,14 @@
8693
8780
  }
8694
8781
  event.preventDefault();
8695
8782
  // The browser changes location before popstate fires. Stop any pending page
8696
- // script immediately, then let navigate() wait for its DOM finalizer.
8783
+ // script immediately, then let navigateRequest() wait for its DOM finalizer.
8697
8784
  if (activeNavigation?.committing)
8698
8785
  cancelNavigation(activeNavigation);
8699
8786
  // The browser has already moved to the destination history entry. Keep the
8700
8787
  // router's entry pointer aligned before any restored page script can navigate.
8701
8788
  currentHistoryEntryId = destinationEntryId;
8702
8789
  // No snapshot, do a normal navigation without changing history (changeState: false)
8703
- navigate({
8790
+ navigateRequest({
8704
8791
  url,
8705
8792
  method: 'GET',
8706
8793
  replace: true,
@@ -8782,10 +8869,12 @@
8782
8869
  onAfterNavigate,
8783
8870
  onBeforeSwap,
8784
8871
  onNavigationEnd,
8872
+ navigate,
8873
+ refresh,
8785
8874
  portalCreate,
8786
8875
  portalDestroy,
8787
8876
  setCspNonce,
8788
- version: '0.4.1'
8877
+ version: '0.5.0'
8789
8878
  };
8790
8879
 
8791
8880
  // This file was generated. Do not modify manually!