gyosjs 0.3.0 → 0.3.2

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.2
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
@@ -5197,6 +5189,10 @@
5197
5189
  const structuralElements = [];
5198
5190
  const root = elements[0];
5199
5191
  elements.forEach(el => {
5192
+ // Await branches are detached and parsed only after their alias scope exists.
5193
+ // Do not evaluate nested structural expressions during the parent scan.
5194
+ if (isAwaitBranchDescendant(el))
5195
+ return;
5200
5196
  const hasIf = el.hasAttribute('*if');
5201
5197
  const hasFor = el.hasAttribute('*for');
5202
5198
  const hasSwitch = el.hasAttribute('*switch');
@@ -5241,6 +5237,20 @@
5241
5237
  }
5242
5238
  });
5243
5239
  }
5240
+ function isAwaitBranchDescendant(element) {
5241
+ let child = element;
5242
+ let parent = element.parentElement;
5243
+ while (parent && parent !== document.body) {
5244
+ if (parent.hasAttribute('*await') && (child.hasAttribute('*pending') ||
5245
+ child.hasAttribute('*then') ||
5246
+ child.hasAttribute('*catch'))) {
5247
+ return true;
5248
+ }
5249
+ child = parent;
5250
+ parent = parent.parentElement;
5251
+ }
5252
+ return false;
5253
+ }
5244
5254
  const elementBelongsToAnotherScope = (el, scope) => {
5245
5255
  const owner = getScopeFromElement(el);
5246
5256
  return Boolean(owner && owner !== scope);
@@ -6762,7 +6772,7 @@
6762
6772
  }
6763
6773
 
6764
6774
  let nonceSource;
6765
- /** Configure the nonce used when Gyos recreates scripts during MPA navigation. */
6775
+ /** Configure the active document nonce used for scripts and styles during MPA navigation. */
6766
6776
  function setCspNonce(source) {
6767
6777
  nonceSource = source;
6768
6778
  }
@@ -7334,9 +7344,33 @@
7334
7344
  window.scrollTo({ top: 0, behavior: 'smooth' });
7335
7345
  }
7336
7346
 
7337
- function serializeAttributes(el) {
7338
- return Array.from(el.attributes)
7339
- .map(attr => `${attr.name}=${attr.value}`)
7347
+ function isNonceMeta(el) {
7348
+ return el.tagName === 'META' && el.getAttribute('name')?.toLowerCase() === 'csp-nonce';
7349
+ }
7350
+ function isStylesheetLink(el) {
7351
+ return el.tagName === 'LINK'
7352
+ && (el.getAttribute('rel') || '').toLowerCase().split(/\s+/).includes('stylesheet');
7353
+ }
7354
+ function usesCspNonce(el) {
7355
+ return el.tagName === 'SCRIPT' || el.tagName === 'STYLE' || isStylesheetLink(el);
7356
+ }
7357
+ function serializeAttributes(el, context) {
7358
+ const attributes = new Map();
7359
+ Array.from(el.attributes).forEach(attr => {
7360
+ if (context.csp && usesCspNonce(el) && attr.name.toLowerCase() === 'nonce')
7361
+ return;
7362
+ if (context.csp && isNonceMeta(el) && attr.name.toLowerCase() === 'content')
7363
+ return;
7364
+ attributes.set(attr.name, attr.value);
7365
+ });
7366
+ if (context.csp && usesCspNonce(el) && context.activeNonce) {
7367
+ attributes.set('nonce', context.activeNonce);
7368
+ }
7369
+ if (context.csp && isNonceMeta(el) && context.activeNonce) {
7370
+ attributes.set('content', context.activeNonce);
7371
+ }
7372
+ return Array.from(attributes)
7373
+ .map(([name, value]) => `${name}=${value}`)
7340
7374
  .sort()
7341
7375
  .join('|');
7342
7376
  }
@@ -7347,23 +7381,61 @@
7347
7381
  }
7348
7382
  return String(hash);
7349
7383
  }
7350
- function elementKey(el) {
7384
+ function elementKey(el, context) {
7351
7385
  const tag = el.tagName.toLowerCase();
7352
7386
  const content = tag === 'script' || tag === 'style' ? hashContent(el.textContent || '') : '';
7353
- return `${tag}|${serializeAttributes(el)}|${content}`;
7387
+ return `${tag}|${serializeAttributes(el, context)}|${content}`;
7354
7388
  }
7355
- function keyedNodes(nodes) {
7389
+ function keyedNodes(nodes, context) {
7356
7390
  const occurrences = new Map();
7357
7391
  return nodes.map(node => {
7358
- const baseKey = elementKey(node);
7392
+ const baseKey = elementKey(node, context);
7359
7393
  const occurrence = occurrences.get(baseKey) || 0;
7360
7394
  occurrences.set(baseKey, occurrence + 1);
7361
7395
  return { key: `${baseKey}|${occurrence}`, node };
7362
7396
  });
7363
7397
  }
7398
+ function canInsertNode(node, context) {
7399
+ if (!context.csp)
7400
+ return true;
7401
+ if (isNonceMeta(node))
7402
+ return Boolean(context.activeNonce);
7403
+ if (node.tagName === 'STYLE')
7404
+ return Boolean(context.activeNonce);
7405
+ if (node.tagName === 'SCRIPT' && !node.hasAttribute('src'))
7406
+ return Boolean(context.activeNonce);
7407
+ return true;
7408
+ }
7409
+ function reconcileCspAttributes(node, context) {
7410
+ if (!context.csp)
7411
+ return;
7412
+ if (isNonceMeta(node)) {
7413
+ if (context.activeNonce)
7414
+ node.setAttribute('content', context.activeNonce);
7415
+ return;
7416
+ }
7417
+ if (!usesCspNonce(node))
7418
+ return;
7419
+ node.removeAttribute('nonce');
7420
+ if (context.activeNonce)
7421
+ node.setAttribute('nonce', context.activeNonce);
7422
+ }
7423
+ function warnSkippedNode(node) {
7424
+ if (node.tagName === 'STYLE') {
7425
+ console.warn('[GyosJS CSP] Skipped an inline MPA style because no active document nonce is configured.');
7426
+ }
7427
+ else if (node.tagName === 'SCRIPT' && !node.hasAttribute('src')) {
7428
+ console.warn('[GyosJS CSP] Skipped an inline MPA script because no active document nonce is configured.');
7429
+ }
7430
+ }
7364
7431
  async function diffNodes(currentNodes, nextNodes, container = document.head, signal) {
7365
- const current = keyedNodes(currentNodes);
7366
- const next = keyedNodes(nextNodes);
7432
+ const csp = expressionRuntimeMode() === 'csp';
7433
+ const context = {
7434
+ csp,
7435
+ activeNonce: csp ? resolveCspNonce() : undefined
7436
+ };
7437
+ const current = keyedNodes(currentNodes, context);
7438
+ const next = keyedNodes(nextNodes, context);
7367
7439
  const currentKeys = new Set(current.map(item => item.key));
7368
7440
  const nextKeys = new Set(next.map(item => item.key));
7369
7441
  // Remove nodes that are not in the new head
@@ -7377,6 +7449,20 @@
7377
7449
  if (signal?.aborted)
7378
7450
  return;
7379
7451
  if (!currentKeys.has(key)) {
7452
+ if (!canInsertNode(node, context)) {
7453
+ warnSkippedNode(node);
7454
+ continue;
7455
+ }
7456
+ if (node.tagName === 'STYLE') {
7457
+ const style = document.createElement('style');
7458
+ Array.from(node.attributes).forEach(attr => style.setAttribute(attr.name, attr.value));
7459
+ reconcileCspAttributes(style, context);
7460
+ // CSP checks inline styles as their CSS text is attached. Set the active
7461
+ // nonce first so a response nonce never produces a transient violation.
7462
+ style.textContent = node.textContent || '';
7463
+ container.appendChild(style);
7464
+ continue;
7465
+ }
7380
7466
  if (node.tagName === 'SCRIPT') {
7381
7467
  const originalScript = node;
7382
7468
  const executionKey = getScriptExecutionKey(originalScript);
@@ -7387,6 +7473,7 @@
7387
7473
  }
7388
7474
  const script = document.createElement('script');
7389
7475
  Array.from(node.attributes).forEach(attr => script.setAttribute(attr.name, attr.value));
7476
+ reconcileCspAttributes(script, context);
7390
7477
  script.textContent = node.hasAttribute('g-script-wrap')
7391
7478
  ? wrapScriptExecution(node.textContent || '')
7392
7479
  : node.textContent || '';
@@ -7396,9 +7483,166 @@
7396
7483
  await completion;
7397
7484
  continue;
7398
7485
  }
7399
- container.appendChild(document.importNode(node, true));
7486
+ const imported = document.importNode(node, true);
7487
+ reconcileCspAttributes(imported, context);
7488
+ container.appendChild(imported);
7489
+ }
7490
+ }
7491
+ }
7492
+
7493
+ const RAW_TEXT_ELEMENTS = new Set(['iframe', 'noembed', 'noframes', 'plaintext', 'script', 'textarea', 'title', 'xmp']);
7494
+ function escapeAttribute(value) {
7495
+ return value.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
7496
+ }
7497
+ function findTagEnd(html, start) {
7498
+ let quote = '';
7499
+ for (let index = start; index < html.length; index += 1) {
7500
+ const character = html[index];
7501
+ if (quote) {
7502
+ if (character === quote)
7503
+ quote = '';
7504
+ continue;
7505
+ }
7506
+ if (character === '"' || character === "'") {
7507
+ quote = character;
7508
+ continue;
7509
+ }
7510
+ if (character === '>')
7511
+ return index + 1;
7512
+ }
7513
+ return html.length;
7514
+ }
7515
+ function findClosingTag(html, lowerHtml, name, start) {
7516
+ const needle = `</${name}`;
7517
+ let position = lowerHtml.indexOf(needle, start);
7518
+ while (position !== -1) {
7519
+ const boundary = lowerHtml[position + needle.length];
7520
+ if (boundary === '>' || /\s/.test(boundary || '')) {
7521
+ return {
7522
+ start: position,
7523
+ end: findTagEnd(html, position + needle.length)
7524
+ };
7525
+ }
7526
+ position = lowerHtml.indexOf(needle, position + needle.length);
7527
+ }
7528
+ return null;
7529
+ }
7530
+ function removeNonceAttribute(openingTag) {
7531
+ const prefix = /^<\s*style\b/i.exec(openingTag);
7532
+ if (!prefix)
7533
+ return openingTag;
7534
+ let output = prefix[0];
7535
+ let cursor = prefix[0].length;
7536
+ const limit = openingTag.length - 1;
7537
+ while (cursor < limit) {
7538
+ const attributeStart = cursor;
7539
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
7540
+ cursor += 1;
7541
+ if (cursor >= limit || openingTag[cursor] === '>') {
7542
+ output += openingTag.slice(attributeStart, cursor);
7543
+ break;
7544
+ }
7545
+ const nameStart = cursor;
7546
+ while (cursor < limit && !/[\s=/>]/.test(openingTag[cursor]))
7547
+ cursor += 1;
7548
+ if (cursor === nameStart) {
7549
+ output += openingTag.slice(attributeStart, cursor + 1);
7550
+ cursor += 1;
7551
+ continue;
7552
+ }
7553
+ const name = openingTag.slice(nameStart, cursor).toLowerCase();
7554
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
7555
+ cursor += 1;
7556
+ if (openingTag[cursor] === '=') {
7557
+ cursor += 1;
7558
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
7559
+ cursor += 1;
7560
+ const quote = openingTag[cursor] === '"' || openingTag[cursor] === "'" ? openingTag[cursor] : '';
7561
+ if (quote) {
7562
+ cursor += 1;
7563
+ while (cursor < limit && openingTag[cursor] !== quote)
7564
+ cursor += 1;
7565
+ if (openingTag[cursor] === quote)
7566
+ cursor += 1;
7567
+ }
7568
+ else {
7569
+ while (cursor < limit && !/[\s>]/.test(openingTag[cursor]))
7570
+ cursor += 1;
7571
+ }
7572
+ }
7573
+ if (name !== 'nonce')
7574
+ output += openingTag.slice(attributeStart, cursor);
7575
+ }
7576
+ return output + openingTag.slice(limit);
7577
+ }
7578
+ function withNonce(openingTag, nonce) {
7579
+ const withoutNonce = removeNonceAttribute(openingTag);
7580
+ return `${withoutNonce.slice(0, -1)} nonce="${escapeAttribute(nonce)}">`;
7581
+ }
7582
+ function prepareNavigationHtml(html) {
7583
+ if (expressionRuntimeMode() !== 'csp')
7584
+ return html;
7585
+ const activeNonce = resolveCspNonce();
7586
+ const lowerHtml = html.toLowerCase();
7587
+ let output = '';
7588
+ let cursor = 0;
7589
+ let skippedStyles = 0;
7590
+ while (cursor < html.length) {
7591
+ const tagStart = html.indexOf('<', cursor);
7592
+ if (tagStart === -1) {
7593
+ output += html.slice(cursor);
7594
+ break;
7595
+ }
7596
+ output += html.slice(cursor, tagStart);
7597
+ if (html.startsWith('<!--', tagStart)) {
7598
+ const commentEnd = html.indexOf('-->', tagStart + 4);
7599
+ const end = commentEnd === -1 ? html.length : commentEnd + 3;
7600
+ output += html.slice(tagStart, end);
7601
+ cursor = end;
7602
+ continue;
7603
+ }
7604
+ const tagEnd = findTagEnd(html, tagStart + 1);
7605
+ const openingTag = html.slice(tagStart, tagEnd);
7606
+ const match = /^<\s*(\/?)\s*([a-z][\w:-]*)/i.exec(openingTag);
7607
+ if (!match || match[1]) {
7608
+ output += openingTag;
7609
+ cursor = tagEnd;
7610
+ continue;
7611
+ }
7612
+ const name = match[2].toLowerCase();
7613
+ if (name === 'plaintext') {
7614
+ output += html.slice(tagStart);
7615
+ break;
7616
+ }
7617
+ if (name === 'style' || name === 'noscript' || RAW_TEXT_ELEMENTS.has(name)) {
7618
+ const closingTag = findClosingTag(html, lowerHtml, name, tagEnd);
7619
+ const blockEnd = closingTag?.end ?? html.length;
7620
+ if (name === 'noscript') {
7621
+ cursor = blockEnd;
7622
+ continue;
7623
+ }
7624
+ if (name === 'style') {
7625
+ if (!activeNonce) {
7626
+ skippedStyles += 1;
7627
+ cursor = blockEnd;
7628
+ continue;
7629
+ }
7630
+ output += withNonce(openingTag, activeNonce);
7631
+ output += html.slice(tagEnd, blockEnd);
7632
+ cursor = blockEnd;
7633
+ continue;
7634
+ }
7635
+ output += html.slice(tagStart, blockEnd);
7636
+ cursor = blockEnd;
7637
+ continue;
7400
7638
  }
7639
+ output += openingTag;
7640
+ cursor = tagEnd;
7641
+ }
7642
+ if (skippedStyles > 0) {
7643
+ console.warn(`[GyosJS CSP] Skipped ${skippedStyles} inline MPA style${skippedStyles === 1 ? '' : 's'} because no active document nonce is configured.`);
7401
7644
  }
7645
+ return output;
7402
7646
  }
7403
7647
 
7404
7648
  /**
@@ -7464,7 +7708,10 @@
7464
7708
  root.querySelectorAll('noscript').forEach(element => element.remove());
7465
7709
  }
7466
7710
  function parseNavigationDocument(html) {
7467
- const doc = new DOMParser().parseFromString(html, 'text/html');
7711
+ // Chromium applies the active page CSP while DOMParser creates response
7712
+ // styles. Reconcile their nonce before parsing to avoid transient violations;
7713
+ // diffNodes repeats the check before insertion into the live document.
7714
+ const doc = new DOMParser().parseFromString(prepareNavigationHtml(html), 'text/html');
7468
7715
  // DOMParser disables scripting and parses noscript children as active markup.
7469
7716
  // A boosted page runs with scripting enabled, so those fallbacks must stay inert.
7470
7717
  stripInertFallbackContent(doc);
@@ -8300,7 +8547,7 @@
8300
8547
  portalCreate,
8301
8548
  portalDestroy,
8302
8549
  setCspNonce,
8303
- version: '0.3.0'
8550
+ version: '0.3.2'
8304
8551
  };
8305
8552
 
8306
8553
  const standardExpressionRuntime = {