@rohal12/spindle 0.62.3 → 0.62.5
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 +86 -24
- package/dist/pkg/play-worker.js +95 -7
- package/dist/pkg/play.js +3 -3
- package/dist/pkg/story-variables.js +81 -14
- package/package.json +1 -1
- package/src/code-check.ts +25 -2
- package/src/completion-context.ts +2 -4
- package/src/components/macros/PassageDisplay.tsx +3 -16
- package/src/format/format-passage.ts +3 -3
- package/src/hooks/use-render-options.ts +4 -0
- 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 +48 -1
- package/src/markup/pair.ts +14 -2
- package/src/markup/render.tsx +8 -6
- package/src/play/session.ts +3 -3
- package/src/play/window-events.ts +63 -0
- package/src/play/worker.ts +62 -9
- 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 +2 -0
- package/src/widgets/ast-scanner.ts +2 -1
- package/types/play.d.ts +6 -5
- package/types/tooling.d.ts +2 -0
|
@@ -3825,6 +3825,11 @@ function branchParents(name, macros) {
|
|
|
3825
3825
|
if (!macros) return [...companionParents.get(name) ?? []].filter((parent) => parent !== "listbox");
|
|
3826
3826
|
return macros.filter((macro) => macro.name.toLowerCase() !== "listbox" && macro.subMacros.some((sub) => sub.toLowerCase() === name.toLowerCase())).map((macro) => macro.name.toLowerCase());
|
|
3827
3827
|
}
|
|
3828
|
+
/** The declared parameters of the macro `name`, if it declares any. */
|
|
3829
|
+
function getMacroParameters(name) {
|
|
3830
|
+
return metadataRegistry.get(name)?.parameters;
|
|
3831
|
+
}
|
|
3832
|
+
getMacroParameters.source = (name) => metadataRegistry.get(name)?.source;
|
|
3828
3833
|
function getMacroRegistry() {
|
|
3829
3834
|
return Array.from(metadataRegistry.values());
|
|
3830
3835
|
}
|
|
@@ -3943,6 +3948,8 @@ function pairMarkup(tokens, options = {}) {
|
|
|
3943
3948
|
};
|
|
3944
3949
|
const root = [];
|
|
3945
3950
|
const stack = [];
|
|
3951
|
+
const openPositions = /* @__PURE__ */ new Map();
|
|
3952
|
+
const tagKey = (tag) => `${tag.type}:${lowerName(tag)}`;
|
|
3946
3953
|
const add = (node) => (last$1(stack)?.into ?? root).push(node);
|
|
3947
3954
|
/**
|
|
3948
3955
|
* End the element on top of the stack, with `close` if it has one: its
|
|
@@ -3951,6 +3958,10 @@ function pairMarkup(tokens, options = {}) {
|
|
|
3951
3958
|
const end = (ending, close, owns = ending === "closed") => {
|
|
3952
3959
|
const { node, body } = stack.pop();
|
|
3953
3960
|
const open = node.token;
|
|
3961
|
+
const key = tagKey(open);
|
|
3962
|
+
const positions = openPositions.get(key);
|
|
3963
|
+
positions.pop();
|
|
3964
|
+
if (positions.length === 0) openPositions.delete(key);
|
|
3954
3965
|
const noticedAt = close ? close.end : noticedAtEnd;
|
|
3955
3966
|
for (const { tag } of body.branches) if (open.type !== "macro" || !parentsOf(tag.name).includes(lowerName(open))) misplaced(tag, open, noticedAt);
|
|
3956
3967
|
if (ending === "unclosed") report("unclosed-block", open, `Unclosed ${tagLabel(open)}: no ${closerOf(open)} closes it`, noticedAt, { name: nameOf(open) });
|
|
@@ -3969,8 +3980,7 @@ function pairMarkup(tokens, options = {}) {
|
|
|
3969
3980
|
if (!top) report("stray-closer", token, `${tagLabel(token)} closes nothing: no ${token.type === "html" ? `<${token.tag}>` : `{${token.name}}`} is open here`, token.start, { name: nameOf(token) });
|
|
3970
3981
|
else if (closes(top.node.token, token)) end("closed", token);
|
|
3971
3982
|
else {
|
|
3972
|
-
|
|
3973
|
-
while (outer >= 0 && !closes(stack[outer].node.token, token)) outer--;
|
|
3983
|
+
const outer = last$1(openPositions.get(tagKey(token)) ?? []) ?? -1;
|
|
3974
3984
|
end("mismatch", token, outer === -1);
|
|
3975
3985
|
if (outer !== -1) {
|
|
3976
3986
|
while (stack.length > outer + 1) end("cut", token);
|
|
@@ -4014,6 +4024,10 @@ function pairMarkup(tokens, options = {}) {
|
|
|
4014
4024
|
branches: []
|
|
4015
4025
|
};
|
|
4016
4026
|
node.body = body;
|
|
4027
|
+
const key = tagKey(token);
|
|
4028
|
+
const positions = openPositions.get(key) ?? [];
|
|
4029
|
+
positions.push(stack.length);
|
|
4030
|
+
openPositions.set(key, positions);
|
|
4017
4031
|
stack.push({
|
|
4018
4032
|
node,
|
|
4019
4033
|
body,
|
|
@@ -10211,6 +10225,40 @@ function jsCodeEnd(onOpenEnd) {
|
|
|
10211
10225
|
}
|
|
10212
10226
|
/** The JavaScript reader Spindle uses today: js-lexer, then a lenient scan. */
|
|
10213
10227
|
var defaultCodeEnd = jsCodeEnd();
|
|
10228
|
+
/**
|
|
10229
|
+
* Index possible brace ends for repeated recovery attempts in one template.
|
|
10230
|
+
* Within plain code, nested braces must close first. At a quote, slash or
|
|
10231
|
+
* escape we stop making lexical assumptions, but the remaining raw `}`s
|
|
10232
|
+
* still bound how many pending braces could close. Indexing visits each
|
|
10233
|
+
* opener once, so unmatched plain-code openers can skip suffix scans.
|
|
10234
|
+
*/
|
|
10235
|
+
function indexCodeEnds(input) {
|
|
10236
|
+
const possible = /* @__PURE__ */ new Set();
|
|
10237
|
+
const pending = [];
|
|
10238
|
+
let remaining = 0;
|
|
10239
|
+
let lastBrace = -1;
|
|
10240
|
+
for (let i = 0; i < input.length; i++) if (input[i] === "}") {
|
|
10241
|
+
remaining++;
|
|
10242
|
+
lastBrace = i;
|
|
10243
|
+
}
|
|
10244
|
+
const uncertain = () => {
|
|
10245
|
+
for (let i = Math.max(0, pending.length - remaining); i < pending.length; i++) possible.add(pending[i]);
|
|
10246
|
+
pending.length = 0;
|
|
10247
|
+
};
|
|
10248
|
+
for (let i = 0; i < input.length; i++) {
|
|
10249
|
+
const c = input[i];
|
|
10250
|
+
if (c === "{") pending.push(i);
|
|
10251
|
+
else if (c === "}") {
|
|
10252
|
+
remaining--;
|
|
10253
|
+
const open = pending.pop();
|
|
10254
|
+
if (open !== void 0) possible.add(open);
|
|
10255
|
+
} else if (/['"`/\\]/.test(c)) uncertain();
|
|
10256
|
+
}
|
|
10257
|
+
return (start) => {
|
|
10258
|
+
if (start > lastBrace) return -1;
|
|
10259
|
+
return possible.has(start - 1) ? defaultCodeEnd.closeBrace(input, start, start) : parsedCodeEnd(input, start, "expression");
|
|
10260
|
+
};
|
|
10261
|
+
}
|
|
10214
10262
|
//#endregion
|
|
10215
10263
|
//#region src/markup/code-attributes.ts
|
|
10216
10264
|
/**
|
|
@@ -10236,6 +10284,7 @@ function splitSigilTemplate(template, incomplete = false) {
|
|
|
10236
10284
|
const parts = [];
|
|
10237
10285
|
let text = "";
|
|
10238
10286
|
let i = 0;
|
|
10287
|
+
const closeBrace = indexCodeEnds(template);
|
|
10239
10288
|
while (i < template.length) {
|
|
10240
10289
|
const brace = template.indexOf("{", i);
|
|
10241
10290
|
if (brace === -1) {
|
|
@@ -10244,7 +10293,7 @@ function splitSigilTemplate(template, incomplete = false) {
|
|
|
10244
10293
|
}
|
|
10245
10294
|
text += template.slice(i, brace);
|
|
10246
10295
|
i = brace + 1;
|
|
10247
|
-
let end = isSigil(template[i]) ?
|
|
10296
|
+
let end = isSigil(template[i]) ? closeBrace(i) : -1;
|
|
10248
10297
|
if (end === -1 && incomplete && isSigil(template[i])) end = template.length;
|
|
10249
10298
|
if (end === -1) {
|
|
10250
10299
|
text += "{";
|
|
@@ -11155,6 +11204,10 @@ function widgetParameters(params, holds = {}) {
|
|
|
11155
11204
|
...hasOwn(holds, name) ? { holds: holds[name] } : {}
|
|
11156
11205
|
}));
|
|
11157
11206
|
}
|
|
11207
|
+
/** A raw body is executable only while the builtin implementation owns it. */
|
|
11208
|
+
function executesRawBody(name, parametersOf) {
|
|
11209
|
+
return isRawMacro(name, parametersOf.widget?.(name) !== void 0) && parametersOf.source?.(name) !== "user";
|
|
11210
|
+
}
|
|
11158
11211
|
/**
|
|
11159
11212
|
* `params` with each `string` and `text` parameter (options too) saying
|
|
11160
11213
|
* what it holds: what it declares, else `holds`.
|
|
@@ -11182,7 +11235,9 @@ function withHolds(params, holds) {
|
|
|
11182
11235
|
function parameterLookup(macros) {
|
|
11183
11236
|
const parameters = new NameMap();
|
|
11184
11237
|
const widgets = new NameMap();
|
|
11185
|
-
|
|
11238
|
+
const sources = new NameMap();
|
|
11239
|
+
for (const { name, parameters: params, interpolate, widget, source } of macros) {
|
|
11240
|
+
sources.set(name, source ?? "user");
|
|
11186
11241
|
if (params) parameters.set(name, withHolds(params, holdsByDefault(interpolate)));
|
|
11187
11242
|
if (widget) widgets.set(name, widgetParameters(widget.params, widget.holds) ?? []);
|
|
11188
11243
|
}
|
|
@@ -11196,6 +11251,7 @@ function parameterLookup(macros) {
|
|
|
11196
11251
|
return params;
|
|
11197
11252
|
};
|
|
11198
11253
|
if (widgets.size > 0) lookup.widget = (name) => widgets.get(name);
|
|
11254
|
+
lookup.source = (name) => sources.get(name);
|
|
11199
11255
|
return lookup;
|
|
11200
11256
|
}
|
|
11201
11257
|
/** What a `string` or `text` argument holds when its parameter doesn't say. */
|
|
@@ -11261,7 +11317,7 @@ function* codeAndText(src, tokens, parametersOf) {
|
|
|
11261
11317
|
else if (token.type === "macro" && !token.isClose) {
|
|
11262
11318
|
const name = token.name.toLowerCase();
|
|
11263
11319
|
const widget = parametersOf.widget?.(name);
|
|
11264
|
-
if (name
|
|
11320
|
+
if (executesRawBody(name, parametersOf)) {
|
|
11265
11321
|
const close = closeOfTextBody(t, name);
|
|
11266
11322
|
if (close < 0) continue;
|
|
11267
11323
|
yield {
|
|
@@ -11701,7 +11757,7 @@ function completionContext(source, cursor, macros = []) {
|
|
|
11701
11757
|
} else if (token.type === "link" && token.displayEnd === src.length) return nested(token.display, token.displayStart);
|
|
11702
11758
|
else if (token.type === "text" && !token.comment) {
|
|
11703
11759
|
const opener = tokens[tokens.length - 2];
|
|
11704
|
-
if (opener?.type === "macro" && !opener.isClose &&
|
|
11760
|
+
if (opener?.type === "macro" && !opener.isClose && executesRawBody(opener.name, parametersOf)) return codeAt(token.value, token.start, "statements");
|
|
11705
11761
|
} else if (token.type === "macro" && !token.isClose && token.argsEnd === src.length) {
|
|
11706
11762
|
const name = token.name.toLowerCase();
|
|
11707
11763
|
const args = token.rawArgs;
|
|
@@ -11757,6 +11813,7 @@ function completionContext(source, cursor, macros = []) {
|
|
|
11757
11813
|
//#region src/widgets/ast-scanner.ts
|
|
11758
11814
|
/** The declared parameters of the macros registered so far. */
|
|
11759
11815
|
var registeredParameters = (name) => parameterLookup(getMacroRegistry())(name);
|
|
11816
|
+
registeredParameters.source = getMacroParameters.source;
|
|
11760
11817
|
/**
|
|
11761
11818
|
* Whether the text of an attribute value or macro label renders a
|
|
11762
11819
|
* `{@children}`: it is markup, parsed as the renderer parses it.
|
|
@@ -13031,7 +13088,7 @@ function collectTokenRefs(content, tokens, at, emit, options) {
|
|
|
13031
13088
|
});
|
|
13032
13089
|
} else if (!/^["'`]/.test(first)) scanCode(first, (i) => at(token.argsStart + lead + i), emit);
|
|
13033
13090
|
}
|
|
13034
|
-
if (token.name
|
|
13091
|
+
if (executesRawBody(token.name, parametersOf)) {
|
|
13035
13092
|
let close = t + 1;
|
|
13036
13093
|
while (close < tokens.length) {
|
|
13037
13094
|
const c = tokens[close];
|
|
@@ -13195,6 +13252,11 @@ var Bindings = class {
|
|
|
13195
13252
|
readGroups() {
|
|
13196
13253
|
const { code, groups } = this;
|
|
13197
13254
|
const stack = [];
|
|
13255
|
+
const active = {
|
|
13256
|
+
"{": [],
|
|
13257
|
+
"(": [],
|
|
13258
|
+
"[": []
|
|
13259
|
+
};
|
|
13198
13260
|
for (let i = 0; i < code.length; i++) {
|
|
13199
13261
|
const c = code.charAt(i);
|
|
13200
13262
|
if (c === "{" || c === "(" || c === "[") {
|
|
@@ -13205,15 +13267,16 @@ var Bindings = class {
|
|
|
13205
13267
|
parent: stack[stack.length - 1]
|
|
13206
13268
|
};
|
|
13207
13269
|
groups.push(group);
|
|
13270
|
+
active[c].push(stack.length);
|
|
13208
13271
|
stack.push(group);
|
|
13209
13272
|
this.byOpen.set(i, group);
|
|
13210
13273
|
} else if (CLOSERS[c]) {
|
|
13211
|
-
|
|
13212
|
-
|
|
13213
|
-
if (k
|
|
13274
|
+
const positions = active[CLOSERS[c]];
|
|
13275
|
+
const k = positions[positions.length - 1];
|
|
13276
|
+
if (k !== void 0) {
|
|
13214
13277
|
stack[k].close = i;
|
|
13215
13278
|
this.byClose.set(i, stack[k]);
|
|
13216
|
-
stack.length
|
|
13279
|
+
while (stack.length > k) active[stack.pop().kind].pop();
|
|
13217
13280
|
}
|
|
13218
13281
|
}
|
|
13219
13282
|
}
|
|
@@ -25211,8 +25274,8 @@ async function formatLf(text, options) {
|
|
|
25211
25274
|
output = output.replace(/\n*$/, "\n");
|
|
25212
25275
|
return replaceAll(output.split("\n").map((line) => literalLines.get(line.trim()) ?? line).join("\n"), sentinel, "\n");
|
|
25213
25276
|
}
|
|
25214
|
-
/** HTML
|
|
25215
|
-
var WHITESPACE_AS_WRITTEN = /<(?:pre|textarea)[\s>]|white-space\s*:/i;
|
|
25277
|
+
/** Literal HTML and opaque SVG, whose text can inherit significant whitespace. */
|
|
25278
|
+
var WHITESPACE_AS_WRITTEN = /<(?:pre|textarea|svg)[\s>]|white-space\s*:/i;
|
|
25216
25279
|
/** Preserve whole literal elements, including openers beside nested tags. */
|
|
25217
25280
|
function whitespaceSensitiveLines(source, isBlock) {
|
|
25218
25281
|
const lines = /* @__PURE__ */ new Set();
|
|
@@ -25222,7 +25285,11 @@ function whitespaceSensitiveLines(source, isBlock) {
|
|
|
25222
25285
|
function visit(nodes) {
|
|
25223
25286
|
for (const node of nodes) {
|
|
25224
25287
|
const { token, body } = node;
|
|
25225
|
-
if (token.type === "html" && ([
|
|
25288
|
+
if (token.type === "html" && ([
|
|
25289
|
+
"pre",
|
|
25290
|
+
"textarea",
|
|
25291
|
+
"svg"
|
|
25292
|
+
].includes(token.tag.toLowerCase()) || Object.entries(token.attributes).some(([name, value]) => name.toLowerCase() === "style" && /white-space\s*:/i.test(value)))) {
|
|
25226
25293
|
const first = lineAt(node.start);
|
|
25227
25294
|
const last = lineAt(node.end - 1);
|
|
25228
25295
|
for (let line = first; line <= last; line++) lines.add(line);
|
package/package.json
CHANGED
package/src/code-check.ts
CHANGED
|
@@ -229,11 +229,25 @@ 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
|
+
/** A raw body is executable only while the builtin implementation owns it. */
|
|
237
|
+
export function executesRawBody(
|
|
238
|
+
name: string,
|
|
239
|
+
parametersOf: ParametersOf,
|
|
240
|
+
): boolean {
|
|
241
|
+
return (
|
|
242
|
+
isRawMacro(name, parametersOf.widget?.(name) !== undefined) &&
|
|
243
|
+
parametersOf.source?.(name) !== 'user'
|
|
244
|
+
);
|
|
232
245
|
}
|
|
233
246
|
|
|
234
247
|
/** What the argument check needs to know of a macro (see MacroMetadata). */
|
|
235
248
|
export interface MacroParameters {
|
|
236
249
|
name: string;
|
|
250
|
+
source?: 'builtin' | 'user';
|
|
237
251
|
parameters?: readonly ParameterDef[];
|
|
238
252
|
interpolate?: boolean;
|
|
239
253
|
/** For a widget: its `@` parameters and what some declare to hold. */
|
|
@@ -273,7 +287,15 @@ export function parameterLookup(
|
|
|
273
287
|
): ParametersOf {
|
|
274
288
|
const parameters = new NameMap<readonly ParameterDef[]>();
|
|
275
289
|
const widgets = new NameMap<readonly WidgetParameter[]>();
|
|
276
|
-
|
|
290
|
+
const sources = new NameMap<'builtin' | 'user'>();
|
|
291
|
+
for (const {
|
|
292
|
+
name,
|
|
293
|
+
parameters: params,
|
|
294
|
+
interpolate,
|
|
295
|
+
widget,
|
|
296
|
+
source,
|
|
297
|
+
} of macros) {
|
|
298
|
+
sources.set(name, source ?? 'user');
|
|
277
299
|
if (params)
|
|
278
300
|
parameters.set(name, withHolds(params, holdsByDefault(interpolate)));
|
|
279
301
|
if (widget)
|
|
@@ -289,6 +311,7 @@ export function parameterLookup(
|
|
|
289
311
|
return params;
|
|
290
312
|
};
|
|
291
313
|
if (widgets.size > 0) lookup.widget = (name) => widgets.get(name);
|
|
314
|
+
lookup.source = (name) => sources.get(name);
|
|
292
315
|
return lookup;
|
|
293
316
|
}
|
|
294
317
|
|
|
@@ -396,7 +419,7 @@ export function* codeAndText(
|
|
|
396
419
|
} else if (token.type === 'macro' && !token.isClose) {
|
|
397
420
|
const name = token.name.toLowerCase();
|
|
398
421
|
const widget = parametersOf.widget?.(name);
|
|
399
|
-
if (name
|
|
422
|
+
if (executesRawBody(name, parametersOf)) {
|
|
400
423
|
const close = closeOfTextBody(t, name);
|
|
401
424
|
if (close < 0) continue;
|
|
402
425
|
const body = src.slice(token.end, tokens[close]!.start);
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
attributeValues,
|
|
7
7
|
escapeOffsets,
|
|
8
8
|
parameterLookup,
|
|
9
|
+
executesRawBody,
|
|
9
10
|
type PieceBase,
|
|
10
11
|
} from './code-check';
|
|
11
12
|
import { macroArgumentSpans } from './components/macros/macro-args';
|
|
@@ -141,10 +142,7 @@ export function completionContext(
|
|
|
141
142
|
if (
|
|
142
143
|
opener?.type === 'macro' &&
|
|
143
144
|
!opener.isClose &&
|
|
144
|
-
|
|
145
|
-
opener.name,
|
|
146
|
-
parametersOf.widget?.(opener.name) !== undefined,
|
|
147
|
-
)
|
|
145
|
+
executesRawBody(opener.name, parametersOf)
|
|
148
146
|
) {
|
|
149
147
|
return codeAt(token.value, token.start, 'statements');
|
|
150
148
|
}
|
|
@@ -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;
|
|
@@ -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),
|
|
@@ -17,6 +17,7 @@ export function useRenderOptions(): {
|
|
|
17
17
|
inline: boolean;
|
|
18
18
|
raw: boolean;
|
|
19
19
|
structural: boolean;
|
|
20
|
+
preserveEdges: boolean;
|
|
20
21
|
locals: Record<string, unknown>;
|
|
21
22
|
} {
|
|
22
23
|
return {
|
|
@@ -24,6 +25,9 @@ export function useRenderOptions(): {
|
|
|
24
25
|
inline: useContext(InlineContext),
|
|
25
26
|
raw: useContext(RawTextContext),
|
|
26
27
|
structural: useContext(StructuralContext),
|
|
28
|
+
// These are content fragments (macro/widget bodies and forwarded
|
|
29
|
+
// children), whose edge whitespace separates surrounding prose.
|
|
30
|
+
preserveEdges: true,
|
|
27
31
|
locals: useContext(LocalsValuesContext),
|
|
28
32
|
};
|
|
29
33
|
}
|
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,50 @@ 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(input: string): (start: number) => number {
|
|
187
|
+
const possible = new Set<number>();
|
|
188
|
+
const pending: number[] = [];
|
|
189
|
+
let remaining = 0;
|
|
190
|
+
let lastBrace = -1;
|
|
191
|
+
for (let i = 0; i < input.length; i++) {
|
|
192
|
+
if (input[i] === '}') {
|
|
193
|
+
remaining++;
|
|
194
|
+
lastBrace = i;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const uncertain = () => {
|
|
198
|
+
for (
|
|
199
|
+
let i = Math.max(0, pending.length - remaining);
|
|
200
|
+
i < pending.length;
|
|
201
|
+
i++
|
|
202
|
+
) {
|
|
203
|
+
possible.add(pending[i]!);
|
|
204
|
+
}
|
|
205
|
+
pending.length = 0;
|
|
206
|
+
};
|
|
207
|
+
for (let i = 0; i < input.length; i++) {
|
|
208
|
+
const c = input[i]!;
|
|
209
|
+
if (c === '{') pending.push(i);
|
|
210
|
+
else if (c === '}') {
|
|
211
|
+
remaining--;
|
|
212
|
+
const open = pending.pop();
|
|
213
|
+
if (open !== undefined) possible.add(open);
|
|
214
|
+
} else if (/['"`/\\]/.test(c)) uncertain();
|
|
215
|
+
}
|
|
216
|
+
return (start) => {
|
|
217
|
+
if (start > lastBrace) return -1;
|
|
218
|
+
// A syntax error may end the parser's recovery at an inner `}`, even
|
|
219
|
+
// when neither the token reader nor the lenient reader can close here.
|
|
220
|
+
return possible.has(start - 1)
|
|
221
|
+
? defaultCodeEnd.closeBrace(input, start, start)
|
|
222
|
+
: parsedCodeEnd(input, start, 'expression');
|
|
223
|
+
};
|
|
224
|
+
}
|
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/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 (
|
|
@@ -1075,6 +1075,8 @@ export function renderNodes(
|
|
|
1075
1075
|
raw?: boolean;
|
|
1076
1076
|
/** Content of a structural element (see StructuralContext). */
|
|
1077
1077
|
structural?: boolean;
|
|
1078
|
+
/** A content fragment: keep whitespace separating it from its neighbours. */
|
|
1079
|
+
preserveEdges?: boolean;
|
|
1078
1080
|
},
|
|
1079
1081
|
): preact.ComponentChildren {
|
|
1080
1082
|
if (nodes.length === 0) return null;
|
|
@@ -1131,9 +1133,9 @@ export function renderNodes(
|
|
|
1131
1133
|
// Inline content (inside <span> etc.) never gets <p> wrappers (#220).
|
|
1132
1134
|
const unwrapParagraphs = !!(options?.nobr || options?.inline);
|
|
1133
1135
|
|
|
1134
|
-
// Markdown strips whitespace at paragraph edges.
|
|
1135
|
-
//
|
|
1136
|
-
// `<span>*HP*: </span>{$hp}`)
|
|
1136
|
+
// Markdown strips whitespace at paragraph edges. In content fragments and
|
|
1137
|
+
// without <p> wrappers it separates content from its neighbours (as in
|
|
1138
|
+
// `A.{if true} B.{/if}` or `<span>*HP*: </span>{$hp}`).
|
|
1137
1139
|
const lead = combined.slice(0, skipChars(combined, 0, 1, EDGE_WS));
|
|
1138
1140
|
const trail =
|
|
1139
1141
|
lead.length === combined.length
|
|
@@ -1141,7 +1143,7 @@ export function renderNodes(
|
|
|
1141
1143
|
: combined.slice(skipChars(combined, combined.length, -1, EDGE_WS));
|
|
1142
1144
|
const core = combined.slice(lead.length, combined.length - trail.length);
|
|
1143
1145
|
const edges = (content: preact.ComponentChildren) =>
|
|
1144
|
-
unwrapParagraphs && (lead || trail) ? (
|
|
1146
|
+
(unwrapParagraphs || options?.preserveEdges) && (lead || trail) ? (
|
|
1145
1147
|
<>
|
|
1146
1148
|
{lead}
|
|
1147
1149
|
{content}
|
package/src/play/session.ts
CHANGED
|
@@ -72,9 +72,9 @@ export function workerExecArgv(execArgv: readonly string[]): string[] {
|
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
/**
|
|
75
|
-
* Opens a session that runs the story in a worker
|
|
76
|
-
*
|
|
77
|
-
*
|
|
75
|
+
* Opens a session that runs the story in a worker with happy-dom's DOM.
|
|
76
|
+
* `window` is `globalThis`: properties authored on it are bare globals too.
|
|
77
|
+
* Idle callbacks use a timer-backed scheduler.
|
|
78
78
|
* Requires `trusted: true`: story code has unrestricted host Node access.
|
|
79
79
|
*/
|
|
80
80
|
export function openPlaySession(
|