gyosjs 0.3.0 → 0.3.1

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.1
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
@@ -6788,7 +6780,7 @@ function hideTargetSpinner(spinner) {
6788
6780
  }
6789
6781
 
6790
6782
  let nonceSource;
6791
- /** Configure the nonce used when Gyos recreates scripts during MPA navigation. */
6783
+ /** Configure the active document nonce used for scripts and styles during MPA navigation. */
6792
6784
  function setCspNonce(source) {
6793
6785
  nonceSource = source;
6794
6786
  }
@@ -7360,9 +7352,33 @@ function handleScroll(url, trigger, savedScroll) {
7360
7352
  window.scrollTo({ top: 0, behavior: 'smooth' });
7361
7353
  }
7362
7354
 
7363
- function serializeAttributes(el) {
7364
- return Array.from(el.attributes)
7365
- .map(attr => `${attr.name}=${attr.value}`)
7355
+ function isNonceMeta(el) {
7356
+ return el.tagName === 'META' && el.getAttribute('name')?.toLowerCase() === 'csp-nonce';
7357
+ }
7358
+ function isStylesheetLink(el) {
7359
+ return el.tagName === 'LINK'
7360
+ && (el.getAttribute('rel') || '').toLowerCase().split(/\s+/).includes('stylesheet');
7361
+ }
7362
+ function usesCspNonce(el) {
7363
+ return el.tagName === 'SCRIPT' || el.tagName === 'STYLE' || isStylesheetLink(el);
7364
+ }
7365
+ function serializeAttributes(el, context) {
7366
+ const attributes = new Map();
7367
+ Array.from(el.attributes).forEach(attr => {
7368
+ if (context.csp && usesCspNonce(el) && attr.name.toLowerCase() === 'nonce')
7369
+ return;
7370
+ if (context.csp && isNonceMeta(el) && attr.name.toLowerCase() === 'content')
7371
+ return;
7372
+ attributes.set(attr.name, attr.value);
7373
+ });
7374
+ if (context.csp && usesCspNonce(el) && context.activeNonce) {
7375
+ attributes.set('nonce', context.activeNonce);
7376
+ }
7377
+ if (context.csp && isNonceMeta(el) && context.activeNonce) {
7378
+ attributes.set('content', context.activeNonce);
7379
+ }
7380
+ return Array.from(attributes)
7381
+ .map(([name, value]) => `${name}=${value}`)
7366
7382
  .sort()
7367
7383
  .join('|');
7368
7384
  }
@@ -7373,23 +7389,61 @@ function hashContent(content) {
7373
7389
  }
7374
7390
  return String(hash);
7375
7391
  }
7376
- function elementKey(el) {
7392
+ function elementKey(el, context) {
7377
7393
  const tag = el.tagName.toLowerCase();
7378
7394
  const content = tag === 'script' || tag === 'style' ? hashContent(el.textContent || '') : '';
7379
- return `${tag}|${serializeAttributes(el)}|${content}`;
7395
+ return `${tag}|${serializeAttributes(el, context)}|${content}`;
7380
7396
  }
7381
- function keyedNodes(nodes) {
7397
+ function keyedNodes(nodes, context) {
7382
7398
  const occurrences = new Map();
7383
7399
  return nodes.map(node => {
7384
- const baseKey = elementKey(node);
7400
+ const baseKey = elementKey(node, context);
7385
7401
  const occurrence = occurrences.get(baseKey) || 0;
7386
7402
  occurrences.set(baseKey, occurrence + 1);
7387
7403
  return { key: `${baseKey}|${occurrence}`, node };
7388
7404
  });
7389
7405
  }
7406
+ function canInsertNode(node, context) {
7407
+ if (!context.csp)
7408
+ return true;
7409
+ if (isNonceMeta(node))
7410
+ return Boolean(context.activeNonce);
7411
+ if (node.tagName === 'STYLE')
7412
+ return Boolean(context.activeNonce);
7413
+ if (node.tagName === 'SCRIPT' && !node.hasAttribute('src'))
7414
+ return Boolean(context.activeNonce);
7415
+ return true;
7416
+ }
7417
+ function reconcileCspAttributes(node, context) {
7418
+ if (!context.csp)
7419
+ return;
7420
+ if (isNonceMeta(node)) {
7421
+ if (context.activeNonce)
7422
+ node.setAttribute('content', context.activeNonce);
7423
+ return;
7424
+ }
7425
+ if (!usesCspNonce(node))
7426
+ return;
7427
+ node.removeAttribute('nonce');
7428
+ if (context.activeNonce)
7429
+ node.setAttribute('nonce', context.activeNonce);
7430
+ }
7431
+ function warnSkippedNode(node) {
7432
+ if (node.tagName === 'STYLE') {
7433
+ console.warn('[GyosJS CSP] Skipped an inline MPA style because no active document nonce is configured.');
7434
+ }
7435
+ else if (node.tagName === 'SCRIPT' && !node.hasAttribute('src')) {
7436
+ console.warn('[GyosJS CSP] Skipped an inline MPA script because no active document nonce is configured.');
7437
+ }
7438
+ }
7390
7439
  async function diffNodes(currentNodes, nextNodes, container = document.head, signal) {
7391
- const current = keyedNodes(currentNodes);
7392
- const next = keyedNodes(nextNodes);
7440
+ const csp = expressionRuntimeMode() === 'csp';
7441
+ const context = {
7442
+ csp,
7443
+ activeNonce: csp ? resolveCspNonce() : undefined
7444
+ };
7445
+ const current = keyedNodes(currentNodes, context);
7446
+ const next = keyedNodes(nextNodes, context);
7393
7447
  const currentKeys = new Set(current.map(item => item.key));
7394
7448
  const nextKeys = new Set(next.map(item => item.key));
7395
7449
  // Remove nodes that are not in the new head
@@ -7403,6 +7457,20 @@ async function diffNodes(currentNodes, nextNodes, container = document.head, sig
7403
7457
  if (signal?.aborted)
7404
7458
  return;
7405
7459
  if (!currentKeys.has(key)) {
7460
+ if (!canInsertNode(node, context)) {
7461
+ warnSkippedNode(node);
7462
+ continue;
7463
+ }
7464
+ if (node.tagName === 'STYLE') {
7465
+ const style = document.createElement('style');
7466
+ Array.from(node.attributes).forEach(attr => style.setAttribute(attr.name, attr.value));
7467
+ reconcileCspAttributes(style, context);
7468
+ // CSP checks inline styles as their CSS text is attached. Set the active
7469
+ // nonce first so a response nonce never produces a transient violation.
7470
+ style.textContent = node.textContent || '';
7471
+ container.appendChild(style);
7472
+ continue;
7473
+ }
7406
7474
  if (node.tagName === 'SCRIPT') {
7407
7475
  const originalScript = node;
7408
7476
  const executionKey = getScriptExecutionKey(originalScript);
@@ -7413,6 +7481,7 @@ async function diffNodes(currentNodes, nextNodes, container = document.head, sig
7413
7481
  }
7414
7482
  const script = document.createElement('script');
7415
7483
  Array.from(node.attributes).forEach(attr => script.setAttribute(attr.name, attr.value));
7484
+ reconcileCspAttributes(script, context);
7416
7485
  script.textContent = node.hasAttribute('g-script-wrap')
7417
7486
  ? wrapScriptExecution(node.textContent || '')
7418
7487
  : node.textContent || '';
@@ -7422,9 +7491,166 @@ async function diffNodes(currentNodes, nextNodes, container = document.head, sig
7422
7491
  await completion;
7423
7492
  continue;
7424
7493
  }
7425
- container.appendChild(document.importNode(node, true));
7494
+ const imported = document.importNode(node, true);
7495
+ reconcileCspAttributes(imported, context);
7496
+ container.appendChild(imported);
7497
+ }
7498
+ }
7499
+ }
7500
+
7501
+ const RAW_TEXT_ELEMENTS = new Set(['iframe', 'noembed', 'noframes', 'plaintext', 'script', 'textarea', 'title', 'xmp']);
7502
+ function escapeAttribute(value) {
7503
+ return value.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
7504
+ }
7505
+ function findTagEnd(html, start) {
7506
+ let quote = '';
7507
+ for (let index = start; index < html.length; index += 1) {
7508
+ const character = html[index];
7509
+ if (quote) {
7510
+ if (character === quote)
7511
+ quote = '';
7512
+ continue;
7513
+ }
7514
+ if (character === '"' || character === "'") {
7515
+ quote = character;
7516
+ continue;
7517
+ }
7518
+ if (character === '>')
7519
+ return index + 1;
7520
+ }
7521
+ return html.length;
7522
+ }
7523
+ function findClosingTag(html, lowerHtml, name, start) {
7524
+ const needle = `</${name}`;
7525
+ let position = lowerHtml.indexOf(needle, start);
7526
+ while (position !== -1) {
7527
+ const boundary = lowerHtml[position + needle.length];
7528
+ if (boundary === '>' || /\s/.test(boundary || '')) {
7529
+ return {
7530
+ start: position,
7531
+ end: findTagEnd(html, position + needle.length)
7532
+ };
7533
+ }
7534
+ position = lowerHtml.indexOf(needle, position + needle.length);
7535
+ }
7536
+ return null;
7537
+ }
7538
+ function removeNonceAttribute(openingTag) {
7539
+ const prefix = /^<\s*style\b/i.exec(openingTag);
7540
+ if (!prefix)
7541
+ return openingTag;
7542
+ let output = prefix[0];
7543
+ let cursor = prefix[0].length;
7544
+ const limit = openingTag.length - 1;
7545
+ while (cursor < limit) {
7546
+ const attributeStart = cursor;
7547
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
7548
+ cursor += 1;
7549
+ if (cursor >= limit || openingTag[cursor] === '>') {
7550
+ output += openingTag.slice(attributeStart, cursor);
7551
+ break;
7552
+ }
7553
+ const nameStart = cursor;
7554
+ while (cursor < limit && !/[\s=/>]/.test(openingTag[cursor]))
7555
+ cursor += 1;
7556
+ if (cursor === nameStart) {
7557
+ output += openingTag.slice(attributeStart, cursor + 1);
7558
+ cursor += 1;
7559
+ continue;
7560
+ }
7561
+ const name = openingTag.slice(nameStart, cursor).toLowerCase();
7562
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
7563
+ cursor += 1;
7564
+ if (openingTag[cursor] === '=') {
7565
+ cursor += 1;
7566
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
7567
+ cursor += 1;
7568
+ const quote = openingTag[cursor] === '"' || openingTag[cursor] === "'" ? openingTag[cursor] : '';
7569
+ if (quote) {
7570
+ cursor += 1;
7571
+ while (cursor < limit && openingTag[cursor] !== quote)
7572
+ cursor += 1;
7573
+ if (openingTag[cursor] === quote)
7574
+ cursor += 1;
7575
+ }
7576
+ else {
7577
+ while (cursor < limit && !/[\s>]/.test(openingTag[cursor]))
7578
+ cursor += 1;
7579
+ }
7580
+ }
7581
+ if (name !== 'nonce')
7582
+ output += openingTag.slice(attributeStart, cursor);
7583
+ }
7584
+ return output + openingTag.slice(limit);
7585
+ }
7586
+ function withNonce(openingTag, nonce) {
7587
+ const withoutNonce = removeNonceAttribute(openingTag);
7588
+ return `${withoutNonce.slice(0, -1)} nonce="${escapeAttribute(nonce)}">`;
7589
+ }
7590
+ function prepareNavigationHtml(html) {
7591
+ if (expressionRuntimeMode() !== 'csp')
7592
+ return html;
7593
+ const activeNonce = resolveCspNonce();
7594
+ const lowerHtml = html.toLowerCase();
7595
+ let output = '';
7596
+ let cursor = 0;
7597
+ let skippedStyles = 0;
7598
+ while (cursor < html.length) {
7599
+ const tagStart = html.indexOf('<', cursor);
7600
+ if (tagStart === -1) {
7601
+ output += html.slice(cursor);
7602
+ break;
7603
+ }
7604
+ output += html.slice(cursor, tagStart);
7605
+ if (html.startsWith('<!--', tagStart)) {
7606
+ const commentEnd = html.indexOf('-->', tagStart + 4);
7607
+ const end = commentEnd === -1 ? html.length : commentEnd + 3;
7608
+ output += html.slice(tagStart, end);
7609
+ cursor = end;
7610
+ continue;
7611
+ }
7612
+ const tagEnd = findTagEnd(html, tagStart + 1);
7613
+ const openingTag = html.slice(tagStart, tagEnd);
7614
+ const match = /^<\s*(\/?)\s*([a-z][\w:-]*)/i.exec(openingTag);
7615
+ if (!match || match[1]) {
7616
+ output += openingTag;
7617
+ cursor = tagEnd;
7618
+ continue;
7619
+ }
7620
+ const name = match[2].toLowerCase();
7621
+ if (name === 'plaintext') {
7622
+ output += html.slice(tagStart);
7623
+ break;
7624
+ }
7625
+ if (name === 'style' || name === 'noscript' || RAW_TEXT_ELEMENTS.has(name)) {
7626
+ const closingTag = findClosingTag(html, lowerHtml, name, tagEnd);
7627
+ const blockEnd = closingTag?.end ?? html.length;
7628
+ if (name === 'noscript') {
7629
+ cursor = blockEnd;
7630
+ continue;
7631
+ }
7632
+ if (name === 'style') {
7633
+ if (!activeNonce) {
7634
+ skippedStyles += 1;
7635
+ cursor = blockEnd;
7636
+ continue;
7637
+ }
7638
+ output += withNonce(openingTag, activeNonce);
7639
+ output += html.slice(tagEnd, blockEnd);
7640
+ cursor = blockEnd;
7641
+ continue;
7642
+ }
7643
+ output += html.slice(tagStart, blockEnd);
7644
+ cursor = blockEnd;
7645
+ continue;
7426
7646
  }
7647
+ output += openingTag;
7648
+ cursor = tagEnd;
7649
+ }
7650
+ if (skippedStyles > 0) {
7651
+ console.warn(`[GyosJS CSP] Skipped ${skippedStyles} inline MPA style${skippedStyles === 1 ? '' : 's'} because no active document nonce is configured.`);
7427
7652
  }
7653
+ return output;
7428
7654
  }
7429
7655
 
7430
7656
  /**
@@ -7490,7 +7716,10 @@ function stripInertFallbackContent(root) {
7490
7716
  root.querySelectorAll('noscript').forEach(element => element.remove());
7491
7717
  }
7492
7718
  function parseNavigationDocument(html) {
7493
- const doc = new DOMParser().parseFromString(html, 'text/html');
7719
+ // Chromium applies the active page CSP while DOMParser creates response
7720
+ // styles. Reconcile their nonce before parsing to avoid transient violations;
7721
+ // diffNodes repeats the check before insertion into the live document.
7722
+ const doc = new DOMParser().parseFromString(prepareNavigationHtml(html), 'text/html');
7494
7723
  // DOMParser disables scripting and parses noscript children as active markup.
7495
7724
  // A boosted page runs with scripting enabled, so those fallbacks must stay inert.
7496
7725
  stripInertFallbackContent(doc);
@@ -8326,7 +8555,7 @@ const Gyos = {
8326
8555
  portalCreate,
8327
8556
  portalDestroy,
8328
8557
  setCspNonce,
8329
- version: '0.3.0'
8558
+ version: '0.3.1'
8330
8559
  };
8331
8560
 
8332
8561
  setExpressionRuntime(standardExpressionRuntime);