svelte-highlight 7.22.0 → 7.23.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/ansi.js CHANGED
@@ -132,6 +132,46 @@ function applySgr(style, params) {
132
132
  }
133
133
  }
134
134
 
135
+ /**
136
+ * SGR parameters from `text.slice(start, end)` (the body between `ESC[` and
137
+ * `m`): `;`-separated integers, with an empty part meaning 0.
138
+ *
139
+ * The common all-digits body is parsed in place by char code, skipping the
140
+ * split/map/filter chain's three array allocations per sequence (see
141
+ * bench/ansi.bench.ts). Anything else (sign, `.`, `:`, whitespace, or a part
142
+ * too long to accumulate exactly) falls back to that chain, so `Number()`'s
143
+ * edge cases keep the same result.
144
+ * @param {string} text
145
+ * @param {number} start
146
+ * @param {number} end
147
+ * @returns {number[]}
148
+ */
149
+ function parseSgrParams(text, start, end) {
150
+ /** @type {number[]} */
151
+ const params = [];
152
+ let value = 0;
153
+ let digits = 0;
154
+ for (let k = start; k < end; k += 1) {
155
+ const code = text.charCodeAt(k);
156
+ if (code === 0x3b) {
157
+ params.push(value);
158
+ value = 0;
159
+ digits = 0;
160
+ } else if (code >= 0x30 && code <= 0x39 && digits < 15) {
161
+ value = value * 10 + (code - 0x30);
162
+ digits += 1;
163
+ } else {
164
+ return text
165
+ .slice(start, end)
166
+ .split(";")
167
+ .map((part) => (part === "" ? 0 : Number(part)))
168
+ .filter((n) => Number.isInteger(n));
169
+ }
170
+ }
171
+ params.push(value);
172
+ return params;
173
+ }
174
+
135
175
  /**
136
176
  * Build a segment from `text` and active fields in `style`.
137
177
  * @param {string} text
@@ -161,6 +201,22 @@ function toSegment(text, style, link) {
161
201
 
162
202
  const ESC = "\x1b";
163
203
 
204
+ /**
205
+ * Index of the next ESC or `\r` at or after `from` (or `text.length`):
206
+ * the end of a run of plain text the parsers can copy as-is.
207
+ * @param {string} text
208
+ * @param {number} from
209
+ */
210
+ function plainRunEnd(text, from) {
211
+ let j = from;
212
+ while (j < text.length) {
213
+ const code = text.charCodeAt(j);
214
+ if (code === 0x1b || code === 0x0d) break;
215
+ j += 1;
216
+ }
217
+ return j;
218
+ }
219
+
164
220
  // Schemes allowed as OSC 8 hyperlink targets. Anything else (javascript:,
165
221
  // data:, vbscript:, scheme-less strings) is rejected like an empty uri.
166
222
  const ALLOWED_LINK_SCHEMES = ["http:", "https:", "mailto:"];
@@ -249,12 +305,7 @@ export function parseAnsi(text) {
249
305
  if (final === "m") {
250
306
  // SGR applies to text that follows.
251
307
  flush();
252
- const body = text.slice(i + 2, j);
253
- const params = body
254
- .split(";")
255
- .map((part) => (part === "" ? 0 : Number(part)))
256
- .filter((n) => Number.isInteger(n));
257
- applySgr(style, params);
308
+ applySgr(style, parseSgrParams(text, i + 2, j));
258
309
  }
259
310
  // Non-SGR CSI sequences (cursor moves, etc.) are skipped.
260
311
  i = j + 1;
@@ -358,8 +409,11 @@ export function parseAnsi(text) {
358
409
  continue;
359
410
  }
360
411
 
361
- buffer += ch;
362
- i += 1;
412
+ // Plain text: append the whole run up to the next ESC or \r as one
413
+ // slice instead of one char at a time (see bench/ansi.bench.ts).
414
+ const runEnd = plainRunEnd(text, i + 1);
415
+ buffer += text.slice(i, runEnd);
416
+ i = runEnd;
363
417
  }
364
418
 
365
419
  flush();
@@ -389,6 +443,10 @@ export function createAnsiSession() {
389
443
  // resolve once more input arrived.
390
444
  let pending = "";
391
445
  let finished = false;
446
+ // Lowest index in `segments` that changed since the last delta() call.
447
+ // flush() only ever pushes at or above it, so only resetLine(), which
448
+ // pops and truncates settled segments, has to lower it.
449
+ let changedFrom = 0;
392
450
 
393
451
  const flush = () => {
394
452
  if (buffer) {
@@ -405,15 +463,13 @@ export function createAnsiSession() {
405
463
  }
406
464
  buffer = "";
407
465
  let last = segments.pop();
408
- while (last !== undefined) {
409
- const segmentBreak = last.text.lastIndexOf("\n");
410
- if (segmentBreak === -1) {
411
- last = segments.pop();
412
- continue;
413
- }
414
- last.text = last.text.slice(0, segmentBreak + 1);
466
+ while (last !== undefined && last.text.lastIndexOf("\n") === -1) {
467
+ last = segments.pop();
468
+ }
469
+ if (segments.length < changedFrom) changedFrom = segments.length;
470
+ if (last !== undefined) {
471
+ last.text = last.text.slice(0, last.text.lastIndexOf("\n") + 1);
415
472
  segments.push(last);
416
- return;
417
473
  }
418
474
  };
419
475
 
@@ -451,12 +507,7 @@ export function createAnsiSession() {
451
507
  const final = input[j];
452
508
  if (final === "m") {
453
509
  flush();
454
- const body = input.slice(i + 2, j);
455
- const params = body
456
- .split(";")
457
- .map((part) => (part === "" ? 0 : Number(part)))
458
- .filter((n) => Number.isInteger(n));
459
- applySgr(style, params);
510
+ applySgr(style, parseSgrParams(input, i + 2, j));
460
511
  }
461
512
  i = j + 1;
462
513
  continue;
@@ -561,8 +612,9 @@ export function createAnsiSession() {
561
612
  continue;
562
613
  }
563
614
 
564
- buffer += ch;
565
- i += 1;
615
+ const runEnd = plainRunEnd(input, i + 1);
616
+ buffer += input.slice(i, runEnd);
617
+ i = runEnd;
566
618
  }
567
619
 
568
620
  return "";
@@ -578,6 +630,15 @@ export function createAnsiSession() {
578
630
  if (buffer) result.push(toSegment(buffer, style, link));
579
631
  return result;
580
632
  },
633
+ delta() {
634
+ const start = changedFrom;
635
+ const changed = segments.slice(start);
636
+ if (buffer) changed.push(toSegment(buffer, style, link));
637
+ // The live trailing segment (index `segments.length`) is rebuilt from
638
+ // `buffer` on every call, so the next delta() always re-sends it.
639
+ changedFrom = segments.length;
640
+ return { start, segments: changed };
641
+ },
581
642
  finish() {
582
643
  if (!finished) {
583
644
  scan(pending, true);
package/css-walk.js CHANGED
@@ -64,34 +64,31 @@ export function findBlockEnd(css, start) {
64
64
  export function splitTopLevel(str, delim) {
65
65
  const parts = [];
66
66
  let depth = 0;
67
- let current = "";
67
+ // Each part is the contiguous run since the last delimiter, so it's sliced
68
+ // out once rather than built up a char at a time (see
69
+ // bench/scoped.bench.ts).
70
+ let partStart = 0;
68
71
  let i = 0;
69
72
  while (i < str.length) {
70
73
  const ch = str[i];
71
74
  if (ch === "/" && str[i + 1] === "*") {
72
75
  const end = str.indexOf("*/", i + 2);
73
- const stop = end === -1 ? str.length : end + 2;
74
- current += str.slice(i, stop);
75
- i = stop;
76
+ i = end === -1 ? str.length : end + 2;
76
77
  continue;
77
78
  }
78
79
  if (ch === '"' || ch === "'") {
79
- const stop = findStringEnd(str, i);
80
- current += str.slice(i, stop);
81
- i = stop;
80
+ i = findStringEnd(str, i);
82
81
  continue;
83
82
  }
84
83
  if (ch === "(" || ch === "[") depth += 1;
85
84
  else if (ch === ")" || ch === "]") depth -= 1;
86
85
 
87
86
  if (ch === delim && depth === 0) {
88
- parts.push(current);
89
- current = "";
90
- } else {
91
- current += ch;
87
+ parts.push(str.slice(partStart, i));
88
+ partStart = i + 1;
92
89
  }
93
90
  i += 1;
94
91
  }
95
- parts.push(current);
92
+ parts.push(str.slice(partStart));
96
93
  return parts;
97
94
  }
@@ -0,0 +1,118 @@
1
+ /**
2
+ * CSS Custom Highlight API paint helpers for HighlightEditable's
3
+ * `"css-highlights"` engine, kept DOM-free so they can be benchmarked
4
+ * (see css-paint.bench.ts).
5
+ */
6
+
7
+ import { CLOSE, OPEN, TEXT, toRanges } from "./engine.js";
8
+ import { sharedEvents } from "./incremental-tokenize.js";
9
+
10
+ /**
11
+ * @typedef {import("./engine.d.ts").ScopeEvent} ScopeEvent
12
+ * @typedef {import("./engine.d.ts").TokenRange} TokenRange
13
+ * @typedef {import("./incremental-tokenize.js").EventReuse} EventReuse
14
+ */
15
+
16
+ /**
17
+ * The token ranges painted on one line, `[lineStart, lineEnd)` in document
18
+ * offsets, as offsets relative to `lineStart` - what a single-line repaint
19
+ * registers against that line's text node. Same ranges as `toRanges(events)`
20
+ * clipped to the line, with empty clips dropped.
21
+ * @param {ScopeEvent[]} events
22
+ * @param {number} lineStart
23
+ * @param {number} lineEnd
24
+ * @returns {TokenRange[]}
25
+ */
26
+ export function lineTokenRanges(events, lineStart, lineEnd) {
27
+ // Rather than run `toRanges` over the whole document (one object per
28
+ // token) and clip, walk to the line tracking only the open scopes and
29
+ // the offset, then run `toRanges` over just the line: its scopes
30
+ // reopened, then its events with the first and last text cut to the
31
+ // line. css-paint.bench.ts: ~4x faster at a mid-document line, ~1.8x
32
+ // at the last line, and O(line) instead of O(document) at the first.
33
+ /** @type {ScopeEvent[]} */
34
+ const line = [];
35
+ let offset = 0;
36
+ let i = 0;
37
+ for (; i < events.length; i++) {
38
+ const event = /** @type {ScopeEvent} */ (events[i]);
39
+ if (event.t === OPEN) line.push(event);
40
+ else if (event.t === CLOSE) line.pop();
41
+ else if (offset + event.v.length > lineStart) break;
42
+ else offset += event.v.length;
43
+ }
44
+ for (; i < events.length && offset < lineEnd; i++) {
45
+ const event = /** @type {ScopeEvent} */ (events[i]);
46
+ if (event.t !== TEXT) {
47
+ line.push(event);
48
+ continue;
49
+ }
50
+ const length = event.v.length;
51
+ line.push(
52
+ offset >= lineStart && offset + length <= lineEnd
53
+ ? event
54
+ : {
55
+ t: TEXT,
56
+ v: event.v.slice(
57
+ Math.max(lineStart - offset, 0),
58
+ Math.min(lineEnd - offset, length),
59
+ ),
60
+ },
61
+ );
62
+ offset += length;
63
+ }
64
+ return toRanges(line);
65
+ }
66
+
67
+ /**
68
+ * Where the lines whose token ranges may have changed end, after an edit
69
+ * that starts on the line at offset `from`: the "\n" ending the line where
70
+ * `events` rejoin `previousEvents` (the first of their shared trailing
71
+ * events, see `sharedEvents`), or the end of `code` when they never do or
72
+ * different scopes are open there. Every later line has the same events as
73
+ * before, with the same scopes open at its start, so its ranges still hold.
74
+ * @param {string} code
75
+ * @param {ScopeEvent[]} events
76
+ * @param {ScopeEvent[] | undefined} previousEvents
77
+ * @param {EventReuse | undefined} reuse
78
+ * @param {number} from
79
+ * @returns {number}
80
+ */
81
+ export function retokenizedEnd(code, events, previousEvents, reuse, from) {
82
+ if (previousEvents === undefined) return code.length;
83
+ const { prefix, suffix } = sharedEvents(previousEvents, events, reuse);
84
+ if (suffix === 0) return code.length;
85
+
86
+ // The shared prefix opens the same scopes on both sides, so walk it
87
+ // once, then each side's changed events.
88
+ /** @type {string[]} */
89
+ const scopes = [];
90
+ let offset = 0;
91
+ for (let i = 0; i < prefix; i++) {
92
+ const event = /** @type {ScopeEvent} */ (events[i]);
93
+ if (event.t === OPEN) scopes.push(event.s);
94
+ else if (event.t === CLOSE) scopes.pop();
95
+ else offset += event.v.length;
96
+ }
97
+ const prevScopes = scopes.slice();
98
+ for (let i = prefix; i < events.length - suffix; i++) {
99
+ const event = /** @type {ScopeEvent} */ (events[i]);
100
+ if (event.t === OPEN) scopes.push(event.s);
101
+ else if (event.t === CLOSE) scopes.pop();
102
+ else offset += event.v.length;
103
+ }
104
+ for (let i = prefix; i < previousEvents.length - suffix; i++) {
105
+ const event = /** @type {ScopeEvent} */ (previousEvents[i]);
106
+ if (event.t === OPEN) prevScopes.push(event.s);
107
+ else if (event.t === CLOSE) prevScopes.pop();
108
+ }
109
+ if (
110
+ scopes.length !== prevScopes.length ||
111
+ scopes.some((scope, i) => scope !== prevScopes[i])
112
+ ) {
113
+ return code.length;
114
+ }
115
+
116
+ const newline = code.indexOf("\n", Math.max(offset, from));
117
+ return newline === -1 ? code.length : newline;
118
+ }