svelte-highlight 7.23.2 → 7.24.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.
Files changed (94) hide show
  1. package/AnsiOutput.svelte +33 -6
  2. package/AnsiOutput.svelte.d.ts +8 -0
  3. package/CodeToolbar.svelte +144 -0
  4. package/CodeToolbar.svelte.d.ts +125 -0
  5. package/CodeWindow.svelte +19 -0
  6. package/CodeWindow.svelte.d.ts +10 -0
  7. package/CopyButton.svelte +107 -3
  8. package/CopyButton.svelte.d.ts +17 -1
  9. package/DiffFileList.svelte +523 -0
  10. package/DiffFileList.svelte.d.ts +94 -0
  11. package/DiffFileListItem.svelte +44 -0
  12. package/DiffMinimap.svelte +99 -0
  13. package/DiffMinimap.svelte.d.ts +17 -0
  14. package/DiffStats.svelte +43 -0
  15. package/DiffStats.svelte.d.ts +17 -0
  16. package/DiffView.svelte +1213 -0
  17. package/DiffView.svelte.d.ts +76 -0
  18. package/FileTabs.svelte +2 -5
  19. package/Highlight.svelte +158 -2
  20. package/Highlight.svelte.d.ts +50 -2
  21. package/HighlightAuto.svelte.d.ts +3 -2
  22. package/HighlightAutoLazy.svelte +112 -0
  23. package/HighlightAutoLazy.svelte.d.ts +66 -0
  24. package/HighlightDiff.svelte +162 -0
  25. package/HighlightDiff.svelte.d.ts +102 -0
  26. package/HighlightEditable.svelte +65 -756
  27. package/HighlightEditable.svelte.d.ts +15 -5
  28. package/HighlightStream.svelte +117 -218
  29. package/HighlightStream.svelte.d.ts +8 -0
  30. package/HighlightSvelte.svelte.d.ts +3 -2
  31. package/HighlightVirtual.svelte +41 -8
  32. package/HighlightVirtual.svelte.d.ts +8 -0
  33. package/LangTag.svelte +33 -4
  34. package/LangTag.svelte.d.ts +6 -2
  35. package/LineNumbers.svelte +25 -4
  36. package/LineNumbers.svelte.d.ts +8 -0
  37. package/MarkdownStream.svelte +1 -0
  38. package/README.md +735 -18
  39. package/ToolbarButton.svelte +90 -0
  40. package/ToolbarButton.svelte.d.ts +106 -0
  41. package/Typewriter.svelte +59 -10
  42. package/Typewriter.svelte.d.ts +7 -0
  43. package/WrapToggle.svelte +34 -0
  44. package/WrapToggle.svelte.d.ts +39 -0
  45. package/action.js +7 -3
  46. package/ansi.d.ts +1 -3
  47. package/auto-detect.d.ts +52 -0
  48. package/auto-detect.js +208 -0
  49. package/code-region.d.ts +7 -0
  50. package/code-region.js +20 -0
  51. package/css-highlights.js +115 -0
  52. package/css-walk.js +50 -0
  53. package/diff-controller.d.ts +120 -0
  54. package/diff-controller.js +721 -0
  55. package/diff-edits.d.ts +101 -0
  56. package/diff-edits.js +890 -0
  57. package/diff-html.d.ts +8 -0
  58. package/diff-html.js +60 -0
  59. package/diff-languages.d.ts +4 -0
  60. package/diff-languages.js +73 -0
  61. package/diff.d.ts +161 -0
  62. package/diff.js +1111 -0
  63. package/editable-commands.js +81 -0
  64. package/editable-css-paint.js +5 -3
  65. package/editable-dom-paint.js +1 -1
  66. package/editable-history.js +199 -0
  67. package/editable-line-dom.js +176 -1
  68. package/editable-painter.js +356 -0
  69. package/editable.d.ts +118 -0
  70. package/editable.js +513 -0
  71. package/engine.d.ts +1 -1
  72. package/fence.js +1 -1
  73. package/highlight-cascade.js +679 -0
  74. package/highlight-theme.js +6 -47
  75. package/incremental-tokenize.js +1 -1
  76. package/index.d.ts +39 -6
  77. package/index.js +30 -0
  78. package/languages/detect-index.d.ts +16 -0
  79. package/languages/detect-index.js +6 -0
  80. package/languages/loaders.d.ts +8 -0
  81. package/languages/loaders.js +370 -0
  82. package/package.json +50 -2
  83. package/preprocessor.js +20 -5
  84. package/search.js +2 -2
  85. package/stream-highlighter.js +198 -0
  86. package/stream.css +16 -0
  87. package/stream.d.ts +60 -0
  88. package/stream.js +283 -0
  89. package/style-registry.js +34 -3
  90. package/themes/_base-css.d.ts +1 -0
  91. package/themes/_base-css.js +1 -0
  92. package/typewriter-units.d.ts +3 -0
  93. package/typewriter-units.js +43 -0
  94. package/virtual-window.js +9 -4
@@ -36,15 +36,17 @@ export type HighlightEditableProps = HTMLAttributes<HTMLPreElement> & {
36
36
  * Rendering engine. `"css-highlights"` (experimental) paints tokens via
37
37
  * the CSS Custom Highlight API instead of `<span>`s (Chrome 105+, Safari
38
38
  * 17.2+, Firefox 140+), falling back to `"dom"` elsewhere (see
39
- * `resolvedEngine()`). Colors only, for single-class `.hljs-<scope>` rules.
39
+ * `resolvedEngine()`). Colors only: `::highlight()` ignores
40
+ * `font-weight` and `font-style`, so bold and italic are dropped.
40
41
  * @default "dom"
41
42
  */
42
43
  engine?: "dom" | "css-highlights";
43
44
 
44
45
  /**
45
46
  * Theme CSS (`svelte-highlight/styles/<theme>`) or `ThemePalette`
46
- * (`svelte-highlight/themes/<theme>`) for `"css-highlights"` mode. Only
47
- * `color`/`background-color` of single-class `.hljs-<scope>` rules apply.
47
+ * (`svelte-highlight/themes/<theme>`) for `"css-highlights"` mode. Each
48
+ * token gets the `color` and `background-color` that the theme gives
49
+ * its `<span>` in the `"dom"` engine, compound scopes included.
48
50
  * @example
49
51
  * import a11yDark from "svelte-highlight/styles/a11y-dark";
50
52
  * @example
@@ -52,6 +54,13 @@ export type HighlightEditableProps = HTMLAttributes<HTMLPreElement> & {
52
54
  */
53
55
  theme?: string | ThemePalette;
54
56
 
57
+ /**
58
+ * Accessible name of the editor (`aria-label`). The editor has
59
+ * `role="textbox"`, so name it for what it edits.
60
+ * @default "Code editor"
61
+ */
62
+ label?: string;
63
+
55
64
  /**
56
65
  * Color of the focus outline.
57
66
  * @default "#4589ff"
@@ -129,8 +138,9 @@ export default class HighlightEditable extends SvelteComponentTyped<
129
138
  canRedo(): boolean;
130
139
 
131
140
  /**
132
- * The engine actually in use: `engine`, or `"dom"` if `"css-highlights"`
133
- * was requested but `CSS.highlights` is unavailable.
141
+ * The engine on screen. `"css-highlights"` once it paints. `"dom"`
142
+ * before mount, where `CSS.highlights` is missing, while the color
143
+ * resolver loads, and while that load has failed.
134
144
  */
135
145
  resolvedEngine(): "dom" | "css-highlights";
136
146
  }
@@ -51,35 +51,41 @@
51
51
  */
52
52
  export let doneText = "Code finished streaming";
53
53
 
54
- import { createEventDispatcher, onMount, tick } from "svelte";
55
- import { extendLines } from "./engine.js";
56
- import { ensureRegistered, registry } from "./registry.js";
57
- import { createFinalHighlighter } from "./stream-final-highlight.js";
58
- import { createCompletedHtmlBuffer } from "./stream-highlighted.js";
59
- import { computeStagedTailPreview } from "./stream-preview.js";
60
- import { regenerate } from "./stream-regenerate.js";
61
- import {
62
- buildSealedChunkHtml,
63
- pushSealedChunk,
64
- } from "./stream-sealed-chunks.js";
54
+ /**
55
+ * Accessible name of the scrollable code region. Inside a CodeWindow, the
56
+ * window's `title` or `label` is the default.
57
+ * @type {string | undefined}
58
+ */
59
+ export let label = undefined;
60
+
61
+ import { createEventDispatcher, getContext, onMount, tick } from "svelte";
62
+ import { CODE_REGION, NO_REGION_LABEL } from "./code-region.js";
63
+ import { createStreamView } from "./stream.js";
64
+ import { createStreamHighlighter } from "./stream-highlighter.js";
65
65
  import { createTokenizedDocument } from "./tokenized-document.js";
66
66
  import { watchLineHeight, windowRange } from "./virtual-window.js";
67
67
 
68
- // Full chunks become one immutable HTML string, so the keyed each-block
69
- // only diffs the bounded unsealed tail.
70
- const SEAL_CHUNK_LINES = 256;
71
-
72
68
  const dispatch = createEventDispatcher();
73
69
 
70
+ /** @type {import("svelte/store").Readable<string | undefined>} */
71
+ const windowLabel = getContext(CODE_REGION) ?? NO_REGION_LABEL;
72
+ $: regionName = label ?? $windowLabel ?? "Code";
73
+
74
74
  /** @type {HTMLElement} */
75
75
  let container;
76
76
 
77
+ /** @type {HTMLElement | null} */
78
+ let codeElement = null;
79
+
80
+ /** @type {ReturnType<typeof createStreamView> | undefined} */
81
+ let view;
82
+ /** @type {HTMLElement | null} */
83
+ let viewElement = null;
84
+
85
+ // Server and pre-mount markup only. After mount, `view` owns `<code>`.
77
86
  /** @type {string} */
78
87
  let highlighted = "";
79
88
 
80
- /** @type {ReturnType<typeof requestAnimationFrame> | undefined} */
81
- let frame;
82
-
83
89
  let mounted = false;
84
90
 
85
91
  // Reset when `done` goes false so a restarted stream fires `done` again.
@@ -113,181 +119,45 @@
113
119
  /** @type {number | undefined} */
114
120
  let dispatchedWindowLineCount;
115
121
 
116
- /** @type {ReturnType<typeof registry.createSession> | undefined} */
117
- let session;
118
- let sessionLanguageName = "";
119
- // Prefix of `code` already fed to `session`.
120
- let fedCode = "";
121
-
122
- let finalizedPendingHtml = "";
123
- /** @type {string[]} */
124
- let finalizedOpenScopes = [];
125
- let renderedCommittedCount = 0;
126
- /** @type {import("./stream-preview.d.ts").PreviewCache | undefined} */
127
- let previewCache;
128
-
129
- // Append-only: past entries are never rebuilt.
130
- /** @type {string[]} */
131
- let sealedChunks = [];
132
- let sealedLineCount = 0;
133
- // Completed lines for the `highlight` payload, so it isn't rebuilt per repaint.
134
- const completedHtml = createCompletedHtmlBuffer();
135
- const finalHighlighter = createFinalHighlighter();
136
- // Bounded by SEAL_CHUNK_LINES, so touching it every repaint stays O(1).
137
- /** @type {string[]} */
138
- let unsealedLines = [];
139
- // unsealedLines + the live preview line(s).
140
- /** @type {string[]} */
141
- let tailLines = [];
142
-
143
- function ensureSession() {
144
- if (
145
- session &&
146
- sessionLanguageName === language.name &&
147
- code.startsWith(fedCode)
148
- ) {
149
- return;
150
- }
151
- ensureRegistered(language);
152
- if (session && sessionLanguageName === language.name) {
153
- // Not a pure append: patch in place rather than lose every sealed chunk.
154
- const next = regenerate({
155
- session,
156
- fedCode,
157
- code,
158
- sealedChunks,
159
- completedHtml,
160
- chunkLines: SEAL_CHUNK_LINES,
161
- });
162
- fedCode = code;
163
- previewCache = undefined;
164
- finalizedPendingHtml = next.pendingHtml;
165
- finalizedOpenScopes = next.openScopes;
166
- renderedCommittedCount = next.committedCount;
167
- sealedChunks = next.sealedChunks;
168
- sealedLineCount = next.sealedLineCount;
169
- unsealedLines = next.unsealedLines;
170
- tailLines = unsealedLines;
171
- return;
172
- }
173
- resetStreamingState();
174
- session = registry.createSession(language.name);
175
- sessionLanguageName = language.name;
176
- }
177
-
178
- // Also runs after the done pass, so a closed stream retains only its final
179
- // HTML; resuming re-feeds the whole buffer.
180
- function resetStreamingState() {
181
- session = undefined;
182
- sessionLanguageName = "";
183
- fedCode = "";
184
- finalizedPendingHtml = "";
185
- finalizedOpenScopes = [];
186
- renderedCommittedCount = 0;
187
- previewCache = undefined;
188
- sealedChunks = [];
189
- sealedLineCount = 0;
190
- completedHtml.reset();
191
- unsealedLines = [];
192
- tailLines = [];
193
- }
194
-
195
- function sealChunk() {
196
- const chunkLines = unsealedLines.slice(0, SEAL_CHUNK_LINES);
197
- sealedChunks = pushSealedChunk(
198
- sealedChunks,
199
- buildSealedChunkHtml(chunkLines, sealedLineCount),
200
- );
201
- sealedLineCount += chunkLines.length;
202
- unsealedLines = unsealedLines.slice(SEAL_CHUNK_LINES);
203
- }
204
-
205
- function repaint() {
206
- if (done) {
207
- // Full re-parse for multi-line lookahead (heredocs, etc.).
208
- ensureRegistered(language);
209
- highlighted = finalHighlighter.highlight(registry, code, language.name);
210
- if (session) resetStreamingState();
211
- } else {
212
- ensureSession();
213
- if (code.length > fedCode.length) {
214
- session.append(code.slice(fedCode.length));
215
- fedCode = code;
216
- }
217
-
218
- const committed = session.events();
219
- if (committed.length > renderedCommittedCount) {
220
- const result = extendLines(
221
- committed.slice(renderedCommittedCount),
222
- finalizedOpenScopes,
223
- finalizedPendingHtml,
224
- );
225
- if (result.completedLines.length > 0) {
226
- completedHtml.appendLines(result.completedLines);
227
- unsealedLines = unsealedLines.concat(result.completedLines);
228
- while (unsealedLines.length >= SEAL_CHUNK_LINES) sealChunk();
229
- }
230
- finalizedPendingHtml = result.pendingHtml;
231
- finalizedOpenScopes = result.openScopes;
232
- renderedCommittedCount = committed.length;
233
- }
234
-
235
- const { previewLines, cache } = computeStagedTailPreview({
236
- registry,
237
- language: sessionLanguageName,
238
- session,
239
- fedCode,
240
- openScopes: finalizedOpenScopes,
241
- pendingHtml: finalizedPendingHtml,
242
- cache: previewCache,
243
- });
244
- previewCache = cache;
245
-
246
- tailLines = [...unsealedLines, ...previewLines];
247
-
248
- // A trailing empty preview keeps a final `\n` when the stream ends a line.
249
- const completed = completedHtml.toString();
250
- highlighted =
251
- completedHtml.lineCount === 0
252
- ? previewLines.join("\n")
253
- : `${completed}\n${previewLines.join("\n")}`;
254
- }
122
+ /** @type {ReturnType<typeof createStreamHighlighter> | undefined} */
123
+ let serverHighlighter = createStreamHighlighter();
255
124
 
125
+ function renderBeforeMount() {
126
+ if (!serverHighlighter) return;
127
+ const state = done
128
+ ? serverHighlighter.finish(code, language)
129
+ : serverHighlighter.update(code, language);
130
+ highlighted = state.highlighted;
256
131
  dispatch("highlight", { highlighted });
257
-
258
- if (autoScroll) {
259
- tick().then(() => {
260
- if (stickToBottom) scrollToBottom();
261
- });
262
- }
263
132
  }
264
133
 
265
- function scrollToBottom() {
266
- if (container) container.scrollTop = container.scrollHeight;
134
+ /** @param {HTMLElement | null} element */
135
+ function attachView(element) {
136
+ if (element === viewElement) return;
137
+ view?.destroy();
138
+ view = undefined;
139
+ viewElement = element;
140
+ if (!element) return;
141
+ // The view has its own state from here on.
142
+ serverHighlighter = undefined;
143
+ highlighted = "";
144
+ view = createStreamView(element, {
145
+ code,
146
+ language,
147
+ done,
148
+ caret,
149
+ autoScroll,
150
+ onHighlight: (html) => dispatch("highlight", { highlighted: html }),
151
+ onDone: () => dispatch("done"),
152
+ });
267
153
  }
268
154
 
269
155
  function onScroll() {
270
- if (!container) return;
156
+ if (!container || !virtualize) return;
271
157
  const gap =
272
158
  container.scrollHeight - container.scrollTop - container.clientHeight;
273
159
  stickToBottom = gap <= 4;
274
- if (virtualize) scheduleVirtualRepaint();
275
- }
276
-
277
- function cancelFrame() {
278
- if (frame != null) {
279
- cancelAnimationFrame(frame);
280
- frame = undefined;
281
- }
282
- }
283
-
284
- // Coalesce chunk bursts into one highlight pass per frame.
285
- function scheduleRepaint() {
286
- if (frame != null) return;
287
- frame = requestAnimationFrame(() => {
288
- frame = undefined;
289
- repaint();
290
- });
160
+ scheduleVirtualRepaint();
291
161
  }
292
162
 
293
163
  function ensureVirtualDoc() {
@@ -392,33 +262,30 @@
392
262
  () => probe,
393
263
  () => vLineHeight,
394
264
  (height) => (vLineHeight = height),
265
+ tick,
395
266
  );
396
267
  }
397
268
 
398
- $: {
269
+ // SSR and pre-mount: one synchronous pass for the initial markup.
270
+ $: if (!virtualize && !mounted) {
399
271
  void code;
400
272
  void language;
401
- if (virtualize) {
402
- // Rendering is handled by the blocks below; only dispatch `done` here.
403
- if (mounted && done) {
404
- if (!doneDispatched) {
405
- doneDispatched = true;
406
- dispatch("done");
407
- }
408
- } else {
409
- doneDispatched = false;
410
- }
411
- } else if (mounted && !done) {
412
- doneDispatched = false;
413
- scheduleRepaint();
414
- } else {
415
- // SSR, pre-mount, and final done pass: synchronous, no rAF.
416
- cancelFrame();
417
- repaint();
418
- if (mounted && !doneDispatched) {
273
+ void done;
274
+ renderBeforeMount();
275
+ }
276
+
277
+ $: attachView(virtualize ? null : codeElement);
278
+ $: view?.update({ code, language, done, caret, autoScroll });
279
+
280
+ // Virtual rendering is handled by the blocks below; only dispatch `done`.
281
+ $: if (virtualize) {
282
+ if (mounted && done) {
283
+ if (!doneDispatched) {
419
284
  doneDispatched = true;
420
285
  dispatch("done");
421
286
  }
287
+ } else {
288
+ doneDispatched = false;
422
289
  }
423
290
  }
424
291
 
@@ -443,8 +310,7 @@
443
310
  computeVirtualWindow();
444
311
  }
445
312
 
446
- $: useSplitRendering = mounted && !done;
447
- $: showCaret = useSplitRendering && caret;
313
+ $: showCaret = mounted && !done && caret;
448
314
 
449
315
  onMount(() => {
450
316
  mounted = true;
@@ -459,7 +325,7 @@
459
325
  }
460
326
  }
461
327
  return () => {
462
- cancelFrame();
328
+ view?.destroy();
463
329
  cancelVirtualFrame();
464
330
  resizeObserver?.disconnect();
465
331
  };
@@ -467,27 +333,53 @@
467
333
  </script>
468
334
 
469
335
  {#if virtualize}
336
+ <!-- svelte-ignore a11y-no-noninteractive-tabindex -->
470
337
  <pre
471
338
  bind:this={container}
472
339
  class:hljs={true}
473
340
  class:shl-virtual={true}
341
+ tabindex="0"
342
+ role="region"
343
+ aria-label={regionName}
474
344
  aria-busy={!done}
475
345
  on:scroll={onScroll}
476
- {...$$restProps}
477
- ><code>{#if !mounted}{code}{:else}<span class="shl-virtual-sizer" style="height: {vLineCount * vLineHeight}px;"><span class="shl-virtual-window" style="transform: translateY({vStart * vLineHeight}px);">{#each vVisibleLines as line, i (vStart + i)}<span class="highlight-stream-line" data-line={vStart + i}>{@html line}</span>{#if showCaret && vEnd === vLineCount && i === vVisibleLines.length - 1}<span class="highlight-stream-caret" aria-hidden="true"></span>{/if}{"\n"}{/each}</span></span>{/if}</code><span
478
- bind:this={probe}
479
- class="shl-virtual-probe highlight-stream-line"
480
- aria-hidden="true"
481
- >&nbsp;</span></pre>
346
+ {...$$restProps}><code
347
+ >{#if !mounted}{code}{:else}<span
348
+ class="shl-virtual-sizer"
349
+ style="height: {vLineCount * vLineHeight}px;"
350
+ ><span
351
+ class="shl-virtual-window"
352
+ style="transform: translateY({vStart * vLineHeight}px);"
353
+ >{#each vVisibleLines as line, i (vStart + i)}<span
354
+ class="highlight-stream-line"
355
+ data-line={vStart + i}>{@html line}</span
356
+ >{#if showCaret && vEnd === vLineCount && i === vVisibleLines.length - 1}<span
357
+ class="highlight-stream-caret"
358
+ aria-hidden="true"></span>{/if}{"\n"}{/each}</span
359
+ ></span
360
+ >{/if}</code
361
+ ><span
362
+ bind:this={probe}
363
+ class="shl-virtual-probe highlight-stream-line"
364
+ aria-hidden="true">&nbsp;</span
365
+ ></pre>
482
366
  {:else}
367
+ <!-- svelte-ignore a11y-no-noninteractive-tabindex -->
483
368
  <pre
484
369
  bind:this={container}
485
370
  aria-busy={!done}
486
371
  on:scroll={onScroll}
487
- {...$$restProps}
488
- ><code
489
- class:hljs={true}
490
- >{#if useSplitRendering}{#each sealedChunks as chunk, c (c)}{@html chunk}{/each}{#each tailLines as line, li (sealedLineCount + li)}{#if sealedLineCount + li > 0}{"\n"}{/if}<span class="highlight-stream-line" data-line={sealedLineCount + li}>{@html line}</span>{/each}{#if showCaret}<span class="highlight-stream-caret" aria-hidden="true"></span>{/if}{:else}{@html highlighted}{/if}</code></pre>
372
+ {...$$restProps}>{#if mounted}<code
373
+ bind:this={codeElement}
374
+ class="hljs"
375
+ tabindex="0"
376
+ role="region"
377
+ aria-label={regionName}></code>{:else}<code
378
+ class:hljs={true}
379
+ tabindex="0"
380
+ role="region"
381
+ aria-label={regionName}>{@html highlighted}</code
382
+ >{/if}</pre>
491
383
  {/if}
492
384
 
493
385
  <span class="visually-hidden" role="status" aria-live="polite"
@@ -499,11 +391,17 @@
499
391
  display: block;
500
392
  position: relative;
501
393
  overflow: auto;
502
- /* biome-ignore lint/complexity/noImportantStyles: must beat a consumer inline style, not just cascade order */
394
+ /* Must beat a consumer inline style, not just cascade order. */
503
395
  white-space: pre !important;
504
396
  margin: 0;
505
397
  }
506
398
 
399
+ /* Draw the ring inside, or the scroller clips it in Safari. */
400
+ .shl-virtual:focus-visible,
401
+ code:focus-visible {
402
+ outline-offset: -2px;
403
+ }
404
+
507
405
  .shl-virtual-sizer {
508
406
  display: block;
509
407
  position: relative;
@@ -535,7 +433,8 @@
535
433
  border: 0;
536
434
  }
537
435
 
538
- .highlight-stream-caret {
436
+ /* Global: the stream view creates the caret outside the template. */
437
+ :global(.highlight-stream-caret) {
539
438
  display: inline-block;
540
439
  width: var(--caret-width, 0.6em);
541
440
  height: var(--caret-height, 1.1em);
@@ -545,7 +444,7 @@
545
444
  animation: highlight-stream-blink var(--caret-blink, 1s) step-end infinite;
546
445
  }
547
446
 
548
- @keyframes highlight-stream-blink {
447
+ @keyframes -global-highlight-stream-blink {
549
448
  50% {
550
449
  opacity: 0;
551
450
  }
@@ -60,6 +60,14 @@ export type HighlightStreamProps = HTMLAttributes<HTMLPreElement> & {
60
60
  */
61
61
  doneText?: string;
62
62
 
63
+ /**
64
+ * Accessible name of the scrollable code region. The region is
65
+ * keyboard-focusable so keyboard users can scroll it. Inside a
66
+ * `CodeWindow`, the window's `title` or `label` is the default.
67
+ * @default "Code"
68
+ */
69
+ label?: string;
70
+
63
71
  /**
64
72
  * Width of the blinking caret.
65
73
  * @default "0.6em"
@@ -1,10 +1,11 @@
1
1
  import type { SvelteComponentTyped } from "svelte";
2
2
  import type { HTMLAttributes } from "svelte/elements";
3
3
  import type { ScopeEvent } from "./engine.d.ts";
4
- import type { LangtagProps } from "./Highlight.svelte";
4
+ import type { CodeRegionProps, LangtagProps } from "./Highlight.svelte";
5
5
 
6
6
  export type HighlightSvelteProps = HTMLAttributes<HTMLPreElement> &
7
- LangtagProps & {
7
+ LangtagProps &
8
+ CodeRegionProps & {
8
9
  /** Code to highlight. */
9
10
  code: any;
10
11
  };
@@ -27,12 +27,23 @@
27
27
  */
28
28
  export let tokenizeAhead = false;
29
29
 
30
- import { createEventDispatcher, onMount, tick } from "svelte";
30
+ /**
31
+ * Accessible name of the scrollable code region. Inside a CodeWindow, the
32
+ * window's `title` or `label` is the default.
33
+ * @type {string | undefined}
34
+ */
35
+ export let label = undefined;
36
+
37
+ import { createEventDispatcher, getContext, onMount, tick } from "svelte";
38
+ import { CODE_REGION, NO_REGION_LABEL } from "./code-region.js";
31
39
  import { createTokenizedDocument } from "./tokenized-document.js";
32
40
  import { watchLineHeight, windowRange } from "./virtual-window.js";
33
41
 
34
42
  const dispatch = createEventDispatcher();
35
43
 
44
+ /** @type {import("svelte/store").Readable<string | undefined>} */
45
+ const windowLabel = getContext(CODE_REGION) ?? NO_REGION_LABEL;
46
+
36
47
  /** @type {HTMLElement} */
37
48
  let container;
38
49
 
@@ -154,6 +165,7 @@
154
165
  () => probe,
155
166
  () => lineHeight,
156
167
  (height) => (lineHeight = height),
168
+ tick,
157
169
  );
158
170
  }
159
171
 
@@ -273,28 +285,49 @@
273
285
  });
274
286
  </script>
275
287
 
288
+ <!-- svelte-ignore a11y-no-noninteractive-tabindex -->
276
289
  <pre
277
290
  bind:this={container}
278
291
  class:shl-virtual={true}
279
292
  class:hljs={true}
293
+ tabindex="0"
294
+ role="region"
295
+ aria-label={label ?? $windowLabel ?? "Code"}
280
296
  on:scroll={onScroll}
281
- {...$$restProps}
282
- ><code>{#if !hydrated}{source}{:else}<span class="shl-virtual-sizer" style="height: {lineCount * lineHeight}px;"><span class="shl-virtual-window" style="transform: translateY({start * lineHeight}px);">{#each visibleLines as line, i (start + i)}<span class="shl-virtual-line" data-line={start + i}>{@html line}</span>{"\n"}{/each}</span></span>{/if}</code><span
283
- bind:this={probe}
284
- class="shl-virtual-probe shl-virtual-line"
285
- aria-hidden="true"
286
- >&nbsp;</span></pre>
297
+ {...$$restProps}><code
298
+ >{#if !hydrated}{source}{:else}<span
299
+ class="shl-virtual-sizer"
300
+ style="height: {lineCount * lineHeight}px;"
301
+ ><span
302
+ class="shl-virtual-window"
303
+ style="transform: translateY({start * lineHeight}px);"
304
+ >{#each visibleLines as line, i (start + i)}<span
305
+ class="shl-virtual-line"
306
+ data-line={start + i}>{@html line}</span
307
+ >{"\n"}{/each}</span
308
+ ></span
309
+ >{/if}</code
310
+ ><span
311
+ bind:this={probe}
312
+ class="shl-virtual-probe shl-virtual-line"
313
+ aria-hidden="true">&nbsp;</span
314
+ ></pre>
287
315
 
288
316
  <style>
289
317
  .shl-virtual {
290
318
  display: block;
291
319
  position: relative;
292
320
  overflow: auto;
293
- /* biome-ignore lint/complexity/noImportantStyles: must beat a consumer inline style, not just cascade order */
321
+ /* Must beat a consumer inline style, not just cascade order. */
294
322
  white-space: pre !important;
295
323
  margin: 0;
296
324
  }
297
325
 
326
+ /* Draw the ring inside, or a clipping parent hides it in Safari. */
327
+ .shl-virtual:focus-visible {
328
+ outline-offset: -2px;
329
+ }
330
+
298
331
  .shl-virtual-sizer {
299
332
  display: block;
300
333
  position: relative;
@@ -32,6 +32,14 @@ export type HighlightVirtualProps = HTMLAttributes<HTMLPreElement> & {
32
32
  * @default false
33
33
  */
34
34
  tokenizeAhead?: boolean;
35
+
36
+ /**
37
+ * Accessible name of the scrollable code region. The region is
38
+ * keyboard-focusable so keyboard users can scroll it. Inside a
39
+ * `CodeWindow`, the window's `title` or `label` is the default.
40
+ * @default "Code"
41
+ */
42
+ label?: string;
35
43
  };
36
44
 
37
45
  export type HighlightVirtualEvents = {
package/LangTag.svelte CHANGED
@@ -13,20 +13,44 @@
13
13
 
14
14
  /** @type {boolean} */
15
15
  export let wrap = false;
16
+
17
+ /**
18
+ * Accessible name of the scrollable code region. Inside a CodeWindow, the
19
+ * window's `title` or `label` is the default.
20
+ * @type {string | undefined}
21
+ */
22
+ export let label = undefined;
23
+
24
+ /**
25
+ * Bindable `<code>` element, for callers that paint it after mount
26
+ * (e.g. `Highlight`'s "css-highlights" engine).
27
+ * @type {HTMLElement | undefined}
28
+ */
29
+ export let codeElement = undefined;
30
+
31
+ import { getContext } from "svelte";
32
+ import { CODE_REGION, NO_REGION_LABEL } from "./code-region.js";
33
+
34
+ /** @type {import("svelte/store").Readable<string | undefined>} */
35
+ const windowLabel = getContext(CODE_REGION) ?? NO_REGION_LABEL;
16
36
  </script>
17
37
 
38
+ <!-- svelte-ignore a11y-no-noninteractive-tabindex -->
18
39
  <pre
19
- class:langtag={langtag}
20
- class:wrap={wrap}
40
+ class:langtag
41
+ class:wrap
21
42
  data-language={languageName}
22
43
  style:overflow-x="var(--overflow-x, auto)"
23
44
  style:overflow-y="var(--overflow-y, auto)"
24
45
  style:border-radius="var(--border-radius, 0)"
25
46
  style:width="var(--width, auto)"
26
47
  style:max-width="var(--max-width, none)"
27
- {...$$restProps}
28
- ><code
48
+ {...$$restProps}><code
49
+ bind:this={codeElement}
29
50
  class:hljs={true}
51
+ tabindex="0"
52
+ role="region"
53
+ aria-label={label ?? $windowLabel ?? "Code"}
30
54
  >{#if highlighted}{@html highlighted}{:else}{code}{/if}</code
31
55
  ></pre>
32
56
 
@@ -36,4 +60,9 @@
36
60
  pre {
37
61
  direction: ltr;
38
62
  }
63
+
64
+ /* Draw the ring inside, or the scrolling <pre> clips it in Safari. */
65
+ code:focus-visible {
66
+ outline-offset: -2px;
67
+ }
39
68
  </style>