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.auto.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
  */
@@ -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
  }
@@ -7334,9 +7326,33 @@
7334
7326
  window.scrollTo({ top: 0, behavior: 'smooth' });
7335
7327
  }
7336
7328
 
7337
- function serializeAttributes(el) {
7338
- return Array.from(el.attributes)
7339
- .map(attr => `${attr.name}=${attr.value}`)
7329
+ function isNonceMeta(el) {
7330
+ return el.tagName === 'META' && el.getAttribute('name')?.toLowerCase() === 'csp-nonce';
7331
+ }
7332
+ function isStylesheetLink(el) {
7333
+ return el.tagName === 'LINK'
7334
+ && (el.getAttribute('rel') || '').toLowerCase().split(/\s+/).includes('stylesheet');
7335
+ }
7336
+ function usesCspNonce(el) {
7337
+ return el.tagName === 'SCRIPT' || el.tagName === 'STYLE' || isStylesheetLink(el);
7338
+ }
7339
+ function serializeAttributes(el, context) {
7340
+ const attributes = new Map();
7341
+ Array.from(el.attributes).forEach(attr => {
7342
+ if (context.csp && usesCspNonce(el) && attr.name.toLowerCase() === 'nonce')
7343
+ return;
7344
+ if (context.csp && isNonceMeta(el) && attr.name.toLowerCase() === 'content')
7345
+ return;
7346
+ attributes.set(attr.name, attr.value);
7347
+ });
7348
+ if (context.csp && usesCspNonce(el) && context.activeNonce) {
7349
+ attributes.set('nonce', context.activeNonce);
7350
+ }
7351
+ if (context.csp && isNonceMeta(el) && context.activeNonce) {
7352
+ attributes.set('content', context.activeNonce);
7353
+ }
7354
+ return Array.from(attributes)
7355
+ .map(([name, value]) => `${name}=${value}`)
7340
7356
  .sort()
7341
7357
  .join('|');
7342
7358
  }
@@ -7347,23 +7363,61 @@
7347
7363
  }
7348
7364
  return String(hash);
7349
7365
  }
7350
- function elementKey(el) {
7366
+ function elementKey(el, context) {
7351
7367
  const tag = el.tagName.toLowerCase();
7352
7368
  const content = tag === 'script' || tag === 'style' ? hashContent(el.textContent || '') : '';
7353
- return `${tag}|${serializeAttributes(el)}|${content}`;
7369
+ return `${tag}|${serializeAttributes(el, context)}|${content}`;
7354
7370
  }
7355
- function keyedNodes(nodes) {
7371
+ function keyedNodes(nodes, context) {
7356
7372
  const occurrences = new Map();
7357
7373
  return nodes.map(node => {
7358
- const baseKey = elementKey(node);
7374
+ const baseKey = elementKey(node, context);
7359
7375
  const occurrence = occurrences.get(baseKey) || 0;
7360
7376
  occurrences.set(baseKey, occurrence + 1);
7361
7377
  return { key: `${baseKey}|${occurrence}`, node };
7362
7378
  });
7363
7379
  }
7380
+ function canInsertNode(node, context) {
7381
+ if (!context.csp)
7382
+ return true;
7383
+ if (isNonceMeta(node))
7384
+ return Boolean(context.activeNonce);
7385
+ if (node.tagName === 'STYLE')
7386
+ return Boolean(context.activeNonce);
7387
+ if (node.tagName === 'SCRIPT' && !node.hasAttribute('src'))
7388
+ return Boolean(context.activeNonce);
7389
+ return true;
7390
+ }
7391
+ function reconcileCspAttributes(node, context) {
7392
+ if (!context.csp)
7393
+ return;
7394
+ if (isNonceMeta(node)) {
7395
+ if (context.activeNonce)
7396
+ node.setAttribute('content', context.activeNonce);
7397
+ return;
7398
+ }
7399
+ if (!usesCspNonce(node))
7400
+ return;
7401
+ node.removeAttribute('nonce');
7402
+ if (context.activeNonce)
7403
+ node.setAttribute('nonce', context.activeNonce);
7404
+ }
7405
+ function warnSkippedNode(node) {
7406
+ if (node.tagName === 'STYLE') {
7407
+ console.warn('[GyosJS CSP] Skipped an inline MPA style because no active document nonce is configured.');
7408
+ }
7409
+ else if (node.tagName === 'SCRIPT' && !node.hasAttribute('src')) {
7410
+ console.warn('[GyosJS CSP] Skipped an inline MPA script because no active document nonce is configured.');
7411
+ }
7412
+ }
7364
7413
  async function diffNodes(currentNodes, nextNodes, container = document.head, signal) {
7365
- const current = keyedNodes(currentNodes);
7366
- const next = keyedNodes(nextNodes);
7414
+ const csp = expressionRuntimeMode() === 'csp';
7415
+ const context = {
7416
+ csp,
7417
+ activeNonce: csp ? resolveCspNonce() : undefined
7418
+ };
7419
+ const current = keyedNodes(currentNodes, context);
7420
+ const next = keyedNodes(nextNodes, context);
7367
7421
  const currentKeys = new Set(current.map(item => item.key));
7368
7422
  const nextKeys = new Set(next.map(item => item.key));
7369
7423
  // Remove nodes that are not in the new head
@@ -7377,6 +7431,20 @@
7377
7431
  if (signal?.aborted)
7378
7432
  return;
7379
7433
  if (!currentKeys.has(key)) {
7434
+ if (!canInsertNode(node, context)) {
7435
+ warnSkippedNode(node);
7436
+ continue;
7437
+ }
7438
+ if (node.tagName === 'STYLE') {
7439
+ const style = document.createElement('style');
7440
+ Array.from(node.attributes).forEach(attr => style.setAttribute(attr.name, attr.value));
7441
+ reconcileCspAttributes(style, context);
7442
+ // CSP checks inline styles as their CSS text is attached. Set the active
7443
+ // nonce first so a response nonce never produces a transient violation.
7444
+ style.textContent = node.textContent || '';
7445
+ container.appendChild(style);
7446
+ continue;
7447
+ }
7380
7448
  if (node.tagName === 'SCRIPT') {
7381
7449
  const originalScript = node;
7382
7450
  const executionKey = getScriptExecutionKey(originalScript);
@@ -7387,6 +7455,7 @@
7387
7455
  }
7388
7456
  const script = document.createElement('script');
7389
7457
  Array.from(node.attributes).forEach(attr => script.setAttribute(attr.name, attr.value));
7458
+ reconcileCspAttributes(script, context);
7390
7459
  script.textContent = node.hasAttribute('g-script-wrap')
7391
7460
  ? wrapScriptExecution(node.textContent || '')
7392
7461
  : node.textContent || '';
@@ -7396,9 +7465,166 @@
7396
7465
  await completion;
7397
7466
  continue;
7398
7467
  }
7399
- container.appendChild(document.importNode(node, true));
7468
+ const imported = document.importNode(node, true);
7469
+ reconcileCspAttributes(imported, context);
7470
+ container.appendChild(imported);
7471
+ }
7472
+ }
7473
+ }
7474
+
7475
+ const RAW_TEXT_ELEMENTS = new Set(['iframe', 'noembed', 'noframes', 'plaintext', 'script', 'textarea', 'title', 'xmp']);
7476
+ function escapeAttribute(value) {
7477
+ return value.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
7478
+ }
7479
+ function findTagEnd(html, start) {
7480
+ let quote = '';
7481
+ for (let index = start; index < html.length; index += 1) {
7482
+ const character = html[index];
7483
+ if (quote) {
7484
+ if (character === quote)
7485
+ quote = '';
7486
+ continue;
7487
+ }
7488
+ if (character === '"' || character === "'") {
7489
+ quote = character;
7490
+ continue;
7491
+ }
7492
+ if (character === '>')
7493
+ return index + 1;
7494
+ }
7495
+ return html.length;
7496
+ }
7497
+ function findClosingTag(html, lowerHtml, name, start) {
7498
+ const needle = `</${name}`;
7499
+ let position = lowerHtml.indexOf(needle, start);
7500
+ while (position !== -1) {
7501
+ const boundary = lowerHtml[position + needle.length];
7502
+ if (boundary === '>' || /\s/.test(boundary || '')) {
7503
+ return {
7504
+ start: position,
7505
+ end: findTagEnd(html, position + needle.length)
7506
+ };
7507
+ }
7508
+ position = lowerHtml.indexOf(needle, position + needle.length);
7509
+ }
7510
+ return null;
7511
+ }
7512
+ function removeNonceAttribute(openingTag) {
7513
+ const prefix = /^<\s*style\b/i.exec(openingTag);
7514
+ if (!prefix)
7515
+ return openingTag;
7516
+ let output = prefix[0];
7517
+ let cursor = prefix[0].length;
7518
+ const limit = openingTag.length - 1;
7519
+ while (cursor < limit) {
7520
+ const attributeStart = cursor;
7521
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
7522
+ cursor += 1;
7523
+ if (cursor >= limit || openingTag[cursor] === '>') {
7524
+ output += openingTag.slice(attributeStart, cursor);
7525
+ break;
7526
+ }
7527
+ const nameStart = cursor;
7528
+ while (cursor < limit && !/[\s=/>]/.test(openingTag[cursor]))
7529
+ cursor += 1;
7530
+ if (cursor === nameStart) {
7531
+ output += openingTag.slice(attributeStart, cursor + 1);
7532
+ cursor += 1;
7533
+ continue;
7534
+ }
7535
+ const name = openingTag.slice(nameStart, cursor).toLowerCase();
7536
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
7537
+ cursor += 1;
7538
+ if (openingTag[cursor] === '=') {
7539
+ cursor += 1;
7540
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
7541
+ cursor += 1;
7542
+ const quote = openingTag[cursor] === '"' || openingTag[cursor] === "'" ? openingTag[cursor] : '';
7543
+ if (quote) {
7544
+ cursor += 1;
7545
+ while (cursor < limit && openingTag[cursor] !== quote)
7546
+ cursor += 1;
7547
+ if (openingTag[cursor] === quote)
7548
+ cursor += 1;
7549
+ }
7550
+ else {
7551
+ while (cursor < limit && !/[\s>]/.test(openingTag[cursor]))
7552
+ cursor += 1;
7553
+ }
7554
+ }
7555
+ if (name !== 'nonce')
7556
+ output += openingTag.slice(attributeStart, cursor);
7557
+ }
7558
+ return output + openingTag.slice(limit);
7559
+ }
7560
+ function withNonce(openingTag, nonce) {
7561
+ const withoutNonce = removeNonceAttribute(openingTag);
7562
+ return `${withoutNonce.slice(0, -1)} nonce="${escapeAttribute(nonce)}">`;
7563
+ }
7564
+ function prepareNavigationHtml(html) {
7565
+ if (expressionRuntimeMode() !== 'csp')
7566
+ return html;
7567
+ const activeNonce = resolveCspNonce();
7568
+ const lowerHtml = html.toLowerCase();
7569
+ let output = '';
7570
+ let cursor = 0;
7571
+ let skippedStyles = 0;
7572
+ while (cursor < html.length) {
7573
+ const tagStart = html.indexOf('<', cursor);
7574
+ if (tagStart === -1) {
7575
+ output += html.slice(cursor);
7576
+ break;
7577
+ }
7578
+ output += html.slice(cursor, tagStart);
7579
+ if (html.startsWith('<!--', tagStart)) {
7580
+ const commentEnd = html.indexOf('-->', tagStart + 4);
7581
+ const end = commentEnd === -1 ? html.length : commentEnd + 3;
7582
+ output += html.slice(tagStart, end);
7583
+ cursor = end;
7584
+ continue;
7585
+ }
7586
+ const tagEnd = findTagEnd(html, tagStart + 1);
7587
+ const openingTag = html.slice(tagStart, tagEnd);
7588
+ const match = /^<\s*(\/?)\s*([a-z][\w:-]*)/i.exec(openingTag);
7589
+ if (!match || match[1]) {
7590
+ output += openingTag;
7591
+ cursor = tagEnd;
7592
+ continue;
7593
+ }
7594
+ const name = match[2].toLowerCase();
7595
+ if (name === 'plaintext') {
7596
+ output += html.slice(tagStart);
7597
+ break;
7598
+ }
7599
+ if (name === 'style' || name === 'noscript' || RAW_TEXT_ELEMENTS.has(name)) {
7600
+ const closingTag = findClosingTag(html, lowerHtml, name, tagEnd);
7601
+ const blockEnd = closingTag?.end ?? html.length;
7602
+ if (name === 'noscript') {
7603
+ cursor = blockEnd;
7604
+ continue;
7605
+ }
7606
+ if (name === 'style') {
7607
+ if (!activeNonce) {
7608
+ skippedStyles += 1;
7609
+ cursor = blockEnd;
7610
+ continue;
7611
+ }
7612
+ output += withNonce(openingTag, activeNonce);
7613
+ output += html.slice(tagEnd, blockEnd);
7614
+ cursor = blockEnd;
7615
+ continue;
7616
+ }
7617
+ output += html.slice(tagStart, blockEnd);
7618
+ cursor = blockEnd;
7619
+ continue;
7400
7620
  }
7621
+ output += openingTag;
7622
+ cursor = tagEnd;
7623
+ }
7624
+ if (skippedStyles > 0) {
7625
+ console.warn(`[GyosJS CSP] Skipped ${skippedStyles} inline MPA style${skippedStyles === 1 ? '' : 's'} because no active document nonce is configured.`);
7401
7626
  }
7627
+ return output;
7402
7628
  }
7403
7629
 
7404
7630
  /**
@@ -7464,7 +7690,10 @@
7464
7690
  root.querySelectorAll('noscript').forEach(element => element.remove());
7465
7691
  }
7466
7692
  function parseNavigationDocument(html) {
7467
- const doc = new DOMParser().parseFromString(html, 'text/html');
7693
+ // Chromium applies the active page CSP while DOMParser creates response
7694
+ // styles. Reconcile their nonce before parsing to avoid transient violations;
7695
+ // diffNodes repeats the check before insertion into the live document.
7696
+ const doc = new DOMParser().parseFromString(prepareNavigationHtml(html), 'text/html');
7468
7697
  // DOMParser disables scripting and parses noscript children as active markup.
7469
7698
  // A boosted page runs with scripting enabled, so those fallbacks must stay inert.
7470
7699
  stripInertFallbackContent(doc);
@@ -8300,7 +8529,7 @@
8300
8529
  portalCreate,
8301
8530
  portalDestroy,
8302
8531
  setCspNonce,
8303
- version: '0.3.0'
8532
+ version: '0.3.1'
8304
8533
  };
8305
8534
 
8306
8535
  const standardExpressionRuntime = {