@rohal12/spindle 0.62.5 → 0.62.7
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 +126 -70
- package/dist/pkg/story-variables.js +286 -131
- package/dist/pkg/tooling.js +1 -0
- package/package.json +1 -1
- package/src/code-check.ts +21 -6
- package/src/completion-context.ts +56 -9
- package/src/components/macros/detached-body.tsx +7 -0
- package/src/components/macros/macro-args.ts +84 -1
- package/src/define-macro.ts +3 -57
- package/src/hooks/use-modal-focus.ts +1 -11
- package/src/markup/code-end.ts +26 -6
- package/src/markup/parse.ts +22 -12
- package/src/markup/render.tsx +1 -1
- package/src/story-variables.ts +58 -26
- package/src/tooling.ts +61 -11
- package/src/types-drift-check.ts +9 -0
- package/src/utils/element-visibility.ts +22 -9
- package/types/tooling.d.ts +51 -2
package/dist/pkg/tooling.js
CHANGED
package/package.json
CHANGED
package/src/code-check.ts
CHANGED
|
@@ -233,17 +233,25 @@ export interface ParametersOf {
|
|
|
233
233
|
source?(name: string): 'builtin' | 'user' | undefined;
|
|
234
234
|
}
|
|
235
235
|
|
|
236
|
-
/**
|
|
237
|
-
export function
|
|
236
|
+
/** Whether builtin-only interpretation still belongs to this implementation. */
|
|
237
|
+
export function usesBuiltinSemantics(
|
|
238
238
|
name: string,
|
|
239
239
|
parametersOf: ParametersOf,
|
|
240
240
|
): boolean {
|
|
241
241
|
return (
|
|
242
|
-
|
|
242
|
+
parametersOf.widget?.(name) === undefined &&
|
|
243
243
|
parametersOf.source?.(name) !== 'user'
|
|
244
244
|
);
|
|
245
245
|
}
|
|
246
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);
|
|
253
|
+
}
|
|
254
|
+
|
|
247
255
|
/** What the argument check needs to know of a macro (see MacroMetadata). */
|
|
248
256
|
export interface MacroParameters {
|
|
249
257
|
name: string;
|
|
@@ -298,13 +306,14 @@ export function parameterLookup(
|
|
|
298
306
|
sources.set(name, source ?? 'user');
|
|
299
307
|
if (params)
|
|
300
308
|
parameters.set(name, withHolds(params, holdsByDefault(interpolate)));
|
|
309
|
+
else parameters.delete(name);
|
|
301
310
|
if (widget)
|
|
302
311
|
widgets.set(name, widgetParameters(widget.params, widget.holds) ?? []);
|
|
303
312
|
}
|
|
304
313
|
const lookup: ParametersOf = (name) => {
|
|
305
314
|
if (widgets.has(name)) return undefined;
|
|
306
315
|
let params = parameters.get(name);
|
|
307
|
-
if (!params) {
|
|
316
|
+
if (!params && sources.get(name) !== 'user') {
|
|
308
317
|
const sub = subMacroParameters(name);
|
|
309
318
|
if (sub) parameters.set(name, (params = withHolds(sub, 'text')));
|
|
310
319
|
}
|
|
@@ -434,7 +443,10 @@ export function* codeAndText(
|
|
|
434
443
|
continue;
|
|
435
444
|
}
|
|
436
445
|
const args = token.rawArgs;
|
|
437
|
-
if (
|
|
446
|
+
if (
|
|
447
|
+
PASSAGE_BODIES.has(name) &&
|
|
448
|
+
usesBuiltinSemantics(name, parametersOf)
|
|
449
|
+
) {
|
|
438
450
|
// The body names the passage when it is only text
|
|
439
451
|
const close = closeOfTextBody(t, name);
|
|
440
452
|
if (close > t + 1) {
|
|
@@ -453,7 +465,10 @@ export function* codeAndText(
|
|
|
453
465
|
if (!args) continue;
|
|
454
466
|
const label = `{${token.name} ${args}}`;
|
|
455
467
|
const argsAt = token.argsStart;
|
|
456
|
-
if (
|
|
468
|
+
if (
|
|
469
|
+
CONDITION_MACROS.has(name) &&
|
|
470
|
+
usesBuiltinSemantics(name, parametersOf)
|
|
471
|
+
) {
|
|
457
472
|
yield {
|
|
458
473
|
kind: 'code',
|
|
459
474
|
code: args,
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
escapeOffsets,
|
|
8
8
|
parameterLookup,
|
|
9
9
|
executesRawBody,
|
|
10
|
+
usesBuiltinSemantics,
|
|
10
11
|
type PieceBase,
|
|
11
12
|
} from './code-check';
|
|
12
13
|
import { macroArgumentSpans } from './components/macros/macro-args';
|
|
@@ -19,9 +20,22 @@ import {
|
|
|
19
20
|
} from './js-lexer';
|
|
20
21
|
import type { ToolingMacro } from './tooling';
|
|
21
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
|
+
|
|
22
29
|
export type CompletionContext =
|
|
23
30
|
| { kind: 'literal' }
|
|
24
|
-
| (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
|
+
});
|
|
25
39
|
|
|
26
40
|
const LITERAL = { kind: 'literal' } as const;
|
|
27
41
|
|
|
@@ -45,6 +59,7 @@ export function completionContext(
|
|
|
45
59
|
code: string,
|
|
46
60
|
goal: JsGoal,
|
|
47
61
|
map: Mapping,
|
|
62
|
+
containers: readonly CompletionContainer[],
|
|
48
63
|
): CompletionContext => {
|
|
49
64
|
// A following identifier probes the lexer's state at EOF. It cannot
|
|
50
65
|
// close a string, regex, comment or template, and needs no guessed
|
|
@@ -72,6 +87,7 @@ export function completionContext(
|
|
|
72
87
|
goal,
|
|
73
88
|
offset,
|
|
74
89
|
cursor: code.length,
|
|
90
|
+
...(containers.length ? { containers } : {}),
|
|
75
91
|
...(offsets.some((at, i) => at !== offset + i)
|
|
76
92
|
? { sourceOffsets: offsets }
|
|
77
93
|
: {}),
|
|
@@ -84,6 +100,7 @@ export function completionContext(
|
|
|
84
100
|
text = false,
|
|
85
101
|
depth = 0,
|
|
86
102
|
sigilsOnly = false,
|
|
103
|
+
containers: readonly CompletionContainer[] = [],
|
|
87
104
|
): CompletionContext => {
|
|
88
105
|
if (depth >= 64) return LITERAL;
|
|
89
106
|
if (sigilsOnly) {
|
|
@@ -91,7 +108,12 @@ export function completionContext(
|
|
|
91
108
|
for (const part of splitSigilTemplate(src, true)) {
|
|
92
109
|
if ('expr' in part) {
|
|
93
110
|
if (at + 1 + part.expr.length === src.length) {
|
|
94
|
-
return codeContext(
|
|
111
|
+
return codeContext(
|
|
112
|
+
part.expr,
|
|
113
|
+
'expression',
|
|
114
|
+
(i) => map(at + 1 + i),
|
|
115
|
+
containers,
|
|
116
|
+
);
|
|
95
117
|
}
|
|
96
118
|
at += part.expr.length + 2;
|
|
97
119
|
} else at += ('text' in part ? part.text : part.verbatim).length;
|
|
@@ -108,12 +130,13 @@ export function completionContext(
|
|
|
108
130
|
const token = tokens[tokens.length - 1];
|
|
109
131
|
if (!token || token.end !== src.length) return LITERAL;
|
|
110
132
|
const codeAt = (code: string, at: number, goal: JsGoal = 'expression') =>
|
|
111
|
-
codeContext(code, goal, (i) => map(at + i));
|
|
133
|
+
codeContext(code, goal, (i) => map(at + i), containers);
|
|
112
134
|
const nested = (
|
|
113
135
|
value: string,
|
|
114
136
|
at: number,
|
|
115
137
|
offsets?: readonly number[],
|
|
116
138
|
onlySigils = false,
|
|
139
|
+
container?: CompletionContainer,
|
|
117
140
|
) =>
|
|
118
141
|
read(
|
|
119
142
|
value,
|
|
@@ -121,6 +144,7 @@ export function completionContext(
|
|
|
121
144
|
true,
|
|
122
145
|
depth + 1,
|
|
123
146
|
onlySigils,
|
|
147
|
+
container ? [...containers, container] : containers,
|
|
124
148
|
);
|
|
125
149
|
|
|
126
150
|
if (token.type === 'variable' && token.nameEnd === src.length) {
|
|
@@ -132,11 +156,21 @@ export function completionContext(
|
|
|
132
156
|
if (token.type === 'html') {
|
|
133
157
|
for (const { name, value, at } of attributeValues(src, token)) {
|
|
134
158
|
if (at + value.length === src.length) {
|
|
135
|
-
|
|
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
|
+
});
|
|
136
168
|
}
|
|
137
169
|
}
|
|
138
170
|
} else if (token.type === 'link' && token.displayEnd === src.length) {
|
|
139
|
-
return nested(token.display, token.displayStart
|
|
171
|
+
return nested(token.display, token.displayStart, undefined, false, {
|
|
172
|
+
kind: 'link-label',
|
|
173
|
+
});
|
|
140
174
|
} else if (token.type === 'text' && !token.comment) {
|
|
141
175
|
const opener = tokens[tokens.length - 2];
|
|
142
176
|
if (
|
|
@@ -154,7 +188,10 @@ export function completionContext(
|
|
|
154
188
|
const name = token.name.toLowerCase();
|
|
155
189
|
const args = token.rawArgs;
|
|
156
190
|
const widget = parametersOf.widget?.(name);
|
|
157
|
-
if (
|
|
191
|
+
if (
|
|
192
|
+
usesBuiltinSemantics(name, parametersOf) &&
|
|
193
|
+
['if', 'elseif', 'case'].includes(name)
|
|
194
|
+
) {
|
|
158
195
|
return codeAt(args, token.argsStart);
|
|
159
196
|
}
|
|
160
197
|
const stringContext = (
|
|
@@ -176,13 +213,23 @@ export function completionContext(
|
|
|
176
213
|
? stringLiteralValue(raw + raw[0])
|
|
177
214
|
: quoted.value;
|
|
178
215
|
if (value == null) return LITERAL;
|
|
216
|
+
const container: CompletionContainer = {
|
|
217
|
+
kind: 'string',
|
|
218
|
+
quote: raw[0] as '"' | "'",
|
|
219
|
+
escapes,
|
|
220
|
+
};
|
|
179
221
|
const offsets = escapeOffsets(raw.slice(1), at + 1, value, escapes);
|
|
180
222
|
if (holds === 'expression' || holds === 'statements') {
|
|
181
|
-
return codeContext(
|
|
182
|
-
|
|
223
|
+
return codeContext(
|
|
224
|
+
value,
|
|
225
|
+
holds,
|
|
226
|
+
(i) => map(offsets?.[i] ?? at + 1 + i),
|
|
227
|
+
[...containers, container],
|
|
183
228
|
);
|
|
184
229
|
}
|
|
185
|
-
return holds === 'markup'
|
|
230
|
+
return holds === 'markup'
|
|
231
|
+
? nested(value, at + 1, offsets, false, container)
|
|
232
|
+
: LITERAL;
|
|
186
233
|
};
|
|
187
234
|
if (widget) {
|
|
188
235
|
const spans = splitArgSpans(args);
|
|
@@ -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
|
}
|
|
@@ -19,7 +19,12 @@ import type { Span as SourceSpan } from '../../markup/tokens';
|
|
|
19
19
|
import type { MacroArgs, ParameterDef } from '../../registry';
|
|
20
20
|
import type { StoryState } from '../../store';
|
|
21
21
|
import { noPassageError } from '../../runtime-errors';
|
|
22
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
parseMemberIdentifier,
|
|
24
|
+
parseReferencePath,
|
|
25
|
+
stringLiteralValue,
|
|
26
|
+
} from '../../js-lexer';
|
|
27
|
+
import { RESERVED_NAME } from '../../utils/namespace';
|
|
23
28
|
import {
|
|
24
29
|
endsWithOperator,
|
|
25
30
|
isWhitespace,
|
|
@@ -451,6 +456,84 @@ export function readBoundVariable(rawArgs: string): string {
|
|
|
451
456
|
return first ? slice(src, first) : '';
|
|
452
457
|
}
|
|
453
458
|
|
|
459
|
+
/** A runtime input binding, with UTF-16 spans into the raw arguments. */
|
|
460
|
+
export interface BoundVariable extends SourceSpan {
|
|
461
|
+
/** Decoded story-variable root and dot members. */
|
|
462
|
+
name: string;
|
|
463
|
+
path: string[];
|
|
464
|
+
/** Whether the author wrote the optional literal `$` sigil. */
|
|
465
|
+
explicitSigil: boolean;
|
|
466
|
+
/** Editable root spelling, excluding quotes, sigil and dot members. */
|
|
467
|
+
nameStart: number;
|
|
468
|
+
nameEnd: number;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Read the first term as the runtime's story-variable binding. Quotes are
|
|
473
|
+
* stripped, but this is an identifier path, not a JavaScript string literal:
|
|
474
|
+
* only identifier escapes are decoded. Returns null for a missing binding;
|
|
475
|
+
* throws for rejected namespaces, invalid escapes and reserved path names.
|
|
476
|
+
* The full span excludes quotes and includes the sigil when written.
|
|
477
|
+
*/
|
|
478
|
+
export function parseBoundVariable(rawArgs: string): BoundVariable | null {
|
|
479
|
+
const term = readBoundVariable(rawArgs);
|
|
480
|
+
if (!term) return null;
|
|
481
|
+
const lead = rawArgs.length - rawArgs.trimStart().length;
|
|
482
|
+
const offsets: number[] = [];
|
|
483
|
+
for (let i = 0; i < term.length; i++) {
|
|
484
|
+
if (term[i] !== '"' && term[i] !== "'") offsets.push(lead + i);
|
|
485
|
+
}
|
|
486
|
+
const firstToken = term.replace(/["']/g, '');
|
|
487
|
+
const unbindable: Record<string, string> = {
|
|
488
|
+
'%': 'transient variables',
|
|
489
|
+
_: 'temporary variables',
|
|
490
|
+
'@': 'locals',
|
|
491
|
+
};
|
|
492
|
+
const kind = unbindable[firstToken.charAt(0)];
|
|
493
|
+
if (kind) {
|
|
494
|
+
throw new Error(`${kind} (${firstToken}) cannot be bound to input macros`);
|
|
495
|
+
}
|
|
496
|
+
const explicitSigil = firstToken.startsWith('$');
|
|
497
|
+
if (!explicitSigil && firstToken.split('.')[0]!.includes('\\')) {
|
|
498
|
+
throw new Error(
|
|
499
|
+
'Escaped binding identifiers require a literal $ story-variable sigil',
|
|
500
|
+
);
|
|
501
|
+
}
|
|
502
|
+
const segments = firstToken
|
|
503
|
+
.replace(/^\$/, '')
|
|
504
|
+
.split('.')
|
|
505
|
+
.map((segment, index) => {
|
|
506
|
+
if (!segment.includes('\\')) return segment;
|
|
507
|
+
if (index > 0) {
|
|
508
|
+
const name = parseMemberIdentifier(segment);
|
|
509
|
+
if (name === null) throw new Error('Invalid bound identifier');
|
|
510
|
+
return name;
|
|
511
|
+
}
|
|
512
|
+
const reference = parseReferencePath(`$${segment}`);
|
|
513
|
+
if (!reference || reference.nested)
|
|
514
|
+
throw new Error('Invalid bound identifier');
|
|
515
|
+
return reference.name;
|
|
516
|
+
});
|
|
517
|
+
if (segments.includes(RESERVED_NAME)) {
|
|
518
|
+
throw new Error(
|
|
519
|
+
`"$${segments.join('.')}" cannot be bound: ${RESERVED_NAME} is reserved`,
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
const rootStart = explicitSigil ? 1 : 0;
|
|
523
|
+
const rootEnd = firstToken.indexOf('.', rootStart);
|
|
524
|
+
const nameEnd = rootEnd < 0 ? firstToken.length : rootEnd;
|
|
525
|
+
return {
|
|
526
|
+
name: segments[0]!,
|
|
527
|
+
path: segments.slice(1),
|
|
528
|
+
explicitSigil,
|
|
529
|
+
start: offsets[0] ?? lead,
|
|
530
|
+
end: offsets.length ? offsets[offsets.length - 1]! + 1 : lead,
|
|
531
|
+
nameStart: offsets[rootStart] ?? lead + rootStart,
|
|
532
|
+
nameEnd:
|
|
533
|
+
offsets[nameEnd - 1] === undefined ? lead : offsets[nameEnd - 1]! + 1,
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
|
|
454
537
|
/** A consumed argument and its declared parameter, with UTF-16 source offsets. */
|
|
455
538
|
export interface MacroArgumentSpan extends SourceSpan {
|
|
456
539
|
/** The original parameter object, including for nested keyword options. */
|
package/src/define-macro.ts
CHANGED
|
@@ -29,8 +29,6 @@ import { executeMutation, mutateState, readState } from './execute-mutation';
|
|
|
29
29
|
import { evaluateDisplay } from './expression';
|
|
30
30
|
import { useStoryFields } from './hooks/use-story-fields';
|
|
31
31
|
import { getByPath, setByPath } from './utils/object-path';
|
|
32
|
-
import { RESERVED_NAME } from './utils/namespace';
|
|
33
|
-
import { parseMemberIdentifier, parseReferencePath } from './js-lexer';
|
|
34
32
|
import { useAction } from './hooks/use-action';
|
|
35
33
|
import type { UseActionOptions } from './hooks/use-action';
|
|
36
34
|
import { collectText } from './utils/extract-text';
|
|
@@ -40,7 +38,7 @@ import { wrapContent } from './components/macros/display';
|
|
|
40
38
|
import {
|
|
41
39
|
MacroArgumentError,
|
|
42
40
|
parseMacroArgs,
|
|
43
|
-
|
|
41
|
+
parseBoundVariable,
|
|
44
42
|
} from './components/macros/macro-args';
|
|
45
43
|
import { MacroError } from './components/macros/MacroError';
|
|
46
44
|
import {
|
|
@@ -241,66 +239,14 @@ export function defineMacro<const P extends readonly ParameterDef[] = []>(
|
|
|
241
239
|
}
|
|
242
240
|
|
|
243
241
|
if (config.storeVar) {
|
|
244
|
-
const firstToken = readBoundVariable(props.rawArgs).replace(/["']/g, '');
|
|
245
|
-
|
|
246
|
-
// Only story variables ($name) can be bound: another sigil would be
|
|
247
|
-
// read as a story key spelled with it (#414)
|
|
248
|
-
const unbindable: Record<string, string> = {
|
|
249
|
-
'%': 'transient variables',
|
|
250
|
-
_: 'temporary variables',
|
|
251
|
-
'@': 'locals',
|
|
252
|
-
};
|
|
253
|
-
const kind = unbindable[firstToken.charAt(0)];
|
|
254
|
-
if (kind) {
|
|
255
|
-
return h(
|
|
256
|
-
'span',
|
|
257
|
-
{ class: 'error' },
|
|
258
|
-
`{${config.name}}: ${kind} (${firstToken}) cannot be bound to input macros`,
|
|
259
|
-
);
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
if (
|
|
263
|
-
!firstToken.startsWith('$') &&
|
|
264
|
-
firstToken.split('.')[0]!.includes('\\')
|
|
265
|
-
) {
|
|
266
|
-
return h(MacroError, {
|
|
267
|
-
macro: config.name,
|
|
268
|
-
error: new Error(
|
|
269
|
-
'Escaped binding identifiers require a literal $ story-variable sigil',
|
|
270
|
-
),
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
|
|
274
242
|
let segments: string[];
|
|
275
243
|
try {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
.split('.')
|
|
279
|
-
.map((segment, index) => {
|
|
280
|
-
if (!segment.includes('\\')) return segment;
|
|
281
|
-
// Members follow JavaScript's identifier grammar, which accepts
|
|
282
|
-
// more names than story-variable roots (for example $foo and é).
|
|
283
|
-
if (index > 0) {
|
|
284
|
-
const name = parseMemberIdentifier(segment);
|
|
285
|
-
if (name === null) throw new Error('Invalid bound identifier');
|
|
286
|
-
return name;
|
|
287
|
-
}
|
|
288
|
-
const reference = parseReferencePath(`$${segment}`);
|
|
289
|
-
if (!reference || reference.nested)
|
|
290
|
-
throw new Error('Invalid bound identifier');
|
|
291
|
-
return reference.name;
|
|
292
|
-
});
|
|
244
|
+
const binding = parseBoundVariable(props.rawArgs);
|
|
245
|
+
segments = binding ? [binding.name, ...binding.path] : [''];
|
|
293
246
|
} catch (error) {
|
|
294
247
|
return h(MacroError, { macro: config.name, error });
|
|
295
248
|
}
|
|
296
249
|
const varExpr = segments.join('.');
|
|
297
|
-
if (segments.includes(RESERVED_NAME)) {
|
|
298
|
-
return h(
|
|
299
|
-
'span',
|
|
300
|
-
{ class: 'error' },
|
|
301
|
-
`{${config.name}}: "$${varExpr}" cannot be bound: ${RESERVED_NAME} is reserved`,
|
|
302
|
-
);
|
|
303
|
-
}
|
|
304
250
|
ctx.varName = varExpr;
|
|
305
251
|
ctx.value = getByPath(useStoryFields('variables').variables, segments);
|
|
306
252
|
// In program order, also when mutation code performs the input
|
|
@@ -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/markup/code-end.ts
CHANGED
|
@@ -183,7 +183,9 @@ export const defaultCodeEnd: CodeEnd = jsCodeEnd();
|
|
|
183
183
|
* still bound how many pending braces could close. Indexing visits each
|
|
184
184
|
* opener once, so unmatched plain-code openers can skip suffix scans.
|
|
185
185
|
*/
|
|
186
|
-
export function indexCodeEnds(
|
|
186
|
+
export function indexCodeEnds(
|
|
187
|
+
input: string,
|
|
188
|
+
): (start: number, codeStart?: number, limit?: number) => number {
|
|
187
189
|
const possible = new Set<number>();
|
|
188
190
|
const pending: number[] = [];
|
|
189
191
|
let remaining = 0;
|
|
@@ -213,12 +215,30 @@ export function indexCodeEnds(input: string): (start: number) => number {
|
|
|
213
215
|
if (open !== undefined) possible.add(open);
|
|
214
216
|
} else if (/['"`/\\]/.test(c)) uncertain();
|
|
215
217
|
}
|
|
216
|
-
return (start) => {
|
|
217
|
-
if (start > lastBrace) return -1;
|
|
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);
|
|
218
221
|
// A syntax error may end the parser's recovery at an inner `}`, even
|
|
219
222
|
// when neither the token reader nor the lenient reader can close here.
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
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;
|
|
223
243
|
};
|
|
224
244
|
}
|
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
|
@@ -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;
|