svelte-highlight 7.12.0 → 7.13.0

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/scoped.js CHANGED
@@ -1,9 +1,9 @@
1
1
  /**
2
- * Scope highlight.js theme CSS under a class name.
3
- * Used by `HighlightStyle` and the docs styles build.
2
+ * Scope highlight.js theme CSS under a class.
3
+ * Used by HighlightStyle and the docs styles build.
4
4
  */
5
5
 
6
- // At-rules whose bodies contain nested style rules and must be recursed into.
6
+ // @media, @supports, etc.: recurse into the block body.
7
7
  const GROUP_AT_RULES = new Set([
8
8
  "media",
9
9
  "supports",
@@ -18,7 +18,6 @@ const AT_RULE_NAME = /^@([a-zA-Z-]+)/;
18
18
  const WHITESPACE = /\s/;
19
19
 
20
20
  /**
21
- * Returns the index just past a string literal starting at `start`.
22
21
  * @param {string} css
23
22
  * @param {number} start
24
23
  */
@@ -38,8 +37,7 @@ function findStringEnd(css, start) {
38
37
  }
39
38
 
40
39
  /**
41
- * Returns the index of the `}` that closes the block whose contents begin at
42
- * `start`, accounting for nested blocks, comments and strings.
40
+ * Index after the `}` that closes the block starting at `start`.
43
41
  * @param {string} css
44
42
  * @param {number} start
45
43
  */
@@ -69,8 +67,7 @@ function findBlockEnd(css, start) {
69
67
  }
70
68
 
71
69
  /**
72
- * Splits a string on a top-level delimiter, ignoring delimiters nested inside
73
- * parentheses, brackets, strings or comments (e.g. commas in `:not(a, b)`).
70
+ * Split on `delim`, skipping nested `()`, `[]`, strings, and comments.
74
71
  * @param {string} str
75
72
  * @param {string} delim
76
73
  */
@@ -110,8 +107,6 @@ function splitTopLevel(str, delim) {
110
107
  }
111
108
 
112
109
  /**
113
- * Splits a single selector into leading trivia (whitespace/comments), its
114
- * significant core, and trailing whitespace, so a transform only sees the core.
115
110
  * @param {string} selector
116
111
  * @returns {[string, string, string]} `[leading, core, trailing]`
117
112
  */
@@ -155,7 +150,7 @@ function rewriteSelectorList(selectorList, transform) {
155
150
  return splitTopLevel(selectorList, ",")
156
151
  .map((selector) => {
157
152
  const [leading, core, trailing] = splitTrivia(selector);
158
- // Empty or whitespace/comments-only. Leave untouched.
153
+ // Whitespace/comments only.
159
154
  if (core === "") return selector;
160
155
  return `${leading}${transform(core)}${trailing}`;
161
156
  })
@@ -173,19 +168,17 @@ function rewriteBlock(prelude, body, transform) {
173
168
  const match = AT_RULE_NAME.exec(trimmed);
174
169
  const name = match?.[1]?.toLowerCase() ?? "";
175
170
  if (GROUP_AT_RULES.has(name)) {
176
- // Conditional group rule (e.g. @media): scope the nested rules.
171
+ // @media etc.: scope nested rules.
177
172
  return `${prelude}{${rewriteRules(body, transform)}}`;
178
173
  }
179
- // @keyframes, @font-face, @page, etc. Do not scope the body.
174
+ // @keyframes, @font-face: leave body alone.
180
175
  return `${prelude}{${body}}`;
181
176
  }
182
177
  return `${rewriteSelectorList(prelude, transform)}{${body}}`;
183
178
  }
184
179
 
185
180
  /**
186
- * Walks a sequence of CSS rules, applying `transform` to each individual
187
- * selector of every style rule while preserving comments, strings and
188
- * at-rules (descending into conditional group rules like `@media`).
181
+ * Walk CSS rules and run `transform` on each selector.
189
182
  * @param {string} css
190
183
  * @param {(selector: string) => string} transform
191
184
  */
@@ -216,7 +209,7 @@ function rewriteRules(css, transform) {
216
209
  continue;
217
210
  }
218
211
  if (ch === ";") {
219
- // Statement at-rule without a block (e.g. `@import`); emit unchanged.
212
+ // Blockless at-rule (@import): emit as-is.
220
213
  out += prelude + ch;
221
214
  prelude = "";
222
215
  i += 1;
@@ -247,17 +240,55 @@ export function scopeSelectors(css, transform) {
247
240
  * @returns {string}
248
241
  */
249
242
  export function scopeStyle(style, scope) {
250
- const scopeSelector = `.${scope}`;
243
+ const body = scopedBody(style, scope);
244
+ const match = STYLE_TAG.exec(style);
245
+ return match ? `${match[1] ?? ""}${body}${match[3] ?? ""}` : body;
246
+ }
247
+
248
+ /**
249
+ * Scope under `.scope`, strip any `<style>` wrapper, return bare CSS.
250
+ * @param {string} style
251
+ * @param {string} scope
252
+ * @param {string} [prefix]
253
+ * @returns {string}
254
+ */
255
+ function scopedBody(style, scope, prefix = "") {
256
+ const scopeSelector = prefix ? `${prefix} .${scope}` : `.${scope}`;
251
257
  /** @type {(selector: string) => string} */
252
258
  const transform = (selector) => `${scopeSelector} ${selector}`;
253
259
  const match = STYLE_TAG.exec(style);
254
- if (match) {
255
- const open = match[1] ?? "";
256
- const css = match[2] ?? "";
257
- const close = match[3] ?? "";
258
- return `${open}${scopeSelectors(css, transform)}${close}`;
260
+ const css = match ? (match[2] ?? "") : style;
261
+ return scopeSelectors(css, transform);
262
+ }
263
+
264
+ /**
265
+ * Build a combined light/dark stylesheet scoped under `.scope`.
266
+ *
267
+ * - `"auto"` wraps each theme in `@media (prefers-color-scheme: …)`.
268
+ * - `"light"` / `"dark"` emit only that single theme.
269
+ * - any other string is treated as a CSS selector that gates the dark block
270
+ * (light stays the default), e.g. `[data-theme="dark"]`.
271
+ *
272
+ * @param {string} light Light theme CSS (optionally `<style>`-wrapped).
273
+ * @param {string} dark Dark theme CSS (optionally `<style>`-wrapped).
274
+ * @param {string} scope Scope class name without a leading `.`.
275
+ * @param {string} [mode] `"auto"` | `"light"` | `"dark"` | a CSS selector.
276
+ * @returns {string}
277
+ */
278
+ export function dualStyle(light, dark, scope, mode = "auto") {
279
+ let css;
280
+ if (mode === "light") {
281
+ css = scopedBody(light, scope);
282
+ } else if (mode === "dark") {
283
+ css = scopedBody(dark, scope);
284
+ } else if (mode === "auto") {
285
+ css =
286
+ `@media (prefers-color-scheme: light){${scopedBody(light, scope)}}` +
287
+ `@media (prefers-color-scheme: dark){${scopedBody(dark, scope)}}`;
288
+ } else {
289
+ css = scopedBody(light, scope) + scopedBody(dark, scope, mode);
259
290
  }
260
- return scopeSelectors(style, transform);
291
+ return `<style>${css}</style>`;
261
292
  }
262
293
 
263
294
  /**