@rohal12/spindle 0.64.32 → 0.64.33

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.
@@ -10353,7 +10353,7 @@ function peg$parse(input, options) {
10353
10353
  return lexeme("text", location(), { value });
10354
10354
  }
10355
10355
  function peg$f24() {
10356
- return completionMode || offset() <= lastCommentClose;
10356
+ return completionMode || findDelimiter("-->", offset()) >= 0;
10357
10357
  }
10358
10358
  function peg$f25(value) {
10359
10359
  const node = lexeme("text", location(), { value });
@@ -10410,8 +10410,8 @@ function peg$parse(input, options) {
10410
10410
  fail("Unclosed link: [[ without ]]", location(), "unclosed-link");
10411
10411
  }
10412
10412
  function peg$f37(start) {
10413
- const close = input.indexOf("]]", start);
10414
- const nested = input.indexOf("[[", start);
10413
+ const close = findDelimiter("]]", start);
10414
+ const nested = findDelimiter("[[", start);
10415
10415
  linkTextEnd = Math.min(close < 0 ? input.length : close, nested < 0 ? input.length : nested);
10416
10416
  return true;
10417
10417
  }
@@ -13271,7 +13271,7 @@ function peg$parse(input, options) {
13271
13271
  const textMode = options.text === true;
13272
13272
  const completionMode = options.completion === true;
13273
13273
  const hooks = options.hooks;
13274
- const lastCommentClose = input.lastIndexOf("-->");
13274
+ const findDelimiter = options.findDelimiter;
13275
13275
  /** End of the stretch a `.|n|` repetition consumes (see skipTo). */
13276
13276
  let skipEnd = -1;
13277
13277
  /** Whether the attribute whose value is being read holds code. */
@@ -13605,6 +13605,10 @@ function isBlockMacro(name) {
13605
13605
  if (DEFINED_MACROS.has(name)) return DEFINED_BLOCK_MACROS.has(name);
13606
13606
  return isBuiltinBlockMacro(name);
13607
13607
  }
13608
+ /** Explicit implementations can also be invoked outside a companion owner. */
13609
+ function isStandaloneMacro(name) {
13610
+ return WIDGETS.has(name) || getMacro(name) !== void 0;
13611
+ }
13608
13612
  /** Built-in defaults without any process-global registrations or widgets. */
13609
13613
  function isBuiltinBlockMacro(name) {
13610
13614
  return BLOCK_MACROS.has(name);
@@ -13769,6 +13773,7 @@ var closes = (open, close) => open.type === close.type && lowerName(open) === lo
13769
13773
  function pairMarkup(tokens, options = {}) {
13770
13774
  const isBlock = options.isBlock ?? isBlockMacro;
13771
13775
  const isRaw = options.isRaw ?? isRawMacro;
13776
+ const isStandalone = options.isStandalone ?? isStandaloneMacro;
13772
13777
  const parentsOf = (name) => {
13773
13778
  if (options.branchParents) return options.branchParents(name);
13774
13779
  const registered = branchParents(name);
@@ -13848,7 +13853,7 @@ function pairMarkup(tokens, options = {}) {
13848
13853
  }
13849
13854
  continue;
13850
13855
  }
13851
- if (token.type === "macro" && !token.isClose && parentsOf(token.name).length > 0) {
13856
+ if (token.type === "macro" && !token.isClose && parentsOf(token.name).length > 0 && (!isStandalone(token.name) || last(stack)?.node.token.type === "macro" && parentsOf(token.name).includes(lowerName(last(stack).node.token)))) {
13852
13857
  const tag = token;
13853
13858
  const frame = last(stack);
13854
13859
  if (!frame) {
@@ -14378,7 +14383,7 @@ var MarkupError = class extends Error {
14378
14383
  this.name = "MarkupError";
14379
14384
  }
14380
14385
  };
14381
- function run(source, startRule, options, completion = false) {
14386
+ function run(source, startRule, options, completion = false, findDelimiter = indexDelimiters(source)) {
14382
14387
  const hooks = {
14383
14388
  ...defaultHooks,
14384
14389
  ...options.hooks
@@ -14392,7 +14397,8 @@ function run(source, startRule, options, completion = false) {
14392
14397
  startRule,
14393
14398
  text: options.text === true,
14394
14399
  hooks,
14395
- completion
14400
+ completion,
14401
+ findDelimiter
14396
14402
  });
14397
14403
  } catch (err) {
14398
14404
  const { location, code } = err;
@@ -14404,6 +14410,20 @@ function run(source, startRule, options, completion = false) {
14404
14410
  throw err;
14405
14411
  }
14406
14412
  }
14413
+ /** Literal delimiter searches shared by suffix and prefix recovery attempts. */
14414
+ function indexDelimiters(source) {
14415
+ const positions = /* @__PURE__ */ new Map();
14416
+ return (delimiter, from, end = source.length) => {
14417
+ let offsets = positions.get(delimiter);
14418
+ if (!offsets) {
14419
+ offsets = [];
14420
+ for (let at = source.indexOf(delimiter); at >= 0; at = source.indexOf(delimiter, at + 1)) offsets.push(at);
14421
+ positions.set(delimiter, offsets);
14422
+ }
14423
+ const at = offsets[upperBound(offsets, from - 1)];
14424
+ return at !== void 0 && at + delimiter.length <= end ? at : -1;
14425
+ };
14426
+ }
14407
14427
  /**
14408
14428
  * Read markup that may be malformed: its tokens, their pairing and every
14409
14429
  * error (malformed tags, and closers and branches that pair with nothing),
@@ -14420,6 +14440,7 @@ function analyzeMarkup(source, options = {}) {
14420
14440
  isBlock: hooks.isBlock,
14421
14441
  isRaw: hooks.isRaw,
14422
14442
  branchParents: hooks.branchParents,
14443
+ isStandalone: hooks.isStandalone,
14423
14444
  source
14424
14445
  });
14425
14446
  if (errors.length === 0 && paired.errors.length === 0) return {
@@ -14492,6 +14513,7 @@ function scanMarkupTolerant(source, options = {}, completion = false) {
14492
14513
  const damage = [];
14493
14514
  let position;
14494
14515
  const codeEnd = indexCodeEnds(source);
14516
+ const findDelimiter = indexDelimiters(source);
14495
14517
  const customCloseBrace = options.hooks?.closeBrace;
14496
14518
  const lastBrace = source.lastIndexOf("}");
14497
14519
  /** `text` shifted by `base`, tokenized: the tokens or the error. */
@@ -14509,7 +14531,10 @@ function scanMarkupTolerant(source, options = {}, completion = false) {
14509
14531
  return run(text, "Tokens", {
14510
14532
  ...options,
14511
14533
  hooks
14512
- }, completion).map((token) => shiftOffsets(token, base));
14534
+ }, completion, (delimiter, from) => {
14535
+ const at = findDelimiter(delimiter, base + from, base + text.length);
14536
+ return at < 0 ? -1 : at - base;
14537
+ }).map((token) => shiftOffsets(token, base));
14513
14538
  } catch (error) {
14514
14539
  if (!(error instanceof MarkupError)) throw error;
14515
14540
  return error;
@@ -15263,6 +15288,7 @@ function parameterLookup(macros) {
15263
15288
  lookup.source = (name) => sources.get(name);
15264
15289
  const definitions = new NameMap(effectiveNamedDefinitions([...list.filter((macro) => !macro.widget), ...list.filter((macro) => macro.widget)]).map((macro) => [macro.name, macro]));
15265
15290
  lookup.pairing = {
15291
+ isStandalone: (name) => widgets.has(name) || sources.get(name) === "user",
15266
15292
  isBlock: (name) => definitions.get(name)?.block ?? isBuiltinBlockMacro(name),
15267
15293
  isRaw: (name) => isRawMacro(name, widgets.has(name)),
15268
15294
  branchParents: branchParentLookup([...definitions.values()].map((macro) => ({
@@ -21190,6 +21216,10 @@ var FOCUSABLE = [
21190
21216
  "[contenteditable]",
21191
21217
  "[tabindex]"
21192
21218
  ].join(",");
21219
+ /** Only the first summary child of a details element is its native toggle. */
21220
+ function isDetailsToggle(el) {
21221
+ return el.localName === "summary" && el.parentElement?.localName === "details" && Array.from(el.parentElement.children).find((c) => c.localName === "summary") === el;
21222
+ }
21193
21223
  /**
21194
21224
  * Whether `el` can be focused at all: shown, rendered, enabled (also by its
21195
21225
  * fieldset) and not inert.
@@ -21198,7 +21228,8 @@ function isAvailable(el) {
21198
21228
  if (!isRendered(el)) return false;
21199
21229
  if ((el.ownerDocument.defaultView ?? window).getComputedStyle(el).display === "contents") return false;
21200
21230
  for (let at = el; at; at = composedParent(at)) if (at.hasAttribute("inert")) return false;
21201
- return !el.matches(":disabled");
21231
+ if (el.matches(":disabled")) return false;
21232
+ return tabIndexAttr(el) !== null || el.tabIndex >= 0 || el.isContentEditable && !el.parentElement?.isContentEditable || isDetailsToggle(el);
21202
21233
  }
21203
21234
  /** The `tabindex` of `el` as an integer, or null when absent or invalid. */
21204
21235
  function tabIndexAttr(el) {
@@ -21228,10 +21259,7 @@ function isTabStop(el, radioStops) {
21228
21259
  const tabIndex = tabIndexAttr(el);
21229
21260
  if (tabIndex !== null && tabIndex < 0) return false;
21230
21261
  if (tabIndex === null && el.hasAttribute("contenteditable") && !el.matches("a[href], button, input, select, textarea, iframe, summary") && (!el.isContentEditable || el.parentElement?.isContentEditable)) return false;
21231
- if (el.localName === "summary") {
21232
- const details = el.parentElement;
21233
- if (details?.localName !== "details" || Array.from(details.children).find((c) => c.localName === "summary") !== el) return tabIndex !== null;
21234
- }
21262
+ if (el.localName === "summary" && !isDetailsToggle(el)) return tabIndex !== null;
21235
21263
  const input = el;
21236
21264
  if (el.localName === "input" && input.type === "radio" && input.name) {
21237
21265
  const tree = el.getRootNode();
@@ -23241,6 +23269,7 @@ function validateMarkup(passages, options) {
23241
23269
  const diagnostics = [];
23242
23270
  const positions = /* @__PURE__ */ new Map();
23243
23271
  const hooks = {
23272
+ ...options.parametersOf?.pairing?.isStandalone && { isStandalone: options.parametersOf.pairing.isStandalone },
23244
23273
  ...options.isBlockMacro && { isBlock: options.isBlockMacro },
23245
23274
  ...options.branchParents && { branchParents: options.branchParents },
23246
23275
  isRaw: (name) => isRawMacro(name, options.parametersOf ? options.parametersOf.widget?.(name) !== void 0 : void 0)
@@ -655,7 +655,7 @@ function peg$parse(input, options) {
655
655
  return lexeme("text", location(), { value });
656
656
  }
657
657
  function peg$f24() {
658
- return completionMode || offset() <= lastCommentClose;
658
+ return completionMode || findDelimiter("-->", offset()) >= 0;
659
659
  }
660
660
  function peg$f25(value) {
661
661
  const node = lexeme("text", location(), { value });
@@ -712,8 +712,8 @@ function peg$parse(input, options) {
712
712
  fail("Unclosed link: [[ without ]]", location(), "unclosed-link");
713
713
  }
714
714
  function peg$f37(start) {
715
- const close = input.indexOf("]]", start);
716
- const nested = input.indexOf("[[", start);
715
+ const close = findDelimiter("]]", start);
716
+ const nested = findDelimiter("[[", start);
717
717
  linkTextEnd = Math.min(close < 0 ? input.length : close, nested < 0 ? input.length : nested);
718
718
  return true;
719
719
  }
@@ -3573,7 +3573,7 @@ function peg$parse(input, options) {
3573
3573
  const textMode = options.text === true;
3574
3574
  const completionMode = options.completion === true;
3575
3575
  const hooks = options.hooks;
3576
- const lastCommentClose = input.lastIndexOf("-->");
3576
+ const findDelimiter = options.findDelimiter;
3577
3577
  /** End of the stretch a `.|n|` repetition consumes (see skipTo). */
3578
3578
  let skipEnd = -1;
3579
3579
  /** Whether the attribute whose value is being read holds code. */
@@ -3710,7 +3710,11 @@ function consumesOptionClauses(macro) {
3710
3710
  const name = macro.name.toLowerCase();
3711
3711
  return macro.source === "builtin" && !macro.widget && (name === "listbox" || name === "cycle");
3712
3712
  }
3713
- new NameMap();
3713
+ // src/registry.ts
3714
+ var registry = new NameMap();
3715
+ function getMacro(name) {
3716
+ return registry.get(name);
3717
+ }
3714
3718
  new NameMap();
3715
3719
  new NameSet();
3716
3720
  /**
@@ -3870,6 +3874,10 @@ function isBlockMacro(name) {
3870
3874
  if (DEFINED_MACROS.has(name)) return DEFINED_BLOCK_MACROS.has(name);
3871
3875
  return isBuiltinBlockMacro(name);
3872
3876
  }
3877
+ /** Explicit implementations can also be invoked outside a companion owner. */
3878
+ function isStandaloneMacro(name) {
3879
+ return WIDGETS.has(name) || getMacro(name) !== void 0;
3880
+ }
3873
3881
  /** Built-in defaults without any process-global registrations or widgets. */
3874
3882
  function isBuiltinBlockMacro(name) {
3875
3883
  return BLOCK_MACROS.has(name);
@@ -4030,6 +4038,7 @@ var closes = (open, close) => open.type === close.type && lowerName(open) === lo
4030
4038
  function pairMarkup(tokens, options = {}) {
4031
4039
  const isBlock = options.isBlock ?? isBlockMacro;
4032
4040
  const isRaw = options.isRaw ?? isRawMacro;
4041
+ const isStandalone = options.isStandalone ?? isStandaloneMacro;
4033
4042
  const parentsOf = (name) => {
4034
4043
  if (options.branchParents) return options.branchParents(name);
4035
4044
  const registered = branchParents(name);
@@ -4109,7 +4118,7 @@ function pairMarkup(tokens, options = {}) {
4109
4118
  }
4110
4119
  continue;
4111
4120
  }
4112
- if (token.type === "macro" && !token.isClose && parentsOf(token.name).length > 0) {
4121
+ if (token.type === "macro" && !token.isClose && parentsOf(token.name).length > 0 && (!isStandalone(token.name) || last$1(stack)?.node.token.type === "macro" && parentsOf(token.name).includes(lowerName(last$1(stack).node.token)))) {
4113
4122
  const tag = token;
4114
4123
  const frame = last$1(stack);
4115
4124
  if (!frame) {
@@ -10858,7 +10867,7 @@ var MarkupError = class extends Error {
10858
10867
  this.name = "MarkupError";
10859
10868
  }
10860
10869
  };
10861
- function run$1(source, startRule, options, completion = false) {
10870
+ function run$1(source, startRule, options, completion = false, findDelimiter = indexDelimiters(source)) {
10862
10871
  const hooks = {
10863
10872
  ...defaultHooks,
10864
10873
  ...options.hooks
@@ -10872,7 +10881,8 @@ function run$1(source, startRule, options, completion = false) {
10872
10881
  startRule,
10873
10882
  text: options.text === true,
10874
10883
  hooks,
10875
- completion
10884
+ completion,
10885
+ findDelimiter
10876
10886
  });
10877
10887
  } catch (err) {
10878
10888
  const { location, code } = err;
@@ -10884,6 +10894,20 @@ function run$1(source, startRule, options, completion = false) {
10884
10894
  throw err;
10885
10895
  }
10886
10896
  }
10897
+ /** Literal delimiter searches shared by suffix and prefix recovery attempts. */
10898
+ function indexDelimiters(source) {
10899
+ const positions = /* @__PURE__ */ new Map();
10900
+ return (delimiter, from, end = source.length) => {
10901
+ let offsets = positions.get(delimiter);
10902
+ if (!offsets) {
10903
+ offsets = [];
10904
+ for (let at = source.indexOf(delimiter); at >= 0; at = source.indexOf(delimiter, at + 1)) offsets.push(at);
10905
+ positions.set(delimiter, offsets);
10906
+ }
10907
+ const at = offsets[upperBound(offsets, from - 1)];
10908
+ return at !== void 0 && at + delimiter.length <= end ? at : -1;
10909
+ };
10910
+ }
10887
10911
  /**
10888
10912
  * Read markup that may be malformed: its tokens, their pairing and every
10889
10913
  * error (malformed tags, and closers and branches that pair with nothing),
@@ -10900,6 +10924,7 @@ function analyzeMarkup(source, options = {}) {
10900
10924
  isBlock: hooks.isBlock,
10901
10925
  isRaw: hooks.isRaw,
10902
10926
  branchParents: hooks.branchParents,
10927
+ isStandalone: hooks.isStandalone,
10903
10928
  source
10904
10929
  });
10905
10930
  if (errors.length === 0 && paired.errors.length === 0) return {
@@ -10984,6 +11009,7 @@ function scanMarkupTolerant(source, options = {}, completion = false) {
10984
11009
  const damage = [];
10985
11010
  let position;
10986
11011
  const codeEnd = indexCodeEnds(source);
11012
+ const findDelimiter = indexDelimiters(source);
10987
11013
  const customCloseBrace = options.hooks?.closeBrace;
10988
11014
  const lastBrace = source.lastIndexOf("}");
10989
11015
  /** `text` shifted by `base`, tokenized: the tokens or the error. */
@@ -11001,7 +11027,10 @@ function scanMarkupTolerant(source, options = {}, completion = false) {
11001
11027
  return run$1(text, "Tokens", {
11002
11028
  ...options,
11003
11029
  hooks
11004
- }, completion).map((token) => shiftOffsets(token, base));
11030
+ }, completion, (delimiter, from) => {
11031
+ const at = findDelimiter(delimiter, base + from, base + text.length);
11032
+ return at < 0 ? -1 : at - base;
11033
+ }).map((token) => shiftOffsets(token, base));
11005
11034
  } catch (error) {
11006
11035
  if (!(error instanceof MarkupError)) throw error;
11007
11036
  return error;
@@ -11715,6 +11744,7 @@ function parameterLookup(macros) {
11715
11744
  lookup.source = (name) => sources.get(name);
11716
11745
  const definitions = new NameMap(effectiveNamedDefinitions([...list.filter((macro) => !macro.widget), ...list.filter((macro) => macro.widget)]).map((macro) => [macro.name, macro]));
11717
11746
  lookup.pairing = {
11747
+ isStandalone: (name) => widgets.has(name) || sources.get(name) === "user",
11718
11748
  isBlock: (name) => definitions.get(name)?.block ?? isBuiltinBlockMacro(name),
11719
11749
  isRaw: (name) => isRawMacro(name, widgets.has(name)),
11720
11750
  branchParents: branchParentLookup([...definitions.values()].map((macro) => ({
@@ -12655,6 +12685,7 @@ function validateMarkup(passages, options) {
12655
12685
  const diagnostics = [];
12656
12686
  const positions = /* @__PURE__ */ new Map();
12657
12687
  const hooks = {
12688
+ ...options.parametersOf?.pairing?.isStandalone && { isStandalone: options.parametersOf.pairing.isStandalone },
12658
12689
  ...options.isBlockMacro && { isBlock: options.isBlockMacro },
12659
12690
  ...options.branchParents && { branchParents: options.branchParents },
12660
12691
  isRaw: (name) => isRawMacro(name, options.parametersOf ? options.parametersOf.widget?.(name) !== void 0 : void 0)
@@ -13969,29 +14000,46 @@ var Bindings = class {
13969
14000
  */
13970
14001
  statementEnd(from) {
13971
14002
  const { code } = this;
13972
- const start = this.forward(from);
13973
- const block = this.byOpen.get(start);
13974
- if (block?.kind === "{") return Math.min(block.close + 1, code.length);
13975
- const word = this.wordAt(start);
13976
- if (/^(?:if|for|while|with)$/.test(word)) {
13977
- let paren = this.forward(start + word.length);
13978
- if (word === "for" && this.wordAt(paren) === "await") paren = this.forward(paren + 5);
13979
- const header = this.byOpen.get(paren);
13980
- if (header) {
13981
- const end = this.statementEnd(header.close + 1);
14003
+ const pendingIf = [];
14004
+ statements: while (true) {
14005
+ const start = this.forward(from);
14006
+ const block = this.byOpen.get(start);
14007
+ let end;
14008
+ if (block?.kind === "{") end = Math.min(block.close + 1, code.length);
14009
+ else {
14010
+ const word = this.wordAt(start);
14011
+ if (/^(?:if|for|while|with)$/.test(word)) {
14012
+ let paren = this.forward(start + word.length);
14013
+ if (word === "for" && this.wordAt(paren) === "await") paren = this.forward(paren + 5);
14014
+ const header = this.byOpen.get(paren);
14015
+ if (header) {
14016
+ pendingIf.push(word === "if");
14017
+ from = header.close + 1;
14018
+ continue;
14019
+ }
14020
+ }
14021
+ for (end = start; end < code.length; end++) {
14022
+ const c = code.charAt(end);
14023
+ if (c === ";") {
14024
+ end++;
14025
+ break;
14026
+ }
14027
+ if (CLOSERS[c]) break;
14028
+ const group = this.byOpen.get(end);
14029
+ if (group) end = group.close;
14030
+ else if (c === "\n" && this.startsStatement(end)) break;
14031
+ }
14032
+ }
14033
+ while (pendingIf.length) {
14034
+ const isIf = pendingIf.pop();
13982
14035
  const next = this.forward(end);
13983
- return word === "if" && this.wordAt(next) === "else" ? this.statementEnd(next + 4) : end;
14036
+ if (isIf && this.wordAt(next) === "else") {
14037
+ from = next + 4;
14038
+ continue statements;
14039
+ }
13984
14040
  }
14041
+ return end;
13985
14042
  }
13986
- for (let end = start; end < code.length; end++) {
13987
- const c = code.charAt(end);
13988
- if (c === ";") return end + 1;
13989
- if (CLOSERS[c]) return end;
13990
- const group = this.byOpen.get(end);
13991
- if (group) end = group.close;
13992
- else if (c === "\n" && this.startsStatement(end)) return end;
13993
- }
13994
- return code.length;
13995
14043
  }
13996
14044
  /** Whether the line break at `index` ends a statement, and one follows. */
13997
14045
  startsStatement(index) {
@@ -25888,6 +25936,7 @@ async function formatLf(text, options) {
25888
25936
  return (await format(code, prettier, prettierOptions) ?? code).trim() + "\n";
25889
25937
  }
25890
25938
  const isBlock = options.isBlock ?? defaultIsBlock(body, options.blockMacros);
25939
+ if (analyzeMarkup(body, { hooks: { isBlock } }).errors.some((error) => error.code === "nesting-limit")) return text;
25891
25940
  const isDedenting = options.isDedentingSubMacro ?? ((name) => DEFAULT_DEDENTING.has(name.toLowerCase()));
25892
25941
  const sentinel = newlineSentinel(body);
25893
25942
  const literalSentinel = newlineSentinel(body + sentinel);
@@ -26378,6 +26427,7 @@ function tokenizationOptions(options) {
26378
26427
  isRaw: (name) => isRawMacro(name, parametersOf.widget?.(name) !== void 0),
26379
26428
  isBlock: blockLookup(macros),
26380
26429
  branchParents: branchParentLookup(macros),
26430
+ isStandalone: parametersOf.pairing.isStandalone,
26381
26431
  ...options.hooks
26382
26432
  }
26383
26433
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rohal12/spindle",
3
- "version": "0.64.32",
3
+ "version": "0.64.33",
4
4
  "type": "module",
5
5
  "description": "A Preact-based story format for Twine 2.",
6
6
  "license": "Unlicense",
package/src/code-check.ts CHANGED
@@ -396,6 +396,7 @@ export function parameterLookup(
396
396
  ]).map((macro) => [macro.name, macro]),
397
397
  );
398
398
  lookup.pairing = {
399
+ isStandalone: (name) => widgets.has(name) || sources.get(name) === 'user',
399
400
  isBlock: (name) =>
400
401
  definitions.get(name)?.block ?? isBuiltinBlockMacro(name),
401
402
  isRaw: (name) => isRawMacro(name, widgets.has(name)),
@@ -216,6 +216,15 @@ async function formatLf(
216
216
  }
217
217
 
218
218
  const isBlock = options.isBlock ?? defaultIsBlock(body, options.blockMacros);
219
+ // Pairing retains a tolerant tree beyond the runtime's nesting limit.
220
+ // Leave unsupported passages intact before any recursive formatter walks.
221
+ if (
222
+ analyzeMarkup(body, { hooks: { isBlock } }).errors.some(
223
+ (error) => error.code === 'nesting-limit',
224
+ )
225
+ ) {
226
+ return text;
227
+ }
219
228
  const isDedenting =
220
229
  options.isDedentingSubMacro ??
221
230
  ((name: string) => DEFAULT_DEDENTING.has(name.toLowerCase()));
@@ -22,6 +22,17 @@ const FOCUSABLE = [
22
22
  '[tabindex]',
23
23
  ].join(',');
24
24
 
25
+ /** Only the first summary child of a details element is its native toggle. */
26
+ function isDetailsToggle(el: HTMLElement): boolean {
27
+ return (
28
+ el.localName === 'summary' &&
29
+ el.parentElement?.localName === 'details' &&
30
+ Array.from(el.parentElement.children).find(
31
+ (c) => c.localName === 'summary',
32
+ ) === el
33
+ );
34
+ }
35
+
25
36
  /**
26
37
  * Whether `el` can be focused at all: shown, rendered, enabled (also by its
27
38
  * fieldset) and not inert.
@@ -38,7 +49,16 @@ function isAvailable(el: HTMLElement): boolean {
38
49
  if (at.hasAttribute('inert')) return false;
39
50
  }
40
51
  // Also covers controls disabled by a <fieldset disabled> ancestor
41
- return !el.matches(':disabled');
52
+ if (el.matches(':disabled')) return false;
53
+ // [tabindex] also selects empty/invalid attributes on plain elements.
54
+ // Those do not make the element focusable. Editing hosts can still
55
+ // report tabIndex -1; negative valid tabindexes allow scripted focus.
56
+ return (
57
+ tabIndexAttr(el) !== null ||
58
+ el.tabIndex >= 0 ||
59
+ (el.isContentEditable && !el.parentElement?.isContentEditable) ||
60
+ isDetailsToggle(el)
61
+ );
42
62
  }
43
63
 
44
64
  /** The `tabindex` of `el` as an integer, or null when absent or invalid. */
@@ -92,15 +112,8 @@ function isTabStop(el: HTMLElement, radioStops: RadioStops): boolean {
92
112
  return false;
93
113
  }
94
114
  // Only a details element's first summary child is its toggle
95
- if (el.localName === 'summary') {
96
- const details = el.parentElement;
97
- if (
98
- details?.localName !== 'details' ||
99
- Array.from(details.children).find((c) => c.localName === 'summary') !== el
100
- ) {
101
- return tabIndex !== null;
102
- }
103
- }
115
+ if (el.localName === 'summary' && !isDetailsToggle(el))
116
+ return tabIndex !== null;
104
117
  // localName, not instanceof: elements of an embedded document are of
105
118
  // another realm's classes
106
119
  const input = el as HTMLInputElement;
@@ -336,33 +336,51 @@ export class Bindings {
336
336
  */
337
337
  private statementEnd(from: number): number {
338
338
  const { code } = this;
339
- const start = this.forward(from);
340
- const block = this.byOpen.get(start);
341
- if (block?.kind === '{') return Math.min(block.close + 1, code.length);
342
- const word = this.wordAt(start);
343
- if (/^(?:if|for|while|with)$/.test(word)) {
344
- let paren = this.forward(start + word.length);
345
- if (word === 'for' && this.wordAt(paren) === 'await') {
346
- paren = this.forward(paren + 5);
339
+ // Suspend enclosing statements on the heap: distributed scripts can
340
+ // contain arbitrarily deep unbraced bodies, even if JS cannot run them.
341
+ const pendingIf: boolean[] = [];
342
+ statements: while (true) {
343
+ const start = this.forward(from);
344
+ const block = this.byOpen.get(start);
345
+ let end: number;
346
+ if (block?.kind === '{') {
347
+ end = Math.min(block.close + 1, code.length);
348
+ } else {
349
+ const word = this.wordAt(start);
350
+ if (/^(?:if|for|while|with)$/.test(word)) {
351
+ let paren = this.forward(start + word.length);
352
+ if (word === 'for' && this.wordAt(paren) === 'await') {
353
+ paren = this.forward(paren + 5);
354
+ }
355
+ const header = this.byOpen.get(paren);
356
+ if (header) {
357
+ pendingIf.push(word === 'if');
358
+ from = header.close + 1;
359
+ continue;
360
+ }
361
+ }
362
+ for (end = start; end < code.length; end++) {
363
+ const c = code.charAt(end);
364
+ if (c === ';') {
365
+ end++;
366
+ break;
367
+ }
368
+ if (CLOSERS[c]) break;
369
+ const group = this.byOpen.get(end);
370
+ if (group) end = group.close;
371
+ else if (c === '\n' && this.startsStatement(end)) break;
372
+ }
347
373
  }
348
- const header = this.byOpen.get(paren);
349
- if (header) {
350
- const end = this.statementEnd(header.close + 1);
374
+ while (pendingIf.length) {
375
+ const isIf = pendingIf.pop();
351
376
  const next = this.forward(end);
352
- return word === 'if' && this.wordAt(next) === 'else'
353
- ? this.statementEnd(next + 4)
354
- : end;
377
+ if (isIf && this.wordAt(next) === 'else') {
378
+ from = next + 4;
379
+ continue statements;
380
+ }
355
381
  }
382
+ return end;
356
383
  }
357
- for (let end = start; end < code.length; end++) {
358
- const c = code.charAt(end);
359
- if (c === ';') return end + 1;
360
- if (CLOSERS[c]) return end;
361
- const group = this.byOpen.get(end);
362
- if (group) end = group.close;
363
- else if (c === '\n' && this.startsStatement(end)) return end;
364
- }
365
- return code.length;
366
384
  }
367
385
 
368
386
  /** Whether the line break at `index` ends a statement, and one follows. */
package/src/markup/ast.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  import { withSelectors, type Selectors, type VariableScope } from './tokens';
6
6
  import type { PairedBranch, PairedNode } from './pair';
7
7
  import { NameSet } from '../utils/macro-names';
8
- import { getMacroParameters } from '../registry';
8
+ import { getMacro, getMacroParameters } from '../registry';
9
9
 
10
10
  export interface TextNode {
11
11
  type: 'text';
@@ -108,6 +108,11 @@ export function isBlockMacro(name: string): boolean {
108
108
  return isBuiltinBlockMacro(name);
109
109
  }
110
110
 
111
+ /** Explicit implementations can also be invoked outside a companion owner. */
112
+ export function isStandaloneMacro(name: string): boolean {
113
+ return WIDGETS.has(name) || getMacro(name) !== undefined;
114
+ }
115
+
111
116
  /** Built-in defaults without any process-global registrations or widgets. */
112
117
  export function isBuiltinBlockMacro(name: string): boolean {
113
118
  return BLOCK_MACROS.has(name);
@@ -12,7 +12,7 @@
12
12
  */
13
13
  import { branchParents, getMacroParameters } from '../registry';
14
14
  import { sourcePositions } from './line-column';
15
- import { isBlockMacro, isRawMacro } from './ast';
15
+ import { isBlockMacro, isRawMacro, isStandaloneMacro } from './ast';
16
16
  import type { HtmlToken, MacroToken, Span, Token } from './tokens';
17
17
 
18
18
  /** What is wrong with the pairing, by kind. */
@@ -82,6 +82,8 @@ export interface PairMarkupOptions {
82
82
  isRaw?(name: string): boolean;
83
83
  /** Parents declaring a branching sub-macro (default: registered metadata). */
84
84
  branchParents?(name: string): readonly string[];
85
+ /** Whether a companion name also has a standalone macro/widget implementation. */
86
+ isStandalone?(name: string): boolean;
85
87
  /**
86
88
  * The markup the tokens are of: lets a message say at which line and column
87
89
  * an element was opened (else it says at which offset).
@@ -152,6 +154,7 @@ export function pairMarkup(
152
154
  ): PairedMarkup {
153
155
  const isBlock = options.isBlock ?? isBlockMacro;
154
156
  const isRaw = options.isRaw ?? isRawMacro;
157
+ const isStandalone = options.isStandalone ?? isStandaloneMacro;
155
158
  const parentsOf = (name: string): readonly string[] => {
156
159
  if (options.branchParents) return options.branchParents(name);
157
160
  const registered = branchParents(name);
@@ -291,7 +294,12 @@ export function pairMarkup(
291
294
  if (
292
295
  token.type === 'macro' &&
293
296
  !token.isClose &&
294
- parentsOf(token.name).length > 0
297
+ parentsOf(token.name).length > 0 &&
298
+ (!isStandalone(token.name) ||
299
+ (last(stack)?.node.token.type === 'macro' &&
300
+ parentsOf(token.name).includes(
301
+ lowerName(last(stack)!.node.token as MacroToken),
302
+ )))
295
303
  ) {
296
304
  const tag = token as MacroToken;
297
305
  const frame = last(stack);