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.csp.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
  */
@@ -12285,6 +12285,21 @@
12285
12285
  * Registry of all scopes
12286
12286
  */
12287
12287
  const scopeRegistry = new Map();
12288
+ const warnedReveals = new WeakSet();
12289
+ function diagnoseUnscopedReveals(root) {
12290
+ if (!DEBUG())
12291
+ return;
12292
+ const elements = [...root.querySelectorAll('[g-reveal]')];
12293
+ if (root instanceof Element && root.hasAttribute('g-reveal'))
12294
+ elements.unshift(root);
12295
+ for (const element of elements) {
12296
+ if (warnedReveals.has(element) || isInIgnoredTree(element)
12297
+ || element.closest('[g-scope]') || getScopeFromElement(element))
12298
+ continue;
12299
+ warnedReveals.add(element);
12300
+ console.warn('[Gyos] g-reveal has no owning scope. Add g-scope to its container; global reveal CSS may otherwise hide this content.', element);
12301
+ }
12302
+ }
12288
12303
  /**
12289
12304
  * Register a scope
12290
12305
  */
@@ -12332,6 +12347,7 @@
12332
12347
  allElements.sort((a, b) => getDepth(b) - getDepth(a));
12333
12348
  // Filter out elements inside structural directives - they will be mounted by structural processor
12334
12349
  allElements.forEach(el => !hasStructuralParent(el) && mount(el));
12350
+ diagnoseUnscopedReveals(document.body);
12335
12351
  }
12336
12352
  function mountTree(el) {
12337
12353
  if (isInIgnoredTree(el) || !document.body.contains(el))
@@ -12342,6 +12358,7 @@
12342
12358
  // Child scopes own their subtree before an existing parent scope parses the insertion.
12343
12359
  candidates.sort((a, b) => getDepth(b) - getDepth(a));
12344
12360
  candidates.forEach(element => !hasStructuralParent(element) && mount(element));
12361
+ diagnoseUnscopedReveals(el);
12345
12362
  if (getMountedScope(el))
12346
12363
  return;
12347
12364
  const ownerScope = getScopeFromElement(el);
@@ -12360,7 +12377,7 @@
12360
12377
  return;
12361
12378
  // Check if element removed but still has g-scope (possible in some edge cases)
12362
12379
  getAllMountedScopes().forEach((_, mountedEl) => {
12363
- if (!document.body.contains(mountedEl)) {
12380
+ if (!mountedEl.isConnected) {
12364
12381
  disposeEffects(mountedEl);
12365
12382
  }
12366
12383
  });
@@ -13672,7 +13689,8 @@
13672
13689
  el.dataset.gyosPersistId = key;
13673
13690
  return key;
13674
13691
  }
13675
- // Hidden parking lot keeps persisted nodes alive (e.g. audio keeps playing)
13692
+ // Keep the parking host outside body: body itself may be the swap target.
13693
+ // A disconnected parking host makes browsers pause otherwise persisted media.
13676
13694
  const persistParking = typeof document !== 'undefined'
13677
13695
  ? (() => {
13678
13696
  const park = document.createElement('div');
@@ -13681,12 +13699,7 @@
13681
13699
  park.style.top = '-99999px';
13682
13700
  park.style.width = '0';
13683
13701
  park.style.height = '0';
13684
- if (document.readyState === 'loading') {
13685
- document.addEventListener('DOMContentLoaded', () => document.body.appendChild(park));
13686
- }
13687
- else {
13688
- document.body.appendChild(park);
13689
- }
13702
+ document.documentElement.appendChild(park);
13690
13703
  return park;
13691
13704
  })()
13692
13705
  : null;
@@ -13697,8 +13710,8 @@
13697
13710
  return elements;
13698
13711
  }
13699
13712
  function ensureParkingIsConnected() {
13700
- if (persistParking && document.body && !persistParking.isConnected) {
13701
- document.body.appendChild(persistParking);
13713
+ if (persistParking && persistParking.parentElement !== document.documentElement) {
13714
+ document.documentElement.appendChild(persistParking);
13702
13715
  }
13703
13716
  }
13704
13717
  function detachPersist(root = document) {
@@ -13716,12 +13729,18 @@
13716
13729
  });
13717
13730
  }
13718
13731
  function mergePersistIntoLive(root) {
13732
+ // Return the host to the current body before scope mounting/cleanup inspects it.
13733
+ if (persistParking && persistParking.parentElement !== document.body) {
13734
+ document.body.appendChild(persistParking);
13735
+ }
13719
13736
  if (!persistEntries.size)
13720
13737
  return;
13721
13738
  // If placeholders lack ids but we only have one persist entry, assign its key for convenience
13722
13739
  if (persistEntries.size === 1) {
13723
13740
  const loneKey = Array.from(persistEntries.keys())[0];
13724
13741
  elementsIn(root).forEach(el => {
13742
+ if (persistParking?.contains(el))
13743
+ return;
13725
13744
  if (!getPersistKey(el, false)) {
13726
13745
  el.dataset.gyosPersistId = loneKey;
13727
13746
  }
@@ -13729,6 +13748,8 @@
13729
13748
  }
13730
13749
  // Element placeholders
13731
13750
  elementsIn(root).forEach(placeholder => {
13751
+ if (persistParking?.contains(placeholder))
13752
+ return;
13732
13753
  const key = getPersistKey(placeholder, false);
13733
13754
  if (!key)
13734
13755
  return;
@@ -13746,6 +13767,8 @@
13746
13767
  const toReplace = [];
13747
13768
  while (walker.nextNode()) {
13748
13769
  const comment = walker.currentNode;
13770
+ if (persistParking?.contains(comment))
13771
+ continue;
13749
13772
  const match = comment.data.match(/g-persist[:=]\s*([A-Za-z0-9_\-:.]+)/);
13750
13773
  if (match) {
13751
13774
  toReplace.push({ node: comment, key: match[1] });
@@ -14403,6 +14426,36 @@
14403
14426
  * Implements attribute-driven routing, snapshot caching, and persistent islands.
14404
14427
  */
14405
14428
  const submittingForms = new WeakSet();
14429
+ async function visit(url, options, fresh) {
14430
+ const destination = new URL(url, document.baseURI);
14431
+ if (!['http:', 'https:'].includes(destination.protocol) || destination.origin !== location.origin || destination.username || destination.password) {
14432
+ throw new TypeError('Gyos.navigate requires a same-origin HTTP(S) URL without credentials.');
14433
+ }
14434
+ if (options.target !== undefined && !(document.querySelector(options.target) instanceof HTMLElement)) {
14435
+ throw new TypeError('Gyos.navigate target does not exist.');
14436
+ }
14437
+ if (!document.querySelector('[g-boost]')) {
14438
+ throw new TypeError('Gyos.navigate requires a g-boost layout.');
14439
+ }
14440
+ startRouter();
14441
+ let result;
14442
+ await navigateRequest({
14443
+ url: destination.href, method: 'GET',
14444
+ changeState: options.history !== 'none', replace: options.history === 'replace',
14445
+ targetSelector: options.target, strictTarget: options.target !== undefined, swapMode: options.swap,
14446
+ currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, fresh,
14447
+ onEnd: context => { result = context; }
14448
+ });
14449
+ return result;
14450
+ }
14451
+ /** Navigate through the boost pipeline. Resolves after navigation-end hooks. */
14452
+ function navigate(url, options = {}) {
14453
+ return visit(url, options, false);
14454
+ }
14455
+ /** Fetch the current URL again, preserving history and scroll by default. */
14456
+ function refresh(options = {}) {
14457
+ return visit(location.href, { history: 'none', scroll: false, ...options }, true);
14458
+ }
14406
14459
  const snapshots = new Map();
14407
14460
  const preloadCache = new Map();
14408
14461
  const preloadControllers = new Map();
@@ -14789,12 +14842,27 @@
14789
14842
  }
14790
14843
  console.error(`[GyosRouter] ${method} navigation could not be boosted; the request will not be replayed.`);
14791
14844
  }
14792
- async function navigate(opts) {
14845
+ async function navigateRequest(opts) {
14846
+ const finishUnstarted = () => {
14847
+ opts.onEnd?.({ from: currentUrl, url: opts.url, method: opts.method,
14848
+ trigger: opts.trigger || null, target: null, form: null, popstate: false,
14849
+ signal: opts.signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
14850
+ };
14851
+ if (opts.signal?.aborted) {
14852
+ finishUnstarted();
14853
+ return;
14854
+ }
14793
14855
  while (activeNavigation) {
14794
14856
  const previous = activeNavigation;
14795
14857
  if (previous.committing) {
14796
14858
  log('[GyosRouter] Waiting for previous DOM commit');
14797
- await previous.done;
14859
+ try {
14860
+ await (opts.signal ? waitForNavigation(previous.done, opts.signal) : previous.done);
14861
+ }
14862
+ catch {
14863
+ finishUnstarted();
14864
+ return;
14865
+ }
14798
14866
  continue;
14799
14867
  }
14800
14868
  log('[GyosRouter] Cancelling previous navigation');
@@ -14813,6 +14881,10 @@
14813
14881
  resolveDone
14814
14882
  };
14815
14883
  activeNavigation = transaction;
14884
+ const abort = () => cancelNavigation(transaction);
14885
+ opts.signal?.addEventListener('abort', abort, { once: true });
14886
+ if (opts.signal?.aborted)
14887
+ abort();
14816
14888
  const currentBaseUrl = document.baseURI;
14817
14889
  rememberResourceUrls(document, currentBaseUrl);
14818
14890
  const context = {
@@ -14836,6 +14908,7 @@
14836
14908
  }
14837
14909
  };
14838
14910
  try {
14911
+ assertActiveNavigation(transaction);
14839
14912
  if (formEvent)
14840
14913
  document.dispatchEvent(new CustomEvent('gyos:form-start', { detail: { ...formEvent } }));
14841
14914
  runNavigationHooks(beforeNavigate, opts.url, context);
@@ -14848,6 +14921,10 @@
14848
14921
  saveScrollPosition();
14849
14922
  // Pick target element to swap
14850
14923
  let target = resolveTarget(opts.trigger, opts.targetSelector, opts.targetOutletIndex);
14924
+ if (opts.strictTarget && (!opts.targetSelector || !target?.matches(opts.targetSelector))) {
14925
+ context.outcome = 'invalid-response';
14926
+ return; // A queued explicit target disappeared; never broaden the swap.
14927
+ }
14851
14928
  context.target = target;
14852
14929
  if (!target) {
14853
14930
  if (isActiveNavigation(transaction))
@@ -14892,7 +14969,12 @@
14892
14969
  else {
14893
14970
  // Check preload cache if GET
14894
14971
  const preloadKey = canonicalUrl(finalUrl, true);
14895
- if (opts.method.toUpperCase() === 'GET' && preloadCache.has(preloadKey)) {
14972
+ if (opts.fresh) {
14973
+ preloadControllers.get(preloadKey)?.abort();
14974
+ preloadControllers.delete(preloadKey);
14975
+ preloadCache.delete(preloadKey);
14976
+ }
14977
+ if (!opts.fresh && opts.method.toUpperCase() === 'GET' && preloadCache.has(preloadKey)) {
14896
14978
  log('[GyosRouter] using preload cache for', finalUrl);
14897
14979
  try {
14898
14980
  response = await waitForNavigation(preloadCache.get(preloadKey), transaction.controller.signal);
@@ -15100,7 +15182,10 @@
15100
15182
  finalizeCommit();
15101
15183
  }
15102
15184
  // Handle scroll after mount scopes
15103
- if (changesState) {
15185
+ if (opts.scroll === false) {
15186
+ // Explicit refresh/navigation policy keeps the current viewport.
15187
+ }
15188
+ else if (changesState) {
15104
15189
  handleScroll(targetUrl, opts.trigger);
15105
15190
  }
15106
15191
  else if (opts.popstate) {
@@ -15165,6 +15250,7 @@
15165
15250
  activeNavigation = null;
15166
15251
  progressBar?.complete();
15167
15252
  }
15253
+ opts.signal?.removeEventListener('abort', abort);
15168
15254
  transaction.resolveDone();
15169
15255
  if (formEvent)
15170
15256
  submittingForms.delete(formEvent.form);
@@ -15175,6 +15261,7 @@
15175
15261
  detail: { ...formEvent, url, status, outcome, committed, fallback }
15176
15262
  }));
15177
15263
  }
15264
+ opts.onEnd?.({ ...context });
15178
15265
  }
15179
15266
  }
15180
15267
  function shouldBoost(el) {
@@ -15205,7 +15292,7 @@
15205
15292
  const scope = getScopeFromElement(element);
15206
15293
  const jsonData = evaluateExpression(decodeURIComponent(element.getAttribute(paramsAttr) || ''), scope, false);
15207
15294
  // The default changeState is false, as this is commonly used for: load more, infinite scroll, etc.
15208
- navigate({
15295
+ navigateRequest({
15209
15296
  url: parsedUrl.toString(), method, trigger: element, jsonData, changeState: false
15210
15297
  });
15211
15298
  }
@@ -15254,7 +15341,7 @@
15254
15341
  return;
15255
15342
  event.preventDefault();
15256
15343
  // The default changeState is true, as this is commonly used for: navigation, etc.
15257
- navigate({ url: url.toString(), method: 'GET', trigger: link, changeState: true });
15344
+ navigateRequest({ url: url.toString(), method: 'GET', trigger: link, changeState: true });
15258
15345
  }
15259
15346
  function onMouseOverCapture(event) {
15260
15347
  const target = event.target;
@@ -15310,7 +15397,7 @@
15310
15397
  return;
15311
15398
  const formData = new FormData(form, submitter);
15312
15399
  submittingForms.add(form);
15313
- void navigate({ url: url.toString(), method, trigger: form, form, formData, formEncoding, changeState: method === 'GET' });
15400
+ void navigateRequest({ url: url.toString(), method, trigger: form, form, formData, formEncoding, changeState: method === 'GET' });
15314
15401
  }
15315
15402
  function onPopState(event) {
15316
15403
  const url = window.location.href;
@@ -15328,14 +15415,14 @@
15328
15415
  }
15329
15416
  event.preventDefault();
15330
15417
  // The browser changes location before popstate fires. Stop any pending page
15331
- // script immediately, then let navigate() wait for its DOM finalizer.
15418
+ // script immediately, then let navigateRequest() wait for its DOM finalizer.
15332
15419
  if (activeNavigation?.committing)
15333
15420
  cancelNavigation(activeNavigation);
15334
15421
  // The browser has already moved to the destination history entry. Keep the
15335
15422
  // router's entry pointer aligned before any restored page script can navigate.
15336
15423
  currentHistoryEntryId = destinationEntryId;
15337
15424
  // No snapshot, do a normal navigation without changing history (changeState: false)
15338
- navigate({
15425
+ navigateRequest({
15339
15426
  url,
15340
15427
  method: 'GET',
15341
15428
  replace: true,
@@ -15417,10 +15504,12 @@
15417
15504
  onAfterNavigate,
15418
15505
  onBeforeSwap,
15419
15506
  onNavigationEnd,
15507
+ navigate,
15508
+ refresh,
15420
15509
  portalCreate,
15421
15510
  portalDestroy,
15422
15511
  setCspNonce,
15423
- version: '0.4.1'
15512
+ version: '0.5.0'
15424
15513
  };
15425
15514
 
15426
15515
  setExpressionRuntime(cspExpressionRuntime);
@@ -15451,6 +15540,7 @@
15451
15540
  exports.mountAll = mountAll;
15452
15541
  exports.mountTree = mountTree;
15453
15542
  exports.mountedScopes = getAllMountedScopes;
15543
+ exports.navigate = navigate;
15454
15544
  exports.nextTick = nextTick;
15455
15545
  exports.off = off;
15456
15546
  exports.on = on;
@@ -15464,6 +15554,7 @@
15464
15554
  exports.portalDestroy = portalDestroy;
15465
15555
  exports.provide = provide;
15466
15556
  exports.ready = ready;
15557
+ exports.refresh = refresh;
15467
15558
  exports.registerTransition = registerTransition;
15468
15559
  exports.removeStore = removeStore;
15469
15560
  exports.scope = scope;