@rohal12/spindle 0.62.9 → 0.63.0

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.
@@ -3753,14 +3753,16 @@ var BLOCK_MACROS = new NameSet([
3753
3753
  ]);
3754
3754
  /**
3755
3755
  * The block widgets (those whose body renders `{@children}`), kept apart from
3756
- * BLOCK_MACROS: a widget is redefined, so it is a block only while its latest
3757
- * definition is, whereas the macros in BLOCK_MACROS stay blocks.
3756
+ * built-in defaults: a widget is a block only while its latest definition is.
3758
3757
  */
3759
3758
  var BLOCK_WIDGETS = new NameSet();
3760
3759
  /** All widgets, including inline ones, override built-in parsing semantics. */
3761
3760
  var WIDGETS = new NameSet();
3762
3761
  /** Classification owned by defineMacro, independent of explicit registrations. */
3763
3762
  var DEFINED_BLOCK_MACROS = new NameSet();
3763
+ var DEFINED_MACROS = new NameSet();
3764
+ /** Explicit registrations remain independent of macro/widget replacements. */
3765
+ var REGISTERED_BLOCK_MACROS = new NameSet();
3764
3766
  /** Macros whose body is JavaScript source, kept verbatim. */
3765
3767
  var RAW_BODY_MACROS = /* @__PURE__ */ new Set(["do"]);
3766
3768
  /** Whether a macro's body is JavaScript, kept verbatim (`{do}`). */
@@ -3769,7 +3771,14 @@ function isRawMacro(name, widget = WIDGETS.has(name)) {
3769
3771
  }
3770
3772
  /** Whether a macro takes a body closed by `{/name}`. */
3771
3773
  function isBlockMacro(name) {
3772
- return BLOCK_MACROS.has(name) || DEFINED_BLOCK_MACROS.has(name) || BLOCK_WIDGETS.has(name);
3774
+ if (REGISTERED_BLOCK_MACROS.has(name)) return true;
3775
+ if (WIDGETS.has(name)) return BLOCK_WIDGETS.has(name);
3776
+ if (DEFINED_MACROS.has(name)) return DEFINED_BLOCK_MACROS.has(name);
3777
+ return BLOCK_MACROS.has(name);
3778
+ }
3779
+ /** Explicit parser registrations, unaffected by effective tooling metadata. */
3780
+ function isRegisteredBlockMacro(name) {
3781
+ return REGISTERED_BLOCK_MACROS.has(name);
3773
3782
  }
3774
3783
  /** Macros whose branches render as one: the node holds one per branch. */
3775
3784
  var BRANCHING = /* @__PURE__ */ new Set([
@@ -9690,118 +9699,144 @@ function propertyName(node, src) {
9690
9699
  * identifiers here are declared (a `let`, a parameter), where a sigil
9691
9700
  * variable can't be. `shorthand`: the node is a shorthand property's value.
9692
9701
  */
9693
- function walk(node, src, out, binding, shorthand, access = "read") {
9694
- if (Array.isArray(node)) {
9695
- for (const n of node) walk(n, src, out, binding, false, access);
9696
- return;
9697
- }
9698
- if (!isNode(node)) return;
9699
- const sub = (child, bind = false, short = false, role = "read") => walk(child, src, out, bind, short, role);
9700
- switch (node.type) {
9701
- case "Identifier": {
9702
- const sigil = sigilOf(node, src);
9703
- if (!sigil) return;
9704
- if (binding) throw new CodeSyntaxError(`"${node.name}" is a ${NAMESPACE_NAME[sigil[0]]} variable and can't be declared`, src, node.start);
9705
- out.refs.push({
9706
- sigil: sigil[0],
9707
- name: sigil[1],
9708
- start: node.start,
9709
- end: node.end,
9710
- shorthand,
9711
- read: access !== "write",
9712
- write: access !== "read"
9713
- });
9702
+ function walk(root, src, out, binding, shorthand, access = "read") {
9703
+ const pending = [[
9704
+ root,
9705
+ binding,
9706
+ shorthand,
9707
+ access
9708
+ ]];
9709
+ while (pending.length) {
9710
+ const [node, bind, short, role] = pending.pop();
9711
+ const children = [];
9712
+ visit(node, bind, short, role, children);
9713
+ for (let i = children.length - 1; i >= 0; i--) pending.push(children[i]);
9714
+ }
9715
+ function visit(node, binding, shorthand, access, children) {
9716
+ if (Array.isArray(node)) {
9717
+ for (const n of node) children.push([
9718
+ n,
9719
+ binding,
9720
+ false,
9721
+ access
9722
+ ]);
9714
9723
  return;
9715
9724
  }
9716
- case "Literal":
9717
- if (typeof node.value === "string") out.strings.push(src.slice(node.start + 1, node.end - 1));
9718
- return;
9719
- case "TemplateElement":
9720
- out.strings.push(node.value.raw);
9721
- return;
9722
- case "MemberExpression":
9723
- sub(node.object);
9724
- if (node.computed) sub(node.property);
9725
- else propertyName(node.property, src);
9726
- return;
9727
- case "Property":
9728
- if (node.computed) sub(node.key);
9729
- else if (!node.shorthand) propertyName(node.key, src);
9730
- if (node.shorthand) {
9731
- const value = node.value;
9732
- if (value.type === "AssignmentPattern") {
9733
- sub(value.left, binding, true, access);
9734
- sub(value.right);
9735
- } else sub(value, binding, true, access);
9736
- } else sub(node.value, binding, false, access);
9737
- return;
9738
- case "MethodDefinition":
9739
- case "PropertyDefinition":
9740
- if (node.computed) sub(node.key);
9741
- else propertyName(node.key, src);
9742
- sub(node.value);
9743
- return;
9744
- case "LabeledStatement":
9745
- sub(node.body);
9746
- return;
9747
- case "BreakStatement":
9748
- case "ContinueStatement":
9749
- case "MetaProperty":
9750
- case "PrivateIdentifier": return;
9751
- case "VariableDeclarator":
9752
- sub(node.id, true);
9753
- sub(node.init);
9754
- return;
9755
- case "FunctionDeclaration":
9756
- case "FunctionExpression":
9757
- case "ArrowFunctionExpression":
9758
- sub(node.id, true);
9759
- for (const p of node.params) sub(p, true);
9760
- sub(node.body);
9761
- return;
9762
- case "ClassDeclaration":
9763
- case "ClassExpression":
9764
- sub(node.id, true);
9765
- sub(node.superClass);
9766
- sub(node.body);
9767
- return;
9768
- case "CatchClause":
9769
- sub(node.param, true);
9770
- sub(node.body);
9771
- return;
9772
- case "AssignmentExpression":
9773
- sub(node.left, false, false, node.operator === "=" ? "write" : "readwrite");
9774
- sub(node.right);
9775
- return;
9776
- case "UnaryExpression":
9777
- sub(node.argument, false, false, node.operator === "delete" ? "write" : "read");
9778
- return;
9779
- case "UpdateExpression":
9780
- sub(node.argument, false, false, "readwrite");
9781
- return;
9782
- case "ForInStatement":
9783
- case "ForOfStatement":
9784
- sub(node.left, false, false, "write");
9785
- sub(node.right);
9786
- sub(node.body);
9787
- return;
9788
- case "AssignmentPattern":
9789
- sub(node.left, binding, false, access);
9790
- sub(node.right);
9791
- return;
9792
- case "ObjectPattern":
9793
- case "ArrayPattern":
9794
- case "RestElement":
9795
- for (const key of [
9796
- "properties",
9797
- "elements",
9798
- "argument"
9799
- ]) sub(node[key], binding, false, access);
9800
- return;
9801
- default: for (const key in node) {
9802
- if (key === "type" || key === "start" || key === "end") continue;
9803
- const v = node[key];
9804
- if (typeof v === "object" && v !== null) sub(v);
9725
+ if (!isNode(node)) return;
9726
+ const sub = (child, bind = false, short = false, role = "read") => {
9727
+ children.push([
9728
+ child,
9729
+ bind,
9730
+ short,
9731
+ role
9732
+ ]);
9733
+ };
9734
+ switch (node.type) {
9735
+ case "Identifier": {
9736
+ const sigil = sigilOf(node, src);
9737
+ if (!sigil) return;
9738
+ if (binding) throw new CodeSyntaxError(`"${node.name}" is a ${NAMESPACE_NAME[sigil[0]]} variable and can't be declared`, src, node.start);
9739
+ out.refs.push({
9740
+ sigil: sigil[0],
9741
+ name: sigil[1],
9742
+ start: node.start,
9743
+ end: node.end,
9744
+ shorthand,
9745
+ read: access !== "write",
9746
+ write: access !== "read"
9747
+ });
9748
+ return;
9749
+ }
9750
+ case "Literal":
9751
+ if (typeof node.value === "string") out.strings.push(src.slice(node.start + 1, node.end - 1));
9752
+ return;
9753
+ case "TemplateElement":
9754
+ out.strings.push(node.value.raw);
9755
+ return;
9756
+ case "MemberExpression":
9757
+ sub(node.object);
9758
+ if (node.computed) sub(node.property);
9759
+ else propertyName(node.property, src);
9760
+ return;
9761
+ case "Property":
9762
+ if (node.computed) sub(node.key);
9763
+ else if (!node.shorthand) propertyName(node.key, src);
9764
+ if (node.shorthand) {
9765
+ const value = node.value;
9766
+ if (value.type === "AssignmentPattern") {
9767
+ sub(value.left, binding, true, access);
9768
+ sub(value.right);
9769
+ } else sub(value, binding, true, access);
9770
+ } else sub(node.value, binding, false, access);
9771
+ return;
9772
+ case "MethodDefinition":
9773
+ case "PropertyDefinition":
9774
+ if (node.computed) sub(node.key);
9775
+ else propertyName(node.key, src);
9776
+ sub(node.value);
9777
+ return;
9778
+ case "LabeledStatement":
9779
+ sub(node.body);
9780
+ return;
9781
+ case "BreakStatement":
9782
+ case "ContinueStatement":
9783
+ case "MetaProperty":
9784
+ case "PrivateIdentifier": return;
9785
+ case "VariableDeclarator":
9786
+ sub(node.id, true);
9787
+ sub(node.init);
9788
+ return;
9789
+ case "FunctionDeclaration":
9790
+ case "FunctionExpression":
9791
+ case "ArrowFunctionExpression":
9792
+ sub(node.id, true);
9793
+ for (const p of node.params) sub(p, true);
9794
+ sub(node.body);
9795
+ return;
9796
+ case "ClassDeclaration":
9797
+ case "ClassExpression":
9798
+ sub(node.id, true);
9799
+ sub(node.superClass);
9800
+ sub(node.body);
9801
+ return;
9802
+ case "CatchClause":
9803
+ sub(node.param, true);
9804
+ sub(node.body);
9805
+ return;
9806
+ case "AssignmentExpression":
9807
+ sub(node.left, false, false, node.operator === "=" ? "write" : "readwrite");
9808
+ sub(node.right);
9809
+ return;
9810
+ case "UnaryExpression":
9811
+ sub(node.argument, false, false, node.operator === "delete" ? "write" : "read");
9812
+ return;
9813
+ case "UpdateExpression":
9814
+ sub(node.argument, false, false, "readwrite");
9815
+ return;
9816
+ case "ForInStatement":
9817
+ case "ForOfStatement":
9818
+ sub(node.left, false, false, "write");
9819
+ sub(node.right);
9820
+ sub(node.body);
9821
+ return;
9822
+ case "AssignmentPattern":
9823
+ sub(node.left, binding, false, access);
9824
+ sub(node.right);
9825
+ return;
9826
+ case "ObjectPattern":
9827
+ case "ArrayPattern":
9828
+ case "RestElement":
9829
+ for (const key of [
9830
+ "properties",
9831
+ "elements",
9832
+ "argument"
9833
+ ]) sub(node[key], binding, false, access);
9834
+ return;
9835
+ default: for (const key in node) {
9836
+ if (key === "type" || key === "start" || key === "end") continue;
9837
+ const v = node[key];
9838
+ if (typeof v === "object" && v !== null) sub(v);
9839
+ }
9805
9840
  }
9806
9841
  }
9807
9842
  }
@@ -10571,7 +10606,7 @@ function scanMarkupTolerant(source, options = {}, completion = false) {
10571
10606
  const rest = source.slice(base);
10572
10607
  const result = attempt(rest, base);
10573
10608
  if (Array.isArray(result)) {
10574
- tokens.push(...result);
10609
+ for (const token of result) tokens.push(token);
10575
10610
  break;
10576
10611
  }
10577
10612
  const at = base + result.offset;
@@ -10584,7 +10619,7 @@ function scanMarkupTolerant(source, options = {}, completion = false) {
10584
10619
  cut = prefix.offset;
10585
10620
  prefix = attempt(rest.slice(0, cut), base);
10586
10621
  }
10587
- tokens.push(...prefix);
10622
+ for (const token of prefix) tokens.push(token);
10588
10623
  damage.push(base + cut);
10589
10624
  if (cut < rest.length) {
10590
10625
  const start = base + cut;
@@ -11296,6 +11331,27 @@ function subMacroParameters(name) {
11296
11331
  return SUB_MACRO_PARAMETERS.get(name.toLowerCase());
11297
11332
  }
11298
11333
  //#endregion
11334
+ //#region src/markup/expansion-budget.ts
11335
+ var _MarkupExpansionBudget;
11336
+ /** Shared bounds for reparsing markup inside labels, selectors and attributes. */
11337
+ var MarkupExpansionBudget = class MarkupExpansionBudget {
11338
+ constructor(sourceLength) {
11339
+ this.depth = 0;
11340
+ this.remaining = Math.max(65536, sourceLength * 8);
11341
+ }
11342
+ enter(length) {
11343
+ if (this.depth >= MarkupExpansionBudget.maxDepth || length > this.remaining) return false;
11344
+ this.remaining -= length;
11345
+ this.depth++;
11346
+ return true;
11347
+ }
11348
+ leave() {
11349
+ this.depth--;
11350
+ }
11351
+ };
11352
+ _MarkupExpansionBudget = MarkupExpansionBudget;
11353
+ _MarkupExpansionBudget.maxDepth = 64;
11354
+ //#endregion
11299
11355
  //#region src/code-check.ts
11300
11356
  /** While a pass runs: the code it parsed so far, by goal and source. */
11301
11357
  var parses = null;
@@ -11446,6 +11502,7 @@ function* codeAndText(src, tokens, parametersOf) {
11446
11502
  offset: token.targetStart,
11447
11503
  length: token.targetEnd - token.targetStart,
11448
11504
  macro: "link",
11505
+ ...usesBuiltinSemantics("link", parametersOf) && { navigation: true },
11449
11506
  label: src.slice(token.start, token.end)
11450
11507
  };
11451
11508
  if (token.display.includes("{")) yield textPiece(token.display, token.displayStart, "In the label of a link: ");
@@ -11515,7 +11572,10 @@ function* codeAndText(src, tokens, parametersOf) {
11515
11572
  continue;
11516
11573
  }
11517
11574
  const params = parametersOf(name);
11518
- if (params) yield* argPieces(args, argsAt, params, token.name, label);
11575
+ if (params) {
11576
+ const navigation = usesBuiltinSemantics(name, parametersOf) ? name === "goto" || name === "link" ? ["passage"] : name === "watch" ? ["goto"] : [] : [];
11577
+ yield* argPieces(args, argsAt, params, token.name, label, navigation);
11578
+ }
11519
11579
  if (widget) yield* widgetPieces(args, argsAt, widget, token.name, label);
11520
11580
  }
11521
11581
  }
@@ -11529,37 +11589,50 @@ function* codeAndText(src, tokens, parametersOf) {
11529
11589
  function* passagePiecesOf(src, tokens, parametersOf, options = {}) {
11530
11590
  let position;
11531
11591
  const locate = (offset) => (position ?? (position = sourcePositions(src)))(offset);
11532
- yield* expand(src, src, tokens, parametersOf, options, (i) => i, void 0, locate);
11592
+ yield* expand(src, src, tokens, parametersOf, options, (i) => i, void 0, locate, new MarkupExpansionBudget(src.length));
11533
11593
  }
11534
11594
  /**
11535
11595
  * The pieces of `tokens`, the tokens of `src`, which is the text described by
11536
11596
  * `where` (none for `root`, the markup of the whole passage), and whose
11537
11597
  * offsets `toRoot` turns into offsets in `root`.
11538
11598
  */
11539
- function* expand(root, src, tokens, parametersOf, options, toRoot, where, locate) {
11599
+ function* expand(root, src, tokens, parametersOf, options, toRoot, where, locate, budget) {
11540
11600
  for (const flat of codeAndText(src, tokens, parametersOf)) {
11541
11601
  const piece = where ? nestedPiece(flat, toRoot, where) : flat;
11542
11602
  if (flat.kind !== "text" || !flat.text.includes("{")) {
11543
11603
  yield piece;
11544
11604
  continue;
11545
11605
  }
11546
- const toText = (i) => toRoot(pieceOffset(flat, i));
11547
- const inner = analyzeMarkup(flat.text, {
11548
- text: true,
11549
- hooks: {
11550
- ...options.isBlock && { isBlock: options.isBlock },
11551
- isRaw: (name) => isRawMacro(name, parametersOf.widget?.(name) !== void 0)
11552
- }
11553
- });
11554
- yield {
11555
- ...piece,
11556
- tokens: inner.tokens.map((t) => mapOffsets(t, toText)),
11557
- errors: inner.errors.map((e) => {
11558
- const { line, column } = locate(toText(e.offset));
11559
- return new MarkupError(e.reason, toText(e.offset), line, column, e.code, toText(e.end), e.data);
11560
- })
11561
- };
11562
- yield* expand(root, flat.text, inner.tokens, parametersOf, options, toText, flat.where, locate);
11606
+ const toText = (i) => pieceOffset(piece, i);
11607
+ if (!budget.enter(flat.text.length)) {
11608
+ const at = toText(0);
11609
+ const { line, column } = locate(at);
11610
+ yield {
11611
+ ...piece,
11612
+ errors: [new MarkupError("Markup in labels and attributes exceeds the nesting or work limit", at, line, column, "nesting-limit", toText(flat.text.length), { limit: String(MarkupExpansionBudget.maxDepth) })]
11613
+ };
11614
+ continue;
11615
+ }
11616
+ try {
11617
+ const inner = analyzeMarkup(flat.text, {
11618
+ text: true,
11619
+ hooks: {
11620
+ ...options.isBlock && { isBlock: options.isBlock },
11621
+ isRaw: (name) => isRawMacro(name, parametersOf.widget?.(name) !== void 0)
11622
+ }
11623
+ });
11624
+ yield {
11625
+ ...piece,
11626
+ tokens: inner.tokens.map((t) => mapOffsets(t, toText)),
11627
+ errors: inner.errors.map((e) => {
11628
+ const { line, column } = locate(toText(e.offset));
11629
+ return new MarkupError(e.reason, toText(e.offset), line, column, e.code, toText(e.end), e.data);
11630
+ })
11631
+ };
11632
+ yield* expand(root, flat.text, inner.tokens, parametersOf, options, toText, flat.where, locate, budget);
11633
+ } finally {
11634
+ budget.leave();
11635
+ }
11563
11636
  }
11564
11637
  }
11565
11638
  /** `piece`, of a text in another, with its offsets turned by `toRoot`. */
@@ -11638,7 +11711,7 @@ function escapeOffsets(raw, at, value, escapes) {
11638
11711
  return escaped ? offsets : void 0;
11639
11712
  }
11640
11713
  /** The code and text in the arguments `args` (at `offset`) of `macro`. */
11641
- function* argPieces(args, offset, params, macro, label) {
11714
+ function* argPieces(args, offset, params, macro, label, navigation = []) {
11642
11715
  let values;
11643
11716
  const spans = /* @__PURE__ */ new Map();
11644
11717
  try {
@@ -11655,7 +11728,7 @@ function* argPieces(args, offset, params, macro, label) {
11655
11728
  };
11656
11729
  return;
11657
11730
  }
11658
- yield* valuePieces(args, offset, params, values, spans, macro, label, "quotes");
11731
+ yield* valuePieces(args, offset, params, values, spans, macro, label, "quotes", navigation);
11659
11732
  }
11660
11733
  /**
11661
11734
  * The code and text in the arguments `args` (at `offset`) of the widget
@@ -11688,7 +11761,7 @@ function* widgetPieces(args, offset, params, macro, label) {
11688
11761
  * (at `offset`), whose `spans` tell where each was written and whose strings
11689
11762
  * are unescaped as `escapes` says.
11690
11763
  */
11691
- function* valuePieces(args, offset, params, values, spans, macro, label, escapes) {
11764
+ function* valuePieces(args, offset, params, values, spans, macro, label, escapes, navigation = []) {
11692
11765
  const name = macro.toLowerCase();
11693
11766
  /** The pieces of the parameters `list`, with their values in `from`. */
11694
11767
  function* visit(list, from) {
@@ -11709,7 +11782,8 @@ function* valuePieces(args, offset, params, values, spans, macro, label, escapes
11709
11782
  offset: offset + start,
11710
11783
  length: end - start,
11711
11784
  macro: name,
11712
- label
11785
+ label,
11786
+ ...navigation.includes(param.name) && { navigation: true }
11713
11787
  });
11714
11788
  const goal = codeGoal(param);
11715
11789
  if (param.type === "passage") {
@@ -11987,18 +12061,19 @@ registeredParameters.source = getMacroParameters.source;
11987
12061
  * Whether the text of an attribute value or macro label renders a
11988
12062
  * `{@children}`: it is markup, parsed as the renderer parses it.
11989
12063
  */
11990
- function textContainsChildren(text, parametersOf, hooks) {
12064
+ function textContainsChildren(text, parametersOf, hooks, budget = new MarkupExpansionBudget(text.length)) {
11991
12065
  if (!text.includes("{")) return false;
11992
- let nodes;
12066
+ if (!budget.enter(text.length)) return false;
11993
12067
  try {
11994
- nodes = parseMarkup(text, {
12068
+ return astContainsChildren(parseMarkup(text, {
11995
12069
  text: true,
11996
12070
  hooks
11997
- });
12071
+ }), parametersOf, hooks, budget);
11998
12072
  } catch {
11999
12073
  return false;
12074
+ } finally {
12075
+ budget.leave();
12000
12076
  }
12001
- return astContainsChildren(nodes, parametersOf, hooks);
12002
12077
  }
12003
12078
  /**
12004
12079
  * Whether a tag renders a `{@children}` in an attribute value or a macro
@@ -12008,19 +12083,21 @@ function textContainsChildren(text, parametersOf, hooks) {
12008
12083
  * are read from it); a token made from an AST has none. Discovery hooks
12009
12084
  * also apply inside these texts, before widgets have been registered.
12010
12085
  */
12011
- function tokenTextContainsChildren(token, parametersOf = registeredParameters, src = "", hooks) {
12012
- for (const piece of codeAndText(src, [token], parametersOf)) if (piece.kind === "text" && textContainsChildren(piece.text, parametersOf, hooks)) return true;
12086
+ function tokenTextContainsChildren(token, parametersOf = registeredParameters, src = "", hooks, budget) {
12087
+ for (const piece of codeAndText(src, [token], parametersOf)) if (piece.kind === "text" && textContainsChildren(piece.text, parametersOf, hooks, budget)) return true;
12013
12088
  return false;
12014
12089
  }
12015
12090
  /**
12016
- * Recursively scan an AST node array for a VariableNode
12091
+ * Scan an AST node array for a VariableNode
12017
12092
  * with scope 'local' and name 'children' (@children), in the content and in
12018
12093
  * the attributes and labels that render markup. A nested `{widget}` is not
12019
12094
  * entered: its slot belongs to the innermost widget (as widgetDefinitions
12020
12095
  * reads it), and a definition renders nothing of its own.
12021
12096
  */
12022
- function astContainsChildren(nodes, parametersOf = registeredParameters, hooks) {
12023
- for (const node of nodes) {
12097
+ function astContainsChildren(nodes, parametersOf = registeredParameters, hooks, budget) {
12098
+ const pending = [...nodes];
12099
+ while (pending.length) {
12100
+ const node = pending.pop();
12024
12101
  if (node.type === "variable" && node.scope === "local" && node.name === "children") return true;
12025
12102
  if (node.type !== "html" && node.type !== "macro") continue;
12026
12103
  if (node.type === "macro" && node.name.toLowerCase() === "widget") continue;
@@ -12043,10 +12120,9 @@ function astContainsChildren(nodes, parametersOf = registeredParameters, hooks)
12043
12120
  nameEnd: 0,
12044
12121
  argsStart: 0,
12045
12122
  argsEnd: 0
12046
- }, parametersOf, "", hooks) || astContainsChildren(node.children, parametersOf, hooks)) return true;
12047
- if (node.type === "macro" && node.branches) {
12048
- for (const branch of node.branches) if (astContainsChildren(branch.children, parametersOf, hooks)) return true;
12049
- }
12123
+ }, parametersOf, "", hooks, budget)) return true;
12124
+ for (const child of node.children) pending.push(child);
12125
+ if (node.type === "macro" && node.branches) for (const branch of node.branches) for (const child of branch.children) pending.push(child);
12050
12126
  }
12051
12127
  return false;
12052
12128
  }
@@ -12127,6 +12203,7 @@ function widgetDefinitions$1(passages, parametersOf = registeredParameters) {
12127
12203
  const hooks = { isRaw: (name) => isRawMacro(name, replacesDo || parametersOf.widget?.(name) !== void 0) };
12128
12204
  for (const passage of list) {
12129
12205
  if (passage.name !== "StoryInit" && !passage.tags?.includes("widget")) continue;
12206
+ const budget = new MarkupExpansionBudget(passage.content.length);
12130
12207
  const open = [];
12131
12208
  const done = [];
12132
12209
  for (const token of tokenizeMarkupTolerant$1(passage.content, { hooks }).tokens) if (token.type === "macro" && token.name.toLowerCase() === "widget") {
@@ -12141,7 +12218,7 @@ function widgetDefinitions$1(passages, parametersOf = registeredParameters) {
12141
12218
  closeStart: token.start
12142
12219
  });
12143
12220
  }
12144
- } else if (open.length > 0 && (token.type === "variable" && token.scope === "local" && token.name === "children" || tokenTextContainsChildren(token, parametersOf, passage.content, hooks))) open[open.length - 1].isBlock = true;
12221
+ } else if (open.length > 0 && (token.type === "variable" && token.scope === "local" && token.name === "children" || tokenTextContainsChildren(token, parametersOf, passage.content, hooks, budget))) open[open.length - 1].isBlock = true;
12145
12222
  done.push(...open);
12146
12223
  const defs = [];
12147
12224
  for (const { token, isBlock, closeStart } of done) try {
@@ -12178,6 +12255,26 @@ function effectiveWidgetDefinitions(definitions) {
12178
12255
  return [...effective.values()];
12179
12256
  }
12180
12257
  //#endregion
12258
+ //#region src/passage-roles.ts
12259
+ /** Runtime-owned passage roles, shared with stateless authoring tools. */
12260
+ var SPECIAL_PASSAGES = /* @__PURE__ */ new Set([
12261
+ "StoryInit",
12262
+ "StoryInterface",
12263
+ "StoryVariables",
12264
+ "StoryTransients",
12265
+ "StoryLoading",
12266
+ "SaveTitle",
12267
+ "PassageReady",
12268
+ "PassageHeader",
12269
+ "PassageFooter",
12270
+ "PassageDone",
12271
+ "StoryContext"
12272
+ ]);
12273
+ /** Whether `name` is a special passage, which navigation never goes to. */
12274
+ function isSpecialPassage(name) {
12275
+ return SPECIAL_PASSAGES.has(name);
12276
+ }
12277
+ //#endregion
12181
12278
  //#region src/markup/validate.ts
12182
12279
  /**
12183
12280
  * Markup validation: parse every passage when the story starts and report
@@ -12361,6 +12458,13 @@ function validateMarkup(passages, options) {
12361
12458
  report(passage, at, at + piece.length, "argument-error", `${where}${piece.message} in ${piece.label}`, { macro: piece.macro });
12362
12459
  break;
12363
12460
  case "passage":
12461
+ if (piece.navigation && isSpecialPassage(piece.name)) {
12462
+ report(passage, at, at + piece.length, "special-passage", `${where}Cannot navigate to special passage ${JSON.stringify(piece.name)} in ${piece.label}.`, {
12463
+ name: piece.name,
12464
+ macro: piece.macro
12465
+ });
12466
+ break;
12467
+ }
12364
12468
  if (options.checkPassageNames !== false && !passageNames.has(piece.name)) {
12365
12469
  const best = closestName(piece.name, passageNames);
12366
12470
  report(passage, at, at + piece.length, "unknown-passage", `${where}No passage named ${JSON.stringify(piece.name)} in ${piece.label}.${didYouMean(best, JSON.stringify)}`, {
@@ -13097,7 +13201,15 @@ function scanSigilReferences(value, at, emit) {
13097
13201
  */
13098
13202
  function scanInterpolations(text, at, emit, options) {
13099
13203
  if (!text.includes("{")) return;
13100
- collectTokenRefs(text, tokensOf(text, true, options), at, emit, options);
13204
+ const budget = options.expansionBudget;
13205
+ if (!budget.enter(text.length)) return;
13206
+ try {
13207
+ const offset = at(0);
13208
+ const sourceOffsets = at(text.length) - offset === text.length ? void 0 : Array.from({ length: text.length + 1 }, (_, i) => at(i));
13209
+ collectTokenRefs(text, tokensOf(text, true, options), (i) => sourceOffsets?.[i] ?? offset + i, emit, options);
13210
+ } finally {
13211
+ budget.leave();
13212
+ }
13101
13213
  }
13102
13214
  /**
13103
13215
  * Report `$var` references in JavaScript code, lexed as the expression
@@ -13279,7 +13391,10 @@ function collectTokenRefs(content, tokens, at, emit, options) {
13279
13391
  */
13280
13392
  function collectVariableReferences(content, options) {
13281
13393
  const found = [];
13282
- withParseCache(() => collectTokenRefs(content, tokensOf(content, false, options), (i) => i, (ref) => found.push(ref), options));
13394
+ withParseCache(() => collectTokenRefs(content, tokensOf(content, false, options), (i) => i, (ref) => found.push(ref), {
13395
+ ...options,
13396
+ expansionBudget: new MarkupExpansionBudget(content.length)
13397
+ }));
13283
13398
  return found.sort((a, b) => a.start - b.start);
13284
13399
  }
13285
13400
  /**
@@ -25873,20 +25988,23 @@ function validateStoryMarkup(passages, macros, options = {}) {
25873
25988
  const all = effectiveToolingMacros(macros);
25874
25989
  const known = /* @__PURE__ */ new Set();
25875
25990
  const widgets = effectiveWidgetDefinitions(widgetDefinitions$1(list, parameterLookup(all)));
25876
- const blocks = new Set(widgets.filter((def) => def.block).map((def) => def.name.toLowerCase()));
25877
25991
  const parametersOf = parameterLookup([...all, ...definedWidgetMacros(widgets)]);
25878
25992
  for (const macro of all) {
25879
25993
  const name = macro.name.toLowerCase();
25880
25994
  known.add(name);
25881
25995
  for (const sub of macro.subMacros) known.add(sub.toLowerCase());
25882
- if (macro.block) blocks.add(name);
25883
25996
  }
25884
25997
  return validateMarkup(list, {
25885
25998
  branchParents: (name) => branchParents(name, all),
25886
25999
  isKnownMacro: (name) => known.has(name),
25887
26000
  macroNames: known,
25888
26001
  parametersOf,
25889
- isBlockMacro: (name) => blocks.has(name.toLowerCase()) || isBlockMacro(name),
26002
+ isBlockMacro: blockLookup([...all, ...widgets.map((w) => ({
26003
+ name: w.name,
26004
+ block: w.block,
26005
+ subMacros: [],
26006
+ widget: w
26007
+ }))]),
25890
26008
  checkPassageNames: options.checkPassageNames,
25891
26009
  tolerant: options.tolerant
25892
26010
  });
@@ -25900,8 +26018,9 @@ function definedWidgetMacros(widgets) {
25900
26018
  }
25901
26019
  /** Whether a macro takes a body, among `macros` and the built-in ones. */
25902
26020
  function blockLookup(macros) {
25903
- const blocks = new Set(macros.filter((m) => m.block).map((m) => m.name.toLowerCase()));
25904
- return (name) => blocks.has(name.toLowerCase()) || isBlockMacro(name);
26021
+ const definitions = new NameMap();
26022
+ for (const macro of effectiveToolingMacros(macros)) definitions.set(macro.name, macro.block);
26023
+ return (name) => isRegisteredBlockMacro(name) || (definitions.get(name) ?? isBlockMacro(name));
25905
26024
  }
25906
26025
  /**
25907
26026
  * What a passage's markup runs and names, and where: the pieces of code
@@ -26000,4 +26119,4 @@ function validateVariableReferences(passages, declarations, macros = []) {
26000
26119
  return checkVariableReferences(list.filter((p) => !p.tags?.some((t) => NOT_MARKUP_TAGS.includes(t))), declarations, referenceOptions(macros, false, list));
26001
26120
  }
26002
26121
  //#endregion
26003
- export { MarkupError, SIGIL_SCOPES, checkParameterTypes, collectStoryPassageReferences, completionContext, discoverMacros, effectiveWidgetDefinitions, endsWithOperator, evaluatePassageName, findCodeEnd, formatDiagnostic, formatPassage, isBlockMacro, isSigil, lexJs, lexTemplate, macroArgumentSpans, pairMarkup, parseBoundVariable, parseCode, parseDeclarations, parseSelectors, parseStoryVariables, parseWidgetDef, passagePieces, passagePiecesOfTokens, passageTarget, pieceOffset, readQuoted, scanStringLiteral, splitArgs, splitIncludeFlag, splitTopLevel, stripLooseQuotes, tokenizeMarkup, tokenizeMarkupTolerant, transform, unescapeQuoted, validateStoryMarkup, validateVariableReferences, variableReferences, widgetDefinitions };
26122
+ export { MarkupError, SIGIL_SCOPES, checkParameterTypes, collectStoryPassageReferences, completionContext, discoverMacros, effectiveWidgetDefinitions, endsWithOperator, evaluatePassageName, findCodeEnd, formatDiagnostic, formatPassage, isBlockMacro, isSigil, isSpecialPassage, lexJs, lexTemplate, macroArgumentSpans, pairMarkup, parseBoundVariable, parseCode, parseDeclarations, parseSelectors, parseStoryVariables, parseWidgetDef, passagePieces, passagePiecesOfTokens, passageTarget, pieceOffset, readQuoted, scanStringLiteral, splitArgs, splitIncludeFlag, splitTopLevel, stripLooseQuotes, tokenizeMarkup, tokenizeMarkupTolerant, transform, unescapeQuoted, validateStoryMarkup, validateVariableReferences, variableReferences, widgetDefinitions };