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
  */
@@ -8911,32 +8911,24 @@ function applyTransitionStyles() {
8911
8911
  * Handles:
8912
8912
  * 1. Literal string: "fade" or "slide-down"
8913
8913
  * 2. Expression with {}: "{transition}"
8914
- * 3. Expression without {}: "transition" (evaluate as variable)
8914
+ * 3. Quoted literal kept for backwards compatibility: "'fade'"
8915
8915
  *
8916
8916
  * @param attrValue - The g-transition attribute value
8917
8917
  * @param scope - Current scope for expression evaluation
8918
8918
  * @returns The transition name (string)
8919
8919
  */
8920
8920
  function parseTransitionName(attrValue, scope) {
8921
- if (attrValue.includes('{') && attrValue.includes('}')) {
8922
- // Extract expression from {} and evaluate it
8923
- const match = attrValue.match(/\{([^}]+)\}/);
8924
- if (match) {
8925
- return evaluateExpression(match[1].trim(), scope);
8926
- }
8927
- }
8928
- // Try to evaluate as expression first (variable name)
8929
- // If it fails or returns undefined, use as literal string
8930
- try {
8931
- const evaluated = evaluateExpression(attrValue, scope);
8932
- if (evaluated !== undefined && evaluated !== null) {
8933
- return evaluated;
8934
- }
8921
+ const value = attrValue.trim();
8922
+ const expression = /^\{([\s\S]+)\}$/.exec(value);
8923
+ if (expression) {
8924
+ const evaluated = evaluateExpression(expression[1].trim(), scope);
8925
+ return evaluated == null ? '' : String(evaluated);
8935
8926
  }
8936
- catch (e) {
8937
- // If evaluation fails, use value as literal string (built-in transition name)
8927
+ if (value.length >= 2 && ((value.startsWith("'") && value.endsWith("'"))
8928
+ || (value.startsWith('"') && value.endsWith('"')))) {
8929
+ return value.slice(1, -1);
8938
8930
  }
8939
- return attrValue;
8931
+ return value;
8940
8932
  }
8941
8933
  /**
8942
8934
  * Parse transition config from element's g-transition attribute
@@ -13405,7 +13397,7 @@ function hideTargetSpinner(spinner) {
13405
13397
  }
13406
13398
 
13407
13399
  let nonceSource;
13408
- /** Configure the nonce used when Gyos recreates scripts during MPA navigation. */
13400
+ /** Configure the active document nonce used for scripts and styles during MPA navigation. */
13409
13401
  function setCspNonce(source) {
13410
13402
  nonceSource = source;
13411
13403
  }
@@ -13977,9 +13969,33 @@ function handleScroll(url, trigger, savedScroll) {
13977
13969
  window.scrollTo({ top: 0, behavior: 'smooth' });
13978
13970
  }
13979
13971
 
13980
- function serializeAttributes(el) {
13981
- return Array.from(el.attributes)
13982
- .map(attr => `${attr.name}=${attr.value}`)
13972
+ function isNonceMeta(el) {
13973
+ return el.tagName === 'META' && el.getAttribute('name')?.toLowerCase() === 'csp-nonce';
13974
+ }
13975
+ function isStylesheetLink(el) {
13976
+ return el.tagName === 'LINK'
13977
+ && (el.getAttribute('rel') || '').toLowerCase().split(/\s+/).includes('stylesheet');
13978
+ }
13979
+ function usesCspNonce(el) {
13980
+ return el.tagName === 'SCRIPT' || el.tagName === 'STYLE' || isStylesheetLink(el);
13981
+ }
13982
+ function serializeAttributes(el, context) {
13983
+ const attributes = new Map();
13984
+ Array.from(el.attributes).forEach(attr => {
13985
+ if (context.csp && usesCspNonce(el) && attr.name.toLowerCase() === 'nonce')
13986
+ return;
13987
+ if (context.csp && isNonceMeta(el) && attr.name.toLowerCase() === 'content')
13988
+ return;
13989
+ attributes.set(attr.name, attr.value);
13990
+ });
13991
+ if (context.csp && usesCspNonce(el) && context.activeNonce) {
13992
+ attributes.set('nonce', context.activeNonce);
13993
+ }
13994
+ if (context.csp && isNonceMeta(el) && context.activeNonce) {
13995
+ attributes.set('content', context.activeNonce);
13996
+ }
13997
+ return Array.from(attributes)
13998
+ .map(([name, value]) => `${name}=${value}`)
13983
13999
  .sort()
13984
14000
  .join('|');
13985
14001
  }
@@ -13990,23 +14006,61 @@ function hashContent(content) {
13990
14006
  }
13991
14007
  return String(hash);
13992
14008
  }
13993
- function elementKey(el) {
14009
+ function elementKey(el, context) {
13994
14010
  const tag = el.tagName.toLowerCase();
13995
14011
  const content = tag === 'script' || tag === 'style' ? hashContent(el.textContent || '') : '';
13996
- return `${tag}|${serializeAttributes(el)}|${content}`;
14012
+ return `${tag}|${serializeAttributes(el, context)}|${content}`;
13997
14013
  }
13998
- function keyedNodes(nodes) {
14014
+ function keyedNodes(nodes, context) {
13999
14015
  const occurrences = new Map();
14000
14016
  return nodes.map(node => {
14001
- const baseKey = elementKey(node);
14017
+ const baseKey = elementKey(node, context);
14002
14018
  const occurrence = occurrences.get(baseKey) || 0;
14003
14019
  occurrences.set(baseKey, occurrence + 1);
14004
14020
  return { key: `${baseKey}|${occurrence}`, node };
14005
14021
  });
14006
14022
  }
14023
+ function canInsertNode(node, context) {
14024
+ if (!context.csp)
14025
+ return true;
14026
+ if (isNonceMeta(node))
14027
+ return Boolean(context.activeNonce);
14028
+ if (node.tagName === 'STYLE')
14029
+ return Boolean(context.activeNonce);
14030
+ if (node.tagName === 'SCRIPT' && !node.hasAttribute('src'))
14031
+ return Boolean(context.activeNonce);
14032
+ return true;
14033
+ }
14034
+ function reconcileCspAttributes(node, context) {
14035
+ if (!context.csp)
14036
+ return;
14037
+ if (isNonceMeta(node)) {
14038
+ if (context.activeNonce)
14039
+ node.setAttribute('content', context.activeNonce);
14040
+ return;
14041
+ }
14042
+ if (!usesCspNonce(node))
14043
+ return;
14044
+ node.removeAttribute('nonce');
14045
+ if (context.activeNonce)
14046
+ node.setAttribute('nonce', context.activeNonce);
14047
+ }
14048
+ function warnSkippedNode(node) {
14049
+ if (node.tagName === 'STYLE') {
14050
+ console.warn('[GyosJS CSP] Skipped an inline MPA style because no active document nonce is configured.');
14051
+ }
14052
+ else if (node.tagName === 'SCRIPT' && !node.hasAttribute('src')) {
14053
+ console.warn('[GyosJS CSP] Skipped an inline MPA script because no active document nonce is configured.');
14054
+ }
14055
+ }
14007
14056
  async function diffNodes(currentNodes, nextNodes, container = document.head, signal) {
14008
- const current = keyedNodes(currentNodes);
14009
- const next = keyedNodes(nextNodes);
14057
+ const csp = expressionRuntimeMode() === 'csp';
14058
+ const context = {
14059
+ csp,
14060
+ activeNonce: csp ? resolveCspNonce() : undefined
14061
+ };
14062
+ const current = keyedNodes(currentNodes, context);
14063
+ const next = keyedNodes(nextNodes, context);
14010
14064
  const currentKeys = new Set(current.map(item => item.key));
14011
14065
  const nextKeys = new Set(next.map(item => item.key));
14012
14066
  // Remove nodes that are not in the new head
@@ -14020,6 +14074,20 @@ async function diffNodes(currentNodes, nextNodes, container = document.head, sig
14020
14074
  if (signal?.aborted)
14021
14075
  return;
14022
14076
  if (!currentKeys.has(key)) {
14077
+ if (!canInsertNode(node, context)) {
14078
+ warnSkippedNode(node);
14079
+ continue;
14080
+ }
14081
+ if (node.tagName === 'STYLE') {
14082
+ const style = document.createElement('style');
14083
+ Array.from(node.attributes).forEach(attr => style.setAttribute(attr.name, attr.value));
14084
+ reconcileCspAttributes(style, context);
14085
+ // CSP checks inline styles as their CSS text is attached. Set the active
14086
+ // nonce first so a response nonce never produces a transient violation.
14087
+ style.textContent = node.textContent || '';
14088
+ container.appendChild(style);
14089
+ continue;
14090
+ }
14023
14091
  if (node.tagName === 'SCRIPT') {
14024
14092
  const originalScript = node;
14025
14093
  const executionKey = getScriptExecutionKey(originalScript);
@@ -14030,6 +14098,7 @@ async function diffNodes(currentNodes, nextNodes, container = document.head, sig
14030
14098
  }
14031
14099
  const script = document.createElement('script');
14032
14100
  Array.from(node.attributes).forEach(attr => script.setAttribute(attr.name, attr.value));
14101
+ reconcileCspAttributes(script, context);
14033
14102
  script.textContent = node.hasAttribute('g-script-wrap')
14034
14103
  ? wrapScriptExecution(node.textContent || '')
14035
14104
  : node.textContent || '';
@@ -14039,11 +14108,168 @@ async function diffNodes(currentNodes, nextNodes, container = document.head, sig
14039
14108
  await completion;
14040
14109
  continue;
14041
14110
  }
14042
- container.appendChild(document.importNode(node, true));
14111
+ const imported = document.importNode(node, true);
14112
+ reconcileCspAttributes(imported, context);
14113
+ container.appendChild(imported);
14043
14114
  }
14044
14115
  }
14045
14116
  }
14046
14117
 
14118
+ const RAW_TEXT_ELEMENTS = new Set(['iframe', 'noembed', 'noframes', 'plaintext', 'script', 'textarea', 'title', 'xmp']);
14119
+ function escapeAttribute(value) {
14120
+ return value.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
14121
+ }
14122
+ function findTagEnd(html, start) {
14123
+ let quote = '';
14124
+ for (let index = start; index < html.length; index += 1) {
14125
+ const character = html[index];
14126
+ if (quote) {
14127
+ if (character === quote)
14128
+ quote = '';
14129
+ continue;
14130
+ }
14131
+ if (character === '"' || character === "'") {
14132
+ quote = character;
14133
+ continue;
14134
+ }
14135
+ if (character === '>')
14136
+ return index + 1;
14137
+ }
14138
+ return html.length;
14139
+ }
14140
+ function findClosingTag(html, lowerHtml, name, start) {
14141
+ const needle = `</${name}`;
14142
+ let position = lowerHtml.indexOf(needle, start);
14143
+ while (position !== -1) {
14144
+ const boundary = lowerHtml[position + needle.length];
14145
+ if (boundary === '>' || /\s/.test(boundary || '')) {
14146
+ return {
14147
+ start: position,
14148
+ end: findTagEnd(html, position + needle.length)
14149
+ };
14150
+ }
14151
+ position = lowerHtml.indexOf(needle, position + needle.length);
14152
+ }
14153
+ return null;
14154
+ }
14155
+ function removeNonceAttribute(openingTag) {
14156
+ const prefix = /^<\s*style\b/i.exec(openingTag);
14157
+ if (!prefix)
14158
+ return openingTag;
14159
+ let output = prefix[0];
14160
+ let cursor = prefix[0].length;
14161
+ const limit = openingTag.length - 1;
14162
+ while (cursor < limit) {
14163
+ const attributeStart = cursor;
14164
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
14165
+ cursor += 1;
14166
+ if (cursor >= limit || openingTag[cursor] === '>') {
14167
+ output += openingTag.slice(attributeStart, cursor);
14168
+ break;
14169
+ }
14170
+ const nameStart = cursor;
14171
+ while (cursor < limit && !/[\s=/>]/.test(openingTag[cursor]))
14172
+ cursor += 1;
14173
+ if (cursor === nameStart) {
14174
+ output += openingTag.slice(attributeStart, cursor + 1);
14175
+ cursor += 1;
14176
+ continue;
14177
+ }
14178
+ const name = openingTag.slice(nameStart, cursor).toLowerCase();
14179
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
14180
+ cursor += 1;
14181
+ if (openingTag[cursor] === '=') {
14182
+ cursor += 1;
14183
+ while (cursor < limit && /\s/.test(openingTag[cursor]))
14184
+ cursor += 1;
14185
+ const quote = openingTag[cursor] === '"' || openingTag[cursor] === "'" ? openingTag[cursor] : '';
14186
+ if (quote) {
14187
+ cursor += 1;
14188
+ while (cursor < limit && openingTag[cursor] !== quote)
14189
+ cursor += 1;
14190
+ if (openingTag[cursor] === quote)
14191
+ cursor += 1;
14192
+ }
14193
+ else {
14194
+ while (cursor < limit && !/[\s>]/.test(openingTag[cursor]))
14195
+ cursor += 1;
14196
+ }
14197
+ }
14198
+ if (name !== 'nonce')
14199
+ output += openingTag.slice(attributeStart, cursor);
14200
+ }
14201
+ return output + openingTag.slice(limit);
14202
+ }
14203
+ function withNonce(openingTag, nonce) {
14204
+ const withoutNonce = removeNonceAttribute(openingTag);
14205
+ return `${withoutNonce.slice(0, -1)} nonce="${escapeAttribute(nonce)}">`;
14206
+ }
14207
+ function prepareNavigationHtml(html) {
14208
+ if (expressionRuntimeMode() !== 'csp')
14209
+ return html;
14210
+ const activeNonce = resolveCspNonce();
14211
+ const lowerHtml = html.toLowerCase();
14212
+ let output = '';
14213
+ let cursor = 0;
14214
+ let skippedStyles = 0;
14215
+ while (cursor < html.length) {
14216
+ const tagStart = html.indexOf('<', cursor);
14217
+ if (tagStart === -1) {
14218
+ output += html.slice(cursor);
14219
+ break;
14220
+ }
14221
+ output += html.slice(cursor, tagStart);
14222
+ if (html.startsWith('<!--', tagStart)) {
14223
+ const commentEnd = html.indexOf('-->', tagStart + 4);
14224
+ const end = commentEnd === -1 ? html.length : commentEnd + 3;
14225
+ output += html.slice(tagStart, end);
14226
+ cursor = end;
14227
+ continue;
14228
+ }
14229
+ const tagEnd = findTagEnd(html, tagStart + 1);
14230
+ const openingTag = html.slice(tagStart, tagEnd);
14231
+ const match = /^<\s*(\/?)\s*([a-z][\w:-]*)/i.exec(openingTag);
14232
+ if (!match || match[1]) {
14233
+ output += openingTag;
14234
+ cursor = tagEnd;
14235
+ continue;
14236
+ }
14237
+ const name = match[2].toLowerCase();
14238
+ if (name === 'plaintext') {
14239
+ output += html.slice(tagStart);
14240
+ break;
14241
+ }
14242
+ if (name === 'style' || name === 'noscript' || RAW_TEXT_ELEMENTS.has(name)) {
14243
+ const closingTag = findClosingTag(html, lowerHtml, name, tagEnd);
14244
+ const blockEnd = closingTag?.end ?? html.length;
14245
+ if (name === 'noscript') {
14246
+ cursor = blockEnd;
14247
+ continue;
14248
+ }
14249
+ if (name === 'style') {
14250
+ if (!activeNonce) {
14251
+ skippedStyles += 1;
14252
+ cursor = blockEnd;
14253
+ continue;
14254
+ }
14255
+ output += withNonce(openingTag, activeNonce);
14256
+ output += html.slice(tagEnd, blockEnd);
14257
+ cursor = blockEnd;
14258
+ continue;
14259
+ }
14260
+ output += html.slice(tagStart, blockEnd);
14261
+ cursor = blockEnd;
14262
+ continue;
14263
+ }
14264
+ output += openingTag;
14265
+ cursor = tagEnd;
14266
+ }
14267
+ if (skippedStyles > 0) {
14268
+ console.warn(`[GyosJS CSP] Skipped ${skippedStyles} inline MPA style${skippedStyles === 1 ? '' : 's'} because no active document nonce is configured.`);
14269
+ }
14270
+ return output;
14271
+ }
14272
+
14047
14273
  /**
14048
14274
  * GyosJs Router v0
14049
14275
  * MPA boost navigation inspired by Turbo Drive.
@@ -14107,7 +14333,10 @@ function stripInertFallbackContent(root) {
14107
14333
  root.querySelectorAll('noscript').forEach(element => element.remove());
14108
14334
  }
14109
14335
  function parseNavigationDocument(html) {
14110
- const doc = new DOMParser().parseFromString(html, 'text/html');
14336
+ // Chromium applies the active page CSP while DOMParser creates response
14337
+ // styles. Reconcile their nonce before parsing to avoid transient violations;
14338
+ // diffNodes repeats the check before insertion into the live document.
14339
+ const doc = new DOMParser().parseFromString(prepareNavigationHtml(html), 'text/html');
14111
14340
  // DOMParser disables scripting and parses noscript children as active markup.
14112
14341
  // A boosted page runs with scripting enabled, so those fallbacks must stay inert.
14113
14342
  stripInertFallbackContent(doc);
@@ -14943,7 +15172,7 @@ const Gyos = {
14943
15172
  portalCreate,
14944
15173
  portalDestroy,
14945
15174
  setCspNonce,
14946
- version: '0.3.0'
15175
+ version: '0.3.1'
14947
15176
  };
14948
15177
 
14949
15178
  setExpressionRuntime(cspExpressionRuntime);