@rohal12/spindle 0.64.13 → 0.64.15

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.
@@ -13254,6 +13254,7 @@ var BLOCK_MACROS = new NameSet([
13254
13254
  "do",
13255
13255
  "button",
13256
13256
  "link",
13257
+ "dialog",
13257
13258
  "listbox",
13258
13259
  "cycle",
13259
13260
  "switch",
@@ -13616,41 +13617,6 @@ function lastEnd(body) {
13616
13617
  */
13617
13618
  var NON_STRING_QUOTE_PREFIX = /[\p{L}\p{N}_\\]/u;
13618
13619
  /**
13619
- * Skip the literal opening at `i`: a '…' or "…" string, which must close on
13620
- * its line, or a `…` template, whose `${…}` parts are read as braces.
13621
- * Returns the index just past its closing quote or backtick, or -1.
13622
- */
13623
- function skipLiteral(input, i) {
13624
- const quote = input[i];
13625
- const template = quote === "`";
13626
- let ranOut = false;
13627
- for (let j = i + 1; j < input.length;) {
13628
- const c = input[j];
13629
- if (c === "\\") j += 2;
13630
- else if (c === quote) return {
13631
- end: j + 1,
13632
- ranOut
13633
- };
13634
- else if (!template && c === "\n") return {
13635
- end: -1,
13636
- ranOut
13637
- };
13638
- else if (template && c === "$" && input[j + 1] === "{") {
13639
- const close = lenientRead(input, j + 2);
13640
- ranOut ||= close.ranOut;
13641
- if (close.end === -1) return {
13642
- end: -1,
13643
- ranOut
13644
- };
13645
- j = close.end + 1;
13646
- } else j++;
13647
- }
13648
- return {
13649
- end: -1,
13650
- ranOut: ranOut || template
13651
- };
13652
- }
13653
- /**
13654
13620
  * The closing `}` of braces read leniently from `i` (just past a `{`): braces
13655
13621
  * count except inside string and template literals, and a quote that can't
13656
13622
  * start a string (an apostrophe, or one not closed on its line) is text, as
@@ -13676,31 +13642,71 @@ function lenientRead(input, i) {
13676
13642
  }
13677
13643
  var memoInput = "";
13678
13644
  var memo = /* @__PURE__ */ new Map();
13679
- function scanLenient(input, i) {
13680
- let depth = 0;
13681
- let ranOut = false;
13682
- while (i < input.length) {
13683
- const c = input[i];
13684
- if (c === "{") {
13685
- depth++;
13686
- i++;
13687
- } else if (c === "}") {
13688
- if (depth === 0) return {
13689
- end: i,
13690
- ranOut
13691
- };
13692
- depth--;
13693
- i++;
13694
- } else if (c === "`" || (c === "\"" || c === "'") && !(i > 0 && NON_STRING_QUOTE_PREFIX.test(input[i - 1]))) {
13695
- const close = skipLiteral(input, i);
13696
- ranOut ||= close.ranOut;
13697
- i = close.end === -1 ? i + 1 : close.end;
13698
- } else i++;
13699
- }
13700
- return {
13645
+ function scanLenient(input, start) {
13646
+ const codeFrame = (start) => ({
13647
+ kind: "code",
13648
+ start,
13649
+ i: start,
13650
+ depth: 0,
13651
+ ranOut: false
13652
+ });
13653
+ const frames = [codeFrame(start)];
13654
+ let result = {
13701
13655
  end: -1,
13702
13656
  ranOut: true
13703
13657
  };
13658
+ const finish = (end, ranOut) => {
13659
+ const frame = frames.pop();
13660
+ result = {
13661
+ end,
13662
+ ranOut
13663
+ };
13664
+ if (frame.kind === "code") memo.set(frame.start, result);
13665
+ const parent = frames[frames.length - 1];
13666
+ if (!parent) return;
13667
+ parent.ranOut ||= ranOut;
13668
+ if (parent.kind === "code") parent.i = end === -1 ? parent.i + 1 : end;
13669
+ else if (end === -1) parent.i = input.length;
13670
+ else parent.i = end + 1;
13671
+ };
13672
+ while (frames.length) {
13673
+ const frame = frames[frames.length - 1];
13674
+ const { i, ranOut } = frame;
13675
+ if (i >= input.length) {
13676
+ finish(-1, ranOut || frame.kind === "code" || frame.quote === "`");
13677
+ continue;
13678
+ }
13679
+ const c = input[i];
13680
+ if (frame.kind === "literal") {
13681
+ if (c === "\\") frame.i += 2;
13682
+ else if (c === frame.quote) finish(i + 1, ranOut);
13683
+ else if (frame.quote !== "`" && c === "\n") finish(-1, ranOut);
13684
+ else if (frame.quote === "`" && c === "$" && input[i + 1] === "{") {
13685
+ const cached = memo.get(i + 2);
13686
+ if (cached) {
13687
+ frame.ranOut ||= cached.ranOut;
13688
+ frame.i = cached.end === -1 ? input.length : cached.end + 1;
13689
+ } else frames.push(codeFrame(i + 2));
13690
+ } else frame.i++;
13691
+ } else if (c === "{") {
13692
+ frame.depth++;
13693
+ frame.i++;
13694
+ } else if (c === "}") {
13695
+ if (frame.depth === 0) finish(i, ranOut);
13696
+ else {
13697
+ frame.depth--;
13698
+ frame.i++;
13699
+ }
13700
+ } else if (c === "`" || (c === "\"" || c === "'") && !(i > 0 && NON_STRING_QUOTE_PREFIX.test(input[i - 1]))) frames.push({
13701
+ kind: "literal",
13702
+ start: i,
13703
+ i: i + 1,
13704
+ quote: c,
13705
+ ranOut: false
13706
+ });
13707
+ else frame.i++;
13708
+ }
13709
+ return result;
13704
13710
  }
13705
13711
  /**
13706
13712
  * The JavaScript reader Spindle uses: js-lexer, then a lenient scan. With
@@ -24754,6 +24760,7 @@ function useDetachedBody() {
24754
24760
  mounted.current.splice(at, 1);
24755
24761
  render(null, container);
24756
24762
  } : null;
24763
+ mounted.current.push(container);
24757
24764
  render(/* @__PURE__ */ jsx(FrozenStateContext.Provider, {
24758
24765
  value: frozenAlways,
24759
24766
  children: /* @__PURE__ */ jsx(LocalsUpdateContext.Provider, {
@@ -24799,7 +24806,6 @@ function useDetachedBody() {
24799
24806
  })
24800
24807
  })
24801
24808
  }), container);
24802
- mounted.current.push(container);
24803
24809
  };
24804
24810
  return (children) => runWithCommittedMutations(() => run(children));
24805
24811
  }
@@ -3633,6 +3633,7 @@ var BLOCK_MACROS = new NameSet([
3633
3633
  "do",
3634
3634
  "button",
3635
3635
  "link",
3636
+ "dialog",
3636
3637
  "listbox",
3637
3638
  "cycle",
3638
3639
  "switch",
@@ -10182,41 +10183,6 @@ function completionIdentifier(source, cursor, goal = "expression") {
10182
10183
  */
10183
10184
  var NON_STRING_QUOTE_PREFIX = /[\p{L}\p{N}_\\]/u;
10184
10185
  /**
10185
- * Skip the literal opening at `i`: a '…' or "…" string, which must close on
10186
- * its line, or a `…` template, whose `${…}` parts are read as braces.
10187
- * Returns the index just past its closing quote or backtick, or -1.
10188
- */
10189
- function skipLiteral(input, i) {
10190
- const quote = input[i];
10191
- const template = quote === "`";
10192
- let ranOut = false;
10193
- for (let j = i + 1; j < input.length;) {
10194
- const c = input[j];
10195
- if (c === "\\") j += 2;
10196
- else if (c === quote) return {
10197
- end: j + 1,
10198
- ranOut
10199
- };
10200
- else if (!template && c === "\n") return {
10201
- end: -1,
10202
- ranOut
10203
- };
10204
- else if (template && c === "$" && input[j + 1] === "{") {
10205
- const close = lenientRead(input, j + 2);
10206
- ranOut || (ranOut = close.ranOut);
10207
- if (close.end === -1) return {
10208
- end: -1,
10209
- ranOut
10210
- };
10211
- j = close.end + 1;
10212
- } else j++;
10213
- }
10214
- return {
10215
- end: -1,
10216
- ranOut: ranOut || template
10217
- };
10218
- }
10219
- /**
10220
10186
  * The closing `}` of braces read leniently from `i` (just past a `{`): braces
10221
10187
  * count except inside string and template literals, and a quote that can't
10222
10188
  * start a string (an apostrophe, or one not closed on its line) is text, as
@@ -10242,31 +10208,71 @@ function lenientRead(input, i) {
10242
10208
  }
10243
10209
  var memoInput = "";
10244
10210
  var memo = /* @__PURE__ */ new Map();
10245
- function scanLenient(input, i) {
10246
- let depth = 0;
10247
- let ranOut = false;
10248
- while (i < input.length) {
10249
- const c = input[i];
10250
- if (c === "{") {
10251
- depth++;
10252
- i++;
10253
- } else if (c === "}") {
10254
- if (depth === 0) return {
10255
- end: i,
10256
- ranOut
10257
- };
10258
- depth--;
10259
- i++;
10260
- } else if (c === "`" || (c === "\"" || c === "'") && !(i > 0 && NON_STRING_QUOTE_PREFIX.test(input[i - 1]))) {
10261
- const close = skipLiteral(input, i);
10262
- ranOut || (ranOut = close.ranOut);
10263
- i = close.end === -1 ? i + 1 : close.end;
10264
- } else i++;
10265
- }
10266
- return {
10211
+ function scanLenient(input, start) {
10212
+ const codeFrame = (start) => ({
10213
+ kind: "code",
10214
+ start,
10215
+ i: start,
10216
+ depth: 0,
10217
+ ranOut: false
10218
+ });
10219
+ const frames = [codeFrame(start)];
10220
+ let result = {
10267
10221
  end: -1,
10268
10222
  ranOut: true
10269
10223
  };
10224
+ const finish = (end, ranOut) => {
10225
+ const frame = frames.pop();
10226
+ result = {
10227
+ end,
10228
+ ranOut
10229
+ };
10230
+ if (frame.kind === "code") memo.set(frame.start, result);
10231
+ const parent = frames[frames.length - 1];
10232
+ if (!parent) return;
10233
+ parent.ranOut || (parent.ranOut = ranOut);
10234
+ if (parent.kind === "code") parent.i = end === -1 ? parent.i + 1 : end;
10235
+ else if (end === -1) parent.i = input.length;
10236
+ else parent.i = end + 1;
10237
+ };
10238
+ while (frames.length) {
10239
+ const frame = frames[frames.length - 1];
10240
+ const { i, ranOut } = frame;
10241
+ if (i >= input.length) {
10242
+ finish(-1, ranOut || frame.kind === "code" || frame.quote === "`");
10243
+ continue;
10244
+ }
10245
+ const c = input[i];
10246
+ if (frame.kind === "literal") {
10247
+ if (c === "\\") frame.i += 2;
10248
+ else if (c === frame.quote) finish(i + 1, ranOut);
10249
+ else if (frame.quote !== "`" && c === "\n") finish(-1, ranOut);
10250
+ else if (frame.quote === "`" && c === "$" && input[i + 1] === "{") {
10251
+ const cached = memo.get(i + 2);
10252
+ if (cached) {
10253
+ frame.ranOut || (frame.ranOut = cached.ranOut);
10254
+ frame.i = cached.end === -1 ? input.length : cached.end + 1;
10255
+ } else frames.push(codeFrame(i + 2));
10256
+ } else frame.i++;
10257
+ } else if (c === "{") {
10258
+ frame.depth++;
10259
+ frame.i++;
10260
+ } else if (c === "}") {
10261
+ if (frame.depth === 0) finish(i, ranOut);
10262
+ else {
10263
+ frame.depth--;
10264
+ frame.i++;
10265
+ }
10266
+ } else if (c === "`" || (c === "\"" || c === "'") && !(i > 0 && NON_STRING_QUOTE_PREFIX.test(input[i - 1]))) frames.push({
10267
+ kind: "literal",
10268
+ start: i,
10269
+ i: i + 1,
10270
+ quote: c,
10271
+ ranOut: false
10272
+ });
10273
+ else frame.i++;
10274
+ }
10275
+ return result;
10270
10276
  }
10271
10277
  /**
10272
10278
  * The JavaScript reader Spindle uses: js-lexer, then a lenient scan. With
@@ -13558,7 +13564,7 @@ var Bindings = class {
13558
13564
  innermostVisible(bindings, at) {
13559
13565
  const useFunction = this.functionStart(at);
13560
13566
  const visible = bindings.filter((b) => b.scope.start <= at && at < b.scope.end && (!b.ordered || b.at < at || this.functionStart(b.at) !== useFunction));
13561
- const innermost = Math.max(...visible.map((b) => b.scope.start));
13567
+ const innermost = visible.reduce((start, b) => Math.max(start, b.scope.start), -Infinity);
13562
13568
  return visible.filter((b) => b.scope.start === innermost);
13563
13569
  }
13564
13570
  readGroups() {
@@ -25657,6 +25663,14 @@ async function formatLf(text, options) {
25657
25663
  }
25658
25664
  /** Literal HTML and opaque SVG, whose text can inherit significant whitespace. */
25659
25665
  var WHITESPACE_AS_WRITTEN = /<(?:pre|textarea|svg)[\s>]|white-space\s*:/i;
25666
+ /** JavaScript MIME types (including legacy aliases) and module scripts. */
25667
+ var JAVASCRIPT_TYPE = /^(?:module|(?:text|application)\/(?:x-)?(?:java|ecma)script|text\/(?:javascript1\.[0-5]|jscript|livescript))$/i;
25668
+ /** Data scripts have opaque bodies, even inside a larger HTML region. */
25669
+ function isDataScript(token) {
25670
+ if (token.tag.toLowerCase() !== "script") return false;
25671
+ const type = Object.entries(token.attributes).find(([name]) => name.toLowerCase() === "type")?.[1].trim();
25672
+ return !!type && !JAVASCRIPT_TYPE.test(type);
25673
+ }
25660
25674
  /** HTML whitespace formatting must not collapse the Markdown in its bodies. */
25661
25675
  var HTML_MARKDOWN_BOUNDARIES = /* @__PURE__ */ new Set([
25662
25676
  "hardBreakTrailing",
@@ -25684,13 +25698,13 @@ function hasMarkdownBoundaries(source, isBlock) {
25684
25698
  /** Preserve whole literal elements, including openers beside nested tags. */
25685
25699
  function whitespaceSensitiveLines(source, isBlock) {
25686
25700
  const lines = /* @__PURE__ */ new Set();
25687
- if (!WHITESPACE_AS_WRITTEN.test(source)) return lines;
25701
+ if (!WHITESPACE_AS_WRITTEN.test(source) && !/<script[\s>]/i.test(source)) return lines;
25688
25702
  const lineAt = sourceLines(source);
25689
25703
  const { nodes } = analyzeMarkup(source, { hooks: { isBlock } });
25690
25704
  function visit(nodes) {
25691
25705
  for (const node of nodes) {
25692
25706
  const { token, body } = node;
25693
- if (token.type === "html" && ([
25707
+ if (token.type === "html" && (isDataScript(token) || [
25694
25708
  "pre",
25695
25709
  "textarea",
25696
25710
  "svg"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rohal12/spindle",
3
- "version": "0.64.13",
3
+ "version": "0.64.15",
4
4
  "type": "module",
5
5
  "description": "A Preact-based story format for Twine 2.",
6
6
  "license": "Unlicense",
@@ -149,6 +149,9 @@ export function useDetachedBody(): (children: ASTNode[]) => void {
149
149
  render(null, container);
150
150
  }
151
151
  : null;
152
+ // Replacement can happen synchronously inside render (for example {do}).
153
+ // Register first so that cancellation also sees the body doing it.
154
+ mounted.current.push(container);
152
155
  render(
153
156
  <FrozenStateContext.Provider value={frozenAlways}>
154
157
  <LocalsUpdateContext.Provider value={updater}>
@@ -185,7 +188,6 @@ export function useDetachedBody(): (children: ASTNode[]) => void {
185
188
  </FrozenStateContext.Provider>,
186
189
  container,
187
190
  );
188
- mounted.current.push(container);
189
191
  };
190
192
  return (children) => runWithCommittedMutations(() => run(children));
191
193
  }
@@ -1,7 +1,7 @@
1
1
  import { isBlockMacro } from '../markup/ast';
2
2
  import { analyzeMarkup, tokenizeMarkupTolerant } from '../markup/parse';
3
3
  import type { PairedNode } from '../markup/pair';
4
- import type { MacroToken } from '../markup/tokens';
4
+ import type { HtmlToken, MacroToken } from '../markup/tokens';
5
5
  import { segmentRegions } from './segment';
6
6
  import { markdownLines } from './markdown-lines';
7
7
  import { sourceLines } from './source-lines';
@@ -379,6 +379,19 @@ async function formatLf(
379
379
  /** Literal HTML and opaque SVG, whose text can inherit significant whitespace. */
380
380
  const WHITESPACE_AS_WRITTEN = /<(?:pre|textarea|svg)[\s>]|white-space\s*:/i;
381
381
 
382
+ /** JavaScript MIME types (including legacy aliases) and module scripts. */
383
+ const JAVASCRIPT_TYPE =
384
+ /^(?:module|(?:text|application)\/(?:x-)?(?:java|ecma)script|text\/(?:javascript1\.[0-5]|jscript|livescript))$/i;
385
+
386
+ /** Data scripts have opaque bodies, even inside a larger HTML region. */
387
+ function isDataScript(token: HtmlToken): boolean {
388
+ if (token.tag.toLowerCase() !== 'script') return false;
389
+ const type = Object.entries(token.attributes)
390
+ .find(([name]) => name.toLowerCase() === 'type')?.[1]
391
+ .trim();
392
+ return !!type && !JAVASCRIPT_TYPE.test(type);
393
+ }
394
+
382
395
  /** HTML whitespace formatting must not collapse the Markdown in its bodies. */
383
396
  const HTML_MARKDOWN_BOUNDARIES = new Set([
384
397
  'hardBreakTrailing',
@@ -420,7 +433,8 @@ function whitespaceSensitiveLines(
420
433
  isBlock: (name: string) => boolean,
421
434
  ): Set<number> {
422
435
  const lines = new Set<number>();
423
- if (!WHITESPACE_AS_WRITTEN.test(source)) return lines;
436
+ if (!WHITESPACE_AS_WRITTEN.test(source) && !/<script[\s>]/i.test(source))
437
+ return lines;
424
438
  const lineAt = sourceLines(source);
425
439
  const { nodes } = analyzeMarkup(source, { hooks: { isBlock } });
426
440
  function visit(nodes: PairedNode[]): void {
@@ -428,7 +442,8 @@ function whitespaceSensitiveLines(
428
442
  const { token, body } = node;
429
443
  if (
430
444
  token.type === 'html' &&
431
- (['pre', 'textarea', 'svg'].includes(token.tag.toLowerCase()) ||
445
+ (isDataScript(token) ||
446
+ ['pre', 'textarea', 'svg'].includes(token.tag.toLowerCase()) ||
432
447
  Object.entries(token.attributes).some(
433
448
  ([name, value]) =>
434
449
  name.toLowerCase() === 'style' && /white-space\s*:/i.test(value),
@@ -152,7 +152,10 @@ export class Bindings {
152
152
  at < b.scope.end &&
153
153
  (!b.ordered || b.at < at || this.functionStart(b.at) !== useFunction),
154
154
  );
155
- const innermost = Math.max(...visible.map((b) => b.scope.start));
155
+ const innermost = visible.reduce(
156
+ (start, b) => Math.max(start, b.scope.start),
157
+ -Infinity,
158
+ );
156
159
  return visible.filter((b) => b.scope.start === innermost);
157
160
  }
158
161
 
package/src/markup/ast.ts CHANGED
@@ -55,6 +55,7 @@ const BLOCK_MACROS = new NameSet([
55
55
  'do',
56
56
  'button',
57
57
  'link',
58
+ 'dialog',
58
59
  'listbox',
59
60
  'cycle',
60
61
  'switch',
@@ -35,30 +35,6 @@ interface Lenient {
35
35
  ranOut: boolean;
36
36
  }
37
37
 
38
- /**
39
- * Skip the literal opening at `i`: a '…' or "…" string, which must close on
40
- * its line, or a `…` template, whose `${…}` parts are read as braces.
41
- * Returns the index just past its closing quote or backtick, or -1.
42
- */
43
- function skipLiteral(input: string, i: number): Lenient {
44
- const quote = input[i];
45
- const template = quote === '`';
46
- let ranOut = false;
47
- for (let j = i + 1; j < input.length;) {
48
- const c = input[j];
49
- if (c === '\\') j += 2;
50
- else if (c === quote) return { end: j + 1, ranOut };
51
- else if (!template && c === '\n') return { end: -1, ranOut };
52
- else if (template && c === '$' && input[j + 1] === '{') {
53
- const close = lenientRead(input, j + 2);
54
- ranOut ||= close.ranOut;
55
- if (close.end === -1) return { end: -1, ranOut };
56
- j = close.end + 1;
57
- } else j++;
58
- }
59
- return { end: -1, ranOut: ranOut || template };
60
- }
61
-
62
38
  /**
63
39
  * The closing `}` of braces read leniently from `i` (just past a `{`): braces
64
40
  * count except inside string and template literals, and a quote that can't
@@ -94,31 +70,83 @@ function lenientRead(input: string, i: number): Lenient {
94
70
  let memoInput = '';
95
71
  const memo = new Map<number, Lenient>();
96
72
 
97
- function scanLenient(input: string, i: number): Lenient {
98
- let depth = 0;
99
- let ranOut = false;
100
- while (i < input.length) {
73
+ /** A suspended code or literal scan; nested templates use heap frames. */
74
+ type ScanFrame = {
75
+ start: number;
76
+ i: number;
77
+ ranOut: boolean;
78
+ } & ({ kind: 'code'; depth: number } | { kind: 'literal'; quote: string });
79
+
80
+ function scanLenient(input: string, start: number): Lenient {
81
+ const codeFrame = (start: number): ScanFrame => ({
82
+ kind: 'code',
83
+ start,
84
+ i: start,
85
+ depth: 0,
86
+ ranOut: false,
87
+ });
88
+ const frames: ScanFrame[] = [codeFrame(start)];
89
+ let result: Lenient = { end: -1, ranOut: true };
90
+ const finish = (end: number, ranOut: boolean) => {
91
+ const frame = frames.pop()!;
92
+ result = { end, ranOut };
93
+ if (frame.kind === 'code') memo.set(frame.start, result);
94
+ const parent = frames[frames.length - 1];
95
+ if (!parent) return;
96
+ parent.ranOut ||= ranOut;
97
+ if (parent.kind === 'code') {
98
+ // An unclosed quote/backtick is plain text in the lenient reader.
99
+ parent.i = end === -1 ? parent.i + 1 : end;
100
+ } else if (end === -1) {
101
+ // The interpolation did not close, so neither can its template.
102
+ parent.i = input.length;
103
+ } else {
104
+ parent.i = end + 1;
105
+ }
106
+ };
107
+ while (frames.length) {
108
+ const frame = frames[frames.length - 1]!;
109
+ const { i, ranOut } = frame;
110
+ if (i >= input.length) {
111
+ finish(-1, ranOut || frame.kind === 'code' || frame.quote === '`');
112
+ continue;
113
+ }
101
114
  const c = input[i]!;
102
- if (c === '{') {
103
- depth++;
104
- i++;
115
+ if (frame.kind === 'literal') {
116
+ if (c === '\\') frame.i += 2;
117
+ else if (c === frame.quote) finish(i + 1, ranOut);
118
+ else if (frame.quote !== '`' && c === '\n') finish(-1, ranOut);
119
+ else if (frame.quote === '`' && c === '$' && input[i + 1] === '{') {
120
+ const cached = memo.get(i + 2);
121
+ if (cached) {
122
+ frame.ranOut ||= cached.ranOut;
123
+ frame.i = cached.end === -1 ? input.length : cached.end + 1;
124
+ } else frames.push(codeFrame(i + 2));
125
+ } else frame.i++;
126
+ } else if (c === '{') {
127
+ frame.depth++;
128
+ frame.i++;
105
129
  } else if (c === '}') {
106
- if (depth === 0) return { end: i, ranOut };
107
- depth--;
108
- i++;
130
+ if (frame.depth === 0) finish(i, ranOut);
131
+ else {
132
+ frame.depth--;
133
+ frame.i++;
134
+ }
109
135
  } else if (
110
136
  c === '`' ||
111
137
  ((c === '"' || c === "'") &&
112
138
  !(i > 0 && NON_STRING_QUOTE_PREFIX.test(input[i - 1]!)))
113
139
  ) {
114
- const close = skipLiteral(input, i);
115
- ranOut ||= close.ranOut;
116
- i = close.end === -1 ? i + 1 : close.end;
117
- } else {
118
- i++;
119
- }
140
+ frames.push({
141
+ kind: 'literal',
142
+ start: i,
143
+ i: i + 1,
144
+ quote: c,
145
+ ranOut: false,
146
+ });
147
+ } else frame.i++;
120
148
  }
121
- return { end: -1, ranOut: true };
149
+ return result;
122
150
  }
123
151
 
124
152
  /**
@@ -365,8 +365,16 @@ function spansOf(text: string, reading: Reading): StyledSpan[] {
365
365
  const canvas = new Canvas(text.length);
366
366
  const comment = openComment(tokens);
367
367
  if (comment >= 0) canvas.paint(comment, text.length, 'comment');
368
- for (const start of unclosedStarts(text, reading)) {
369
- canvas.paint(start, text.length, 'error');
368
+ const starts = unclosedStarts(text, reading);
369
+ // Later suffix paints cover earlier ones. Paint only their visible parts,
370
+ // retaining each paint's span boundary without repeatedly filling the tail.
371
+ let end = text.length;
372
+ for (let i = starts.length - 1; i >= 0; i--) {
373
+ const start = starts[i]!;
374
+ if (start < end) {
375
+ canvas.paint(start, end, 'error');
376
+ end = start;
377
+ }
370
378
  }
371
379
  for (const error of errors) {
372
380
  if (!isUnclosed(error)) {
@@ -423,7 +431,9 @@ function openStartOf(text: string, reading: Reading): number {
423
431
  openDo(tokens)?.start ?? -1,
424
432
  openTag(text, tokens),
425
433
  ].filter((start) => start >= 0);
426
- return starts.length > 0 ? Math.min(...starts) : -1;
434
+ return starts.length > 0
435
+ ? starts.reduce((first, start) => Math.min(first, start), Infinity)
436
+ : -1;
427
437
  }
428
438
 
429
439
  /**
@@ -17,8 +17,10 @@ export interface ModeState {
17
17
  * from its start, ending in a line break; `''` when nothing is open.
18
18
  */
19
19
  tail: string;
20
- /** The spans left to emit on the current line, in window offsets. */
20
+ /** The current line’s spans, in window offsets; immutable during emission. */
21
21
  spans: StyledSpan[];
22
+ /** Index of the next span to emit. */
23
+ spanIndex: number;
22
24
  /** Where the current line starts in the window its spans came from. */
23
25
  lineStart: number;
24
26
  /**
@@ -89,6 +91,7 @@ function startLine(stream: StringStream, state: ModeState): void {
89
91
  end: Math.min(span.end, head.length),
90
92
  style: span.style,
91
93
  }));
94
+ state.spanIndex = 0;
92
95
  state.lineStart = lineStart;
93
96
  state.capped = capped;
94
97
  state.context = line.context;
@@ -111,17 +114,18 @@ export function mode(): Mode<ModeState> {
111
114
  context: '',
112
115
  tail: '',
113
116
  spans: [],
117
+ spanIndex: 0,
114
118
  lineStart: 0,
115
119
  capped: false,
116
120
  }),
117
- copyState: (state) => ({ ...state, spans: [...state.spans] }),
121
+ copyState: (state) => ({ ...state }),
118
122
  blankLine: (state) => {
119
123
  if (state.tail) state.tail += '\n';
120
124
  },
121
125
  token: (stream, state) => {
122
126
  try {
123
- if (stream.sol() && state.spans.length === 0) startLine(stream, state);
124
- const span = state.spans[0];
127
+ if (stream.sol()) startLine(stream, state);
128
+ const span = state.spans[state.spanIndex];
125
129
  if (!span) {
126
130
  stream.skipToEnd();
127
131
  return null;
@@ -131,12 +135,13 @@ export function mode(): Mode<ModeState> {
131
135
  stream.pos = span.start - state.lineStart;
132
136
  return null;
133
137
  }
134
- state.spans.shift();
138
+ state.spanIndex++;
135
139
  stream.pos = span.end - state.lineStart;
136
140
  return span.style;
137
141
  } catch {
138
142
  stream.skipToEnd();
139
143
  state.spans = [];
144
+ state.spanIndex = 0;
140
145
  state.tail = '';
141
146
  state.context = '';
142
147
  state.capped = false;