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.
package/dist/gyos.js CHANGED
@@ -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
  */
@@ -5668,6 +5668,21 @@
5668
5668
  * Registry of all scopes
5669
5669
  */
5670
5670
  const scopeRegistry = new Map();
5671
+ const warnedReveals = new WeakSet();
5672
+ function diagnoseUnscopedReveals(root) {
5673
+ if (!DEBUG())
5674
+ return;
5675
+ const elements = [...root.querySelectorAll('[g-reveal]')];
5676
+ if (root instanceof Element && root.hasAttribute('g-reveal'))
5677
+ elements.unshift(root);
5678
+ for (const element of elements) {
5679
+ if (warnedReveals.has(element) || isInIgnoredTree(element)
5680
+ || element.closest('[g-scope]') || getScopeFromElement(element))
5681
+ continue;
5682
+ warnedReveals.add(element);
5683
+ console.warn('[Gyos] g-reveal has no owning scope. Add g-scope to its container; global reveal CSS may otherwise hide this content.', element);
5684
+ }
5685
+ }
5671
5686
  /**
5672
5687
  * Register a scope
5673
5688
  */
@@ -5715,6 +5730,7 @@
5715
5730
  allElements.sort((a, b) => getDepth(b) - getDepth(a));
5716
5731
  // Filter out elements inside structural directives - they will be mounted by structural processor
5717
5732
  allElements.forEach(el => !hasStructuralParent(el) && mount(el));
5733
+ diagnoseUnscopedReveals(document.body);
5718
5734
  }
5719
5735
  function mountTree(el) {
5720
5736
  if (isInIgnoredTree(el) || !document.body.contains(el))
@@ -5725,6 +5741,7 @@
5725
5741
  // Child scopes own their subtree before an existing parent scope parses the insertion.
5726
5742
  candidates.sort((a, b) => getDepth(b) - getDepth(a));
5727
5743
  candidates.forEach(element => !hasStructuralParent(element) && mount(element));
5744
+ diagnoseUnscopedReveals(el);
5728
5745
  if (getMountedScope(el))
5729
5746
  return;
5730
5747
  const ownerScope = getScopeFromElement(el);
@@ -5743,7 +5760,7 @@
5743
5760
  return;
5744
5761
  // Check if element removed but still has g-scope (possible in some edge cases)
5745
5762
  getAllMountedScopes().forEach((_, mountedEl) => {
5746
- if (!document.body.contains(mountedEl)) {
5763
+ if (!mountedEl.isConnected) {
5747
5764
  disposeEffects(mountedEl);
5748
5765
  }
5749
5766
  });
@@ -7055,7 +7072,8 @@
7055
7072
  el.dataset.gyosPersistId = key;
7056
7073
  return key;
7057
7074
  }
7058
- // Hidden parking lot keeps persisted nodes alive (e.g. audio keeps playing)
7075
+ // Keep the parking host outside body: body itself may be the swap target.
7076
+ // A disconnected parking host makes browsers pause otherwise persisted media.
7059
7077
  const persistParking = typeof document !== 'undefined'
7060
7078
  ? (() => {
7061
7079
  const park = document.createElement('div');
@@ -7064,12 +7082,7 @@
7064
7082
  park.style.top = '-99999px';
7065
7083
  park.style.width = '0';
7066
7084
  park.style.height = '0';
7067
- if (document.readyState === 'loading') {
7068
- document.addEventListener('DOMContentLoaded', () => document.body.appendChild(park));
7069
- }
7070
- else {
7071
- document.body.appendChild(park);
7072
- }
7085
+ document.documentElement.appendChild(park);
7073
7086
  return park;
7074
7087
  })()
7075
7088
  : null;
@@ -7080,8 +7093,8 @@
7080
7093
  return elements;
7081
7094
  }
7082
7095
  function ensureParkingIsConnected() {
7083
- if (persistParking && document.body && !persistParking.isConnected) {
7084
- document.body.appendChild(persistParking);
7096
+ if (persistParking && persistParking.parentElement !== document.documentElement) {
7097
+ document.documentElement.appendChild(persistParking);
7085
7098
  }
7086
7099
  }
7087
7100
  function detachPersist(root = document) {
@@ -7099,12 +7112,18 @@
7099
7112
  });
7100
7113
  }
7101
7114
  function mergePersistIntoLive(root) {
7115
+ // Return the host to the current body before scope mounting/cleanup inspects it.
7116
+ if (persistParking && persistParking.parentElement !== document.body) {
7117
+ document.body.appendChild(persistParking);
7118
+ }
7102
7119
  if (!persistEntries.size)
7103
7120
  return;
7104
7121
  // If placeholders lack ids but we only have one persist entry, assign its key for convenience
7105
7122
  if (persistEntries.size === 1) {
7106
7123
  const loneKey = Array.from(persistEntries.keys())[0];
7107
7124
  elementsIn(root).forEach(el => {
7125
+ if (persistParking?.contains(el))
7126
+ return;
7108
7127
  if (!getPersistKey(el, false)) {
7109
7128
  el.dataset.gyosPersistId = loneKey;
7110
7129
  }
@@ -7112,6 +7131,8 @@
7112
7131
  }
7113
7132
  // Element placeholders
7114
7133
  elementsIn(root).forEach(placeholder => {
7134
+ if (persistParking?.contains(placeholder))
7135
+ return;
7115
7136
  const key = getPersistKey(placeholder, false);
7116
7137
  if (!key)
7117
7138
  return;
@@ -7129,6 +7150,8 @@
7129
7150
  const toReplace = [];
7130
7151
  while (walker.nextNode()) {
7131
7152
  const comment = walker.currentNode;
7153
+ if (persistParking?.contains(comment))
7154
+ continue;
7132
7155
  const match = comment.data.match(/g-persist[:=]\s*([A-Za-z0-9_\-:.]+)/);
7133
7156
  if (match) {
7134
7157
  toReplace.push({ node: comment, key: match[1] });
@@ -7786,6 +7809,36 @@
7786
7809
  * Implements attribute-driven routing, snapshot caching, and persistent islands.
7787
7810
  */
7788
7811
  const submittingForms = new WeakSet();
7812
+ async function visit(url, options, fresh) {
7813
+ const destination = new URL(url, document.baseURI);
7814
+ if (!['http:', 'https:'].includes(destination.protocol) || destination.origin !== location.origin || destination.username || destination.password) {
7815
+ throw new TypeError('Gyos.navigate requires a same-origin HTTP(S) URL without credentials.');
7816
+ }
7817
+ if (options.target !== undefined && !(document.querySelector(options.target) instanceof HTMLElement)) {
7818
+ throw new TypeError('Gyos.navigate target does not exist.');
7819
+ }
7820
+ if (!document.querySelector('[g-boost]')) {
7821
+ throw new TypeError('Gyos.navigate requires a g-boost layout.');
7822
+ }
7823
+ startRouter();
7824
+ let result;
7825
+ await navigateRequest({
7826
+ url: destination.href, method: 'GET',
7827
+ changeState: options.history !== 'none', replace: options.history === 'replace',
7828
+ targetSelector: options.target, strictTarget: options.target !== undefined, swapMode: options.swap,
7829
+ currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, fresh,
7830
+ onEnd: context => { result = context; }
7831
+ });
7832
+ return result;
7833
+ }
7834
+ /** Navigate through the boost pipeline. Resolves after navigation-end hooks. */
7835
+ function navigate(url, options = {}) {
7836
+ return visit(url, options, false);
7837
+ }
7838
+ /** Fetch the current URL again, preserving history and scroll by default. */
7839
+ function refresh(options = {}) {
7840
+ return visit(location.href, { history: 'none', scroll: false, ...options }, true);
7841
+ }
7789
7842
  const snapshots = new Map();
7790
7843
  const preloadCache = new Map();
7791
7844
  const preloadControllers = new Map();
@@ -8172,12 +8225,27 @@
8172
8225
  }
8173
8226
  console.error(`[GyosRouter] ${method} navigation could not be boosted; the request will not be replayed.`);
8174
8227
  }
8175
- async function navigate(opts) {
8228
+ async function navigateRequest(opts) {
8229
+ const finishUnstarted = () => {
8230
+ opts.onEnd?.({ from: currentUrl, url: opts.url, method: opts.method,
8231
+ trigger: opts.trigger || null, target: null, form: null, popstate: false,
8232
+ signal: opts.signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
8233
+ };
8234
+ if (opts.signal?.aborted) {
8235
+ finishUnstarted();
8236
+ return;
8237
+ }
8176
8238
  while (activeNavigation) {
8177
8239
  const previous = activeNavigation;
8178
8240
  if (previous.committing) {
8179
8241
  log('[GyosRouter] Waiting for previous DOM commit');
8180
- await previous.done;
8242
+ try {
8243
+ await (opts.signal ? waitForNavigation(previous.done, opts.signal) : previous.done);
8244
+ }
8245
+ catch {
8246
+ finishUnstarted();
8247
+ return;
8248
+ }
8181
8249
  continue;
8182
8250
  }
8183
8251
  log('[GyosRouter] Cancelling previous navigation');
@@ -8196,6 +8264,10 @@
8196
8264
  resolveDone
8197
8265
  };
8198
8266
  activeNavigation = transaction;
8267
+ const abort = () => cancelNavigation(transaction);
8268
+ opts.signal?.addEventListener('abort', abort, { once: true });
8269
+ if (opts.signal?.aborted)
8270
+ abort();
8199
8271
  const currentBaseUrl = document.baseURI;
8200
8272
  rememberResourceUrls(document, currentBaseUrl);
8201
8273
  const context = {
@@ -8219,6 +8291,7 @@
8219
8291
  }
8220
8292
  };
8221
8293
  try {
8294
+ assertActiveNavigation(transaction);
8222
8295
  if (formEvent)
8223
8296
  document.dispatchEvent(new CustomEvent('gyos:form-start', { detail: { ...formEvent } }));
8224
8297
  runNavigationHooks(beforeNavigate, opts.url, context);
@@ -8231,6 +8304,10 @@
8231
8304
  saveScrollPosition();
8232
8305
  // Pick target element to swap
8233
8306
  let target = resolveTarget(opts.trigger, opts.targetSelector, opts.targetOutletIndex);
8307
+ if (opts.strictTarget && (!opts.targetSelector || !target?.matches(opts.targetSelector))) {
8308
+ context.outcome = 'invalid-response';
8309
+ return; // A queued explicit target disappeared; never broaden the swap.
8310
+ }
8234
8311
  context.target = target;
8235
8312
  if (!target) {
8236
8313
  if (isActiveNavigation(transaction))
@@ -8275,7 +8352,12 @@
8275
8352
  else {
8276
8353
  // Check preload cache if GET
8277
8354
  const preloadKey = canonicalUrl(finalUrl, true);
8278
- if (opts.method.toUpperCase() === 'GET' && preloadCache.has(preloadKey)) {
8355
+ if (opts.fresh) {
8356
+ preloadControllers.get(preloadKey)?.abort();
8357
+ preloadControllers.delete(preloadKey);
8358
+ preloadCache.delete(preloadKey);
8359
+ }
8360
+ if (!opts.fresh && opts.method.toUpperCase() === 'GET' && preloadCache.has(preloadKey)) {
8279
8361
  log('[GyosRouter] using preload cache for', finalUrl);
8280
8362
  try {
8281
8363
  response = await waitForNavigation(preloadCache.get(preloadKey), transaction.controller.signal);
@@ -8483,7 +8565,10 @@
8483
8565
  finalizeCommit();
8484
8566
  }
8485
8567
  // Handle scroll after mount scopes
8486
- if (changesState) {
8568
+ if (opts.scroll === false) {
8569
+ // Explicit refresh/navigation policy keeps the current viewport.
8570
+ }
8571
+ else if (changesState) {
8487
8572
  handleScroll(targetUrl, opts.trigger);
8488
8573
  }
8489
8574
  else if (opts.popstate) {
@@ -8548,6 +8633,7 @@
8548
8633
  activeNavigation = null;
8549
8634
  progressBar?.complete();
8550
8635
  }
8636
+ opts.signal?.removeEventListener('abort', abort);
8551
8637
  transaction.resolveDone();
8552
8638
  if (formEvent)
8553
8639
  submittingForms.delete(formEvent.form);
@@ -8558,6 +8644,7 @@
8558
8644
  detail: { ...formEvent, url, status, outcome, committed, fallback }
8559
8645
  }));
8560
8646
  }
8647
+ opts.onEnd?.({ ...context });
8561
8648
  }
8562
8649
  }
8563
8650
  function shouldBoost(el) {
@@ -8588,7 +8675,7 @@
8588
8675
  const scope = getScopeFromElement(element);
8589
8676
  const jsonData = evaluateExpression(decodeURIComponent(element.getAttribute(paramsAttr) || ''), scope, false);
8590
8677
  // The default changeState is false, as this is commonly used for: load more, infinite scroll, etc.
8591
- navigate({
8678
+ navigateRequest({
8592
8679
  url: parsedUrl.toString(), method, trigger: element, jsonData, changeState: false
8593
8680
  });
8594
8681
  }
@@ -8637,7 +8724,7 @@
8637
8724
  return;
8638
8725
  event.preventDefault();
8639
8726
  // The default changeState is true, as this is commonly used for: navigation, etc.
8640
- navigate({ url: url.toString(), method: 'GET', trigger: link, changeState: true });
8727
+ navigateRequest({ url: url.toString(), method: 'GET', trigger: link, changeState: true });
8641
8728
  }
8642
8729
  function onMouseOverCapture(event) {
8643
8730
  const target = event.target;
@@ -8693,7 +8780,7 @@
8693
8780
  return;
8694
8781
  const formData = new FormData(form, submitter);
8695
8782
  submittingForms.add(form);
8696
- void navigate({ url: url.toString(), method, trigger: form, form, formData, formEncoding, changeState: method === 'GET' });
8783
+ void navigateRequest({ url: url.toString(), method, trigger: form, form, formData, formEncoding, changeState: method === 'GET' });
8697
8784
  }
8698
8785
  function onPopState(event) {
8699
8786
  const url = window.location.href;
@@ -8711,14 +8798,14 @@
8711
8798
  }
8712
8799
  event.preventDefault();
8713
8800
  // The browser changes location before popstate fires. Stop any pending page
8714
- // script immediately, then let navigate() wait for its DOM finalizer.
8801
+ // script immediately, then let navigateRequest() wait for its DOM finalizer.
8715
8802
  if (activeNavigation?.committing)
8716
8803
  cancelNavigation(activeNavigation);
8717
8804
  // The browser has already moved to the destination history entry. Keep the
8718
8805
  // router's entry pointer aligned before any restored page script can navigate.
8719
8806
  currentHistoryEntryId = destinationEntryId;
8720
8807
  // No snapshot, do a normal navigation without changing history (changeState: false)
8721
- navigate({
8808
+ navigateRequest({
8722
8809
  url,
8723
8810
  method: 'GET',
8724
8811
  replace: true,
@@ -8800,10 +8887,12 @@
8800
8887
  onAfterNavigate,
8801
8888
  onBeforeSwap,
8802
8889
  onNavigationEnd,
8890
+ navigate,
8891
+ refresh,
8803
8892
  portalCreate,
8804
8893
  portalDestroy,
8805
8894
  setCspNonce,
8806
- version: '0.4.1'
8895
+ version: '0.5.0'
8807
8896
  };
8808
8897
 
8809
8898
  setExpressionRuntime(standardExpressionRuntime);
@@ -8834,6 +8923,7 @@
8834
8923
  exports.mountAll = mountAll;
8835
8924
  exports.mountTree = mountTree;
8836
8925
  exports.mountedScopes = getAllMountedScopes;
8926
+ exports.navigate = navigate;
8837
8927
  exports.nextTick = nextTick;
8838
8928
  exports.off = off;
8839
8929
  exports.on = on;
@@ -8847,6 +8937,7 @@
8847
8937
  exports.portalDestroy = portalDestroy;
8848
8938
  exports.provide = provide;
8849
8939
  exports.ready = ready;
8940
+ exports.refresh = refresh;
8850
8941
  exports.registerTransition = registerTransition;
8851
8942
  exports.removeStore = removeStore;
8852
8943
  exports.scope = scope;