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.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
  */
@@ -2300,32 +2300,24 @@
2300
2300
  * Handles:
2301
2301
  * 1. Literal string: "fade" or "slide-down"
2302
2302
  * 2. Expression with {}: "{transition}"
2303
- * 3. Expression without {}: "transition" (evaluate as variable)
2303
+ * 3. Quoted literal kept for backwards compatibility: "'fade'"
2304
2304
  *
2305
2305
  * @param attrValue - The g-transition attribute value
2306
2306
  * @param scope - Current scope for expression evaluation
2307
2307
  * @returns The transition name (string)
2308
2308
  */
2309
2309
  function parseTransitionName(attrValue, scope) {
2310
- if (attrValue.includes('{') && attrValue.includes('}')) {
2311
- // Extract expression from {} and evaluate it
2312
- const match = attrValue.match(/\{([^}]+)\}/);
2313
- if (match) {
2314
- return evaluateExpression(match[1].trim(), scope);
2315
- }
2316
- }
2317
- // Try to evaluate as expression first (variable name)
2318
- // If it fails or returns undefined, use as literal string
2319
- try {
2320
- const evaluated = evaluateExpression(attrValue, scope);
2321
- if (evaluated !== undefined && evaluated !== null) {
2322
- return evaluated;
2323
- }
2310
+ const value = attrValue.trim();
2311
+ const expression = /^\{([\s\S]+)\}$/.exec(value);
2312
+ if (expression) {
2313
+ const evaluated = evaluateExpression(expression[1].trim(), scope);
2314
+ return evaluated == null ? '' : String(evaluated);
2324
2315
  }
2325
- catch (e) {
2326
- // If evaluation fails, use value as literal string (built-in transition name)
2316
+ if (value.length >= 2 && ((value.startsWith("'") && value.endsWith("'"))
2317
+ || (value.startsWith('"') && value.endsWith('"')))) {
2318
+ return value.slice(1, -1);
2327
2319
  }
2328
- return attrValue;
2320
+ return value;
2329
2321
  }
2330
2322
  /**
2331
2323
  * Parse transition config from element's g-transition attribute
@@ -6794,7 +6786,7 @@
6794
6786
  }
6795
6787
 
6796
6788
  let nonceSource;
6797
- /** Configure the nonce used when Gyos recreates scripts during MPA navigation. */
6789
+ /** Configure the active document nonce used for scripts and styles during MPA navigation. */
6798
6790
  function setCspNonce(source) {
6799
6791
  nonceSource = source;
6800
6792
  }
@@ -7366,9 +7358,33 @@
7366
7358
  window.scrollTo({ top: 0, behavior: 'smooth' });
7367
7359
  }
7368
7360
 
7369
- function serializeAttributes(el) {
7370
- return Array.from(el.attributes)
7371
- .map(attr => `${attr.name}=${attr.value}`)
7361
+ function isNonceMeta(el) {
7362
+ return el.tagName === 'META' && el.getAttribute('name')?.toLowerCase() === 'csp-nonce';
7363
+ }
7364
+ function isStylesheetLink(el) {
7365
+ return el.tagName === 'LINK'
7366
+ && (el.getAttribute('rel') || '').toLowerCase().split(/\s+/).includes('stylesheet');
7367
+ }
7368
+ function usesCspNonce(el) {
7369
+ return el.tagName === 'SCRIPT' || el.tagName === 'STYLE' || isStylesheetLink(el);
7370
+ }
7371
+ function serializeAttributes(el, context) {
7372
+ const attributes = new Map();
7373
+ Array.from(el.attributes).forEach(attr => {
7374
+ if (context.csp && usesCspNonce(el) && attr.name.toLowerCase() === 'nonce')
7375
+ return;
7376
+ if (context.csp && isNonceMeta(el) && attr.name.toLowerCase() === 'content')
7377
+ return;
7378
+ attributes.set(attr.name, attr.value);
7379
+ });
7380
+ if (context.csp && usesCspNonce(el) && context.activeNonce) {
7381
+ attributes.set('nonce', context.activeNonce);
7382
+ }
7383
+ if (context.csp && isNonceMeta(el) && context.activeNonce) {
7384
+ attributes.set('content', context.activeNonce);
7385
+ }
7386
+ return Array.from(attributes)
7387
+ .map(([name, value]) => `${name}=${value}`)
7372
7388
  .sort()
7373
7389
  .join('|');
7374
7390
  }
@@ -7379,23 +7395,61 @@
7379
7395
  }
7380
7396
  return String(hash);
7381
7397
  }
7382
- function elementKey(el) {
7398
+ function elementKey(el, context) {
7383
7399
  const tag = el.tagName.toLowerCase();
7384
7400
  const content = tag === 'script' || tag === 'style' ? hashContent(el.textContent || '') : '';
7385
- return `${tag}|${serializeAttributes(el)}|${content}`;
7401
+ return `${tag}|${serializeAttributes(el, context)}|${content}`;
7386
7402
  }
7387
- function keyedNodes(nodes) {
7403
+ function keyedNodes(nodes, context) {
7388
7404
  const occurrences = new Map();
7389
7405
  return nodes.map(node => {
7390
- const baseKey = elementKey(node);
7406
+ const baseKey = elementKey(node, context);
7391
7407
  const occurrence = occurrences.get(baseKey) || 0;
7392
7408
  occurrences.set(baseKey, occurrence + 1);
7393
7409
  return { key: `${baseKey}|${occurrence}`, node };
7394
7410
  });
7395
7411
  }
7412
+ function canInsertNode(node, context) {
7413
+ if (!context.csp)
7414
+ return true;
7415
+ if (isNonceMeta(node))
7416
+ return Boolean(context.activeNonce);
7417
+ if (node.tagName === 'STYLE')
7418
+ return Boolean(context.activeNonce);
7419
+ if (node.tagName === 'SCRIPT' && !node.hasAttribute('src'))
7420
+ return Boolean(context.activeNonce);
7421
+ return true;
7422
+ }
7423
+ function reconcileCspAttributes(node, context) {
7424
+ if (!context.csp)
7425
+ return;
7426
+ if (isNonceMeta(node)) {
7427
+ if (context.activeNonce)
7428
+ node.setAttribute('content', context.activeNonce);
7429
+ return;
7430
+ }
7431
+ if (!usesCspNonce(node))
7432
+ return;
7433
+ node.removeAttribute('nonce');
7434
+ if (context.activeNonce)
7435
+ node.setAttribute('nonce', context.activeNonce);
7436
+ }
7437
+ function warnSkippedNode(node) {
7438
+ if (node.tagName === 'STYLE') {
7439
+ console.warn('[GyosJS CSP] Skipped an inline MPA style because no active document nonce is configured.');
7440
+ }
7441
+ else if (node.tagName === 'SCRIPT' && !node.hasAttribute('src')) {
7442
+ console.warn('[GyosJS CSP] Skipped an inline MPA script because no active document nonce is configured.');
7443
+ }
7444
+ }
7396
7445
  async function diffNodes(currentNodes, nextNodes, container = document.head, signal) {
7397
- const current = keyedNodes(currentNodes);
7398
- const next = keyedNodes(nextNodes);
7446
+ const csp = expressionRuntimeMode() === 'csp';
7447
+ const context = {
7448
+ csp,
7449
+ activeNonce: csp ? resolveCspNonce() : undefined
7450
+ };
7451
+ const current = keyedNodes(currentNodes, context);
7452
+ const next = keyedNodes(nextNodes, context);
7399
7453
  const currentKeys = new Set(current.map(item => item.key));
7400
7454
  const nextKeys = new Set(next.map(item => item.key));
7401
7455
  // Remove nodes that are not in the new head
@@ -7409,6 +7463,20 @@
7409
7463
  if (signal?.aborted)
7410
7464
  return;
7411
7465
  if (!currentKeys.has(key)) {
7466
+ if (!canInsertNode(node, context)) {
7467
+ warnSkippedNode(node);
7468
+ continue;
7469
+ }
7470
+ if (node.tagName === 'STYLE') {
7471
+ const style = document.createElement('style');
7472
+ Array.from(node.attributes).forEach(attr => style.setAttribute(attr.name, attr.value));
7473
+ reconcileCspAttributes(style, context);
7474
+ // CSP checks inline styles as their CSS text is attached. Set the active
7475
+ // nonce first so a response nonce never produces a transient violation.
7476
+ style.textContent = node.textContent || '';
7477
+ container.appendChild(style);
7478
+ continue;
7479
+ }
7412
7480
  if (node.tagName === 'SCRIPT') {
7413
7481
  const originalScript = node;
7414
7482
  const executionKey = getScriptExecutionKey(originalScript);
@@ -7419,6 +7487,7 @@
7419
7487
  }
7420
7488
  const script = document.createElement('script');
7421
7489
  Array.from(node.attributes).forEach(attr => script.setAttribute(attr.name, attr.value));
7490
+ reconcileCspAttributes(script, context);
7422
7491
  script.textContent = node.hasAttribute('g-script-wrap')
7423
7492
  ? wrapScriptExecution(node.textContent || '')
7424
7493
  : node.textContent || '';
@@ -7428,9 +7497,166 @@
7428
7497
  await completion;
7429
7498
  continue;
7430
7499
  }
7431
- container.appendChild(document.importNode(node, true));
7500
+ const imported = document.importNode(node, true);
7501
+ reconcileCspAttributes(imported, context);
7502
+ container.appendChild(imported);
7503
+ }
7504
+ }
7505
+ }
7506
+
7507
+ const RAW_TEXT_ELEMENTS = new Set(['iframe', 'noembed', 'noframes', 'plaintext', 'script', 'textarea', 'title', 'xmp']);
7508
+ function escapeAttribute(value) {
7509
+ return value.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
7510
+ }
7511
+ function findTagEnd(html, start) {
7512
+ let quote = '';
7513
+ for (let index = start; index < html.length; index += 1) {
7514
+ const character = html[index];
7515
+ if (quote) {
7516
+ if (character === quote)
7517
+ quote = '';
7518
+ continue;
7519
+ }
7520
+ if (character === '"' || character === "'") {
7521
+ quote = character;
7522
+ continue;
7523
+ }
7524
+ if (character === '>')
7525
+ return index + 1;
7526
+ }
7527
+ return html.length;
7528
+ }
7529
+ function findClosingTag(html, lowerHtml, name, start) {
7530
+ const needle = `</${name}`;
7531
+ let position = lowerHtml.indexOf(needle, start);
7532
+ while (position !== -1) {
7533
+ const boundary = lowerHtml[position + needle.length];
7534
+ if (boundary === '>' || /\s/.test(boundary || '')) {
7535
+ return {
7536
+ start: position,
7537
+ end: findTagEnd(html, position + needle.length)
7538
+ };
7539
+ }
7540
+ position = lowerHtml.indexOf(needle, position + needle.length);
7541
+ }
7542
+ return null;
7543
+ }
7544
+ function removeNonceAttribute(openingTag) {
7545
+ const prefix = /^<\s*style\b/i.exec(openingTag);
7546
+ if (!prefix)
7547
+ return openingTag;
7548
+ let output = prefix[0];
7549
+ let cursor = prefix[0].length;
7550
+ const limit = openingTag.length - 1;
7551
+ while (cursor < limit) {
7552
+ const attributeStart = cursor;
7553
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
7554
+ cursor += 1;
7555
+ if (cursor >= limit || openingTag[cursor] === '>') {
7556
+ output += openingTag.slice(attributeStart, cursor);
7557
+ break;
7558
+ }
7559
+ const nameStart = cursor;
7560
+ while (cursor < limit && !/[\s=/>]/.test(openingTag[cursor]))
7561
+ cursor += 1;
7562
+ if (cursor === nameStart) {
7563
+ output += openingTag.slice(attributeStart, cursor + 1);
7564
+ cursor += 1;
7565
+ continue;
7566
+ }
7567
+ const name = openingTag.slice(nameStart, cursor).toLowerCase();
7568
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
7569
+ cursor += 1;
7570
+ if (openingTag[cursor] === '=') {
7571
+ cursor += 1;
7572
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
7573
+ cursor += 1;
7574
+ const quote = openingTag[cursor] === '"' || openingTag[cursor] === "'" ? openingTag[cursor] : '';
7575
+ if (quote) {
7576
+ cursor += 1;
7577
+ while (cursor < limit && openingTag[cursor] !== quote)
7578
+ cursor += 1;
7579
+ if (openingTag[cursor] === quote)
7580
+ cursor += 1;
7581
+ }
7582
+ else {
7583
+ while (cursor < limit && !/[\s>]/.test(openingTag[cursor]))
7584
+ cursor += 1;
7585
+ }
7586
+ }
7587
+ if (name !== 'nonce')
7588
+ output += openingTag.slice(attributeStart, cursor);
7589
+ }
7590
+ return output + openingTag.slice(limit);
7591
+ }
7592
+ function withNonce(openingTag, nonce) {
7593
+ const withoutNonce = removeNonceAttribute(openingTag);
7594
+ return `${withoutNonce.slice(0, -1)} nonce="${escapeAttribute(nonce)}">`;
7595
+ }
7596
+ function prepareNavigationHtml(html) {
7597
+ if (expressionRuntimeMode() !== 'csp')
7598
+ return html;
7599
+ const activeNonce = resolveCspNonce();
7600
+ const lowerHtml = html.toLowerCase();
7601
+ let output = '';
7602
+ let cursor = 0;
7603
+ let skippedStyles = 0;
7604
+ while (cursor < html.length) {
7605
+ const tagStart = html.indexOf('<', cursor);
7606
+ if (tagStart === -1) {
7607
+ output += html.slice(cursor);
7608
+ break;
7609
+ }
7610
+ output += html.slice(cursor, tagStart);
7611
+ if (html.startsWith('<!--', tagStart)) {
7612
+ const commentEnd = html.indexOf('-->', tagStart + 4);
7613
+ const end = commentEnd === -1 ? html.length : commentEnd + 3;
7614
+ output += html.slice(tagStart, end);
7615
+ cursor = end;
7616
+ continue;
7617
+ }
7618
+ const tagEnd = findTagEnd(html, tagStart + 1);
7619
+ const openingTag = html.slice(tagStart, tagEnd);
7620
+ const match = /^<\s*(\/?)\s*([a-z][\w:-]*)/i.exec(openingTag);
7621
+ if (!match || match[1]) {
7622
+ output += openingTag;
7623
+ cursor = tagEnd;
7624
+ continue;
7625
+ }
7626
+ const name = match[2].toLowerCase();
7627
+ if (name === 'plaintext') {
7628
+ output += html.slice(tagStart);
7629
+ break;
7630
+ }
7631
+ if (name === 'style' || name === 'noscript' || RAW_TEXT_ELEMENTS.has(name)) {
7632
+ const closingTag = findClosingTag(html, lowerHtml, name, tagEnd);
7633
+ const blockEnd = closingTag?.end ?? html.length;
7634
+ if (name === 'noscript') {
7635
+ cursor = blockEnd;
7636
+ continue;
7637
+ }
7638
+ if (name === 'style') {
7639
+ if (!activeNonce) {
7640
+ skippedStyles += 1;
7641
+ cursor = blockEnd;
7642
+ continue;
7643
+ }
7644
+ output += withNonce(openingTag, activeNonce);
7645
+ output += html.slice(tagEnd, blockEnd);
7646
+ cursor = blockEnd;
7647
+ continue;
7648
+ }
7649
+ output += html.slice(tagStart, blockEnd);
7650
+ cursor = blockEnd;
7651
+ continue;
7432
7652
  }
7653
+ output += openingTag;
7654
+ cursor = tagEnd;
7655
+ }
7656
+ if (skippedStyles > 0) {
7657
+ console.warn(`[GyosJS CSP] Skipped ${skippedStyles} inline MPA style${skippedStyles === 1 ? '' : 's'} because no active document nonce is configured.`);
7433
7658
  }
7659
+ return output;
7434
7660
  }
7435
7661
 
7436
7662
  /**
@@ -7496,7 +7722,10 @@
7496
7722
  root.querySelectorAll('noscript').forEach(element => element.remove());
7497
7723
  }
7498
7724
  function parseNavigationDocument(html) {
7499
- const doc = new DOMParser().parseFromString(html, 'text/html');
7725
+ // Chromium applies the active page CSP while DOMParser creates response
7726
+ // styles. Reconcile their nonce before parsing to avoid transient violations;
7727
+ // diffNodes repeats the check before insertion into the live document.
7728
+ const doc = new DOMParser().parseFromString(prepareNavigationHtml(html), 'text/html');
7500
7729
  // DOMParser disables scripting and parses noscript children as active markup.
7501
7730
  // A boosted page runs with scripting enabled, so those fallbacks must stay inert.
7502
7731
  stripInertFallbackContent(doc);
@@ -8332,7 +8561,7 @@
8332
8561
  portalCreate,
8333
8562
  portalDestroy,
8334
8563
  setCspNonce,
8335
- version: '0.3.0'
8564
+ version: '0.3.1'
8336
8565
  };
8337
8566
 
8338
8567
  setExpressionRuntime(standardExpressionRuntime);