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