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