gyosjs 0.3.0 → 0.3.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.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * GyosJS v0.3.0
2
+ * GyosJS v0.3.2
3
3
  * (c) 2026
4
4
  * @license MIT
5
5
  */
@@ -2294,32 +2294,24 @@ function applyTransitionStyles() {
2294
2294
  * Handles:
2295
2295
  * 1. Literal string: "fade" or "slide-down"
2296
2296
  * 2. Expression with {}: "{transition}"
2297
- * 3. Expression without {}: "transition" (evaluate as variable)
2297
+ * 3. Quoted literal kept for backwards compatibility: "'fade'"
2298
2298
  *
2299
2299
  * @param attrValue - The g-transition attribute value
2300
2300
  * @param scope - Current scope for expression evaluation
2301
2301
  * @returns The transition name (string)
2302
2302
  */
2303
2303
  function parseTransitionName(attrValue, scope) {
2304
- if (attrValue.includes('{') && attrValue.includes('}')) {
2305
- // Extract expression from {} and evaluate it
2306
- const match = attrValue.match(/\{([^}]+)\}/);
2307
- if (match) {
2308
- return evaluateExpression(match[1].trim(), scope);
2309
- }
2310
- }
2311
- // Try to evaluate as expression first (variable name)
2312
- // If it fails or returns undefined, use as literal string
2313
- try {
2314
- const evaluated = evaluateExpression(attrValue, scope);
2315
- if (evaluated !== undefined && evaluated !== null) {
2316
- return evaluated;
2317
- }
2304
+ const value = attrValue.trim();
2305
+ const expression = /^\{([\s\S]+)\}$/.exec(value);
2306
+ if (expression) {
2307
+ const evaluated = evaluateExpression(expression[1].trim(), scope);
2308
+ return evaluated == null ? '' : String(evaluated);
2318
2309
  }
2319
- catch (e) {
2320
- // If evaluation fails, use value as literal string (built-in transition name)
2310
+ if (value.length >= 2 && ((value.startsWith("'") && value.endsWith("'"))
2311
+ || (value.startsWith('"') && value.endsWith('"')))) {
2312
+ return value.slice(1, -1);
2321
2313
  }
2322
- return attrValue;
2314
+ return value;
2323
2315
  }
2324
2316
  /**
2325
2317
  * Parse transition config from element's g-transition attribute
@@ -5223,6 +5215,10 @@ function processStructuralDirectives(elements, scope) {
5223
5215
  const structuralElements = [];
5224
5216
  const root = elements[0];
5225
5217
  elements.forEach(el => {
5218
+ // Await branches are detached and parsed only after their alias scope exists.
5219
+ // Do not evaluate nested structural expressions during the parent scan.
5220
+ if (isAwaitBranchDescendant(el))
5221
+ return;
5226
5222
  const hasIf = el.hasAttribute('*if');
5227
5223
  const hasFor = el.hasAttribute('*for');
5228
5224
  const hasSwitch = el.hasAttribute('*switch');
@@ -5267,6 +5263,20 @@ function processStructuralDirectives(elements, scope) {
5267
5263
  }
5268
5264
  });
5269
5265
  }
5266
+ function isAwaitBranchDescendant(element) {
5267
+ let child = element;
5268
+ let parent = element.parentElement;
5269
+ while (parent && parent !== document.body) {
5270
+ if (parent.hasAttribute('*await') && (child.hasAttribute('*pending') ||
5271
+ child.hasAttribute('*then') ||
5272
+ child.hasAttribute('*catch'))) {
5273
+ return true;
5274
+ }
5275
+ child = parent;
5276
+ parent = parent.parentElement;
5277
+ }
5278
+ return false;
5279
+ }
5270
5280
  const elementBelongsToAnotherScope = (el, scope) => {
5271
5281
  const owner = getScopeFromElement(el);
5272
5282
  return Boolean(owner && owner !== scope);
@@ -6788,7 +6798,7 @@ function hideTargetSpinner(spinner) {
6788
6798
  }
6789
6799
 
6790
6800
  let nonceSource;
6791
- /** Configure the nonce used when Gyos recreates scripts during MPA navigation. */
6801
+ /** Configure the active document nonce used for scripts and styles during MPA navigation. */
6792
6802
  function setCspNonce(source) {
6793
6803
  nonceSource = source;
6794
6804
  }
@@ -7360,9 +7370,33 @@ function handleScroll(url, trigger, savedScroll) {
7360
7370
  window.scrollTo({ top: 0, behavior: 'smooth' });
7361
7371
  }
7362
7372
 
7363
- function serializeAttributes(el) {
7364
- return Array.from(el.attributes)
7365
- .map(attr => `${attr.name}=${attr.value}`)
7373
+ function isNonceMeta(el) {
7374
+ return el.tagName === 'META' && el.getAttribute('name')?.toLowerCase() === 'csp-nonce';
7375
+ }
7376
+ function isStylesheetLink(el) {
7377
+ return el.tagName === 'LINK'
7378
+ && (el.getAttribute('rel') || '').toLowerCase().split(/\s+/).includes('stylesheet');
7379
+ }
7380
+ function usesCspNonce(el) {
7381
+ return el.tagName === 'SCRIPT' || el.tagName === 'STYLE' || isStylesheetLink(el);
7382
+ }
7383
+ function serializeAttributes(el, context) {
7384
+ const attributes = new Map();
7385
+ Array.from(el.attributes).forEach(attr => {
7386
+ if (context.csp && usesCspNonce(el) && attr.name.toLowerCase() === 'nonce')
7387
+ return;
7388
+ if (context.csp && isNonceMeta(el) && attr.name.toLowerCase() === 'content')
7389
+ return;
7390
+ attributes.set(attr.name, attr.value);
7391
+ });
7392
+ if (context.csp && usesCspNonce(el) && context.activeNonce) {
7393
+ attributes.set('nonce', context.activeNonce);
7394
+ }
7395
+ if (context.csp && isNonceMeta(el) && context.activeNonce) {
7396
+ attributes.set('content', context.activeNonce);
7397
+ }
7398
+ return Array.from(attributes)
7399
+ .map(([name, value]) => `${name}=${value}`)
7366
7400
  .sort()
7367
7401
  .join('|');
7368
7402
  }
@@ -7373,23 +7407,61 @@ function hashContent(content) {
7373
7407
  }
7374
7408
  return String(hash);
7375
7409
  }
7376
- function elementKey(el) {
7410
+ function elementKey(el, context) {
7377
7411
  const tag = el.tagName.toLowerCase();
7378
7412
  const content = tag === 'script' || tag === 'style' ? hashContent(el.textContent || '') : '';
7379
- return `${tag}|${serializeAttributes(el)}|${content}`;
7413
+ return `${tag}|${serializeAttributes(el, context)}|${content}`;
7380
7414
  }
7381
- function keyedNodes(nodes) {
7415
+ function keyedNodes(nodes, context) {
7382
7416
  const occurrences = new Map();
7383
7417
  return nodes.map(node => {
7384
- const baseKey = elementKey(node);
7418
+ const baseKey = elementKey(node, context);
7385
7419
  const occurrence = occurrences.get(baseKey) || 0;
7386
7420
  occurrences.set(baseKey, occurrence + 1);
7387
7421
  return { key: `${baseKey}|${occurrence}`, node };
7388
7422
  });
7389
7423
  }
7424
+ function canInsertNode(node, context) {
7425
+ if (!context.csp)
7426
+ return true;
7427
+ if (isNonceMeta(node))
7428
+ return Boolean(context.activeNonce);
7429
+ if (node.tagName === 'STYLE')
7430
+ return Boolean(context.activeNonce);
7431
+ if (node.tagName === 'SCRIPT' && !node.hasAttribute('src'))
7432
+ return Boolean(context.activeNonce);
7433
+ return true;
7434
+ }
7435
+ function reconcileCspAttributes(node, context) {
7436
+ if (!context.csp)
7437
+ return;
7438
+ if (isNonceMeta(node)) {
7439
+ if (context.activeNonce)
7440
+ node.setAttribute('content', context.activeNonce);
7441
+ return;
7442
+ }
7443
+ if (!usesCspNonce(node))
7444
+ return;
7445
+ node.removeAttribute('nonce');
7446
+ if (context.activeNonce)
7447
+ node.setAttribute('nonce', context.activeNonce);
7448
+ }
7449
+ function warnSkippedNode(node) {
7450
+ if (node.tagName === 'STYLE') {
7451
+ console.warn('[GyosJS CSP] Skipped an inline MPA style because no active document nonce is configured.');
7452
+ }
7453
+ else if (node.tagName === 'SCRIPT' && !node.hasAttribute('src')) {
7454
+ console.warn('[GyosJS CSP] Skipped an inline MPA script because no active document nonce is configured.');
7455
+ }
7456
+ }
7390
7457
  async function diffNodes(currentNodes, nextNodes, container = document.head, signal) {
7391
- const current = keyedNodes(currentNodes);
7392
- const next = keyedNodes(nextNodes);
7458
+ const csp = expressionRuntimeMode() === 'csp';
7459
+ const context = {
7460
+ csp,
7461
+ activeNonce: csp ? resolveCspNonce() : undefined
7462
+ };
7463
+ const current = keyedNodes(currentNodes, context);
7464
+ const next = keyedNodes(nextNodes, context);
7393
7465
  const currentKeys = new Set(current.map(item => item.key));
7394
7466
  const nextKeys = new Set(next.map(item => item.key));
7395
7467
  // Remove nodes that are not in the new head
@@ -7403,6 +7475,20 @@ async function diffNodes(currentNodes, nextNodes, container = document.head, sig
7403
7475
  if (signal?.aborted)
7404
7476
  return;
7405
7477
  if (!currentKeys.has(key)) {
7478
+ if (!canInsertNode(node, context)) {
7479
+ warnSkippedNode(node);
7480
+ continue;
7481
+ }
7482
+ if (node.tagName === 'STYLE') {
7483
+ const style = document.createElement('style');
7484
+ Array.from(node.attributes).forEach(attr => style.setAttribute(attr.name, attr.value));
7485
+ reconcileCspAttributes(style, context);
7486
+ // CSP checks inline styles as their CSS text is attached. Set the active
7487
+ // nonce first so a response nonce never produces a transient violation.
7488
+ style.textContent = node.textContent || '';
7489
+ container.appendChild(style);
7490
+ continue;
7491
+ }
7406
7492
  if (node.tagName === 'SCRIPT') {
7407
7493
  const originalScript = node;
7408
7494
  const executionKey = getScriptExecutionKey(originalScript);
@@ -7413,6 +7499,7 @@ async function diffNodes(currentNodes, nextNodes, container = document.head, sig
7413
7499
  }
7414
7500
  const script = document.createElement('script');
7415
7501
  Array.from(node.attributes).forEach(attr => script.setAttribute(attr.name, attr.value));
7502
+ reconcileCspAttributes(script, context);
7416
7503
  script.textContent = node.hasAttribute('g-script-wrap')
7417
7504
  ? wrapScriptExecution(node.textContent || '')
7418
7505
  : node.textContent || '';
@@ -7422,9 +7509,166 @@ async function diffNodes(currentNodes, nextNodes, container = document.head, sig
7422
7509
  await completion;
7423
7510
  continue;
7424
7511
  }
7425
- container.appendChild(document.importNode(node, true));
7512
+ const imported = document.importNode(node, true);
7513
+ reconcileCspAttributes(imported, context);
7514
+ container.appendChild(imported);
7515
+ }
7516
+ }
7517
+ }
7518
+
7519
+ const RAW_TEXT_ELEMENTS = new Set(['iframe', 'noembed', 'noframes', 'plaintext', 'script', 'textarea', 'title', 'xmp']);
7520
+ function escapeAttribute(value) {
7521
+ return value.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
7522
+ }
7523
+ function findTagEnd(html, start) {
7524
+ let quote = '';
7525
+ for (let index = start; index < html.length; index += 1) {
7526
+ const character = html[index];
7527
+ if (quote) {
7528
+ if (character === quote)
7529
+ quote = '';
7530
+ continue;
7531
+ }
7532
+ if (character === '"' || character === "'") {
7533
+ quote = character;
7534
+ continue;
7535
+ }
7536
+ if (character === '>')
7537
+ return index + 1;
7538
+ }
7539
+ return html.length;
7540
+ }
7541
+ function findClosingTag(html, lowerHtml, name, start) {
7542
+ const needle = `</${name}`;
7543
+ let position = lowerHtml.indexOf(needle, start);
7544
+ while (position !== -1) {
7545
+ const boundary = lowerHtml[position + needle.length];
7546
+ if (boundary === '>' || /\s/.test(boundary || '')) {
7547
+ return {
7548
+ start: position,
7549
+ end: findTagEnd(html, position + needle.length)
7550
+ };
7551
+ }
7552
+ position = lowerHtml.indexOf(needle, position + needle.length);
7553
+ }
7554
+ return null;
7555
+ }
7556
+ function removeNonceAttribute(openingTag) {
7557
+ const prefix = /^<\s*style\b/i.exec(openingTag);
7558
+ if (!prefix)
7559
+ return openingTag;
7560
+ let output = prefix[0];
7561
+ let cursor = prefix[0].length;
7562
+ const limit = openingTag.length - 1;
7563
+ while (cursor < limit) {
7564
+ const attributeStart = cursor;
7565
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
7566
+ cursor += 1;
7567
+ if (cursor >= limit || openingTag[cursor] === '>') {
7568
+ output += openingTag.slice(attributeStart, cursor);
7569
+ break;
7570
+ }
7571
+ const nameStart = cursor;
7572
+ while (cursor < limit && !/[\s=/>]/.test(openingTag[cursor]))
7573
+ cursor += 1;
7574
+ if (cursor === nameStart) {
7575
+ output += openingTag.slice(attributeStart, cursor + 1);
7576
+ cursor += 1;
7577
+ continue;
7578
+ }
7579
+ const name = openingTag.slice(nameStart, cursor).toLowerCase();
7580
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
7581
+ cursor += 1;
7582
+ if (openingTag[cursor] === '=') {
7583
+ cursor += 1;
7584
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
7585
+ cursor += 1;
7586
+ const quote = openingTag[cursor] === '"' || openingTag[cursor] === "'" ? openingTag[cursor] : '';
7587
+ if (quote) {
7588
+ cursor += 1;
7589
+ while (cursor < limit && openingTag[cursor] !== quote)
7590
+ cursor += 1;
7591
+ if (openingTag[cursor] === quote)
7592
+ cursor += 1;
7593
+ }
7594
+ else {
7595
+ while (cursor < limit && !/[\s>]/.test(openingTag[cursor]))
7596
+ cursor += 1;
7597
+ }
7598
+ }
7599
+ if (name !== 'nonce')
7600
+ output += openingTag.slice(attributeStart, cursor);
7601
+ }
7602
+ return output + openingTag.slice(limit);
7603
+ }
7604
+ function withNonce(openingTag, nonce) {
7605
+ const withoutNonce = removeNonceAttribute(openingTag);
7606
+ return `${withoutNonce.slice(0, -1)} nonce="${escapeAttribute(nonce)}">`;
7607
+ }
7608
+ function prepareNavigationHtml(html) {
7609
+ if (expressionRuntimeMode() !== 'csp')
7610
+ return html;
7611
+ const activeNonce = resolveCspNonce();
7612
+ const lowerHtml = html.toLowerCase();
7613
+ let output = '';
7614
+ let cursor = 0;
7615
+ let skippedStyles = 0;
7616
+ while (cursor < html.length) {
7617
+ const tagStart = html.indexOf('<', cursor);
7618
+ if (tagStart === -1) {
7619
+ output += html.slice(cursor);
7620
+ break;
7621
+ }
7622
+ output += html.slice(cursor, tagStart);
7623
+ if (html.startsWith('<!--', tagStart)) {
7624
+ const commentEnd = html.indexOf('-->', tagStart + 4);
7625
+ const end = commentEnd === -1 ? html.length : commentEnd + 3;
7626
+ output += html.slice(tagStart, end);
7627
+ cursor = end;
7628
+ continue;
7629
+ }
7630
+ const tagEnd = findTagEnd(html, tagStart + 1);
7631
+ const openingTag = html.slice(tagStart, tagEnd);
7632
+ const match = /^<\s*(\/?)\s*([a-z][\w:-]*)/i.exec(openingTag);
7633
+ if (!match || match[1]) {
7634
+ output += openingTag;
7635
+ cursor = tagEnd;
7636
+ continue;
7637
+ }
7638
+ const name = match[2].toLowerCase();
7639
+ if (name === 'plaintext') {
7640
+ output += html.slice(tagStart);
7641
+ break;
7642
+ }
7643
+ if (name === 'style' || name === 'noscript' || RAW_TEXT_ELEMENTS.has(name)) {
7644
+ const closingTag = findClosingTag(html, lowerHtml, name, tagEnd);
7645
+ const blockEnd = closingTag?.end ?? html.length;
7646
+ if (name === 'noscript') {
7647
+ cursor = blockEnd;
7648
+ continue;
7649
+ }
7650
+ if (name === 'style') {
7651
+ if (!activeNonce) {
7652
+ skippedStyles += 1;
7653
+ cursor = blockEnd;
7654
+ continue;
7655
+ }
7656
+ output += withNonce(openingTag, activeNonce);
7657
+ output += html.slice(tagEnd, blockEnd);
7658
+ cursor = blockEnd;
7659
+ continue;
7660
+ }
7661
+ output += html.slice(tagStart, blockEnd);
7662
+ cursor = blockEnd;
7663
+ continue;
7426
7664
  }
7665
+ output += openingTag;
7666
+ cursor = tagEnd;
7667
+ }
7668
+ if (skippedStyles > 0) {
7669
+ console.warn(`[GyosJS CSP] Skipped ${skippedStyles} inline MPA style${skippedStyles === 1 ? '' : 's'} because no active document nonce is configured.`);
7427
7670
  }
7671
+ return output;
7428
7672
  }
7429
7673
 
7430
7674
  /**
@@ -7490,7 +7734,10 @@ function stripInertFallbackContent(root) {
7490
7734
  root.querySelectorAll('noscript').forEach(element => element.remove());
7491
7735
  }
7492
7736
  function parseNavigationDocument(html) {
7493
- const doc = new DOMParser().parseFromString(html, 'text/html');
7737
+ // Chromium applies the active page CSP while DOMParser creates response
7738
+ // styles. Reconcile their nonce before parsing to avoid transient violations;
7739
+ // diffNodes repeats the check before insertion into the live document.
7740
+ const doc = new DOMParser().parseFromString(prepareNavigationHtml(html), 'text/html');
7494
7741
  // DOMParser disables scripting and parses noscript children as active markup.
7495
7742
  // A boosted page runs with scripting enabled, so those fallbacks must stay inert.
7496
7743
  stripInertFallbackContent(doc);
@@ -8326,7 +8573,7 @@ const Gyos = {
8326
8573
  portalCreate,
8327
8574
  portalDestroy,
8328
8575
  setCspNonce,
8329
- version: '0.3.0'
8576
+ version: '0.3.2'
8330
8577
  };
8331
8578
 
8332
8579
  setExpressionRuntime(standardExpressionRuntime);