svelte-highlight 7.16.0 → 7.16.2

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.
@@ -39,7 +39,8 @@
39
39
  export let theme = undefined;
40
40
 
41
41
  import { createEventDispatcher, onMount } from "svelte";
42
- import { renderHtml, toRanges } from "./engine.js";
42
+ import { createDomLinePainter } from "./editable-dom-paint.js";
43
+ import { toRanges } from "./engine.js";
43
44
  import {
44
45
  highlightRules,
45
46
  highlightRulesFromPalette,
@@ -49,7 +50,6 @@
49
50
  reparseIncremental,
50
51
  } from "./incremental-tokenize.js";
51
52
  import { ensureRegistered, registry } from "./registry.js";
52
- import { splitLines } from "./split-lines.js";
53
53
  import { diffText } from "./text-diff.js";
54
54
 
55
55
  const dispatch = createEventDispatcher();
@@ -80,6 +80,10 @@
80
80
  /** @type {import("./incremental-tokenize.js").IncrementalParse | undefined} */
81
81
  let incrementalParse;
82
82
 
83
+ // DOM-engine line HTML: pure appends feed a stream session so renderHtml is
84
+ // not re-run over the whole document on every keystroke.
85
+ const domLinePainter = createDomLinePainter({ registry });
86
+
83
87
  function getEvents() {
84
88
  incrementalParse = incrementalParse
85
89
  ? reparseIncremental(registry, language.name, incrementalParse, code)
@@ -123,16 +127,96 @@
123
127
  )}</style>`
124
128
  : "";
125
129
 
130
+ // Sum of textContent.length for el's first `count` children — the
131
+ // character width, within el, of a boundary expressed as a child index.
132
+ function sumChildLengths(el, count) {
133
+ let total = 0;
134
+ for (let i = 0; i < count && i < el.childNodes.length; i++) {
135
+ total += el.childNodes[i].textContent.length;
136
+ }
137
+ return total;
138
+ }
139
+
140
+ // Character offset (same coordinate space as lineStartOffset/setSelection)
141
+ // of the boundary (container, offsetInContainer) from a live Selection
142
+ // Range, computed from integers instead of serializing the document.
143
+ // Exploits the fixed editor structure — one <span> per line (lineEls)
144
+ // joined by literal "\n" text nodes — so a boundary's line is derived
145
+ // from its child position rather than a document-wide walk. Returns null
146
+ // whenever that structure doesn't hold (DOM drifted from lineEls/
147
+ // lineLengths ahead of renderLines' self-heal, or an unrecognized node),
148
+ // so the caller can fall back to the exact cloneRange/toString behavior.
149
+ function offsetOfBoundary(container, offsetInContainer) {
150
+ const expectedChildren = lineEls.length === 0 ? 0 : lineEls.length * 2 - 1;
151
+ if (editor.childNodes.length !== expectedChildren) return null;
152
+
153
+ if (container === editor) {
154
+ if (lineEls.length === 0) return offsetInContainer === 0 ? 0 : null;
155
+ const lineIndex = offsetInContainer >> 1;
156
+ if (lineIndex >= lineEls.length) return null;
157
+ return offsetInContainer % 2 === 0
158
+ ? lineStartOffset(lineIndex)
159
+ : lineStartOffset(lineIndex) + lineLengths[lineIndex];
160
+ }
161
+
162
+ // Walk up to the top-level child of `editor` containing the boundary.
163
+ let node = container;
164
+ while (node.parentNode !== editor) {
165
+ node = node.parentNode;
166
+ if (!node) return null;
167
+ }
168
+
169
+ let childIndex = 0;
170
+ for (let sib = node.previousSibling; sib; sib = sib.previousSibling) {
171
+ childIndex++;
172
+ }
173
+ const lineIndex = childIndex >> 1;
174
+
175
+ if (childIndex % 2 === 1) {
176
+ // The "\n" separator following line `lineIndex`; it has no children,
177
+ // so `container` must be the separator itself.
178
+ if (container !== node || lineIndex >= lineLengths.length) return null;
179
+ return (
180
+ lineStartOffset(lineIndex) + lineLengths[lineIndex] + offsetInContainer
181
+ );
182
+ }
183
+
184
+ const span = node;
185
+ if (lineIndex >= lineEls.length || lineEls[lineIndex] !== span) return null;
186
+
187
+ // Character offset of (container, offsetInContainer) within `span`,
188
+ // walking only its ancestors up to `span` (bounded by that line's size).
189
+ let charOffset =
190
+ container.nodeType === Node.TEXT_NODE
191
+ ? offsetInContainer
192
+ : sumChildLengths(container, offsetInContainer);
193
+ let n = container;
194
+ while (n !== span) {
195
+ for (let sib = n.previousSibling; sib; sib = sib.previousSibling) {
196
+ charOffset += sib.textContent.length;
197
+ }
198
+ n = n.parentNode;
199
+ if (!n) return null;
200
+ }
201
+ return lineStartOffset(lineIndex) + charOffset;
202
+ }
203
+
126
204
  function getSelectionRange() {
127
205
  const selection = window.getSelection();
128
206
  if (!selection || selection.rangeCount === 0) return null;
129
207
  const range = selection.getRangeAt(0);
130
208
  if (!editor.contains(range.commonAncestorContainer)) return null;
209
+ const start = offsetOfBoundary(range.startContainer, range.startOffset);
210
+ const end = offsetOfBoundary(range.endContainer, range.endOffset);
211
+ if (start != null && end != null) return { start, end };
131
212
  const pre = range.cloneRange();
132
213
  pre.selectNodeContents(editor);
133
214
  pre.setEnd(range.startContainer, range.startOffset);
134
- const start = pre.toString().length;
135
- return { start, end: start + range.toString().length };
215
+ const fallbackStart = pre.toString().length;
216
+ return {
217
+ start: fallbackStart,
218
+ end: fallbackStart + range.toString().length,
219
+ };
136
220
  }
137
221
 
138
222
  function getCaretOffset() {
@@ -289,8 +373,23 @@
289
373
  if (textNode) {
290
374
  const lineStart = lineStartOffset(index);
291
375
  const lineEnd = lineStart + lineLengths[index];
292
- for (const token of tokenRanges) {
293
- if (token.end <= lineStart || token.start >= lineEnd) continue;
376
+ // `tokenRanges` is sorted by, and disjoint on, `start` (see the
377
+ // comment on `paintAllLineHighlights`), so `end` is monotonically
378
+ // increasing too. Binary-search the first range that can possibly
379
+ // intersect the line instead of scanning the whole document.
380
+ let lo = 0;
381
+ let hi = tokenRanges.length;
382
+ while (lo < hi) {
383
+ const mid = (lo + hi) >>> 1;
384
+ if (tokenRanges[mid].end <= lineStart) lo = mid + 1;
385
+ else hi = mid;
386
+ }
387
+ for (
388
+ let j = lo;
389
+ j < tokenRanges.length && tokenRanges[j].start < lineEnd;
390
+ j++
391
+ ) {
392
+ const token = tokenRanges[j];
294
393
  const start = Math.max(token.start, lineStart) - lineStart;
295
394
  const end = Math.min(token.end, lineEnd) - lineStart;
296
395
  if (start === end) continue;
@@ -304,14 +403,54 @@
304
403
  lineHighlightRanges[index] = next;
305
404
  }
306
405
 
406
+ // Repaints every line from `tokenRanges` in a single forward sweep rather
407
+ // than rescanning the full (document-sized) range array per line: since
408
+ // `toRanges` emits ranges sorted by, and disjoint on, document offset,
409
+ // `tokenIndex` only ever advances, so total work is O(lines + tokens)
410
+ // instead of O(lines * tokens).
411
+ function paintAllLineHighlights(tokenRanges) {
412
+ let tokenIndex = 0;
413
+ let lineStart = 0;
414
+ for (let i = 0; i < lineEls.length; i++) {
415
+ clearLineHighlights(i);
416
+ const lineEnd = lineStart + lineLengths[i];
417
+ const textNode = lineEls[i].firstChild;
418
+ const next = [];
419
+ if (textNode) {
420
+ while (
421
+ tokenIndex < tokenRanges.length &&
422
+ tokenRanges[tokenIndex].end <= lineStart
423
+ ) {
424
+ tokenIndex++;
425
+ }
426
+ for (
427
+ let j = tokenIndex;
428
+ j < tokenRanges.length && tokenRanges[j].start < lineEnd;
429
+ j++
430
+ ) {
431
+ const token = tokenRanges[j];
432
+ if (token.end <= lineStart) continue;
433
+ const start = Math.max(token.start, lineStart) - lineStart;
434
+ const end = Math.min(token.end, lineEnd) - lineStart;
435
+ if (start === end) continue;
436
+ const range = new Range();
437
+ range.setStart(textNode, start);
438
+ range.setEnd(textNode, end);
439
+ cssHighlightFor(token.scope).add(range);
440
+ next.push({ scope: token.scope, range });
441
+ }
442
+ }
443
+ lineHighlightRanges[i] = next;
444
+ lineStart = lineEnd + 1; // +1 for the "\n" separator
445
+ }
446
+ }
447
+
307
448
  function paintCssHighlights() {
308
449
  const changedIndex = renderLines(code.split("\n"), setText);
309
450
  const tokenRanges = toRanges(getEvents());
310
451
 
311
452
  if (changedIndex == null) {
312
- for (let i = 0; i < lineEls.length; i++) {
313
- paintLineHighlights(i, tokenRanges);
314
- }
453
+ paintAllLineHighlights(tokenRanges);
315
454
  } else {
316
455
  paintLineHighlights(changedIndex, tokenRanges);
317
456
  }
@@ -320,10 +459,11 @@
320
459
 
321
460
  function paint() {
322
461
  if (resolvedEngineValue === "css-highlights") return paintCssHighlights();
323
- const html = renderHtml(getEvents());
324
- // Trailing empty line needs a phantom `\n` for caret placement.
325
- const paintHtml = code === "" || code.endsWith("\n") ? `${html}\n` : html;
326
- return renderLines(splitLines(paintHtml), setHtml);
462
+ const events = getEvents();
463
+ return renderLines(
464
+ domLinePainter.paint(events, code, language.name),
465
+ setHtml,
466
+ );
327
467
  }
328
468
 
329
469
  function renderAt(start, end) {
@@ -350,6 +490,8 @@
350
490
  }
351
491
 
352
492
  function repaintForLanguageChange() {
493
+ incrementalParse = undefined;
494
+ domLinePainter.reset();
353
495
  const caret = document.activeElement === editor ? getCaretOffset() : null;
354
496
  if (caret == null) paint();
355
497
  else renderAt(caret, caret);
@@ -31,6 +31,7 @@
31
31
  import { createEventDispatcher, onMount, tick } from "svelte";
32
32
  import { extendLines } from "./engine.js";
33
33
  import { ensureRegistered, registry } from "./registry.js";
34
+ import { createCompletedHtmlBuffer } from "./stream-highlighted.js";
34
35
 
35
36
  // Lines between sealed chunks. Once a chunk fills, its line spans are
36
37
  // joined into one immutable HTML string and never touched again - keyed
@@ -75,13 +76,15 @@
75
76
  // Sealed (finished, immutable) chunks of `SEAL_CHUNK_LINES` line spans
76
77
  // each, pre-joined into one HTML string apiece - `sealedChunks` is never
77
78
  // mutated in place, only appended to, and past entries are never rebuilt.
78
- // `sealedHtml` mirrors the same sealed prefix as plain (wrapper-free) HTML,
79
- // appended once per sealed chunk, so `highlighted` below is O(tail) per
80
- // repaint instead of rejoining every line streamed so far.
81
79
  /** @type {string[]} */
82
80
  let sealedChunks = [];
83
- let sealedHtml = "";
84
81
  let sealedLineCount = 0;
82
+ // Append-only completed line HTML for the `highlight` event payload.
83
+ // Completed lines are concatenated once as they finalize (O(n) over the
84
+ // stream). Each repaint still assembles `highlighted = completed + preview`
85
+ // so `on:highlight` stays live mid-line; that concat copies the completed
86
+ // string but avoids rebuilding it from sealed DOM chunks.
87
+ const completedHtml = createCompletedHtmlBuffer();
85
88
  // Completed lines not yet folded into a sealed chunk - bounded by
86
89
  // `SEAL_CHUNK_LINES`, so touching it every repaint stays O(1).
87
90
  /** @type {string[]} */
@@ -106,8 +109,8 @@
106
109
  finalizedOpenScopes = [];
107
110
  renderedCommittedCount = 0;
108
111
  sealedChunks = [];
109
- sealedHtml = "";
110
112
  sealedLineCount = 0;
113
+ completedHtml.reset();
111
114
  unsealedLines = [];
112
115
  tailLines = [];
113
116
  }
@@ -119,15 +122,13 @@
119
122
  function sealChunk() {
120
123
  const chunkLines = unsealedLines.slice(0, SEAL_CHUNK_LINES);
121
124
  let domHtml = "";
122
- let plainHtml = "";
123
125
  for (let li = 0; li < chunkLines.length; li++) {
124
126
  const i = sealedLineCount + li;
125
127
  const sep = i > 0 ? "\n" : "";
126
128
  domHtml += `${sep}<span class="highlight-stream-line" data-line="${i}">${chunkLines[li]}</span>`;
127
- plainHtml += sep + chunkLines[li];
128
129
  }
129
- sealedChunks = [...sealedChunks, domHtml];
130
- sealedHtml += plainHtml;
130
+ sealedChunks.push(domHtml);
131
+ sealedChunks = sealedChunks;
131
132
  sealedLineCount += chunkLines.length;
132
133
  unsealedLines = unsealedLines.slice(SEAL_CHUNK_LINES);
133
134
  }
@@ -152,6 +153,7 @@
152
153
  finalizedPendingHtml,
153
154
  );
154
155
  if (result.completedLines.length > 0) {
156
+ completedHtml.appendLines(result.completedLines);
155
157
  unsealedLines = unsealedLines.concat(result.completedLines);
156
158
  while (unsealedLines.length >= SEAL_CHUNK_LINES) sealChunk();
157
159
  }
@@ -174,8 +176,14 @@
174
176
  }
175
177
 
176
178
  tailLines = [...unsealedLines, ...previewLines];
179
+
180
+ // Always assemble a live event payload (including mid-line preview).
181
+ // Trailing empty preview keeps a final `\n` when the stream ends a line.
182
+ const completed = completedHtml.toString();
177
183
  highlighted =
178
- sealedHtml + (sealedLineCount > 0 ? "\n" : "") + tailLines.join("\n");
184
+ completedHtml.lineCount === 0
185
+ ? previewLines.join("\n")
186
+ : `${completed}\n${previewLines.join("\n")}`;
179
187
  }
180
188
 
181
189
  dispatch("highlight", { highlighted });
@@ -27,6 +27,7 @@
27
27
  const HIGHLIGHTED_BACKGROUND = "rgba(254, 241, 96, 0.2)";
28
28
 
29
29
  $: lines = splitLines(highlighted);
30
+ $: highlightedLineSet = new Set(highlightedLines);
30
31
  $: focusMode = highlightedLines.length > 0;
31
32
  $: len_digits = (startingLineNumber + lines.length - 1).toString().length;
32
33
  $: len = len_digits - MIN_DIGITS < 1 ? MIN_DIGITS : len_digits;
@@ -47,7 +48,7 @@
47
48
  <tbody class:hljs={true}>
48
49
  {#each lines as line, i}
49
50
  {@const lineNumber = i + startingLineNumber}
50
- {@const isHighlighted = highlightedLines.includes(i)}
51
+ {@const isHighlighted = highlightedLineSet.has(i)}
51
52
  <tr class:dimmed={focusMode && !isHighlighted}>
52
53
  <td
53
54
  class:hljs={true}
package/Typewriter.svelte CHANGED
@@ -18,7 +18,10 @@
18
18
  export let play = true;
19
19
 
20
20
  import { createEventDispatcher, onMount } from "svelte";
21
- import { tokenizeTypewriter as tokenize } from "./typewriter-units.js";
21
+ import {
22
+ createTypewriterSplitter,
23
+ tokenizeTypewriter as tokenize,
24
+ } from "./typewriter-units.js";
22
25
 
23
26
  const dispatch = createEventDispatcher();
24
27
 
@@ -61,42 +64,6 @@
61
64
  /** The unit currently marked with the caret, if any. @type {HTMLElement | undefined} */
62
65
  let caretMark;
63
66
 
64
- /**
65
- * Split at `count` visible chars. Close open tags in `head`, reopen in `tail`.
66
- * Hidden `tail` reserves space so layout doesn't jump.
67
- */
68
- function split(list, count) {
69
- let head = "";
70
- let shown = 0;
71
- /** @type {{ raw: string; name: string }[]} */
72
- const open = [];
73
- let i = 0;
74
-
75
- for (; i < list.length; i++) {
76
- const unit = list[i];
77
- if (shown >= count) break;
78
- head += unit.raw;
79
- if (unit.visible === 0) {
80
- if (unit.kind === "open")
81
- open.push({ raw: unit.raw, name: unit.name ?? "" });
82
- else if (unit.kind === "close") open.pop();
83
- } else {
84
- shown += unit.visible;
85
- }
86
- }
87
-
88
- // Close open tags in head, reopen them at the start of tail.
89
- let headClose = "";
90
- for (let k = open.length - 1; k >= 0; k--)
91
- headClose += `</${open[k].name}>`;
92
-
93
- let tail = "";
94
- for (const tag of open) tail += tag.raw;
95
- for (; i < list.length; i++) tail += list[i].raw;
96
-
97
- return { head: head + headClose, tail };
98
- }
99
-
100
67
  /** Render every unit once: tags pass through, each visible unit gets a span. */
101
68
  function buildUnitMarkup(list) {
102
69
  let html = "";
@@ -186,6 +153,10 @@
186
153
  }
187
154
 
188
155
  $: units = tokenize(highlighted);
156
+ // `splitter` must be rebuilt whenever `units` does, from the same
157
+ // `highlighted` string `units` was tokenized from -- both are recomputed
158
+ // together in the same reactive flush whenever `highlighted` changes.
159
+ $: splitter = createTypewriterSplitter(units, highlighted);
189
160
  $: total = units.reduce((sum, unit) => sum + unit.visible, 0);
190
161
  $: bigInput = total > UNIT_THRESHOLD;
191
162
  $: useUnitReveal = mounted && !reducedMotion && !bigInput;
@@ -205,11 +176,11 @@
205
176
  sync();
206
177
  }
207
178
 
208
- // SSR / slow-path: full content up front, split() only when actually needed.
179
+ // SSR / slow-path: full content up front, splitter only when actually needed.
209
180
  $: parts = useUnitReveal
210
181
  ? EMPTY_PARTS
211
182
  : mounted
212
- ? split(units, revealed)
183
+ ? splitter.splitAt(revealed)
213
184
  : { head: highlighted, tail: "" };
214
185
  $: showCaret = mounted && !reducedMotion && revealed < total;
215
186
 
@@ -0,0 +1,216 @@
1
+ /**
2
+ * Incremental DOM-engine paint helpers for HighlightEditable.
3
+ *
4
+ * Tokenization via `reparseIncremental` is already incremental, but the
5
+ * default `"dom"` engine still ran `renderHtml` + `splitLines` over the full
6
+ * event stream every keystroke. For pure appends (typing at the end — the
7
+ * common path when growing a document), a dedicated stream session feeds only
8
+ * the new suffix through `extendLines`, so HTML work is O(delta) per keystroke
9
+ * and O(n) total to type a document of size n.
10
+ */
11
+
12
+ import { extendLines, renderHtml } from "./engine.js";
13
+ import { splitLines } from "./split-lines.js";
14
+
15
+ /**
16
+ * @param {string} previousCode
17
+ * @param {string} nextCode
18
+ */
19
+ export function isPureAppend(previousCode, nextCode) {
20
+ return (
21
+ nextCode.length >= previousCode.length && nextCode.startsWith(previousCode)
22
+ );
23
+ }
24
+
25
+ /**
26
+ * Full line-HTML paint via `renderHtml` + `splitLines` (the historical path).
27
+ * @param {import("./engine.d.ts").ScopeEvent[]} events
28
+ * @param {string} code
29
+ * @returns {string[]}
30
+ */
31
+ export function lineHtmlFromEvents(events, code) {
32
+ const html = renderHtml(events);
33
+ const paintHtml = code === "" || code.endsWith("\n") ? `${html}\n` : html;
34
+ return splitLines(paintHtml);
35
+ }
36
+
37
+ /**
38
+ * Build the visible line list from incremental extendLines state, matching
39
+ * `lineHtmlFromEvents` (including the historical phantom trailing newline
40
+ * when `code` is empty or ends in `\n`).
41
+ *
42
+ * @param {string[]} completedLines
43
+ * @param {string[]} previewLines unsealed + pending preview from resume
44
+ * @param {string} code
45
+ * @returns {string[]}
46
+ */
47
+ export function linesFromStreamState(completedLines, previewLines, code) {
48
+ /** @type {string[]} */
49
+ const lines = completedLines.concat(previewLines);
50
+ // Historical paint does `html + "\n"` before splitLines when the source ends
51
+ // on a line boundary, which yields one extra empty caret line beyond the
52
+ // stream-style completed+pending list.
53
+ if (code === "" || code.endsWith("\n")) {
54
+ lines.push("");
55
+ }
56
+ return lines;
57
+ }
58
+
59
+ /**
60
+ * @typedef {import("./engine.d.ts").Registry} Registry
61
+ * @typedef {import("./engine.d.ts").ScopeEvent} ScopeEvent
62
+ * @typedef {import("./engine.d.ts").StreamSession} StreamSession
63
+ */
64
+
65
+ /**
66
+ * Stateful incremental painter driven by pure-append stream sessions.
67
+ *
68
+ * @param {{
69
+ * registry: Registry,
70
+ * }} options
71
+ */
72
+ export function createDomLinePainter({ registry }) {
73
+ /** @type {StreamSession | undefined} */
74
+ let session;
75
+ let sessionLanguage = "";
76
+ let fedCode = "";
77
+ let renderedEventCount = 0;
78
+ /** @type {string[]} */
79
+ let openScopes = [];
80
+ let pendingHtml = "";
81
+ /** @type {string[]} */
82
+ let completedLines = [];
83
+ let lastUsedIncremental = false;
84
+ /** Whether the current session has already accepted at least one append. */
85
+ let hasAppended = false;
86
+ /**
87
+ * Mid-document edits mark the stream session dirty instead of eagerly
88
+ * re-tokenizing. The next pure append pays one full resync, then resumes
89
+ * O(delta) painting.
90
+ */
91
+ let needsResync = false;
92
+
93
+ /**
94
+ * @param {string} nextLanguage
95
+ * @returns {StreamSession}
96
+ */
97
+ function resetSession(nextLanguage) {
98
+ session = registry.createSession(nextLanguage);
99
+ sessionLanguage = nextLanguage;
100
+ fedCode = "";
101
+ renderedEventCount = 0;
102
+ openScopes = [];
103
+ pendingHtml = "";
104
+ completedLines = [];
105
+ hasAppended = false;
106
+ needsResync = false;
107
+ return session;
108
+ }
109
+
110
+ /**
111
+ * Rebuild the stream session from `base` so a subsequent delta append can
112
+ * continue incrementally. Preserves `fedCode` across the reset.
113
+ * @param {string} languageName
114
+ * @param {string} base
115
+ */
116
+ function resyncSession(languageName, base) {
117
+ session = resetSession(languageName);
118
+ if (base.length > 0) {
119
+ session.append(base);
120
+ hasAppended = true;
121
+ consumeCommitted(session.events());
122
+ }
123
+ fedCode = base;
124
+ }
125
+
126
+ /**
127
+ * @param {ScopeEvent[]} committed
128
+ */
129
+ function consumeCommitted(committed) {
130
+ if (!session || committed.length <= renderedEventCount) return;
131
+ const result = extendLines(
132
+ committed.slice(renderedEventCount),
133
+ openScopes,
134
+ pendingHtml,
135
+ );
136
+ if (result.completedLines.length > 0) {
137
+ completedLines = completedLines.concat(result.completedLines);
138
+ }
139
+ openScopes = result.openScopes;
140
+ pendingHtml = result.pendingHtml;
141
+ renderedEventCount = committed.length;
142
+ }
143
+
144
+ /**
145
+ * @param {string} code
146
+ * @returns {string[]}
147
+ */
148
+ function linesForCurrentCode(code) {
149
+ if (!session) return linesFromStreamState([], [""], code);
150
+ const snapshot = session.snapshot();
151
+ /** @type {string[]} */
152
+ let previewLines = [pendingHtml];
153
+ if (snapshot.pos < fedCode.length) {
154
+ const preview = registry.resume(fedCode, sessionLanguage, snapshot);
155
+ const result = extendLines(preview.events, openScopes, pendingHtml);
156
+ previewLines = result.completedLines.concat(result.pendingHtml);
157
+ }
158
+ return linesFromStreamState(completedLines, previewLines, code);
159
+ }
160
+
161
+ return {
162
+ reset() {
163
+ session = undefined;
164
+ sessionLanguage = "";
165
+ fedCode = "";
166
+ renderedEventCount = 0;
167
+ openScopes = [];
168
+ pendingHtml = "";
169
+ completedLines = [];
170
+ lastUsedIncremental = false;
171
+ hasAppended = false;
172
+ needsResync = false;
173
+ },
174
+ lastUsedIncremental() {
175
+ return lastUsedIncremental;
176
+ },
177
+ /**
178
+ * @param {ScopeEvent[]} events Full events from getEvents() — used only
179
+ * for the non-append fallback path (mid-document edits).
180
+ * @param {string} code
181
+ * @param {string} languageName
182
+ * @returns {string[]}
183
+ */
184
+ paint(events, code, languageName) {
185
+ if (session === undefined || languageName !== sessionLanguage) {
186
+ session = resetSession(languageName);
187
+ }
188
+
189
+ if (!isPureAppend(fedCode, code)) {
190
+ lastUsedIncremental = false;
191
+ const lines = lineHtmlFromEvents(events, code);
192
+ // Defer the O(n) stream resync until the next pure append.
193
+ needsResync = true;
194
+ fedCode = code;
195
+ return lines;
196
+ }
197
+
198
+ if (needsResync) {
199
+ resyncSession(languageName, fedCode);
200
+ }
201
+
202
+ const hadContent = hasAppended;
203
+ if (code.length > fedCode.length) {
204
+ session.append(code.slice(fedCode.length));
205
+ fedCode = code;
206
+ hasAppended = true;
207
+ consumeCommitted(session.events());
208
+ } else if (code.length === 0 && fedCode.length === 0) {
209
+ // Empty document paint.
210
+ }
211
+
212
+ lastUsedIncremental = hadContent;
213
+ return linesForCurrentCode(code);
214
+ },
215
+ };
216
+ }
package/engine.js CHANGED
@@ -211,6 +211,13 @@ class Tokenizer {
211
211
  * @type {(MatchCache | undefined)[]}
212
212
  */
213
213
  this.beginCache = [];
214
+ /**
215
+ * Tag names with no `</name` at or after the recorded position. See
216
+ * `hasClosingTag`.
217
+ * @type {Map<string, number>}
218
+ */
219
+ this.noClosingTag = new Map();
220
+ this.noClosingTagCodeLen = 0;
214
221
  }
215
222
 
216
223
  /** @returns {Frame} */
@@ -409,16 +416,18 @@ class Tokenizer {
409
416
  }
410
417
 
411
418
  /**
412
- * Memoized `execValid(re, this.pos, guard)`. Without this, every token
419
+ * Memoized `execValid(re, this.pos, guardFn())`. Without this, every token
413
420
  * boundary re-scans every rule's pattern from scratch. A cached match stays
414
421
  * valid until `this.pos` passes it; a cached miss stays valid until the
415
- * code grows (streaming append).
422
+ * code grows (streaming append). `guardFn` is only called on a cache miss,
423
+ * so guarded rules don't allocate a fresh guard closure on every call once
424
+ * their match is cached.
416
425
  * @param {RegExp} re
417
- * @param {((m: RegExpExecArray) => boolean) | null} guard
426
+ * @param {() => ((m: RegExpExecArray) => boolean) | null} guardFn
418
427
  * @param {MatchCache | undefined} cache
419
428
  * @returns {MatchCache}
420
429
  */
421
- cachedMatch(re, guard, cache) {
430
+ cachedMatch(re, guardFn, cache) {
422
431
  if (cache) {
423
432
  if (cache.match !== null && cache.match.index >= this.pos) return cache;
424
433
  if (cache.match === null && cache.codeLen === this.code.length)
@@ -426,7 +435,7 @@ class Tokenizer {
426
435
  }
427
436
  return {
428
437
  codeLen: this.code.length,
429
- match: this.execValid(re, this.pos, guard),
438
+ match: this.execValid(re, this.pos, guardFn()),
430
439
  };
431
440
  }
432
441
 
@@ -447,6 +456,35 @@ class Tokenizer {
447
456
  return null;
448
457
  }
449
458
 
459
+ /**
460
+ * Whether `</name` occurs at or after `from`.
461
+ *
462
+ * Misses are memoized per tag name, which makes the common "generic, not a
463
+ * tag" verdict cheap: absence from `from` implies absence from every later
464
+ * position, so one scan settles all of that name's later occurrences. Code
465
+ * dense in generics (`Modify<T>`, `Array<T>`) otherwise rescans the whole
466
+ * remaining source per occurrence, which is quadratic in file size.
467
+ *
468
+ * Keyed on `code.length` like `beginCache`: a tokenizer's `code` only ever
469
+ * grows, so an unchanged length means unchanged content, and appended code
470
+ * can supply a closing tag that was legitimately absent before.
471
+ *
472
+ * @param {string} name
473
+ * @param {number} from
474
+ * @returns {boolean}
475
+ */
476
+ hasClosingTag(name, from) {
477
+ if (this.noClosingTagCodeLen !== this.code.length) {
478
+ this.noClosingTag.clear();
479
+ this.noClosingTagCodeLen = this.code.length;
480
+ }
481
+ const absentFrom = this.noClosingTag.get(name);
482
+ if (absentFrom !== undefined && from >= absentFrom) return false;
483
+ if (this.code.indexOf(`</${name}`, from) !== -1) return true;
484
+ this.noClosingTag.set(name, from);
485
+ return false;
486
+ }
487
+
450
488
  /**
451
489
  * hljs javascript/typescript JSX-vs-generic disambiguation for a `<Foo`
452
490
  * match. Re-evaluated when more code arrives so a streaming closing tag
@@ -460,10 +498,7 @@ class Tokenizer {
460
498
  // `Array<Array<number>>`, `<T, A extends keyof T, V>`
461
499
  if (nextChar === "<" || nextChar === ",") return false;
462
500
  // `<something>` - only a tag if a matching closing tag exists later on.
463
- if (
464
- nextChar === ">" &&
465
- this.code.indexOf(`</${match[0].slice(1)}`, afterIdx) === -1
466
- ) {
501
+ if (nextChar === ">" && !this.hasClosingTag(match[0].slice(1), afterIdx)) {
467
502
  return false;
468
503
  }
469
504
  const after = this.code.slice(afterIdx);
@@ -515,7 +550,7 @@ class Tokenizer {
515
550
  const cache = this.cachedMatch(
516
551
  // Every state referenced from a `rules` list has a `begin` pattern.
517
552
  /** @type {RegExp} */ (child.beginRe),
518
- this.beginGuard(child),
553
+ () => this.beginGuard(child),
519
554
  this.beginCache[ruleIdx],
520
555
  );
521
556
  this.beginCache[ruleIdx] = cache;
@@ -527,12 +562,12 @@ class Tokenizer {
527
562
  // d ranges over [1, frames.length - 1], always in bounds.
528
563
  const frame = /** @type {Frame} */ (this.frames[d]);
529
564
  if (frame.state.endRe) {
530
- const guard = frame.state.endSameAsBegin
531
- ? (/** @type {RegExpExecArray} */ m) => m[1] === frame.beginMatch
532
- : null;
533
565
  const cache = this.cachedMatch(
534
566
  frame.state.endRe,
535
- guard,
567
+ () =>
568
+ frame.state.endSameAsBegin
569
+ ? (/** @type {RegExpExecArray} */ m) => m[1] === frame.beginMatch
570
+ : null,
536
571
  frame.endCache,
537
572
  );
538
573
  frame.endCache = cache;
@@ -836,17 +871,29 @@ export function extendLines(
836
871
  const stack = [...openScopes];
837
872
  /** @type {string[]} */
838
873
  const completedLines = [];
839
- const reopenTags = () =>
840
- stack
841
- .map((scope) => `<span class="${scopeToCssClass(scope, classPrefix)}">`)
842
- .join("");
874
+ // Cache the concatenated open/close tag strings for the current stack so a
875
+ // run of newlines between OPEN/CLOSE events reuses them instead of
876
+ // re-joining the whole stack per line break.
877
+ /** @type {string[]} */
878
+ const tags = stack.map(
879
+ (scope) => `<span class="${scopeToCssClass(scope, classPrefix)}">`,
880
+ );
881
+ let reopen = tags.join("");
882
+ let closeAll = "</span>".repeat(stack.length);
843
883
  let current = pendingHtml;
844
884
  for (const event of newEvents) {
845
885
  if (event.t === OPEN) {
846
886
  stack.push(event.s);
847
- current += `<span class="${scopeToCssClass(event.s, classPrefix)}">`;
887
+ const tag = `<span class="${scopeToCssClass(event.s, classPrefix)}">`;
888
+ tags.push(tag);
889
+ reopen += tag;
890
+ closeAll += "</span>";
891
+ current += tag;
848
892
  } else if (event.t === CLOSE) {
849
893
  stack.pop();
894
+ const tag = /** @type {string} */ (tags.pop());
895
+ reopen = reopen.slice(0, reopen.length - tag.length);
896
+ closeAll = closeAll.slice(0, closeAll.length - "</span>".length);
850
897
  current += "</span>";
851
898
  } else {
852
899
  const text = event.v;
@@ -854,9 +901,9 @@ export function extendLines(
854
901
  for (let i = 0; i < text.length; i++) {
855
902
  if (text.charCodeAt(i) === 10) {
856
903
  current += escapeHtml(text.slice(start, i));
857
- current += "</span>".repeat(stack.length);
904
+ current += closeAll;
858
905
  completedLines.push(current);
859
- current = reopenTags();
906
+ current = reopen;
860
907
  start = i + 1;
861
908
  }
862
909
  }
@@ -94,8 +94,16 @@ function stateConverges(a, b) {
94
94
  }
95
95
 
96
96
  /**
97
- * Full parse with a line checkpoint after every line. First paint or language
98
- * change.
97
+ * Default gap between line checkpoints. Matches the spirit of
98
+ * `tokenized-document`'s interval: denser than its windowed default (100)
99
+ * so mid-document edits still resume nearby, sparse enough that a 10k-line
100
+ * file stores O(hundreds) of snapshots rather than O(lines).
101
+ */
102
+ export const CHECKPOINT_INTERVAL = 32;
103
+
104
+ /**
105
+ * Full parse with a line checkpoint every `CHECKPOINT_INTERVAL` lines (and
106
+ * always at the document end). First paint or language change.
99
107
  * @param {Registry} registry
100
108
  * @param {string} language
101
109
  * @param {string} code
@@ -104,8 +112,18 @@ function stateConverges(a, b) {
104
112
  export function parseIncremental(registry, language, code) {
105
113
  const session = registry.createSession(language);
106
114
  const checkpoints = [session.snapshot()];
115
+ let linesSinceCheckpoint = 0;
107
116
  for (const line of splitKeepEnds(code)) {
108
117
  session.append(line);
118
+ linesSinceCheckpoint++;
119
+ if (linesSinceCheckpoint >= CHECKPOINT_INTERVAL) {
120
+ checkpoints.push(session.snapshot());
121
+ linesSinceCheckpoint = 0;
122
+ }
123
+ }
124
+ // Always retain an end checkpoint so resume can land on the final state
125
+ // even when the last interval is incomplete.
126
+ if (linesSinceCheckpoint > 0 || checkpoints.length === 1) {
109
127
  checkpoints.push(session.snapshot());
110
128
  }
111
129
  const { events } = session.finish();
@@ -173,16 +191,19 @@ export function reparseIncremental(registry, language, previous, code) {
173
191
  const newLines = splitKeepEnds(code.slice(resumeCheckpoint.pos));
174
192
  let convergedAtOldIndex = -1;
175
193
  let oldIndex = resumeIndex;
194
+ let linesSinceCheckpoint = 0;
176
195
 
177
- for (const line of newLines) {
178
- session.append(line);
196
+ for (let li = 0; li < newLines.length; li++) {
197
+ session.append(/** @type {string} */ (newLines[li]));
179
198
  const snap = session.snapshot();
180
- // snap.eventCount is session-local; shift to index the combined array.
181
- checkpoints.push({
182
- ...snap,
183
- eventCount: snap.eventCount + prefixEvents.length,
184
- });
185
-
199
+ linesSinceCheckpoint++;
200
+ // Check every line for convergence against previous checkpoints.
201
+ // Store every line for a window right after the edit (typing tends to
202
+ // stay near the cursor, so a follow-up edit here resumes in O(1)
203
+ // instead of walking to the next interval boundary), then fall back to
204
+ // the sparse interval so density doesn't stay O(lines) further out.
205
+ let shouldStore =
206
+ li < CHECKPOINT_INTERVAL || linesSinceCheckpoint >= CHECKPOINT_INTERVAL;
186
207
  if (snap.pos >= newSuffixStart) {
187
208
  const targetOldPos = snap.pos - posOffset;
188
209
  while (
@@ -201,9 +222,19 @@ export function reparseIncremental(registry, language, previous, code) {
201
222
  stateConverges(snap, oldCheckpoint)
202
223
  ) {
203
224
  convergedAtOldIndex = oldIndex;
204
- break;
225
+ shouldStore = true;
205
226
  }
206
227
  }
228
+ if (!shouldStore && li === newLines.length - 1) shouldStore = true;
229
+ if (shouldStore) {
230
+ // snap.eventCount is session-local; shift to index the combined array.
231
+ checkpoints.push({
232
+ ...snap,
233
+ eventCount: snap.eventCount + prefixEvents.length,
234
+ });
235
+ linesSinceCheckpoint = 0;
236
+ }
237
+ if (convergedAtOldIndex >= 0) break;
207
238
  }
208
239
 
209
240
  if (convergedAtOldIndex >= 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-highlight",
3
- "version": "7.16.0",
3
+ "version": "7.16.2",
4
4
  "license": "MIT",
5
5
  "description": "Svelte component library for highlighting code using highlight.js",
6
6
  "author": "Eric Liu (https://github.com/metonym)",
package/preprocessor.js CHANGED
@@ -307,7 +307,10 @@ export function highlightStatic(options = {}) {
307
307
 
308
308
  const htmlByMatch = await Promise.all(
309
309
  matches.map(async (match) => {
310
- const line = locate(content, match.element.start).line;
310
+ // Only ever read by the warn() paths below, and locate() scans
311
+ // `content` from the start, so resolve it lazily: computing it up
312
+ // front made the happy path O(content length) per match.
313
+ const line = () => locate(content, match.element.start).line;
311
314
 
312
315
  /** @type {import("./languages").LanguageType<string>} */
313
316
  let language;
@@ -322,7 +325,7 @@ export function highlightStatic(options = {}) {
322
325
  `could not resolve language module "${match.languageSource}"`,
323
326
  {
324
327
  filename,
325
- line,
328
+ line: line(),
326
329
  cause,
327
330
  },
328
331
  );
@@ -336,7 +339,7 @@ export function highlightStatic(options = {}) {
336
339
  `highlight.js failed to highlight the code (language "${language.name}")`,
337
340
  {
338
341
  filename,
339
- line,
342
+ line: line(),
340
343
  cause,
341
344
  },
342
345
  );
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Append-only buffer for completed (line-finalized) stream HTML.
3
+ *
4
+ * Appending newly completed line HTML is O(n) over the whole stream. Callers
5
+ * still assemble `highlighted = completed + preview` each repaint so the
6
+ * `highlight` event stays live mid-line; that assembly copies the completed
7
+ * string but does not rebuild it from sealed DOM chunks.
8
+ */
9
+
10
+ /**
11
+ * @returns {{
12
+ * appendLines: (lines: string[]) => void,
13
+ * reset: () => void,
14
+ * toString: () => string,
15
+ * lineCount: number,
16
+ * }}
17
+ */
18
+ export function createCompletedHtmlBuffer() {
19
+ let html = "";
20
+ let lineCount = 0;
21
+ return {
22
+ /** @param {string[]} lines */
23
+ appendLines(lines) {
24
+ for (let i = 0; i < lines.length; i++) {
25
+ if (lineCount > 0) html += "\n";
26
+ html += lines[i];
27
+ lineCount++;
28
+ }
29
+ },
30
+ reset() {
31
+ html = "";
32
+ lineCount = 0;
33
+ },
34
+ toString() {
35
+ return html;
36
+ },
37
+ get lineCount() {
38
+ return lineCount;
39
+ },
40
+ };
41
+ }
@@ -11,3 +11,17 @@ export interface TypewriterUnit {
11
11
  * unit (a surrogate pair or an HTML entity counts as a single char).
12
12
  */
13
13
  export declare function tokenizeTypewriter(html: string): TypewriterUnit[];
14
+
15
+ export interface TypewriterSplitter {
16
+ splitAt(count: number): { head: string; tail: string };
17
+ }
18
+
19
+ /**
20
+ * Stateful incremental splitter: repeated `splitAt(count)` calls with a
21
+ * non-decreasing `count` cost O(n) total instead of O(n^2). A `count` lower
22
+ * than the last one served resets and replays from the start.
23
+ */
24
+ export declare function createTypewriterSplitter(
25
+ units: TypewriterUnit[],
26
+ html: string,
27
+ ): TypewriterSplitter;
@@ -71,3 +71,72 @@ export function tokenizeTypewriter(html) {
71
71
 
72
72
  return units;
73
73
  }
74
+
75
+ /**
76
+ * @typedef {Object} TypewriterSplitter
77
+ * @property {(count: number) => { head: string; tail: string }} splitAt
78
+ */
79
+
80
+ /**
81
+ * Stateful incremental version of the old `split(units, count)`: since
82
+ * `units[i].raw` are contiguous, non-overlapping slices exactly partitioning
83
+ * `html` (see `tokenizeTypewriter`), the concatenation of `units[0..i).raw`
84
+ * equals `html.slice(0, rawOffset)` for the matching `rawOffset`, so `head`
85
+ * can be built with a single slice instead of repeated concatenation. A
86
+ * cursor (`i`, `shown`, `rawOffset`, open-tag stack) is kept between calls
87
+ * and only ever advances, so a monotonically increasing sequence of
88
+ * `splitAt(count)` calls costs O(n) total instead of O(n^2). A `count` lower
89
+ * than the last one served resets the cursor and replays from zero.
90
+ * @param {TypewriterUnit[]} units
91
+ * @param {string} html
92
+ * @returns {TypewriterSplitter}
93
+ */
94
+ export function createTypewriterSplitter(units, html) {
95
+ let i = 0;
96
+ let shown = 0;
97
+ let rawOffset = 0;
98
+ /** @type {{ raw: string; name: string }[]} */
99
+ let open = [];
100
+ let lastCount = 0;
101
+
102
+ function reset() {
103
+ i = 0;
104
+ shown = 0;
105
+ rawOffset = 0;
106
+ open = [];
107
+ }
108
+
109
+ /**
110
+ * @param {number} count
111
+ * @returns {{ head: string; tail: string }}
112
+ */
113
+ function splitAt(count) {
114
+ if (count < lastCount) reset();
115
+ lastCount = count;
116
+
117
+ for (; i < units.length; i++) {
118
+ const unit = /** @type {TypewriterUnit} */ (units[i]);
119
+ if (shown >= count) break;
120
+ rawOffset += unit.raw.length;
121
+ if (unit.visible === 0) {
122
+ if (unit.kind === "open")
123
+ open.push({ raw: unit.raw, name: unit.name ?? "" });
124
+ else if (unit.kind === "close") open.pop();
125
+ } else {
126
+ shown += unit.visible;
127
+ }
128
+ }
129
+
130
+ let headClose = "";
131
+ for (let k = open.length - 1; k >= 0; k--)
132
+ headClose += `</${/** @type {{ raw: string; name: string }} */ (open[k]).name}>`;
133
+
134
+ let tail = "";
135
+ for (const tag of open) tail += tag.raw;
136
+ tail += html.slice(rawOffset);
137
+
138
+ return { head: html.slice(0, rawOffset) + headClose, tail };
139
+ }
140
+
141
+ return { splitAt };
142
+ }