@rohal12/spindle 0.64.11 → 0.64.12

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rohal12/spindle",
3
- "version": "0.64.11",
3
+ "version": "0.64.12",
4
4
  "type": "module",
5
5
  "description": "A Preact-based story format for Twine 2.",
6
6
  "license": "Unlicense",
@@ -108,14 +108,14 @@
108
108
  "@vitest/coverage-v8": "^5.0.3",
109
109
  "codemirror": "^5.65.21",
110
110
  "fake-indexeddb": "^6.2.5",
111
- "fallow": "3.34.1",
111
+ "fallow": "3.36.0",
112
112
  "fast-check": "^4.10.2",
113
113
  "happy-dom": "^21.0.4",
114
114
  "husky": "^9.1.7",
115
115
  "js-yaml": "^5.4.3",
116
- "jscpd": "5.4.1",
116
+ "jscpd": "5.4.2",
117
117
  "lint-staged": "^17.6.0",
118
- "peggy": "5.1.0",
118
+ "peggy": "5.1.1",
119
119
  "playwright": "^1.64.0",
120
120
  "prettier": "^3.9.9",
121
121
  "typescript": "^7.0.2",
@@ -1,26 +1,13 @@
1
+ import { tokenizeMarkupTolerant } from '../markup/parse';
2
+ import type { HtmlToken } from '../markup/tokens';
3
+ import { sourceLines } from './source-lines';
4
+
1
5
  /** A run of a passage body that is formatted as one kind of content. */
2
6
  export interface Region {
3
7
  type: 'spindle' | 'html' | 'script' | 'svg';
4
8
  lines: string[];
5
9
  }
6
10
 
7
- const VOID_ELEMENTS = new Set([
8
- 'area',
9
- 'base',
10
- 'br',
11
- 'col',
12
- 'embed',
13
- 'hr',
14
- 'img',
15
- 'input',
16
- 'link',
17
- 'meta',
18
- 'param',
19
- 'source',
20
- 'track',
21
- 'wbr',
22
- ]);
23
-
24
11
  /**
25
12
  * Segment a passage body into regions: spindle (markdown/macros), html blocks, script blocks.
26
13
  */
@@ -32,6 +19,26 @@ export function segmentRegions(body: string): Region[] {
32
19
  }
33
20
  if (lines.length === 0) return [];
34
21
 
22
+ // Count only real tags: comments, attributes and macro strings can all
23
+ // contain tag-shaped text. Index once, including the opening region line.
24
+ const tags: HtmlToken[][] = lines.map(() => []);
25
+ const tagEndLines = lines.map((_, line) => line);
26
+ const lineAt = sourceLines(body);
27
+ for (const token of tokenizeMarkupTolerant(body).tokens) {
28
+ if (token.type !== 'html') continue;
29
+ const line = lineAt(token.start);
30
+ tags[line]!.push(token);
31
+ tagEndLines[line] = Math.max(tagEndLines[line]!, lineAt(token.end - 1));
32
+ }
33
+ const depthOnLine = (line: number, name: string) =>
34
+ tags[line]!.reduce(
35
+ (depth, token) =>
36
+ token.tag.toLowerCase() === name && !token.isSelfClose
37
+ ? depth + (token.isClose ? -1 : 1)
38
+ : depth,
39
+ 0,
40
+ );
41
+
35
42
  const regions: Region[] = [];
36
43
  let i = 0;
37
44
 
@@ -63,16 +70,13 @@ export function segmentRegions(body: string): Region[] {
63
70
  const tagMatch = trimmed.match(/^<([a-zA-Z][\w-]*)/);
64
71
  const tagName = tagMatch![1]!.toLowerCase();
65
72
 
66
- // Skip void elements on their own line — not a multi-line block
67
- if (VOID_ELEMENTS.has(tagName) && !lines[i + 1]?.trim().startsWith('<')) {
68
- pushSpindleLine(regions, line);
69
- i++;
70
- continue;
71
- }
72
-
73
- // Check if this is truly multi-line: does the tag close on the same line?
74
- const selfClosing = new RegExp(`</${tagName}\\s*>`, 'i');
75
- if (selfClosing.test(trimmed)) {
73
+ // All same-name openers must close, even when an inner one closes on
74
+ // the opening line. Self-closing tags do not increase the depth.
75
+ let depth = depthOnLine(i, tagName);
76
+ // A closed or void tag can still span lines. Never send its attribute
77
+ // continuation through prose indentation or split a closing tag.
78
+ let endLine = tagEndLines[i]!;
79
+ if (depth <= 0 && endLine === i) {
76
80
  pushSpindleLine(regions, line);
77
81
  i++;
78
82
  continue;
@@ -80,17 +84,11 @@ export function segmentRegions(body: string): Region[] {
80
84
 
81
85
  // Multi-line HTML block
82
86
  const htmlLines: string[] = [line];
83
- let depth = 1;
84
87
  i++;
85
- while (i < lines.length && depth > 0) {
88
+ while (i < lines.length && (depth > 0 || i <= endLine)) {
86
89
  htmlLines.push(lines[i]!);
87
- const opens = (
88
- lines[i]!.match(new RegExp(`<${tagName}(?:[\\s>]|$)`, 'gi')) ?? []
89
- ).length;
90
- const closes = (
91
- lines[i]!.match(new RegExp(`</${tagName}\\s*>`, 'gi')) ?? []
92
- ).length;
93
- depth += opens - closes;
90
+ endLine = Math.max(endLine, tagEndLines[i]!);
91
+ depth += depthOnLine(i, tagName);
94
92
  i++;
95
93
  }
96
94
  regions.push({ type: 'html', lines: htmlLines });
package/src/js-lexer.ts CHANGED
@@ -371,7 +371,15 @@ function tokenizerAt(
371
371
  goal: JsGoal,
372
372
  { onComment, afterOperand = false }: TokenizerOptions = {},
373
373
  ): ParserState {
374
- const options = onComment ? { ...OPTIONS, onComment } : OPTIONS;
374
+ // Consumers use absolute offsets, never Acorn's token/node locations.
375
+ // Its error locations are computed separately from the source. Supplying
376
+ // a local starting location avoids its backwards newline-prefix search
377
+ // for every expression on a long passage line.
378
+ const options = {
379
+ ...OPTIONS,
380
+ startLocation: { line: 1, column: 0 },
381
+ onComment,
382
+ };
375
383
  const p = new SigilParser(options, src, start) as unknown as ParserState;
376
384
  if (goal === 'expression') p.type = tt.parenL;
377
385
  if (afterOperand) p.exprAllowed = false;