@pie-qti/item-player 0.1.16 → 0.1.18

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,3 +1,25 @@
1
1
  import type { ResolvedItemDeliveryContext } from '@pie-qti/ims-cp-core';
2
2
  export declare function buildScopedStylesheetCss(deliveryContext: ResolvedItemDeliveryContext | undefined, scopeSelector?: string): string;
3
+ /**
4
+ * Confine every selector in `css` to `scopeSelector`.
5
+ *
6
+ * This walks the stylesheet brace-by-brace. It used to be a single regex over
7
+ * `([^{}@]+)\{([^{}]*)\}`, which handled flat selector rules and silently
8
+ * destroyed at-rules. Because `@` was excluded from the selector pattern rather
9
+ * than *understood*, an `@media` block never matched as a unit, so the inner
10
+ * rules matched on their own and were emitted **without the condition** — the
11
+ * media query was dropped and its rules applied unconditionally, so a
12
+ * print-only or narrow-viewport rule took effect everywhere. `@keyframes`
13
+ * degraded the same way, with its percentage selectors scoped into
14
+ * `[scope] 0%` and the animation name discarded, and `@font-face` became
15
+ * `[scope] font-face`.
16
+ *
17
+ * A CSS parser would be the textbook answer and is deliberately not added:
18
+ * scoping needs rule boundaries only, not property grammar, and this runs in
19
+ * the delivery path.
20
+ *
21
+ * Note that `isBlockedStylesheetCss` rejects any stylesheet containing `url(`
22
+ * or `@import` before this is reached, so `@font-face` with a real `src` never
23
+ * arrives here in practice. `@media`, `@supports` and `@keyframes` do.
24
+ */
3
25
  export declare function scopeCssRules(css: string, scopeSelector?: string): string;
@@ -17,24 +17,94 @@ function stylesheetToScopedCss(stylesheet, scopeSelector) {
17
17
  return '';
18
18
  return scoped;
19
19
  }
20
+ /**
21
+ * At-rules whose block contains nested *rules*, so scoping recurses into the
22
+ * block while the prelude is preserved verbatim.
23
+ *
24
+ * Anything not listed here is passed through untouched. That is the safe
25
+ * default both ways: at-rules whose block holds declarations rather than rules
26
+ * (`@font-face`, `@page`, `@property`, `@counter-style`) or non-selector keys
27
+ * (`@keyframes` percentages) must not be scoped, and an at-rule this list has
28
+ * never heard of is more safely left alone than guessed at.
29
+ */
30
+ const NESTED_RULE_AT_RULES = new Set(['media', 'supports', 'container', 'layer', 'scope']);
31
+ /** Matches a leading `:root`, `html` or `body` not followed by more name characters. */
32
+ const DOCUMENT_ROOT_PREFIX = /^(?::root|html|body)(?![\w-])/;
33
+ /** Matches an at-rule name, with any vendor prefix, at the start of a prelude. */
34
+ const AT_RULE_NAME = /^@(?:-[a-z]+-)?([\w-]+)/i;
35
+ /**
36
+ * Confine every selector in `css` to `scopeSelector`.
37
+ *
38
+ * This walks the stylesheet brace-by-brace. It used to be a single regex over
39
+ * `([^{}@]+)\{([^{}]*)\}`, which handled flat selector rules and silently
40
+ * destroyed at-rules. Because `@` was excluded from the selector pattern rather
41
+ * than *understood*, an `@media` block never matched as a unit, so the inner
42
+ * rules matched on their own and were emitted **without the condition** — the
43
+ * media query was dropped and its rules applied unconditionally, so a
44
+ * print-only or narrow-viewport rule took effect everywhere. `@keyframes`
45
+ * degraded the same way, with its percentage selectors scoped into
46
+ * `[scope] 0%` and the animation name discarded, and `@font-face` became
47
+ * `[scope] font-face`.
48
+ *
49
+ * A CSS parser would be the textbook answer and is deliberately not added:
50
+ * scoping needs rule boundaries only, not property grammar, and this runs in
51
+ * the delivery path.
52
+ *
53
+ * Note that `isBlockedStylesheetCss` rejects any stylesheet containing `url(`
54
+ * or `@import` before this is reached, so `@font-face` with a real `src` never
55
+ * arrives here in practice. `@media`, `@supports` and `@keyframes` do.
56
+ */
20
57
  export function scopeCssRules(css, scopeSelector = '[data-qti-item-body-scope]') {
21
- const withoutComments = css.replace(/\/\*[\s\S]*?\*\//g, '');
22
- let scoped = '';
23
- const rulePattern = /([^{}@]+)\{([^{}]*)\}/g;
24
- for (const match of withoutComments.matchAll(rulePattern)) {
25
- const selectorList = match[1]?.trim();
26
- const declarations = match[2]?.trim();
27
- if (!selectorList || !declarations)
28
- continue;
29
- const selectors = selectorList
30
- .split(',')
31
- .map((selector) => scopeSelectorText(selector.trim(), scopeSelector))
32
- .filter(Boolean);
33
- if (selectors.length === 0)
34
- continue;
35
- scoped += `${selectors.join(', ')} { ${declarations} }\n`;
58
+ if (!css)
59
+ return '';
60
+ const scope = scopeSelector?.trim();
61
+ if (!scope)
62
+ return css;
63
+ return scopeRuleList(stripComments(css), scope).trim();
64
+ }
65
+ function scopeRuleList(css, scope) {
66
+ let out = '';
67
+ let index = 0;
68
+ while (index < css.length) {
69
+ const node = readNode(css, index);
70
+ if (node.end <= index)
71
+ break;
72
+ out += renderNode(node, scope);
73
+ index = node.end;
74
+ }
75
+ return out;
76
+ }
77
+ function renderNode(node, scope) {
78
+ if (node.kind === 'statement') {
79
+ const text = node.text.trim();
80
+ return text === ';' ? '' : `${text}\n`;
81
+ }
82
+ if (node.kind === 'trailing') {
83
+ return node.text.trim() ? `${node.text.trim()}\n` : '';
36
84
  }
37
- return scoped.trim();
85
+ const prelude = node.prelude.trim();
86
+ if (!prelude)
87
+ return '';
88
+ if (prelude.startsWith('@')) {
89
+ const name = AT_RULE_NAME.exec(prelude)?.[1]?.toLowerCase();
90
+ if (name && NESTED_RULE_AT_RULES.has(name)) {
91
+ const inner = scopeRuleList(node.block, scope);
92
+ if (!inner.trim())
93
+ return '';
94
+ return `${prelude} {\n${inner}}\n`;
95
+ }
96
+ return `${prelude} {${node.block}}\n`;
97
+ }
98
+ // Rules with no declarations are dropped, as they were before.
99
+ const declarations = node.block.trim();
100
+ if (!declarations)
101
+ return '';
102
+ const selectors = splitTopLevel(prelude, ',')
103
+ .map((selector) => scopeSelectorText(selector.trim(), scope))
104
+ .filter(Boolean);
105
+ if (selectors.length === 0)
106
+ return '';
107
+ return `${selectors.join(', ')} { ${declarations} }\n`;
38
108
  }
39
109
  function getStylesheetScopeSelector(stylesheet, scopeSelector) {
40
110
  if (stylesheet.source !== 'stimulus' || !stylesheet.stimulusIdentifier)
@@ -44,14 +114,174 @@ function getStylesheetScopeSelector(stylesheet, scopeSelector) {
44
114
  function scopeSelectorText(selector, scopeSelector) {
45
115
  if (!selector)
46
116
  return '';
47
- if (selector.startsWith(scopeSelector))
117
+ if (selector === scopeSelector || selector.startsWith(`${scopeSelector} `))
48
118
  return selector;
49
- if (selector === ':root' || selector === 'html' || selector === 'body')
50
- return scopeSelector;
51
- if (selector.startsWith(':'))
52
- return `${scopeSelector}${selector}`;
119
+ // `:root`/`html`/`body` are replaced rather than prefixed: `[scope] :root`
120
+ // cannot match, because `:root` is the document element and is not a
121
+ // descendant of the item body. Whatever followed is preserved, so
122
+ // `html.dark .a` becomes `[scope].dark .a`.
123
+ const rootPrefix = DOCUMENT_ROOT_PREFIX.exec(selector);
124
+ if (rootPrefix)
125
+ return `${scopeSelector}${selector.slice(rootPrefix[0].length)}`;
126
+ // Everything else becomes a descendant, including a leading pseudo. This
127
+ // used to attach — `[scope]:is(.a, .b)` — which is a different selector: an
128
+ // authored `:is(.a, .b) .c` means "some element matching .a or .b", so
129
+ // attaching demands that the item body itself carry the authored class, and
130
+ // it does not. The same holds for `:hover` and `::selection`.
53
131
  return `${scopeSelector} ${selector}`;
54
132
  }
133
+ /**
134
+ * Read one node starting at `start`, tracking strings and parentheses so a `{`
135
+ * inside `content: "{"` or a `,` inside `:is(a, b)` is not mistaken for
136
+ * structure.
137
+ */
138
+ function readNode(css, start) {
139
+ let index = start;
140
+ let quote = null;
141
+ let parenDepth = 0;
142
+ while (index < css.length) {
143
+ const char = css[index];
144
+ if (quote) {
145
+ if (char === '\\')
146
+ index += 1;
147
+ else if (char === quote)
148
+ quote = null;
149
+ }
150
+ else if (char === '"' || char === "'") {
151
+ quote = char;
152
+ }
153
+ else if (char === '(') {
154
+ parenDepth += 1;
155
+ }
156
+ else if (char === ')') {
157
+ if (parenDepth > 0)
158
+ parenDepth -= 1;
159
+ }
160
+ else if (parenDepth === 0 && char === ';') {
161
+ return { kind: 'statement', text: css.slice(start, index + 1), end: index + 1 };
162
+ }
163
+ else if (parenDepth === 0 && char === '{') {
164
+ const blockEnd = findBlockEnd(css, index);
165
+ return {
166
+ kind: 'rule',
167
+ prelude: css.slice(start, index),
168
+ block: css.slice(index + 1, blockEnd.contentEnd),
169
+ end: blockEnd.end,
170
+ };
171
+ }
172
+ index += 1;
173
+ }
174
+ return { kind: 'trailing', text: css.slice(start), end: css.length };
175
+ }
176
+ /**
177
+ * Find the `}` matching the `{` at `openIndex`. Unbalanced input — a truncated
178
+ * stylesheet — is treated as running to the end rather than throwing.
179
+ */
180
+ function findBlockEnd(css, openIndex) {
181
+ let depth = 0;
182
+ let quote = null;
183
+ for (let index = openIndex; index < css.length; index += 1) {
184
+ const char = css[index];
185
+ if (quote) {
186
+ if (char === '\\')
187
+ index += 1;
188
+ else if (char === quote)
189
+ quote = null;
190
+ continue;
191
+ }
192
+ if (char === '"' || char === "'") {
193
+ quote = char;
194
+ continue;
195
+ }
196
+ if (char === '{')
197
+ depth += 1;
198
+ else if (char === '}') {
199
+ depth -= 1;
200
+ if (depth === 0)
201
+ return { contentEnd: index, end: index + 1 };
202
+ }
203
+ }
204
+ return { contentEnd: css.length, end: css.length };
205
+ }
206
+ /** Split on `separator` at paren depth zero and outside strings. */
207
+ function splitTopLevel(value, separator) {
208
+ const parts = [];
209
+ let current = '';
210
+ let quote = null;
211
+ let parenDepth = 0;
212
+ for (let index = 0; index < value.length; index += 1) {
213
+ const char = value[index];
214
+ if (quote) {
215
+ current += char;
216
+ if (char === '\\' && index + 1 < value.length) {
217
+ current += value[index + 1];
218
+ index += 1;
219
+ }
220
+ else if (char === quote) {
221
+ quote = null;
222
+ }
223
+ continue;
224
+ }
225
+ if (char === '"' || char === "'") {
226
+ quote = char;
227
+ current += char;
228
+ continue;
229
+ }
230
+ if (char === '(')
231
+ parenDepth += 1;
232
+ else if (char === ')' && parenDepth > 0)
233
+ parenDepth -= 1;
234
+ if (char === separator && parenDepth === 0) {
235
+ parts.push(current);
236
+ current = '';
237
+ continue;
238
+ }
239
+ current += char;
240
+ }
241
+ parts.push(current);
242
+ return parts;
243
+ }
244
+ /**
245
+ * Remove comments before walking, so a comment between two rules is not
246
+ * absorbed into the next rule's selector list. String-aware, so a literal
247
+ * `content: "/*"` survives — the previous regex strip was not.
248
+ */
249
+ function stripComments(css) {
250
+ let out = '';
251
+ let quote = null;
252
+ let index = 0;
253
+ while (index < css.length) {
254
+ const char = css[index];
255
+ if (quote) {
256
+ out += char;
257
+ if (char === '\\' && index + 1 < css.length) {
258
+ out += css[index + 1];
259
+ index += 2;
260
+ continue;
261
+ }
262
+ if (char === quote)
263
+ quote = null;
264
+ index += 1;
265
+ continue;
266
+ }
267
+ if (char === '"' || char === "'") {
268
+ quote = char;
269
+ out += char;
270
+ index += 1;
271
+ continue;
272
+ }
273
+ if (char === '/' && css[index + 1] === '*') {
274
+ const close = css.indexOf('*/', index + 2);
275
+ index = close === -1 ? css.length : close + 2;
276
+ // Keep a space so `a/**/b` does not become the single token `ab`.
277
+ out += ' ';
278
+ continue;
279
+ }
280
+ out += char;
281
+ index += 1;
282
+ }
283
+ return out;
284
+ }
55
285
  function escapeCssString(value) {
56
286
  return value.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n|\r|\f/g, ' ');
57
287
  }
@@ -1 +1 @@
1
- {"version":3,"file":"stylesheetRender.js","sourceRoot":"","sources":["../../../src/components/utils/stylesheetRender.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AAE9D,MAAM,UAAU,wBAAwB,CACvC,eAAwD,EACxD,aAAa,GAAG,4BAA4B;IAE5C,IAAI,CAAC,eAAe;QAAE,OAAO,EAAE,CAAC;IAChC,OAAO,eAAe,CAAC,WAAW;SAChC,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,qBAAqB,CAAC,UAAU,EAAE,aAAa,CAAC,CAAC;SACrE,MAAM,CAAC,OAAO,CAAC;SACf,IAAI,CAAC,IAAI,CAAC,CAAC;AACd,CAAC;AAED,SAAS,qBAAqB,CAAC,UAAoC,EAAE,aAAqB;I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AO,MAAM,CAAC,IAAI,EAAE,CAAC;AACtB,CAAC;AAED,SAAS,0BAA0B,CAAC,UAAoC,EAAE,aAAqB;IAC9F,IAAI,UAAU,CAAC,MAAM,KAAK,UAAU,IAAI,CAAC,UAAU,CAAC,kBAAkB;QAAE,OAAO,aAAa,CAAC;IAC7F,OAAO,GAAG,aAAa,0BAA0B,eAAe,CAAC,UAAU,CAAC,kBAAkB,CAAC,IAAI,CAAC;AACrG,CAAC;AAED,SAAS,iBAAiB,CAAC,QAAgB,EAAE,aAAqB;IACjE,IAAI,CAAC,QAAQ;QAAE,OAAO,EAAE,CAAC;IACzB,IAAI,QAAQ,CAAC,UAAU,CAAC,aAAa,CAAC;QAAE,OAAO,QAAQ,CAAC;IACxD,IAAI,QAAQ,KAAK,OAAO,IAAI,QAAQ,KAAK,MAAM,IAAI,QAAQ,KAAK,MAAM;QAAE,OAAO,aAAa,CAAC;IAC7F,IAAI,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC;QAAE,OAAO,GAAG,aAAa,GAAG,QAAQ,EAAE,CAAC;IACnE,OAAO,GAAG,aAAa,IAAI,QAAQ,EAAE,CAAC;AACvC,CAAC;AAED,SAAS,eAAe,CAAC,KAAa;IACrC,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC;AACpF,CAAC","sourcesContent":["import type { ResolvedItemDeliveryContext, ResolvedQtiStylesheetRef } from '@pie-qti/ims-cp-core';\nimport { isBlockedStylesheetCss } from '@pie-qti/ims-cp-core';\n\nexport function buildScopedStylesheetCss(\n\tdeliveryContext: ResolvedItemDeliveryContext | undefined,\n\tscopeSelector = '[data-qti-item-body-scope]'\n): string {\n\tif (!deliveryContext) return '';\n\treturn deliveryContext.stylesheets\n\t\t.map((stylesheet) => stylesheetToScopedCss(stylesheet, scopeSelector))\n\t\t.filter(Boolean)\n\t\t.join('\\n');\n}\n\nfunction stylesheetToScopedCss(stylesheet: ResolvedQtiStylesheetRef, scopeSelector: string): string {\n\tconst css = stylesheet.cssText?.trim();\n\tif (!css || isBlockedStylesheetCss(css)) return '';\n\tconst stylesheetScope = getStylesheetScopeSelector(stylesheet, scopeSelector);\n\tconst scoped = scopeCssRules(css, stylesheetScope);\n\tif (!scoped) return '';\n\treturn scoped;\n}\n\nexport function scopeCssRules(css: string, scopeSelector = '[data-qti-item-body-scope]'): string {\n\tconst withoutComments = css.replace(/\\/\\*[\\s\\S]*?\\*\\//g, '');\n\tlet scoped = '';\n\tconst rulePattern = /([^{}@]+)\\{([^{}]*)\\}/g;\n\tfor (const match of withoutComments.matchAll(rulePattern)) {\n\t\tconst selectorList = match[1]?.trim();\n\t\tconst declarations = match[2]?.trim();\n\t\tif (!selectorList || !declarations) continue;\n\t\tconst selectors = selectorList\n\t\t\t.split(',')\n\t\t\t.map((selector) => scopeSelectorText(selector.trim(), scopeSelector))\n\t\t\t.filter(Boolean);\n\t\tif (selectors.length === 0) continue;\n\t\tscoped += `${selectors.join(', ')} { ${declarations} }\\n`;\n\t}\n\treturn scoped.trim();\n}\n\nfunction getStylesheetScopeSelector(stylesheet: ResolvedQtiStylesheetRef, scopeSelector: string): string {\n\tif (stylesheet.source !== 'stimulus' || !stylesheet.stimulusIdentifier) return scopeSelector;\n\treturn `${scopeSelector} [data-stimulus-idref=\"${escapeCssString(stylesheet.stimulusIdentifier)}\"]`;\n}\n\nfunction scopeSelectorText(selector: string, scopeSelector: string): string {\n\tif (!selector) return '';\n\tif (selector.startsWith(scopeSelector)) return selector;\n\tif (selector === ':root' || selector === 'html' || selector === 'body') return scopeSelector;\n\tif (selector.startsWith(':')) return `${scopeSelector}${selector}`;\n\treturn `${scopeSelector} ${selector}`;\n}\n\nfunction escapeCssString(value: string): string {\n\treturn value.replace(/\\\\/g, '\\\\\\\\').replace(/\"/g, '\\\\\"').replace(/\\n|\\r|\\f/g, ' ');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AAE,CAAC;gBAC7C,GAAG,IAAI,GAAG,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;gBACtB,KAAK,IAAI,CAAC,CAAC;gBACX,SAAS;YACV,CAAC;YACD,IAAI,IAAI,KAAK,KAAK;gBAAE,KAAK,GAAG,IAAI,CAAC;YACjC,KAAK,IAAI,CAAC,CAAC;YACX,SAAS;QACV,CAAC;QACD,IAAI,IAAI,KAAK,GAAG,IAAI,IAAI,KAAK,GAAG,EAAE,CAAC;YAClC,KAAK,GAAG,IAAI,CAAC;YACb,GAAG,IAAI,IAAI,CAAC;YACZ,KAAK,IAAI,CAAC,CAAC;YACX,SAAS;QACV,CAAC;QACD,IAAI,IAAI,KAAK,GAAG,IAAI,GAAG,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,GAAG,EAAE,CAAC;YAC5C,MAAM,KAAK,GAAG,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;YAC3C,KAAK,GAAG,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC;YAC9C,kEAAkE;YAClE,GAAG,IAAI,GAAG,CAAC;YACX,SAAS;QACV,CAAC;QACD,GAAG,IAAI,IAAI,CAAC;QACZ,KAAK,IAAI,CAAC,CAAC;IACZ,CAAC;IACD,OAAO,GAAG,CAAC;AACZ,CAAC;AAED,SAAS,eAAe,CAAC,KAAa;IACrC,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC;AACpF,CAAC","sourcesContent":["import type { ResolvedItemDeliveryContext, ResolvedQtiStylesheetRef } from '@pie-qti/ims-cp-core';\nimport { isBlockedStylesheetCss } from '@pie-qti/ims-cp-core';\n\nexport function buildScopedStylesheetCss(\n\tdeliveryContext: ResolvedItemDeliveryContext | undefined,\n\tscopeSelector = '[data-qti-item-body-scope]'\n): string {\n\tif (!deliveryContext) return '';\n\treturn deliveryContext.stylesheets\n\t\t.map((stylesheet) => stylesheetToScopedCss(stylesheet, scopeSelector))\n\t\t.filter(Boolean)\n\t\t.join('\\n');\n}\n\nfunction stylesheetToScopedCss(stylesheet: ResolvedQtiStylesheetRef, scopeSelector: string): string {\n\tconst css = stylesheet.cssText?.trim();\n\tif (!css || isBlockedStylesheetCss(css)) return '';\n\tconst stylesheetScope = getStylesheetScopeSelector(stylesheet, scopeSelector);\n\tconst scoped = scopeCssRules(css, stylesheetScope);\n\tif (!scoped) return '';\n\treturn scoped;\n}\n\n/**\n * At-rules whose block contains nested *rules*, so scoping recurses into the\n * block while the prelude is preserved verbatim.\n *\n * Anything not listed here is passed through untouched. That is the safe\n * default both ways: at-rules whose block holds declarations rather than rules\n * (`@font-face`, `@page`, `@property`, `@counter-style`) or non-selector keys\n * (`@keyframes` percentages) must not be scoped, and an at-rule this list has\n * never heard of is more safely left alone than guessed at.\n */\nconst NESTED_RULE_AT_RULES = new Set(['media', 'supports', 'container', 'layer', 'scope']);\n\n/** Matches a leading `:root`, `html` or `body` not followed by more name characters. */\nconst DOCUMENT_ROOT_PREFIX = /^(?::root|html|body)(?![\\w-])/;\n\n/** Matches an at-rule name, with any vendor prefix, at the start of a prelude. */\nconst AT_RULE_NAME = /^@(?:-[a-z]+-)?([\\w-]+)/i;\n\ntype StyleNode =\n\t/** A `;`-terminated at-rule with no block, such as `@layer a;`. */\n\t| { kind: 'statement'; text: string; end: number }\n\t/** A prelude plus a `{ ... }` block: a style rule or a block at-rule. */\n\t| { kind: 'rule'; prelude: string; block: string; end: number }\n\t/** Trailing text with no rule in it, kept so nothing is silently dropped. */\n\t| { kind: 'trailing'; text: string; end: number };\n\n/**\n * Confine every selector in `css` to `scopeSelector`.\n *\n * This walks the stylesheet brace-by-brace. It used to be a single regex over\n * `([^{}@]+)\\{([^{}]*)\\}`, which handled flat selector rules and silently\n * destroyed at-rules. Because `@` was excluded from the selector pattern rather\n * than *understood*, an `@media` block never matched as a unit, so the inner\n * rules matched on their own and were emitted **without the condition** — the\n * media query was dropped and its rules applied unconditionally, so a\n * print-only or narrow-viewport rule took effect everywhere. `@keyframes`\n * degraded the same way, with its percentage selectors scoped into\n * `[scope] 0%` and the animation name discarded, and `@font-face` became\n * `[scope] font-face`.\n *\n * A CSS parser would be the textbook answer and is deliberately not added:\n * scoping needs rule boundaries only, not property grammar, and this runs in\n * the delivery path.\n *\n * Note that `isBlockedStylesheetCss` rejects any stylesheet containing `url(`\n * or `@import` before this is reached, so `@font-face` with a real `src` never\n * arrives here in practice. `@media`, `@supports` and `@keyframes` do.\n */\nexport function scopeCssRules(css: string, scopeSelector = '[data-qti-item-body-scope]'): string {\n\tif (!css) return '';\n\tconst scope = scopeSelector?.trim();\n\tif (!scope) return css;\n\treturn scopeRuleList(stripComments(css), scope).trim();\n}\n\nfunction scopeRuleList(css: string, scope: string): string {\n\tlet out = '';\n\tlet index = 0;\n\twhile (index < css.length) {\n\t\tconst node = readNode(css, index);\n\t\tif (node.end <= index) break;\n\t\tout += renderNode(node, scope);\n\t\tindex = node.end;\n\t}\n\treturn out;\n}\n\nfunction renderNode(node: StyleNode, scope: string): string {\n\tif (node.kind === 'statement') {\n\t\tconst text = node.text.trim();\n\t\treturn text === ';' ? '' : `${text}\\n`;\n\t}\n\tif (node.kind === 'trailing') {\n\t\treturn node.text.trim() ? `${node.text.trim()}\\n` : '';\n\t}\n\tconst prelude = node.prelude.trim();\n\tif (!prelude) return '';\n\tif (prelude.startsWith('@')) {\n\t\tconst name = AT_RULE_NAME.exec(prelude)?.[1]?.toLowerCase();\n\t\tif (name && NESTED_RULE_AT_RULES.has(name)) {\n\t\t\tconst inner = scopeRuleList(node.block, scope);\n\t\t\tif (!inner.trim()) return '';\n\t\t\treturn `${prelude} {\\n${inner}}\\n`;\n\t\t}\n\t\treturn `${prelude} {${node.block}}\\n`;\n\t}\n\t// Rules with no declarations are dropped, as they were before.\n\tconst declarations = node.block.trim();\n\tif (!declarations) return '';\n\tconst selectors = splitTopLevel(prelude, ',')\n\t\t.map((selector) => scopeSelectorText(selector.trim(), scope))\n\t\t.filter(Boolean);\n\tif (selectors.length === 0) return '';\n\treturn `${selectors.join(', ')} { ${declarations} }\\n`;\n}\n\nfunction getStylesheetScopeSelector(stylesheet: ResolvedQtiStylesheetRef, scopeSelector: string): string {\n\tif (stylesheet.source !== 'stimulus' || !stylesheet.stimulusIdentifier) return scopeSelector;\n\treturn `${scopeSelector} [data-stimulus-idref=\"${escapeCssString(stylesheet.stimulusIdentifier)}\"]`;\n}\n\nfunction scopeSelectorText(selector: string, scopeSelector: string): string {\n\tif (!selector) return '';\n\tif (selector === scopeSelector || selector.startsWith(`${scopeSelector} `)) return selector;\n\t// `:root`/`html`/`body` are replaced rather than prefixed: `[scope] :root`\n\t// cannot match, because `:root` is the document element and is not a\n\t// descendant of the item body. Whatever followed is preserved, so\n\t// `html.dark .a` becomes `[scope].dark .a`.\n\tconst rootPrefix = DOCUMENT_ROOT_PREFIX.exec(selector);\n\tif (rootPrefix) return `${scopeSelector}${selector.slice(rootPrefix[0].length)}`;\n\t// Everything else becomes a descendant, including a leading pseudo. This\n\t// used to attach — `[scope]:is(.a, .b)` — which is a different selector: an\n\t// authored `:is(.a, .b) .c` means \"some element matching .a or .b\", so\n\t// attaching demands that the item body itself carry the authored class, and\n\t// it does not. The same holds for `:hover` and `::selection`.\n\treturn `${scopeSelector} ${selector}`;\n}\n\n/**\n * Read one node starting at `start`, tracking strings and parentheses so a `{`\n * inside `content: \"{\"` or a `,` inside `:is(a, b)` is not mistaken for\n * structure.\n */\nfunction readNode(css: string, start: number): StyleNode {\n\tlet index = start;\n\tlet quote: string | null = null;\n\tlet parenDepth = 0;\n\twhile (index < css.length) {\n\t\tconst char = css[index];\n\t\tif (quote) {\n\t\t\tif (char === '\\\\') index += 1;\n\t\t\telse if (char === quote) quote = null;\n\t\t} else if (char === '\"' || char === \"'\") {\n\t\t\tquote = char;\n\t\t} else if (char === '(') {\n\t\t\tparenDepth += 1;\n\t\t} else if (char === ')') {\n\t\t\tif (parenDepth > 0) parenDepth -= 1;\n\t\t} else if (parenDepth === 0 && char === ';') {\n\t\t\treturn { kind: 'statement', text: css.slice(start, index + 1), end: index + 1 };\n\t\t} else if (parenDepth === 0 && char === '{') {\n\t\t\tconst blockEnd = findBlockEnd(css, index);\n\t\t\treturn {\n\t\t\t\tkind: 'rule',\n\t\t\t\tprelude: css.slice(start, index),\n\t\t\t\tblock: css.slice(index + 1, blockEnd.contentEnd),\n\t\t\t\tend: blockEnd.end,\n\t\t\t};\n\t\t}\n\t\tindex += 1;\n\t}\n\treturn { kind: 'trailing', text: css.slice(start), end: css.length };\n}\n\n/**\n * Find the `}` matching the `{` at `openIndex`. Unbalanced input — a truncated\n * stylesheet — is treated as running to the end rather than throwing.\n */\nfunction findBlockEnd(css: string, openIndex: number): { contentEnd: number; end: number } {\n\tlet depth = 0;\n\tlet quote: string | null = null;\n\tfor (let index = openIndex; index < css.length; index += 1) {\n\t\tconst char = css[index];\n\t\tif (quote) {\n\t\t\tif (char === '\\\\') index += 1;\n\t\t\telse if (char === quote) quote = null;\n\t\t\tcontinue;\n\t\t}\n\t\tif (char === '\"' || char === \"'\") {\n\t\t\tquote = char;\n\t\t\tcontinue;\n\t\t}\n\t\tif (char === '{') depth += 1;\n\t\telse if (char === '}') {\n\t\t\tdepth -= 1;\n\t\t\tif (depth === 0) return { contentEnd: index, end: index + 1 };\n\t\t}\n\t}\n\treturn { contentEnd: css.length, end: css.length };\n}\n\n/** Split on `separator` at paren depth zero and outside strings. */\nfunction splitTopLevel(value: string, separator: string): string[] {\n\tconst parts: string[] = [];\n\tlet current = '';\n\tlet quote: string | null = null;\n\tlet parenDepth = 0;\n\tfor (let index = 0; index < value.length; index += 1) {\n\t\tconst char = value[index] as string;\n\t\tif (quote) {\n\t\t\tcurrent += char;\n\t\t\tif (char === '\\\\' && index + 1 < value.length) {\n\t\t\t\tcurrent += value[index + 1];\n\t\t\t\tindex += 1;\n\t\t\t} else if (char === quote) {\n\t\t\t\tquote = null;\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\t\tif (char === '\"' || char === \"'\") {\n\t\t\tquote = char;\n\t\t\tcurrent += char;\n\t\t\tcontinue;\n\t\t}\n\t\tif (char === '(') parenDepth += 1;\n\t\telse if (char === ')' && parenDepth > 0) parenDepth -= 1;\n\t\tif (char === separator && parenDepth === 0) {\n\t\t\tparts.push(current);\n\t\t\tcurrent = '';\n\t\t\tcontinue;\n\t\t}\n\t\tcurrent += char;\n\t}\n\tparts.push(current);\n\treturn parts;\n}\n\n/**\n * Remove comments before walking, so a comment between two rules is not\n * absorbed into the next rule's selector list. String-aware, so a literal\n * `content: \"/*\"` survives — the previous regex strip was not.\n */\nfunction stripComments(css: string): string {\n\tlet out = '';\n\tlet quote: string | null = null;\n\tlet index = 0;\n\twhile (index < css.length) {\n\t\tconst char = css[index] as string;\n\t\tif (quote) {\n\t\t\tout += char;\n\t\t\tif (char === '\\\\' && index + 1 < css.length) {\n\t\t\t\tout += css[index + 1];\n\t\t\t\tindex += 2;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (char === quote) quote = null;\n\t\t\tindex += 1;\n\t\t\tcontinue;\n\t\t}\n\t\tif (char === '\"' || char === \"'\") {\n\t\t\tquote = char;\n\t\t\tout += char;\n\t\t\tindex += 1;\n\t\t\tcontinue;\n\t\t}\n\t\tif (char === '/' && css[index + 1] === '*') {\n\t\t\tconst close = css.indexOf('*/', index + 2);\n\t\t\tindex = close === -1 ? css.length : close + 2;\n\t\t\t// Keep a space so `a/**/b` does not become the single token `ab`.\n\t\t\tout += ' ';\n\t\t\tcontinue;\n\t\t}\n\t\tout += char;\n\t\tindex += 1;\n\t}\n\treturn out;\n}\n\nfunction escapeCssString(value: string): string {\n\treturn value.replace(/\\\\/g, '\\\\\\\\').replace(/\"/g, '\\\\\"').replace(/\\n|\\r|\\f/g, ' ');\n}\n"]}