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.
@@ -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
  */
@@ -2262,32 +2262,24 @@ function applyTransitionStyles() {
2262
2262
  * Handles:
2263
2263
  * 1. Literal string: "fade" or "slide-down"
2264
2264
  * 2. Expression with {}: "{transition}"
2265
- * 3. Expression without {}: "transition" (evaluate as variable)
2265
+ * 3. Quoted literal kept for backwards compatibility: "'fade'"
2266
2266
  *
2267
2267
  * @param attrValue - The g-transition attribute value
2268
2268
  * @param scope - Current scope for expression evaluation
2269
2269
  * @returns The transition name (string)
2270
2270
  */
2271
2271
  function parseTransitionName(attrValue, scope) {
2272
- if (attrValue.includes('{') && attrValue.includes('}')) {
2273
- // Extract expression from {} and evaluate it
2274
- const match = attrValue.match(/\{([^}]+)\}/);
2275
- if (match) {
2276
- return evaluateExpression(match[1].trim(), scope);
2277
- }
2278
- }
2279
- // Try to evaluate as expression first (variable name)
2280
- // If it fails or returns undefined, use as literal string
2281
- try {
2282
- const evaluated = evaluateExpression(attrValue, scope);
2283
- if (evaluated !== undefined && evaluated !== null) {
2284
- return evaluated;
2285
- }
2272
+ const value = attrValue.trim();
2273
+ const expression = /^\{([\s\S]+)\}$/.exec(value);
2274
+ if (expression) {
2275
+ const evaluated = evaluateExpression(expression[1].trim(), scope);
2276
+ return evaluated == null ? '' : String(evaluated);
2286
2277
  }
2287
- catch (e) {
2288
- // If evaluation fails, use value as literal string (built-in transition name)
2278
+ if (value.length >= 2 && ((value.startsWith("'") && value.endsWith("'"))
2279
+ || (value.startsWith('"') && value.endsWith('"')))) {
2280
+ return value.slice(1, -1);
2289
2281
  }
2290
- return attrValue;
2282
+ return value;
2291
2283
  }
2292
2284
  /**
2293
2285
  * Parse transition config from element's g-transition attribute
@@ -6756,7 +6748,7 @@ function hideTargetSpinner(spinner) {
6756
6748
  }
6757
6749
 
6758
6750
  let nonceSource;
6759
- /** Configure the nonce used when Gyos recreates scripts during MPA navigation. */
6751
+ /** Configure the active document nonce used for scripts and styles during MPA navigation. */
6760
6752
  function setCspNonce(source) {
6761
6753
  nonceSource = source;
6762
6754
  }
@@ -7342,9 +7334,33 @@ function handleScroll(url, trigger, savedScroll) {
7342
7334
  window.scrollTo({ top: 0, behavior: 'smooth' });
7343
7335
  }
7344
7336
 
7345
- function serializeAttributes(el) {
7346
- return Array.from(el.attributes)
7347
- .map(attr => `${attr.name}=${attr.value}`)
7337
+ function isNonceMeta(el) {
7338
+ return el.tagName === 'META' && el.getAttribute('name')?.toLowerCase() === 'csp-nonce';
7339
+ }
7340
+ function isStylesheetLink(el) {
7341
+ return el.tagName === 'LINK'
7342
+ && (el.getAttribute('rel') || '').toLowerCase().split(/\s+/).includes('stylesheet');
7343
+ }
7344
+ function usesCspNonce(el) {
7345
+ return el.tagName === 'SCRIPT' || el.tagName === 'STYLE' || isStylesheetLink(el);
7346
+ }
7347
+ function serializeAttributes(el, context) {
7348
+ const attributes = new Map();
7349
+ Array.from(el.attributes).forEach(attr => {
7350
+ if (context.csp && usesCspNonce(el) && attr.name.toLowerCase() === 'nonce')
7351
+ return;
7352
+ if (context.csp && isNonceMeta(el) && attr.name.toLowerCase() === 'content')
7353
+ return;
7354
+ attributes.set(attr.name, attr.value);
7355
+ });
7356
+ if (context.csp && usesCspNonce(el) && context.activeNonce) {
7357
+ attributes.set('nonce', context.activeNonce);
7358
+ }
7359
+ if (context.csp && isNonceMeta(el) && context.activeNonce) {
7360
+ attributes.set('content', context.activeNonce);
7361
+ }
7362
+ return Array.from(attributes)
7363
+ .map(([name, value]) => `${name}=${value}`)
7348
7364
  .sort()
7349
7365
  .join('|');
7350
7366
  }
@@ -7355,23 +7371,61 @@ function hashContent(content) {
7355
7371
  }
7356
7372
  return String(hash);
7357
7373
  }
7358
- function elementKey(el) {
7374
+ function elementKey(el, context) {
7359
7375
  const tag = el.tagName.toLowerCase();
7360
7376
  const content = tag === 'script' || tag === 'style' ? hashContent(el.textContent || '') : '';
7361
- return `${tag}|${serializeAttributes(el)}|${content}`;
7377
+ return `${tag}|${serializeAttributes(el, context)}|${content}`;
7362
7378
  }
7363
- function keyedNodes(nodes) {
7379
+ function keyedNodes(nodes, context) {
7364
7380
  const occurrences = new Map();
7365
7381
  return nodes.map(node => {
7366
- const baseKey = elementKey(node);
7382
+ const baseKey = elementKey(node, context);
7367
7383
  const occurrence = occurrences.get(baseKey) || 0;
7368
7384
  occurrences.set(baseKey, occurrence + 1);
7369
7385
  return { key: `${baseKey}|${occurrence}`, node };
7370
7386
  });
7371
7387
  }
7388
+ function canInsertNode(node, context) {
7389
+ if (!context.csp)
7390
+ return true;
7391
+ if (isNonceMeta(node))
7392
+ return Boolean(context.activeNonce);
7393
+ if (node.tagName === 'STYLE')
7394
+ return Boolean(context.activeNonce);
7395
+ if (node.tagName === 'SCRIPT' && !node.hasAttribute('src'))
7396
+ return Boolean(context.activeNonce);
7397
+ return true;
7398
+ }
7399
+ function reconcileCspAttributes(node, context) {
7400
+ if (!context.csp)
7401
+ return;
7402
+ if (isNonceMeta(node)) {
7403
+ if (context.activeNonce)
7404
+ node.setAttribute('content', context.activeNonce);
7405
+ return;
7406
+ }
7407
+ if (!usesCspNonce(node))
7408
+ return;
7409
+ node.removeAttribute('nonce');
7410
+ if (context.activeNonce)
7411
+ node.setAttribute('nonce', context.activeNonce);
7412
+ }
7413
+ function warnSkippedNode(node) {
7414
+ if (node.tagName === 'STYLE') {
7415
+ console.warn('[GyosJS CSP] Skipped an inline MPA style because no active document nonce is configured.');
7416
+ }
7417
+ else if (node.tagName === 'SCRIPT' && !node.hasAttribute('src')) {
7418
+ console.warn('[GyosJS CSP] Skipped an inline MPA script because no active document nonce is configured.');
7419
+ }
7420
+ }
7372
7421
  async function diffNodes(currentNodes, nextNodes, container = document.head, signal) {
7373
- const current = keyedNodes(currentNodes);
7374
- const next = keyedNodes(nextNodes);
7422
+ const csp = expressionRuntimeMode() === 'csp';
7423
+ const context = {
7424
+ csp,
7425
+ activeNonce: csp ? resolveCspNonce() : undefined
7426
+ };
7427
+ const current = keyedNodes(currentNodes, context);
7428
+ const next = keyedNodes(nextNodes, context);
7375
7429
  const currentKeys = new Set(current.map(item => item.key));
7376
7430
  const nextKeys = new Set(next.map(item => item.key));
7377
7431
  // Remove nodes that are not in the new head
@@ -7385,6 +7439,20 @@ async function diffNodes(currentNodes, nextNodes, container = document.head, sig
7385
7439
  if (signal?.aborted)
7386
7440
  return;
7387
7441
  if (!currentKeys.has(key)) {
7442
+ if (!canInsertNode(node, context)) {
7443
+ warnSkippedNode(node);
7444
+ continue;
7445
+ }
7446
+ if (node.tagName === 'STYLE') {
7447
+ const style = document.createElement('style');
7448
+ Array.from(node.attributes).forEach(attr => style.setAttribute(attr.name, attr.value));
7449
+ reconcileCspAttributes(style, context);
7450
+ // CSP checks inline styles as their CSS text is attached. Set the active
7451
+ // nonce first so a response nonce never produces a transient violation.
7452
+ style.textContent = node.textContent || '';
7453
+ container.appendChild(style);
7454
+ continue;
7455
+ }
7388
7456
  if (node.tagName === 'SCRIPT') {
7389
7457
  const originalScript = node;
7390
7458
  const executionKey = getScriptExecutionKey(originalScript);
@@ -7395,6 +7463,7 @@ async function diffNodes(currentNodes, nextNodes, container = document.head, sig
7395
7463
  }
7396
7464
  const script = document.createElement('script');
7397
7465
  Array.from(node.attributes).forEach(attr => script.setAttribute(attr.name, attr.value));
7466
+ reconcileCspAttributes(script, context);
7398
7467
  script.textContent = node.hasAttribute('g-script-wrap')
7399
7468
  ? wrapScriptExecution(node.textContent || '')
7400
7469
  : node.textContent || '';
@@ -7404,11 +7473,168 @@ async function diffNodes(currentNodes, nextNodes, container = document.head, sig
7404
7473
  await completion;
7405
7474
  continue;
7406
7475
  }
7407
- container.appendChild(document.importNode(node, true));
7476
+ const imported = document.importNode(node, true);
7477
+ reconcileCspAttributes(imported, context);
7478
+ container.appendChild(imported);
7408
7479
  }
7409
7480
  }
7410
7481
  }
7411
7482
 
7483
+ const RAW_TEXT_ELEMENTS = new Set(['iframe', 'noembed', 'noframes', 'plaintext', 'script', 'textarea', 'title', 'xmp']);
7484
+ function escapeAttribute(value) {
7485
+ return value.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
7486
+ }
7487
+ function findTagEnd(html, start) {
7488
+ let quote = '';
7489
+ for (let index = start; index < html.length; index += 1) {
7490
+ const character = html[index];
7491
+ if (quote) {
7492
+ if (character === quote)
7493
+ quote = '';
7494
+ continue;
7495
+ }
7496
+ if (character === '"' || character === "'") {
7497
+ quote = character;
7498
+ continue;
7499
+ }
7500
+ if (character === '>')
7501
+ return index + 1;
7502
+ }
7503
+ return html.length;
7504
+ }
7505
+ function findClosingTag(html, lowerHtml, name, start) {
7506
+ const needle = `</${name}`;
7507
+ let position = lowerHtml.indexOf(needle, start);
7508
+ while (position !== -1) {
7509
+ const boundary = lowerHtml[position + needle.length];
7510
+ if (boundary === '>' || /\s/.test(boundary || '')) {
7511
+ return {
7512
+ start: position,
7513
+ end: findTagEnd(html, position + needle.length)
7514
+ };
7515
+ }
7516
+ position = lowerHtml.indexOf(needle, position + needle.length);
7517
+ }
7518
+ return null;
7519
+ }
7520
+ function removeNonceAttribute(openingTag) {
7521
+ const prefix = /^<\s*style\b/i.exec(openingTag);
7522
+ if (!prefix)
7523
+ return openingTag;
7524
+ let output = prefix[0];
7525
+ let cursor = prefix[0].length;
7526
+ const limit = openingTag.length - 1;
7527
+ while (cursor < limit) {
7528
+ const attributeStart = cursor;
7529
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
7530
+ cursor += 1;
7531
+ if (cursor >= limit || openingTag[cursor] === '>') {
7532
+ output += openingTag.slice(attributeStart, cursor);
7533
+ break;
7534
+ }
7535
+ const nameStart = cursor;
7536
+ while (cursor < limit && !/[\s=/>]/.test(openingTag[cursor]))
7537
+ cursor += 1;
7538
+ if (cursor === nameStart) {
7539
+ output += openingTag.slice(attributeStart, cursor + 1);
7540
+ cursor += 1;
7541
+ continue;
7542
+ }
7543
+ const name = openingTag.slice(nameStart, cursor).toLowerCase();
7544
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
7545
+ cursor += 1;
7546
+ if (openingTag[cursor] === '=') {
7547
+ cursor += 1;
7548
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
7549
+ cursor += 1;
7550
+ const quote = openingTag[cursor] === '"' || openingTag[cursor] === "'" ? openingTag[cursor] : '';
7551
+ if (quote) {
7552
+ cursor += 1;
7553
+ while (cursor < limit && openingTag[cursor] !== quote)
7554
+ cursor += 1;
7555
+ if (openingTag[cursor] === quote)
7556
+ cursor += 1;
7557
+ }
7558
+ else {
7559
+ while (cursor < limit && !/[\s>]/.test(openingTag[cursor]))
7560
+ cursor += 1;
7561
+ }
7562
+ }
7563
+ if (name !== 'nonce')
7564
+ output += openingTag.slice(attributeStart, cursor);
7565
+ }
7566
+ return output + openingTag.slice(limit);
7567
+ }
7568
+ function withNonce(openingTag, nonce) {
7569
+ const withoutNonce = removeNonceAttribute(openingTag);
7570
+ return `${withoutNonce.slice(0, -1)} nonce="${escapeAttribute(nonce)}">`;
7571
+ }
7572
+ function prepareNavigationHtml(html) {
7573
+ if (expressionRuntimeMode() !== 'csp')
7574
+ return html;
7575
+ const activeNonce = resolveCspNonce();
7576
+ const lowerHtml = html.toLowerCase();
7577
+ let output = '';
7578
+ let cursor = 0;
7579
+ let skippedStyles = 0;
7580
+ while (cursor < html.length) {
7581
+ const tagStart = html.indexOf('<', cursor);
7582
+ if (tagStart === -1) {
7583
+ output += html.slice(cursor);
7584
+ break;
7585
+ }
7586
+ output += html.slice(cursor, tagStart);
7587
+ if (html.startsWith('<!--', tagStart)) {
7588
+ const commentEnd = html.indexOf('-->', tagStart + 4);
7589
+ const end = commentEnd === -1 ? html.length : commentEnd + 3;
7590
+ output += html.slice(tagStart, end);
7591
+ cursor = end;
7592
+ continue;
7593
+ }
7594
+ const tagEnd = findTagEnd(html, tagStart + 1);
7595
+ const openingTag = html.slice(tagStart, tagEnd);
7596
+ const match = /^<\s*(\/?)\s*([a-z][\w:-]*)/i.exec(openingTag);
7597
+ if (!match || match[1]) {
7598
+ output += openingTag;
7599
+ cursor = tagEnd;
7600
+ continue;
7601
+ }
7602
+ const name = match[2].toLowerCase();
7603
+ if (name === 'plaintext') {
7604
+ output += html.slice(tagStart);
7605
+ break;
7606
+ }
7607
+ if (name === 'style' || name === 'noscript' || RAW_TEXT_ELEMENTS.has(name)) {
7608
+ const closingTag = findClosingTag(html, lowerHtml, name, tagEnd);
7609
+ const blockEnd = closingTag?.end ?? html.length;
7610
+ if (name === 'noscript') {
7611
+ cursor = blockEnd;
7612
+ continue;
7613
+ }
7614
+ if (name === 'style') {
7615
+ if (!activeNonce) {
7616
+ skippedStyles += 1;
7617
+ cursor = blockEnd;
7618
+ continue;
7619
+ }
7620
+ output += withNonce(openingTag, activeNonce);
7621
+ output += html.slice(tagEnd, blockEnd);
7622
+ cursor = blockEnd;
7623
+ continue;
7624
+ }
7625
+ output += html.slice(tagStart, blockEnd);
7626
+ cursor = blockEnd;
7627
+ continue;
7628
+ }
7629
+ output += openingTag;
7630
+ cursor = tagEnd;
7631
+ }
7632
+ if (skippedStyles > 0) {
7633
+ console.warn(`[GyosJS CSP] Skipped ${skippedStyles} inline MPA style${skippedStyles === 1 ? '' : 's'} because no active document nonce is configured.`);
7634
+ }
7635
+ return output;
7636
+ }
7637
+
7412
7638
  /**
7413
7639
  * GyosJs Router v0
7414
7640
  * MPA boost navigation inspired by Turbo Drive.
@@ -7472,7 +7698,10 @@ function stripInertFallbackContent(root) {
7472
7698
  root.querySelectorAll('noscript').forEach(element => element.remove());
7473
7699
  }
7474
7700
  function parseNavigationDocument(html) {
7475
- const doc = new DOMParser().parseFromString(html, 'text/html');
7701
+ // Chromium applies the active page CSP while DOMParser creates response
7702
+ // styles. Reconcile their nonce before parsing to avoid transient violations;
7703
+ // diffNodes repeats the check before insertion into the live document.
7704
+ const doc = new DOMParser().parseFromString(prepareNavigationHtml(html), 'text/html');
7476
7705
  // DOMParser disables scripting and parses noscript children as active markup.
7477
7706
  // A boosted page runs with scripting enabled, so those fallbacks must stay inert.
7478
7707
  stripInertFallbackContent(doc);
@@ -8308,7 +8537,7 @@ const Gyos = {
8308
8537
  portalCreate,
8309
8538
  portalDestroy,
8310
8539
  setCspNonce,
8311
- version: '0.3.0'
8540
+ version: '0.3.1'
8312
8541
  };
8313
8542
 
8314
8543
  // This file was generated. Do not modify manually!