@rohal12/spindle 0.62.4 → 0.62.6
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/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +127 -45
- package/dist/pkg/story-variables.js +207 -86
- package/package.json +1 -1
- package/src/code-check.ts +43 -5
- package/src/completion-context.ts +58 -13
- package/src/components/macros/PassageDisplay.tsx +3 -16
- package/src/components/macros/detached-body.tsx +7 -0
- package/src/format/format-passage.ts +3 -3
- package/src/hooks/use-modal-focus.ts +1 -11
- package/src/interpolation.ts +3 -3
- package/src/js-bindings.ts +7 -4
- package/src/js-lexer.ts +5 -1
- package/src/markup/code-attributes.ts +3 -4
- package/src/markup/code-end.ts +68 -1
- package/src/markup/pair.ts +14 -2
- package/src/markup/parse.ts +22 -12
- package/src/markup/render.tsx +3 -3
- package/src/registry.ts +3 -0
- package/src/saves/save-manager.ts +14 -3
- package/src/store.ts +30 -16
- package/src/story-variables.ts +2 -4
- package/src/tooling.ts +57 -9
- package/src/utils/element-visibility.ts +22 -9
- package/src/widgets/ast-scanner.ts +2 -1
- package/types/tooling.d.ts +18 -1
package/src/code-check.ts
CHANGED
|
@@ -229,11 +229,33 @@ export interface ParametersOf {
|
|
|
229
229
|
* commas or spaces (see splitArgs), not by parseMacroArgs.
|
|
230
230
|
*/
|
|
231
231
|
widget?(name: string): readonly WidgetParameter[] | undefined;
|
|
232
|
+
/** Effective registration origin; absent metadata retains builtin defaults. */
|
|
233
|
+
source?(name: string): 'builtin' | 'user' | undefined;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Whether builtin-only interpretation still belongs to this implementation. */
|
|
237
|
+
export function usesBuiltinSemantics(
|
|
238
|
+
name: string,
|
|
239
|
+
parametersOf: ParametersOf,
|
|
240
|
+
): boolean {
|
|
241
|
+
return (
|
|
242
|
+
parametersOf.widget?.(name) === undefined &&
|
|
243
|
+
parametersOf.source?.(name) !== 'user'
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** A raw body is executable only while the builtin implementation owns it. */
|
|
248
|
+
export function executesRawBody(
|
|
249
|
+
name: string,
|
|
250
|
+
parametersOf: ParametersOf,
|
|
251
|
+
): boolean {
|
|
252
|
+
return isRawMacro(name, false) && usesBuiltinSemantics(name, parametersOf);
|
|
232
253
|
}
|
|
233
254
|
|
|
234
255
|
/** What the argument check needs to know of a macro (see MacroMetadata). */
|
|
235
256
|
export interface MacroParameters {
|
|
236
257
|
name: string;
|
|
258
|
+
source?: 'builtin' | 'user';
|
|
237
259
|
parameters?: readonly ParameterDef[];
|
|
238
260
|
interpolate?: boolean;
|
|
239
261
|
/** For a widget: its `@` parameters and what some declare to hold. */
|
|
@@ -273,22 +295,32 @@ export function parameterLookup(
|
|
|
273
295
|
): ParametersOf {
|
|
274
296
|
const parameters = new NameMap<readonly ParameterDef[]>();
|
|
275
297
|
const widgets = new NameMap<readonly WidgetParameter[]>();
|
|
276
|
-
|
|
298
|
+
const sources = new NameMap<'builtin' | 'user'>();
|
|
299
|
+
for (const {
|
|
300
|
+
name,
|
|
301
|
+
parameters: params,
|
|
302
|
+
interpolate,
|
|
303
|
+
widget,
|
|
304
|
+
source,
|
|
305
|
+
} of macros) {
|
|
306
|
+
sources.set(name, source ?? 'user');
|
|
277
307
|
if (params)
|
|
278
308
|
parameters.set(name, withHolds(params, holdsByDefault(interpolate)));
|
|
309
|
+
else parameters.delete(name);
|
|
279
310
|
if (widget)
|
|
280
311
|
widgets.set(name, widgetParameters(widget.params, widget.holds) ?? []);
|
|
281
312
|
}
|
|
282
313
|
const lookup: ParametersOf = (name) => {
|
|
283
314
|
if (widgets.has(name)) return undefined;
|
|
284
315
|
let params = parameters.get(name);
|
|
285
|
-
if (!params) {
|
|
316
|
+
if (!params && sources.get(name) !== 'user') {
|
|
286
317
|
const sub = subMacroParameters(name);
|
|
287
318
|
if (sub) parameters.set(name, (params = withHolds(sub, 'text')));
|
|
288
319
|
}
|
|
289
320
|
return params;
|
|
290
321
|
};
|
|
291
322
|
if (widgets.size > 0) lookup.widget = (name) => widgets.get(name);
|
|
323
|
+
lookup.source = (name) => sources.get(name);
|
|
292
324
|
return lookup;
|
|
293
325
|
}
|
|
294
326
|
|
|
@@ -396,7 +428,7 @@ export function* codeAndText(
|
|
|
396
428
|
} else if (token.type === 'macro' && !token.isClose) {
|
|
397
429
|
const name = token.name.toLowerCase();
|
|
398
430
|
const widget = parametersOf.widget?.(name);
|
|
399
|
-
if (name
|
|
431
|
+
if (executesRawBody(name, parametersOf)) {
|
|
400
432
|
const close = closeOfTextBody(t, name);
|
|
401
433
|
if (close < 0) continue;
|
|
402
434
|
const body = src.slice(token.end, tokens[close]!.start);
|
|
@@ -411,7 +443,10 @@ export function* codeAndText(
|
|
|
411
443
|
continue;
|
|
412
444
|
}
|
|
413
445
|
const args = token.rawArgs;
|
|
414
|
-
if (
|
|
446
|
+
if (
|
|
447
|
+
PASSAGE_BODIES.has(name) &&
|
|
448
|
+
usesBuiltinSemantics(name, parametersOf)
|
|
449
|
+
) {
|
|
415
450
|
// The body names the passage when it is only text
|
|
416
451
|
const close = closeOfTextBody(t, name);
|
|
417
452
|
if (close > t + 1) {
|
|
@@ -430,7 +465,10 @@ export function* codeAndText(
|
|
|
430
465
|
if (!args) continue;
|
|
431
466
|
const label = `{${token.name} ${args}}`;
|
|
432
467
|
const argsAt = token.argsStart;
|
|
433
|
-
if (
|
|
468
|
+
if (
|
|
469
|
+
CONDITION_MACROS.has(name) &&
|
|
470
|
+
usesBuiltinSemantics(name, parametersOf)
|
|
471
|
+
) {
|
|
434
472
|
yield {
|
|
435
473
|
kind: 'code',
|
|
436
474
|
code: args,
|
|
@@ -6,6 +6,8 @@ import {
|
|
|
6
6
|
attributeValues,
|
|
7
7
|
escapeOffsets,
|
|
8
8
|
parameterLookup,
|
|
9
|
+
executesRawBody,
|
|
10
|
+
usesBuiltinSemantics,
|
|
9
11
|
type PieceBase,
|
|
10
12
|
} from './code-check';
|
|
11
13
|
import { macroArgumentSpans } from './components/macros/macro-args';
|
|
@@ -18,9 +20,22 @@ import {
|
|
|
18
20
|
} from './js-lexer';
|
|
19
21
|
import type { ToolingMacro } from './tooling';
|
|
20
22
|
|
|
23
|
+
/** Enclosing text containers, in order from the passage to the cursor. */
|
|
24
|
+
export type CompletionContainer =
|
|
25
|
+
| { kind: 'string'; quote: '"' | "'"; escapes: 'quotes' | 'javascript' }
|
|
26
|
+
| { kind: 'attribute'; name: string; quote: '"' | "'" | null; code: boolean }
|
|
27
|
+
| { kind: 'link-label' };
|
|
28
|
+
|
|
21
29
|
export type CompletionContext =
|
|
22
30
|
| { kind: 'literal' }
|
|
23
|
-
| (PieceBase & {
|
|
31
|
+
| (PieceBase & {
|
|
32
|
+
kind: 'code';
|
|
33
|
+
code: string;
|
|
34
|
+
goal: JsGoal;
|
|
35
|
+
cursor: number;
|
|
36
|
+
/** Encode replacement text through these containers, innermost first. */
|
|
37
|
+
containers?: readonly CompletionContainer[];
|
|
38
|
+
});
|
|
24
39
|
|
|
25
40
|
const LITERAL = { kind: 'literal' } as const;
|
|
26
41
|
|
|
@@ -44,6 +59,7 @@ export function completionContext(
|
|
|
44
59
|
code: string,
|
|
45
60
|
goal: JsGoal,
|
|
46
61
|
map: Mapping,
|
|
62
|
+
containers: readonly CompletionContainer[],
|
|
47
63
|
): CompletionContext => {
|
|
48
64
|
// A following identifier probes the lexer's state at EOF. It cannot
|
|
49
65
|
// close a string, regex, comment or template, and needs no guessed
|
|
@@ -71,6 +87,7 @@ export function completionContext(
|
|
|
71
87
|
goal,
|
|
72
88
|
offset,
|
|
73
89
|
cursor: code.length,
|
|
90
|
+
...(containers.length ? { containers } : {}),
|
|
74
91
|
...(offsets.some((at, i) => at !== offset + i)
|
|
75
92
|
? { sourceOffsets: offsets }
|
|
76
93
|
: {}),
|
|
@@ -83,6 +100,7 @@ export function completionContext(
|
|
|
83
100
|
text = false,
|
|
84
101
|
depth = 0,
|
|
85
102
|
sigilsOnly = false,
|
|
103
|
+
containers: readonly CompletionContainer[] = [],
|
|
86
104
|
): CompletionContext => {
|
|
87
105
|
if (depth >= 64) return LITERAL;
|
|
88
106
|
if (sigilsOnly) {
|
|
@@ -90,7 +108,12 @@ export function completionContext(
|
|
|
90
108
|
for (const part of splitSigilTemplate(src, true)) {
|
|
91
109
|
if ('expr' in part) {
|
|
92
110
|
if (at + 1 + part.expr.length === src.length) {
|
|
93
|
-
return codeContext(
|
|
111
|
+
return codeContext(
|
|
112
|
+
part.expr,
|
|
113
|
+
'expression',
|
|
114
|
+
(i) => map(at + 1 + i),
|
|
115
|
+
containers,
|
|
116
|
+
);
|
|
94
117
|
}
|
|
95
118
|
at += part.expr.length + 2;
|
|
96
119
|
} else at += ('text' in part ? part.text : part.verbatim).length;
|
|
@@ -107,12 +130,13 @@ export function completionContext(
|
|
|
107
130
|
const token = tokens[tokens.length - 1];
|
|
108
131
|
if (!token || token.end !== src.length) return LITERAL;
|
|
109
132
|
const codeAt = (code: string, at: number, goal: JsGoal = 'expression') =>
|
|
110
|
-
codeContext(code, goal, (i) => map(at + i));
|
|
133
|
+
codeContext(code, goal, (i) => map(at + i), containers);
|
|
111
134
|
const nested = (
|
|
112
135
|
value: string,
|
|
113
136
|
at: number,
|
|
114
137
|
offsets?: readonly number[],
|
|
115
138
|
onlySigils = false,
|
|
139
|
+
container?: CompletionContainer,
|
|
116
140
|
) =>
|
|
117
141
|
read(
|
|
118
142
|
value,
|
|
@@ -120,6 +144,7 @@ export function completionContext(
|
|
|
120
144
|
true,
|
|
121
145
|
depth + 1,
|
|
122
146
|
onlySigils,
|
|
147
|
+
container ? [...containers, container] : containers,
|
|
123
148
|
);
|
|
124
149
|
|
|
125
150
|
if (token.type === 'variable' && token.nameEnd === src.length) {
|
|
@@ -131,20 +156,27 @@ export function completionContext(
|
|
|
131
156
|
if (token.type === 'html') {
|
|
132
157
|
for (const { name, value, at } of attributeValues(src, token)) {
|
|
133
158
|
if (at + value.length === src.length) {
|
|
134
|
-
|
|
159
|
+
const span = token.attributeSpans.find(
|
|
160
|
+
(span) => span.valueStart === at,
|
|
161
|
+
);
|
|
162
|
+
return nested(value, at, undefined, isCodeAttribute(name), {
|
|
163
|
+
kind: 'attribute',
|
|
164
|
+
name,
|
|
165
|
+
quote: span?.quote ?? null,
|
|
166
|
+
code: isCodeAttribute(name),
|
|
167
|
+
});
|
|
135
168
|
}
|
|
136
169
|
}
|
|
137
170
|
} else if (token.type === 'link' && token.displayEnd === src.length) {
|
|
138
|
-
return nested(token.display, token.displayStart
|
|
171
|
+
return nested(token.display, token.displayStart, undefined, false, {
|
|
172
|
+
kind: 'link-label',
|
|
173
|
+
});
|
|
139
174
|
} else if (token.type === 'text' && !token.comment) {
|
|
140
175
|
const opener = tokens[tokens.length - 2];
|
|
141
176
|
if (
|
|
142
177
|
opener?.type === 'macro' &&
|
|
143
178
|
!opener.isClose &&
|
|
144
|
-
|
|
145
|
-
opener.name,
|
|
146
|
-
parametersOf.widget?.(opener.name) !== undefined,
|
|
147
|
-
)
|
|
179
|
+
executesRawBody(opener.name, parametersOf)
|
|
148
180
|
) {
|
|
149
181
|
return codeAt(token.value, token.start, 'statements');
|
|
150
182
|
}
|
|
@@ -156,7 +188,10 @@ export function completionContext(
|
|
|
156
188
|
const name = token.name.toLowerCase();
|
|
157
189
|
const args = token.rawArgs;
|
|
158
190
|
const widget = parametersOf.widget?.(name);
|
|
159
|
-
if (
|
|
191
|
+
if (
|
|
192
|
+
usesBuiltinSemantics(name, parametersOf) &&
|
|
193
|
+
['if', 'elseif', 'case'].includes(name)
|
|
194
|
+
) {
|
|
160
195
|
return codeAt(args, token.argsStart);
|
|
161
196
|
}
|
|
162
197
|
const stringContext = (
|
|
@@ -178,13 +213,23 @@ export function completionContext(
|
|
|
178
213
|
? stringLiteralValue(raw + raw[0])
|
|
179
214
|
: quoted.value;
|
|
180
215
|
if (value == null) return LITERAL;
|
|
216
|
+
const container: CompletionContainer = {
|
|
217
|
+
kind: 'string',
|
|
218
|
+
quote: raw[0] as '"' | "'",
|
|
219
|
+
escapes,
|
|
220
|
+
};
|
|
181
221
|
const offsets = escapeOffsets(raw.slice(1), at + 1, value, escapes);
|
|
182
222
|
if (holds === 'expression' || holds === 'statements') {
|
|
183
|
-
return codeContext(
|
|
184
|
-
|
|
223
|
+
return codeContext(
|
|
224
|
+
value,
|
|
225
|
+
holds,
|
|
226
|
+
(i) => map(offsets?.[i] ?? at + 1 + i),
|
|
227
|
+
[...containers, container],
|
|
185
228
|
);
|
|
186
229
|
}
|
|
187
|
-
return holds === 'markup'
|
|
230
|
+
return holds === 'markup'
|
|
231
|
+
? nested(value, at + 1, offsets, false, container)
|
|
232
|
+
: LITERAL;
|
|
188
233
|
};
|
|
189
234
|
if (widget) {
|
|
190
235
|
const spans = splitArgSpans(args);
|
|
@@ -789,9 +789,6 @@ defineMacro({
|
|
|
789
789
|
// Track the resolved transition type for the data-transition prop
|
|
790
790
|
const resolvedTypeRef = useRef<string>('fade');
|
|
791
791
|
|
|
792
|
-
// Track previous history length to detect restart/load
|
|
793
|
-
const prevHistoryLenRef = useRef(0);
|
|
794
|
-
|
|
795
792
|
const displayedId = displayed.id;
|
|
796
793
|
const isOutgoing = useCallback(
|
|
797
794
|
(state: StoryState) => state.navigationId !== displayedId,
|
|
@@ -834,20 +831,10 @@ defineMacro({
|
|
|
834
831
|
// State machine effect — triggers on every navigation
|
|
835
832
|
useEffect(() => {
|
|
836
833
|
const store = useStoryStore.getState();
|
|
837
|
-
const {
|
|
838
|
-
|
|
839
|
-
// Determine if this is a first load, restart, or save-load
|
|
840
|
-
const prevLen = prevHistoryLenRef.current;
|
|
841
|
-
prevHistoryLenRef.current = history.length;
|
|
834
|
+
const { replacementNavigationId, transitionConfig } = store;
|
|
842
835
|
|
|
843
836
|
const isFirstLoad = !displayedPassage;
|
|
844
|
-
const
|
|
845
|
-
history.length === 1 && historyIndex === 0 && prevLen > 1;
|
|
846
|
-
const isLoad =
|
|
847
|
-
history.length > 1 &&
|
|
848
|
-
historyIndex === history.length - 1 &&
|
|
849
|
-
prevLen > 1 &&
|
|
850
|
-
Math.abs(history.length - prevLen) > 1;
|
|
837
|
+
const isReplacement = navigationId === replacementNavigationId;
|
|
851
838
|
|
|
852
839
|
// Already showing this navigation (initial render)
|
|
853
840
|
if (navigationId === displayed.id) {
|
|
@@ -871,7 +858,7 @@ defineMacro({
|
|
|
871
858
|
}
|
|
872
859
|
|
|
873
860
|
// First load, restart, or load → use simple fade (no outgoing phase)
|
|
874
|
-
if (isFirstLoad ||
|
|
861
|
+
if (isFirstLoad || isReplacement) {
|
|
875
862
|
resolvedTypeRef.current = 'fade';
|
|
876
863
|
setDisplayedPassage(currentPassage);
|
|
877
864
|
return;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { getMacroParameters } from '../../registry';
|
|
2
|
+
import { getWidget } from '../../widgets/widget-registry';
|
|
1
3
|
import { render } from 'preact';
|
|
2
4
|
import { useContext, useEffect, useRef } from 'preact/hooks';
|
|
3
5
|
import {
|
|
@@ -52,11 +54,16 @@ function settlesAfterRender(nodes: ASTNode[]): boolean {
|
|
|
52
54
|
case 'macro':
|
|
53
55
|
return (
|
|
54
56
|
SETTLING_MACROS.has(n.name) &&
|
|
57
|
+
!getWidget(n.name) &&
|
|
58
|
+
getMacroParameters.source(n.name) === 'builtin' &&
|
|
55
59
|
settlesAfterRender(n.children) &&
|
|
56
60
|
(n.branches ?? []).every((b) => settlesAfterRender(b.children))
|
|
57
61
|
);
|
|
58
62
|
case 'html':
|
|
59
63
|
return settlesAfterRender(n.children);
|
|
64
|
+
case 'variable':
|
|
65
|
+
// A forwarded slot may render timers or author-defined macros.
|
|
66
|
+
return n.scope !== 'local' || n.name !== 'children';
|
|
60
67
|
default:
|
|
61
68
|
return true;
|
|
62
69
|
}
|
|
@@ -375,8 +375,8 @@ async function formatLf(
|
|
|
375
375
|
);
|
|
376
376
|
}
|
|
377
377
|
|
|
378
|
-
/** HTML
|
|
379
|
-
const WHITESPACE_AS_WRITTEN = /<(?:pre|textarea)[\s>]|white-space\s*:/i;
|
|
378
|
+
/** Literal HTML and opaque SVG, whose text can inherit significant whitespace. */
|
|
379
|
+
const WHITESPACE_AS_WRITTEN = /<(?:pre|textarea|svg)[\s>]|white-space\s*:/i;
|
|
380
380
|
|
|
381
381
|
/** Preserve whole literal elements, including openers beside nested tags. */
|
|
382
382
|
function whitespaceSensitiveLines(
|
|
@@ -392,7 +392,7 @@ function whitespaceSensitiveLines(
|
|
|
392
392
|
const { token, body } = node;
|
|
393
393
|
if (
|
|
394
394
|
token.type === 'html' &&
|
|
395
|
-
(['pre', 'textarea'].includes(token.tag.toLowerCase()) ||
|
|
395
|
+
(['pre', 'textarea', 'svg'].includes(token.tag.toLowerCase()) ||
|
|
396
396
|
Object.entries(token.attributes).some(
|
|
397
397
|
([name, value]) =>
|
|
398
398
|
name.toLowerCase() === 'style' && /white-space\s*:/i.test(value),
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isRendered } from '../utils/element-visibility';
|
|
1
|
+
import { composedParent, isRendered } from '../utils/element-visibility';
|
|
2
2
|
import { useLayoutEffect } from 'preact/hooks';
|
|
3
3
|
import type { RefObject } from 'preact';
|
|
4
4
|
|
|
@@ -17,16 +17,6 @@ const FOCUSABLE = [
|
|
|
17
17
|
'[tabindex]',
|
|
18
18
|
].join(',');
|
|
19
19
|
|
|
20
|
-
/** The parent in the rendered tree, including slots and shadow hosts. */
|
|
21
|
-
function composedParent(el: Element): Element | null {
|
|
22
|
-
return (
|
|
23
|
-
el.assignedSlot ??
|
|
24
|
-
el.parentElement ??
|
|
25
|
-
(el.getRootNode() as ShadowRoot).host ??
|
|
26
|
-
null
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
20
|
function composedChildren(root: ParentNode): Element[] {
|
|
31
21
|
if ((root as Element).localName === 'slot') {
|
|
32
22
|
const assigned = (root as HTMLSlotElement).assignedNodes({
|
package/src/interpolation.ts
CHANGED
|
@@ -255,9 +255,6 @@ class TextEvaluator {
|
|
|
255
255
|
}
|
|
256
256
|
|
|
257
257
|
private macro(node: MacroNode, scope: TextScope, depth: number): string {
|
|
258
|
-
// Rendering ignores a stray sub-macro too (see renderMacro).
|
|
259
|
-
if (isSubMacro(node.name)) return '';
|
|
260
|
-
|
|
261
258
|
const widget = getWidget(node.name);
|
|
262
259
|
if (widget) {
|
|
263
260
|
let locals = scope.locals;
|
|
@@ -285,6 +282,9 @@ class TextEvaluator {
|
|
|
285
282
|
});
|
|
286
283
|
}
|
|
287
284
|
|
|
285
|
+
// Registered widgets take precedence over stray companion markers.
|
|
286
|
+
if (isSubMacro(node.name)) return '';
|
|
287
|
+
|
|
288
288
|
const text = getMacroText(node.name);
|
|
289
289
|
if (!text) {
|
|
290
290
|
const message = getMacro(node.name)
|
package/src/js-bindings.ts
CHANGED
|
@@ -160,6 +160,7 @@ export class Bindings {
|
|
|
160
160
|
private readGroups(): void {
|
|
161
161
|
const { code, groups } = this;
|
|
162
162
|
const stack: Group[] = [];
|
|
163
|
+
const active: Record<Kind, number[]> = { '{': [], '(': [], '[': [] };
|
|
163
164
|
for (let i = 0; i < code.length; i++) {
|
|
164
165
|
const c = code.charAt(i);
|
|
165
166
|
if (c === '{' || c === '(' || c === '[') {
|
|
@@ -170,16 +171,18 @@ export class Bindings {
|
|
|
170
171
|
parent: stack[stack.length - 1],
|
|
171
172
|
};
|
|
172
173
|
groups.push(group);
|
|
174
|
+
active[c].push(stack.length);
|
|
173
175
|
stack.push(group);
|
|
174
176
|
this.byOpen.set(i, group);
|
|
175
177
|
} else if (CLOSERS[c]) {
|
|
176
178
|
// Tolerate a closer with no opener: the innermost of its kind closes
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
if (k
|
|
179
|
+
const positions = active[CLOSERS[c]!];
|
|
180
|
+
const k = positions[positions.length - 1];
|
|
181
|
+
if (k !== undefined) {
|
|
180
182
|
stack[k]!.close = i;
|
|
181
183
|
this.byClose.set(i, stack[k]!);
|
|
182
|
-
|
|
184
|
+
// Discard each recovered group from its kind's index once.
|
|
185
|
+
while (stack.length > k) active[stack.pop()!.kind].pop();
|
|
183
186
|
}
|
|
184
187
|
}
|
|
185
188
|
}
|
package/src/js-lexer.ts
CHANGED
|
@@ -905,7 +905,11 @@ export function findCodeEnd(
|
|
|
905
905
|
* class field named `class` inside a template literal) mislead it; the
|
|
906
906
|
* parser knows. Only asked when the scan finds no end.
|
|
907
907
|
*/
|
|
908
|
-
function parsedCodeEnd(
|
|
908
|
+
export function parsedCodeEnd(
|
|
909
|
+
src: string,
|
|
910
|
+
start: number,
|
|
911
|
+
goal: JsGoal,
|
|
912
|
+
): number {
|
|
909
913
|
const p = tokenizerAt(src, start, goal) as ParserState & {
|
|
910
914
|
parse(): unknown;
|
|
911
915
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isSigil } from './tokens';
|
|
2
|
-
import {
|
|
2
|
+
import { indexCodeEnds } from './code-end';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Attributes whose value is code with braces of its own, not text: event
|
|
@@ -33,6 +33,7 @@ export function splitSigilTemplate(
|
|
|
33
33
|
const parts: SigilPart[] = [];
|
|
34
34
|
let text = '';
|
|
35
35
|
let i = 0;
|
|
36
|
+
const closeBrace = indexCodeEnds(template);
|
|
36
37
|
|
|
37
38
|
while (i < template.length) {
|
|
38
39
|
const brace = template.indexOf('{', i);
|
|
@@ -42,9 +43,7 @@ export function splitSigilTemplate(
|
|
|
42
43
|
}
|
|
43
44
|
text += template.slice(i, brace);
|
|
44
45
|
i = brace + 1;
|
|
45
|
-
let end = isSigil(template[i])
|
|
46
|
-
? defaultCodeEnd.closeBrace(template, i, i)
|
|
47
|
-
: -1;
|
|
46
|
+
let end = isSigil(template[i]) ? closeBrace(i) : -1;
|
|
48
47
|
if (end === -1 && incomplete && isSigil(template[i])) end = template.length;
|
|
49
48
|
if (end === -1) {
|
|
50
49
|
text += '{';
|
package/src/markup/code-end.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { findCodeEnd } from '../js-lexer';
|
|
1
|
+
import { findCodeEnd, parsedCodeEnd } from '../js-lexer';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Where the JavaScript inside markup ends. The grammar (spindle.peggy) leaves
|
|
@@ -175,3 +175,70 @@ export function jsCodeEnd(onOpenEnd?: OnOpenCodeEnd): CodeEnd {
|
|
|
175
175
|
|
|
176
176
|
/** The JavaScript reader Spindle uses today: js-lexer, then a lenient scan. */
|
|
177
177
|
export const defaultCodeEnd: CodeEnd = jsCodeEnd();
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Index possible brace ends for repeated recovery attempts in one template.
|
|
181
|
+
* Within plain code, nested braces must close first. At a quote, slash or
|
|
182
|
+
* escape we stop making lexical assumptions, but the remaining raw `}`s
|
|
183
|
+
* still bound how many pending braces could close. Indexing visits each
|
|
184
|
+
* opener once, so unmatched plain-code openers can skip suffix scans.
|
|
185
|
+
*/
|
|
186
|
+
export function indexCodeEnds(
|
|
187
|
+
input: string,
|
|
188
|
+
): (start: number, codeStart?: number, limit?: number) => number {
|
|
189
|
+
const possible = new Set<number>();
|
|
190
|
+
const pending: number[] = [];
|
|
191
|
+
let remaining = 0;
|
|
192
|
+
let lastBrace = -1;
|
|
193
|
+
for (let i = 0; i < input.length; i++) {
|
|
194
|
+
if (input[i] === '}') {
|
|
195
|
+
remaining++;
|
|
196
|
+
lastBrace = i;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const uncertain = () => {
|
|
200
|
+
for (
|
|
201
|
+
let i = Math.max(0, pending.length - remaining);
|
|
202
|
+
i < pending.length;
|
|
203
|
+
i++
|
|
204
|
+
) {
|
|
205
|
+
possible.add(pending[i]!);
|
|
206
|
+
}
|
|
207
|
+
pending.length = 0;
|
|
208
|
+
};
|
|
209
|
+
for (let i = 0; i < input.length; i++) {
|
|
210
|
+
const c = input[i]!;
|
|
211
|
+
if (c === '{') pending.push(i);
|
|
212
|
+
else if (c === '}') {
|
|
213
|
+
remaining--;
|
|
214
|
+
const open = pending.pop();
|
|
215
|
+
if (open !== undefined) possible.add(open);
|
|
216
|
+
} else if (/['"`/\\]/.test(c)) uncertain();
|
|
217
|
+
}
|
|
218
|
+
return (start, codeStart = start, limit = input.length) => {
|
|
219
|
+
if (start > lastBrace || codeStart >= limit) return -1;
|
|
220
|
+
const source = limit === input.length ? input : input.slice(0, limit);
|
|
221
|
+
// A syntax error may end the parser's recovery at an inner `}`, even
|
|
222
|
+
// when neither the token reader nor the lenient reader can close here.
|
|
223
|
+
// Selectors can put lenientStart after something other than the opener.
|
|
224
|
+
// Macro names can themselves contain braces, which the strict reader
|
|
225
|
+
// skips before reading arguments; the index must not count those.
|
|
226
|
+
if (
|
|
227
|
+
input[start - 1] !== '{' ||
|
|
228
|
+
possible.has(start - 1) ||
|
|
229
|
+
input.slice(start, codeStart).includes('{')
|
|
230
|
+
) {
|
|
231
|
+
return defaultCodeEnd.closeBrace(source, codeStart, start);
|
|
232
|
+
}
|
|
233
|
+
// Acorn locates its starting line and syntax errors by scanning the
|
|
234
|
+
// prefix. Recovery must not hand it the whole passage for every opener.
|
|
235
|
+
// Keep one preceding character so a leading #! cannot become a hashbang.
|
|
236
|
+
const base = Math.max(0, codeStart - 1);
|
|
237
|
+
const end = parsedCodeEnd(
|
|
238
|
+
source.slice(base),
|
|
239
|
+
codeStart - base,
|
|
240
|
+
'expression',
|
|
241
|
+
);
|
|
242
|
+
return end < 0 ? -1 : base + end;
|
|
243
|
+
};
|
|
244
|
+
}
|
package/src/markup/pair.ts
CHANGED
|
@@ -199,6 +199,11 @@ export function pairMarkup(
|
|
|
199
199
|
|
|
200
200
|
const root: PairedNode[] = [];
|
|
201
201
|
const stack: Frame[] = [];
|
|
202
|
+
// Positions of active openers, separated by tag kind as well as name.
|
|
203
|
+
// Every frame is indexed once and removed once, including recovery cuts.
|
|
204
|
+
const openPositions = new Map<string, number[]>();
|
|
205
|
+
const tagKey = (tag: MacroToken | HtmlToken) =>
|
|
206
|
+
`${tag.type}:${lowerName(tag)}`;
|
|
202
207
|
const add = (node: PairedNode) => (last(stack)?.into ?? root).push(node);
|
|
203
208
|
|
|
204
209
|
/**
|
|
@@ -212,6 +217,10 @@ export function pairMarkup(
|
|
|
212
217
|
) => {
|
|
213
218
|
const { node, body } = stack.pop()!;
|
|
214
219
|
const open = node.token as MacroToken | HtmlToken;
|
|
220
|
+
const key = tagKey(open);
|
|
221
|
+
const positions = openPositions.get(key)!;
|
|
222
|
+
positions.pop();
|
|
223
|
+
if (positions.length === 0) openPositions.delete(key);
|
|
215
224
|
const noticedAt = close ? close.end : noticedAtEnd;
|
|
216
225
|
for (const { tag } of body.branches) {
|
|
217
226
|
if (
|
|
@@ -261,8 +270,7 @@ export function pairMarkup(
|
|
|
261
270
|
// The closer ends the innermost element, as the runtime reads it;
|
|
262
271
|
// when it names one further out, it ends that one, and the elements
|
|
263
272
|
// inside it are left unclosed
|
|
264
|
-
|
|
265
|
-
while (outer >= 0 && !closes(stack[outer]!.node.token, token)) outer--;
|
|
273
|
+
const outer = last(openPositions.get(tagKey(token)) ?? []) ?? -1;
|
|
266
274
|
end('mismatch', token, outer === -1);
|
|
267
275
|
if (outer !== -1) {
|
|
268
276
|
while (stack.length > outer + 1) end('cut', token);
|
|
@@ -316,6 +324,10 @@ export function pairMarkup(
|
|
|
316
324
|
}
|
|
317
325
|
const body: PairedBody = { children: [], branches: [] };
|
|
318
326
|
node.body = body;
|
|
327
|
+
const key = tagKey(token as MacroToken | HtmlToken);
|
|
328
|
+
const positions = openPositions.get(key) ?? [];
|
|
329
|
+
positions.push(stack.length);
|
|
330
|
+
openPositions.set(key, positions);
|
|
319
331
|
stack.push({ node, body, into: body.children });
|
|
320
332
|
}
|
|
321
333
|
while (stack.length > 0) end('unclosed');
|
package/src/markup/parse.ts
CHANGED
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
} from './pair';
|
|
14
14
|
import type { Selectors, Token } from './tokens';
|
|
15
15
|
import { isCodeAttribute } from './code-attributes';
|
|
16
|
-
import { defaultCodeEnd, type CodeEnd } from './code-end';
|
|
16
|
+
import { defaultCodeEnd, indexCodeEnds, type CodeEnd } from './code-end';
|
|
17
17
|
import { lineColumn, sourcePositions } from './line-column';
|
|
18
18
|
|
|
19
19
|
export { lineColumn };
|
|
@@ -93,9 +93,12 @@ function run(
|
|
|
93
93
|
options: ParseMarkupOptions,
|
|
94
94
|
completion = false,
|
|
95
95
|
): unknown {
|
|
96
|
-
const hooks = options.hooks
|
|
97
|
-
|
|
98
|
-
|
|
96
|
+
const hooks = { ...defaultHooks, ...options.hooks };
|
|
97
|
+
if (!options.hooks?.closeBrace) {
|
|
98
|
+
const codeEnd = indexCodeEnds(source);
|
|
99
|
+
hooks.closeBrace = (_input, codeStart, lenientStart) =>
|
|
100
|
+
codeEnd(lenientStart, codeStart);
|
|
101
|
+
}
|
|
99
102
|
try {
|
|
100
103
|
return pegParse(source, {
|
|
101
104
|
startRule,
|
|
@@ -272,19 +275,26 @@ export function scanMarkupTolerant(
|
|
|
272
275
|
const errors: MarkupError[] = [];
|
|
273
276
|
const damage: number[] = [];
|
|
274
277
|
let position: ReturnType<typeof sourcePositions> | undefined;
|
|
275
|
-
//
|
|
276
|
-
//
|
|
277
|
-
|
|
278
|
+
// Share the index across recovery attempts, whose inputs are suffixes or
|
|
279
|
+
// truncated prefixes. Re-indexing each attempt would itself be quadratic.
|
|
280
|
+
const codeEnd = indexCodeEnds(source);
|
|
281
|
+
const customCloseBrace = options.hooks?.closeBrace;
|
|
278
282
|
const lastBrace = source.lastIndexOf('}');
|
|
279
|
-
const { closeBrace } = { ...defaultHooks, ...options.hooks };
|
|
280
283
|
/** `text` shifted by `base`, tokenized: the tokens or the error. */
|
|
281
284
|
const attempt = (text: string, base: number): Token[] | MarkupError => {
|
|
282
285
|
const hooks: Partial<MarkupHooks> = {
|
|
283
286
|
...options.hooks,
|
|
284
|
-
closeBrace: (input, codeStart, lenientStart) =>
|
|
285
|
-
base + Math.min(codeStart, lenientStart) > lastBrace
|
|
286
|
-
|
|
287
|
-
|
|
287
|
+
closeBrace: (input, codeStart, lenientStart) => {
|
|
288
|
+
if (base + Math.min(codeStart, lenientStart) > lastBrace) return -1;
|
|
289
|
+
if (customCloseBrace)
|
|
290
|
+
return customCloseBrace(input, codeStart, lenientStart);
|
|
291
|
+
const end = codeEnd(
|
|
292
|
+
base + lenientStart,
|
|
293
|
+
base + codeStart,
|
|
294
|
+
base + text.length,
|
|
295
|
+
);
|
|
296
|
+
return end < 0 ? -1 : end - base;
|
|
297
|
+
},
|
|
288
298
|
};
|
|
289
299
|
try {
|
|
290
300
|
return (
|
package/src/markup/render.tsx
CHANGED
|
@@ -879,8 +879,6 @@ function nodeKey(node: ASTNode): string {
|
|
|
879
879
|
}
|
|
880
880
|
|
|
881
881
|
function renderMacro(node: MacroNode, key: string) {
|
|
882
|
-
if (isSubMacro(node.name)) return null;
|
|
883
|
-
|
|
884
882
|
// What an invocation passes on to a widget and to a built-in macro alike
|
|
885
883
|
const invocation = {
|
|
886
884
|
rawArgs: node.rawArgs,
|
|
@@ -901,6 +899,8 @@ function renderMacro(node: MacroNode, key: string) {
|
|
|
901
899
|
);
|
|
902
900
|
}
|
|
903
901
|
|
|
902
|
+
if (isSubMacro(node.name)) return null;
|
|
903
|
+
|
|
904
904
|
const Component = getMacro(node.name);
|
|
905
905
|
if (Component) {
|
|
906
906
|
return (
|
|
@@ -990,7 +990,7 @@ export function renderInlineNodes(nodes: ASTNode[]): preact.ComponentChildren {
|
|
|
990
990
|
* it as micromark does.
|
|
991
991
|
*/
|
|
992
992
|
const MARKDOWN_SYNTAX_RE =
|
|
993
|
-
/[*_`#|~\[>\\\-+=\r]|!\[|\d
|
|
993
|
+
/[*_`#|~\[>\\\-+=\r]|!\[|\d{1,9}[.)]|&#?[a-zA-Z0-9]+;| {2}\n|<[!?]/;
|
|
994
994
|
/** Two line endings (LF, CRLF or CR) with only whitespace between them. */
|
|
995
995
|
const BLANK_LINE_RE = /(?:\r\n|\r(?!\n)|\n)\s*[\r\n]/;
|
|
996
996
|
const PLACEHOLDER_STRIP_RE = /<span data-tw=[0-9a-z]+:\d+><\/span>/g;
|