@rohal12/spindle 0.62.5 → 0.62.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -53,6 +53,10 @@ function randomUUID() {
53
53
  }
54
54
  //#endregion
55
55
  //#region src/utils/element-visibility.ts
56
+ /** The parent in the rendered tree, including slots and shadow hosts. */
57
+ function composedParent(el) {
58
+ return el.assignedSlot ?? el.parentElement ?? el.getRootNode().host ?? null;
59
+ }
56
60
  /** Elements whose content is never text the reader reads. */
57
61
  var NOT_TEXT = /* @__PURE__ */ new Set([
58
62
  "SCRIPT",
@@ -72,10 +76,14 @@ function isHidden(el) {
72
76
  * toggles it: the browser renders none of that content (#389).
73
77
  */
74
78
  function inClosedDetails(el, root) {
75
- const stop = root?.parentElement;
76
- for (let e = el.parentElement; e && e !== stop; e = e.parentElement) {
77
- if (e.localName !== "details" || e.hasAttribute("open")) continue;
78
- if (!Array.from(e.children).find((c) => c.localName === "summary")?.contains(el)) return true;
79
+ for (let child = el; child !== root;) {
80
+ const parent = composedParent(child);
81
+ if (!parent) break;
82
+ if (parent.localName === "details" && !parent.hasAttribute("open")) {
83
+ const summary = Array.from(parent.children).find((c) => c.localName === "summary");
84
+ if (child !== summary) return true;
85
+ }
86
+ child = parent;
79
87
  }
80
88
  return false;
81
89
  }
@@ -90,7 +98,7 @@ function isRendered(el, root) {
90
98
  const style = (e) => e.ownerDocument.defaultView?.getComputedStyle(e);
91
99
  const visibility = style(el)?.visibility;
92
100
  if (visibility === "hidden" || visibility === "collapse") return false;
93
- for (let e = el; e; e = e.parentElement) {
101
+ for (let e = el; e; e = composedParent(e)) {
94
102
  if (style(e)?.display === "none") return false;
95
103
  if (e === root) break;
96
104
  }
@@ -99,7 +107,7 @@ function isRendered(el, root) {
99
107
  /** Visibility shared by reader observations and native control handling. */
100
108
  function isReaderVisible(el) {
101
109
  if (!el.isConnected || !isRendered(el)) return false;
102
- for (let at = el; at; at = at.parentElement) if (isHidden(at) || at.hasAttribute("inert")) return false;
110
+ for (let at = el; at; at = composedParent(at)) if (isHidden(at) || at.hasAttribute("inert")) return false;
103
111
  return true;
104
112
  }
105
113
  //#endregion
@@ -10786,9 +10794,13 @@ function indexCodeEnds(input) {
10786
10794
  if (open !== void 0) possible.add(open);
10787
10795
  } else if (/['"`/\\]/.test(c)) uncertain();
10788
10796
  }
10789
- return (start) => {
10790
- if (start > lastBrace) return -1;
10791
- return possible.has(start - 1) ? defaultCodeEnd.closeBrace(input, start, start) : parsedCodeEnd(input, start, "expression");
10797
+ return (start, codeStart = start, limit = input.length) => {
10798
+ if (start > lastBrace || codeStart >= limit) return -1;
10799
+ const source = limit === input.length ? input : input.slice(0, limit);
10800
+ if (input[start - 1] !== "{" || possible.has(start - 1) || input.slice(start, codeStart).includes("{")) return defaultCodeEnd.closeBrace(source, codeStart, start);
10801
+ const base = Math.max(0, codeStart - 1);
10802
+ const end = parsedCodeEnd(source.slice(base), codeStart - base, "expression");
10803
+ return end < 0 ? -1 : base + end;
10792
10804
  };
10793
10805
  }
10794
10806
  //#endregion
@@ -10868,10 +10880,14 @@ var MarkupError = class extends Error {
10868
10880
  }
10869
10881
  };
10870
10882
  function run$2(source, startRule, options, completion = false) {
10871
- const hooks = options.hooks ? {
10883
+ const hooks = {
10872
10884
  ...defaultHooks,
10873
10885
  ...options.hooks
10874
- } : defaultHooks;
10886
+ };
10887
+ if (!options.hooks?.closeBrace) {
10888
+ const codeEnd = indexCodeEnds(source);
10889
+ hooks.closeBrace = (_input, codeStart, lenientStart) => codeEnd(lenientStart, codeStart);
10890
+ }
10875
10891
  try {
10876
10892
  return peg$parse(source, {
10877
10893
  startRule,
@@ -10971,16 +10987,19 @@ function scanMarkupTolerant(source, options = {}, completion = false) {
10971
10987
  const errors = [];
10972
10988
  const damage = [];
10973
10989
  let position;
10990
+ const codeEnd = indexCodeEnds(source);
10991
+ const customCloseBrace = options.hooks?.closeBrace;
10974
10992
  const lastBrace = source.lastIndexOf("}");
10975
- const { closeBrace } = {
10976
- ...defaultHooks,
10977
- ...options.hooks
10978
- };
10979
10993
  /** `text` shifted by `base`, tokenized: the tokens or the error. */
10980
10994
  const attempt = (text, base) => {
10981
10995
  const hooks = {
10982
10996
  ...options.hooks,
10983
- closeBrace: (input, codeStart, lenientStart) => base + Math.min(codeStart, lenientStart) > lastBrace ? -1 : closeBrace(input, codeStart, lenientStart)
10997
+ closeBrace: (input, codeStart, lenientStart) => {
10998
+ if (base + Math.min(codeStart, lenientStart) > lastBrace) return -1;
10999
+ if (customCloseBrace) return customCloseBrace(input, codeStart, lenientStart);
11000
+ const end = codeEnd(base + lenientStart, base + codeStart, base + text.length);
11001
+ return end < 0 ? -1 : end - base;
11002
+ }
10984
11003
  };
10985
11004
  try {
10986
11005
  return run$2(text, "Tokens", {
@@ -11867,9 +11886,13 @@ function widgetParameters(params, holds = {}) {
11867
11886
  ...hasOwn(holds, name) ? { holds: holds[name] } : {}
11868
11887
  }));
11869
11888
  }
11889
+ /** Whether builtin-only interpretation still belongs to this implementation. */
11890
+ function usesBuiltinSemantics(name, parametersOf) {
11891
+ return parametersOf.widget?.(name) === void 0 && parametersOf.source?.(name) !== "user";
11892
+ }
11870
11893
  /** A raw body is executable only while the builtin implementation owns it. */
11871
11894
  function executesRawBody(name, parametersOf) {
11872
- return isRawMacro(name, parametersOf.widget?.(name) !== void 0) && parametersOf.source?.(name) !== "user";
11895
+ return isRawMacro(name, false) && usesBuiltinSemantics(name, parametersOf);
11873
11896
  }
11874
11897
  /**
11875
11898
  * `params` with each `string` and `text` parameter (options too) saying
@@ -11902,12 +11925,13 @@ function parameterLookup(macros) {
11902
11925
  for (const { name, parameters: params, interpolate, widget, source } of macros) {
11903
11926
  sources.set(name, source ?? "user");
11904
11927
  if (params) parameters.set(name, withHolds(params, holdsByDefault(interpolate)));
11928
+ else parameters.delete(name);
11905
11929
  if (widget) widgets.set(name, widgetParameters(widget.params, widget.holds) ?? []);
11906
11930
  }
11907
11931
  const lookup = (name) => {
11908
11932
  if (widgets.has(name)) return void 0;
11909
11933
  let params = parameters.get(name);
11910
- if (!params) {
11934
+ if (!params && sources.get(name) !== "user") {
11911
11935
  const sub = subMacroParameters(name);
11912
11936
  if (sub) parameters.set(name, params = withHolds(sub, "text"));
11913
11937
  }
@@ -11994,7 +12018,7 @@ function* codeAndText(src, tokens, parametersOf) {
11994
12018
  continue;
11995
12019
  }
11996
12020
  const args = token.rawArgs;
11997
- if (PASSAGE_BODIES.has(name) && !widget) {
12021
+ if (PASSAGE_BODIES.has(name) && usesBuiltinSemantics(name, parametersOf)) {
11998
12022
  const close = closeOfTextBody(t, name);
11999
12023
  if (close > t + 1) {
12000
12024
  const text = src.slice(token.end, tokens[close].start);
@@ -12012,7 +12036,7 @@ function* codeAndText(src, tokens, parametersOf) {
12012
12036
  if (!args) continue;
12013
12037
  const label = `{${token.name} ${args}}`;
12014
12038
  const argsAt = token.argsStart;
12015
- if (CONDITION_MACROS.has(name) && !widget) {
12039
+ if (CONDITION_MACROS.has(name) && usesBuiltinSemantics(name, parametersOf)) {
12016
12040
  yield {
12017
12041
  kind: "code",
12018
12042
  code: args,
@@ -14490,7 +14514,7 @@ function renderInlineNodes(nodes) {
14490
14514
  * line ending (`\r\n` or `\r`) also takes the full pipeline, which reads
14491
14515
  * it as micromark does.
14492
14516
  */
14493
- var MARKDOWN_SYNTAX_RE = /[*_`#|~\[>\\\-+=\r]|!\[|\d+[.)]|&#?[a-zA-Z0-9]+;| {2}\n|<[!?]/;
14517
+ var MARKDOWN_SYNTAX_RE = /[*_`#|~\[>\\\-+=\r]|!\[|\d{1,9}[.)]|&#?[a-zA-Z0-9]+;| {2}\n|<[!?]/;
14494
14518
  /** Two line endings (LF, CRLF or CR) with only whitespace between them. */
14495
14519
  var BLANK_LINE_RE = /(?:\r\n|\r(?!\n)|\n)\s*[\r\n]/;
14496
14520
  var PLACEHOLDER_STRIP_RE = /<span data-tw=[0-9a-z]+:\d+><\/span>/g;
@@ -20298,10 +20322,6 @@ var FOCUSABLE = [
20298
20322
  "[contenteditable]",
20299
20323
  "[tabindex]"
20300
20324
  ].join(",");
20301
- /** The parent in the rendered tree, including slots and shadow hosts. */
20302
- function composedParent(el) {
20303
- return el.assignedSlot ?? el.parentElement ?? el.getRootNode().host ?? null;
20304
- }
20305
20325
  function composedChildren(root) {
20306
20326
  if (root.localName === "slot") return root.assignedNodes({ flatten: true }).filter((node) => node.nodeType === 1);
20307
20327
  return Array.from(root.children);
@@ -24555,8 +24575,9 @@ var SETTLING_MACROS = new NameSet([
24555
24575
  function settlesAfterRender(nodes) {
24556
24576
  return nodes.every((n) => {
24557
24577
  switch (n.type) {
24558
- case "macro": return SETTLING_MACROS.has(n.name) && settlesAfterRender(n.children) && (n.branches ?? []).every((b) => settlesAfterRender(b.children));
24578
+ case "macro": return SETTLING_MACROS.has(n.name) && !getWidget(n.name) && getMacroParameters.source(n.name) === "builtin" && settlesAfterRender(n.children) && (n.branches ?? []).every((b) => settlesAfterRender(b.children));
24559
24579
  case "html": return settlesAfterRender(n.children);
24580
+ case "variable": return n.scope !== "local" || n.name !== "children";
24560
24581
  default: return true;
24561
24582
  }
24562
24583
  });
@@ -10,6 +10,46 @@ var __exportAll = (all, no_symbols) => {
10
10
  return target;
11
11
  };
12
12
  //#endregion
13
+ //#region src/utils/macro-names.ts
14
+ /**
15
+ * Macro and widget names are case-insensitive: `{StatusBar}` invokes the
16
+ * widget defined as `statusbar`. A NameMap keys its entries by the lowercased
17
+ * name.
18
+ */
19
+ var NameMap = class extends Map {
20
+ get(name) {
21
+ return super.get(name.toLowerCase());
22
+ }
23
+ set(name, value) {
24
+ return super.set(name.toLowerCase(), value);
25
+ }
26
+ has(name) {
27
+ return super.has(name.toLowerCase());
28
+ }
29
+ delete(name) {
30
+ return super.delete(name.toLowerCase());
31
+ }
32
+ };
33
+ /** A set of macro or widget names (see NameMap). */
34
+ var NameSet = class {
35
+ constructor(names = []) {
36
+ this.names = new NameMap();
37
+ for (const name of names) this.add(name);
38
+ }
39
+ add(name) {
40
+ this.names.set(name, true);
41
+ }
42
+ has(name) {
43
+ return this.names.has(name);
44
+ }
45
+ delete(name) {
46
+ this.names.delete(name);
47
+ }
48
+ clear() {
49
+ this.names.clear();
50
+ }
51
+ };
52
+ //#endregion
13
53
  //#region src/markup/tokens.ts
14
54
  /** Variable sigils: story ($), temporary (_), local (@), transient (%). */
15
55
  var SIGIL_SCOPES = {
@@ -3591,46 +3631,6 @@ function peg$parse(input, options) {
3591
3631
  else peg$throw();
3592
3632
  }
3593
3633
  //#endregion
3594
- //#region src/utils/macro-names.ts
3595
- /**
3596
- * Macro and widget names are case-insensitive: `{StatusBar}` invokes the
3597
- * widget defined as `statusbar`. A NameMap keys its entries by the lowercased
3598
- * name.
3599
- */
3600
- var NameMap = class extends Map {
3601
- get(name) {
3602
- return super.get(name.toLowerCase());
3603
- }
3604
- set(name, value) {
3605
- return super.set(name.toLowerCase(), value);
3606
- }
3607
- has(name) {
3608
- return super.has(name.toLowerCase());
3609
- }
3610
- delete(name) {
3611
- return super.delete(name.toLowerCase());
3612
- }
3613
- };
3614
- /** A set of macro or widget names (see NameMap). */
3615
- var NameSet = class {
3616
- constructor(names = []) {
3617
- this.names = new NameMap();
3618
- for (const name of names) this.add(name);
3619
- }
3620
- add(name) {
3621
- this.names.set(name, true);
3622
- }
3623
- has(name) {
3624
- return this.names.has(name);
3625
- }
3626
- delete(name) {
3627
- this.names.delete(name);
3628
- }
3629
- clear() {
3630
- this.names.clear();
3631
- }
3632
- };
3633
- //#endregion
3634
3634
  //#region src/markup/ast.ts
3635
3635
  /**
3636
3636
  * The passage AST (built by parse.ts), and the registry of block macros:
@@ -10254,9 +10254,13 @@ function indexCodeEnds(input) {
10254
10254
  if (open !== void 0) possible.add(open);
10255
10255
  } else if (/['"`/\\]/.test(c)) uncertain();
10256
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");
10257
+ return (start, codeStart = start, limit = input.length) => {
10258
+ if (start > lastBrace || codeStart >= limit) return -1;
10259
+ const source = limit === input.length ? input : input.slice(0, limit);
10260
+ if (input[start - 1] !== "{" || possible.has(start - 1) || input.slice(start, codeStart).includes("{")) return defaultCodeEnd.closeBrace(source, codeStart, start);
10261
+ const base = Math.max(0, codeStart - 1);
10262
+ const end = parsedCodeEnd(source.slice(base), codeStart - base, "expression");
10263
+ return end < 0 ? -1 : base + end;
10260
10264
  };
10261
10265
  }
10262
10266
  //#endregion
@@ -10336,10 +10340,14 @@ var MarkupError = class extends Error {
10336
10340
  }
10337
10341
  };
10338
10342
  function run$1(source, startRule, options, completion = false) {
10339
- const hooks = options.hooks ? {
10343
+ const hooks = {
10340
10344
  ...defaultHooks,
10341
10345
  ...options.hooks
10342
- } : defaultHooks;
10346
+ };
10347
+ if (!options.hooks?.closeBrace) {
10348
+ const codeEnd = indexCodeEnds(source);
10349
+ hooks.closeBrace = (_input, codeStart, lenientStart) => codeEnd(lenientStart, codeStart);
10350
+ }
10343
10351
  try {
10344
10352
  return peg$parse(source, {
10345
10353
  startRule,
@@ -10411,7 +10419,7 @@ function pairingError(position, error) {
10411
10419
  * macros and elements are no error here. Throws a MarkupError for a
10412
10420
  * malformed tag, such as an unclosed `{`, `[[` or attribute value.
10413
10421
  */
10414
- function tokenizeMarkup(source, options = {}) {
10422
+ function tokenizeMarkup$1(source, options = {}) {
10415
10423
  return run$1(source, "Tokens", options);
10416
10424
  }
10417
10425
  /**
@@ -10434,7 +10442,7 @@ function parseSelectors(source, at = 0) {
10434
10442
  * started. Tokens and errors have offsets in `source`; for well-formed
10435
10443
  * markup the tokens are those of `tokenizeMarkup` and there are no errors.
10436
10444
  */
10437
- function tokenizeMarkupTolerant(source, options = {}) {
10445
+ function tokenizeMarkupTolerant$1(source, options = {}) {
10438
10446
  const { tokens, errors } = scanMarkupTolerant(source, options);
10439
10447
  return {
10440
10448
  tokens,
@@ -10451,16 +10459,19 @@ function scanMarkupTolerant(source, options = {}, completion = false) {
10451
10459
  const errors = [];
10452
10460
  const damage = [];
10453
10461
  let position;
10462
+ const codeEnd = indexCodeEnds(source);
10463
+ const customCloseBrace = options.hooks?.closeBrace;
10454
10464
  const lastBrace = source.lastIndexOf("}");
10455
- const { closeBrace } = {
10456
- ...defaultHooks,
10457
- ...options.hooks
10458
- };
10459
10465
  /** `text` shifted by `base`, tokenized: the tokens or the error. */
10460
10466
  const attempt = (text, base) => {
10461
10467
  const hooks = {
10462
10468
  ...options.hooks,
10463
- closeBrace: (input, codeStart, lenientStart) => base + Math.min(codeStart, lenientStart) > lastBrace ? -1 : closeBrace(input, codeStart, lenientStart)
10469
+ closeBrace: (input, codeStart, lenientStart) => {
10470
+ if (base + Math.min(codeStart, lenientStart) > lastBrace) return -1;
10471
+ if (customCloseBrace) return customCloseBrace(input, codeStart, lenientStart);
10472
+ const end = codeEnd(base + lenientStart, base + codeStart, base + text.length);
10473
+ return end < 0 ? -1 : end - base;
10474
+ }
10464
10475
  };
10465
10476
  try {
10466
10477
  return run$1(text, "Tokens", {
@@ -11204,9 +11215,13 @@ function widgetParameters(params, holds = {}) {
11204
11215
  ...hasOwn(holds, name) ? { holds: holds[name] } : {}
11205
11216
  }));
11206
11217
  }
11218
+ /** Whether builtin-only interpretation still belongs to this implementation. */
11219
+ function usesBuiltinSemantics(name, parametersOf) {
11220
+ return parametersOf.widget?.(name) === void 0 && parametersOf.source?.(name) !== "user";
11221
+ }
11207
11222
  /** A raw body is executable only while the builtin implementation owns it. */
11208
11223
  function executesRawBody(name, parametersOf) {
11209
- return isRawMacro(name, parametersOf.widget?.(name) !== void 0) && parametersOf.source?.(name) !== "user";
11224
+ return isRawMacro(name, false) && usesBuiltinSemantics(name, parametersOf);
11210
11225
  }
11211
11226
  /**
11212
11227
  * `params` with each `string` and `text` parameter (options too) saying
@@ -11239,12 +11254,13 @@ function parameterLookup(macros) {
11239
11254
  for (const { name, parameters: params, interpolate, widget, source } of macros) {
11240
11255
  sources.set(name, source ?? "user");
11241
11256
  if (params) parameters.set(name, withHolds(params, holdsByDefault(interpolate)));
11257
+ else parameters.delete(name);
11242
11258
  if (widget) widgets.set(name, widgetParameters(widget.params, widget.holds) ?? []);
11243
11259
  }
11244
11260
  const lookup = (name) => {
11245
11261
  if (widgets.has(name)) return void 0;
11246
11262
  let params = parameters.get(name);
11247
- if (!params) {
11263
+ if (!params && sources.get(name) !== "user") {
11248
11264
  const sub = subMacroParameters(name);
11249
11265
  if (sub) parameters.set(name, params = withHolds(sub, "text"));
11250
11266
  }
@@ -11331,7 +11347,7 @@ function* codeAndText(src, tokens, parametersOf) {
11331
11347
  continue;
11332
11348
  }
11333
11349
  const args = token.rawArgs;
11334
- if (PASSAGE_BODIES.has(name) && !widget) {
11350
+ if (PASSAGE_BODIES.has(name) && usesBuiltinSemantics(name, parametersOf)) {
11335
11351
  const close = closeOfTextBody(t, name);
11336
11352
  if (close > t + 1) {
11337
11353
  const text = src.slice(token.end, tokens[close].start);
@@ -11349,7 +11365,7 @@ function* codeAndText(src, tokens, parametersOf) {
11349
11365
  if (!args) continue;
11350
11366
  const label = `{${token.name} ${args}}`;
11351
11367
  const argsAt = token.argsStart;
11352
- if (CONDITION_MACROS.has(name) && !widget) {
11368
+ if (CONDITION_MACROS.has(name) && usesBuiltinSemantics(name, parametersOf)) {
11353
11369
  yield {
11354
11370
  kind: "code",
11355
11371
  code: args,
@@ -11709,7 +11725,7 @@ var LITERAL = { kind: "literal" };
11709
11725
  function completionContext(source, cursor, macros = []) {
11710
11726
  if (!Number.isInteger(cursor) || cursor < 0 || cursor > source.length) throw new RangeError("cursor must be a UTF-16 offset within source");
11711
11727
  const parametersOf = parameterLookup(macros);
11712
- const codeContext = (code, goal, map) => {
11728
+ const codeContext = (code, goal, map, containers) => {
11713
11729
  const probe = code.length + 1;
11714
11730
  let executable = false;
11715
11731
  lexJs(code + " __spindle_cursor__", {
@@ -11729,15 +11745,16 @@ function completionContext(source, cursor, macros = []) {
11729
11745
  goal,
11730
11746
  offset,
11731
11747
  cursor: code.length,
11748
+ ...containers.length ? { containers } : {},
11732
11749
  ...offsets.some((at, i) => at !== offset + i) ? { sourceOffsets: offsets } : {}
11733
11750
  };
11734
11751
  };
11735
- const read = (src, map, text = false, depth = 0, sigilsOnly = false) => {
11752
+ const read = (src, map, text = false, depth = 0, sigilsOnly = false, containers = []) => {
11736
11753
  if (depth >= 64) return LITERAL;
11737
11754
  if (sigilsOnly) {
11738
11755
  let at = 0;
11739
11756
  for (const part of splitSigilTemplate(src, true)) if ("expr" in part) {
11740
- if (at + 1 + part.expr.length === src.length) return codeContext(part.expr, "expression", (i) => map(at + 1 + i));
11757
+ if (at + 1 + part.expr.length === src.length) return codeContext(part.expr, "expression", (i) => map(at + 1 + i), containers);
11741
11758
  at += part.expr.length + 2;
11742
11759
  } else at += ("text" in part ? part.text : part.verbatim).length;
11743
11760
  return LITERAL;
@@ -11748,13 +11765,21 @@ function completionContext(source, cursor, macros = []) {
11748
11765
  });
11749
11766
  const token = tokens[tokens.length - 1];
11750
11767
  if (!token || token.end !== src.length) return LITERAL;
11751
- const codeAt = (code, at, goal = "expression") => codeContext(code, goal, (i) => map(at + i));
11752
- const nested = (value, at, offsets, onlySigils = false) => read(value, (i) => map(offsets?.[i] ?? at + i), true, depth + 1, onlySigils);
11768
+ const codeAt = (code, at, goal = "expression") => codeContext(code, goal, (i) => map(at + i), containers);
11769
+ const nested = (value, at, offsets, onlySigils = false, container) => read(value, (i) => map(offsets?.[i] ?? at + i), true, depth + 1, onlySigils, container ? [...containers, container] : containers);
11753
11770
  if (token.type === "variable" && token.nameEnd === src.length) return codeAt(src.slice(token.nameStart - 1), token.nameStart - 1);
11754
11771
  if (token.type === "expression" && token.expressionEnd === src.length) return codeAt(token.expression, token.expressionStart);
11755
11772
  if (token.type === "html") {
11756
- for (const { name, value, at } of attributeValues(src, token)) if (at + value.length === src.length) return nested(value, at, void 0, isCodeAttribute(name));
11757
- } else if (token.type === "link" && token.displayEnd === src.length) return nested(token.display, token.displayStart);
11773
+ for (const { name, value, at } of attributeValues(src, token)) if (at + value.length === src.length) {
11774
+ const span = token.attributeSpans.find((span) => span.valueStart === at);
11775
+ return nested(value, at, void 0, isCodeAttribute(name), {
11776
+ kind: "attribute",
11777
+ name,
11778
+ quote: span?.quote ?? null,
11779
+ code: isCodeAttribute(name)
11780
+ });
11781
+ }
11782
+ } else if (token.type === "link" && token.displayEnd === src.length) return nested(token.display, token.displayStart, void 0, false, { kind: "link-label" });
11758
11783
  else if (token.type === "text" && !token.comment) {
11759
11784
  const opener = tokens[tokens.length - 2];
11760
11785
  if (opener?.type === "macro" && !opener.isClose && executesRawBody(opener.name, parametersOf)) return codeAt(token.value, token.start, "statements");
@@ -11762,7 +11787,7 @@ function completionContext(source, cursor, macros = []) {
11762
11787
  const name = token.name.toLowerCase();
11763
11788
  const args = token.rawArgs;
11764
11789
  const widget = parametersOf.widget?.(name);
11765
- if (!widget && [
11790
+ if (usesBuiltinSemantics(name, parametersOf) && [
11766
11791
  "if",
11767
11792
  "elseif",
11768
11793
  "case"
@@ -11776,9 +11801,14 @@ function completionContext(source, cursor, macros = []) {
11776
11801
  if (scanStringLiteral(raw, 0).closed) return LITERAL;
11777
11802
  const value = escapes === "javascript" ? stringLiteralValue(raw + raw[0]) : quoted.value;
11778
11803
  if (value == null) return LITERAL;
11804
+ const container = {
11805
+ kind: "string",
11806
+ quote: raw[0],
11807
+ escapes
11808
+ };
11779
11809
  const offsets = escapeOffsets(raw.slice(1), at + 1, value, escapes);
11780
- if (holds === "expression" || holds === "statements") return codeContext(value, holds, (i) => map(offsets?.[i] ?? at + 1 + i));
11781
- return holds === "markup" ? nested(value, at + 1, offsets) : LITERAL;
11810
+ if (holds === "expression" || holds === "statements") return codeContext(value, holds, (i) => map(offsets?.[i] ?? at + 1 + i), [...containers, container]);
11811
+ return holds === "markup" ? nested(value, at + 1, offsets, false, container) : LITERAL;
11782
11812
  };
11783
11813
  if (widget) {
11784
11814
  const spans = splitArgSpans(args);
@@ -11960,7 +11990,7 @@ function widgetDefinitions$1(passages, parametersOf = registeredParameters) {
11960
11990
  if (passage.name !== "StoryInit" && !passage.tags?.includes("widget")) continue;
11961
11991
  const open = [];
11962
11992
  const done = [];
11963
- for (const token of tokenizeMarkupTolerant(passage.content, { hooks }).tokens) if (token.type === "macro" && token.name.toLowerCase() === "widget") {
11993
+ for (const token of tokenizeMarkupTolerant$1(passage.content, { hooks }).tokens) if (token.type === "macro" && token.name.toLowerCase() === "widget") {
11964
11994
  if (!token.isClose) open.push({
11965
11995
  token,
11966
11996
  isBlock: false
@@ -12151,7 +12181,7 @@ function validateMarkup(passages, options) {
12151
12181
  continue;
12152
12182
  }
12153
12183
  try {
12154
- const tokens = tokenizeMarkup(passage.content, { hooks });
12184
+ const tokens = tokenizeMarkup$1(passage.content, { hooks });
12155
12185
  addWidgets(tokens);
12156
12186
  if (!reported) continue;
12157
12187
  parseMarkup(passage.content, { hooks });
@@ -12660,9 +12690,9 @@ function tokensOf(text, textMode, { tolerant, parametersOf } = {}) {
12660
12690
  text: textMode,
12661
12691
  hooks: parametersOf && { isRaw: (name) => isRawMacro(name, parametersOf.widget?.(name) !== void 0) }
12662
12692
  };
12663
- if (tolerant) return tokenizeMarkupTolerant(text, options).tokens;
12693
+ if (tolerant) return tokenizeMarkupTolerant$1(text, options).tokens;
12664
12694
  try {
12665
- return tokenizeMarkup(text, options);
12695
+ return tokenizeMarkup$1(text, options);
12666
12696
  } catch (err) {
12667
12697
  if (err instanceof MarkupError) return [];
12668
12698
  throw err;
@@ -24893,7 +24923,7 @@ function scanSpindleMarkup(text) {
24893
24923
  if (inAttribute) match.inAttribute = true;
24894
24924
  tokens.push(match);
24895
24925
  };
24896
- for (const t of tokenizeMarkupTolerant(text).tokens) if (t.type === "html") {
24926
+ for (const t of tokenizeMarkupTolerant$1(text).tokens) if (t.type === "html") {
24897
24927
  tags.push({
24898
24928
  start: t.start,
24899
24929
  end: t.end
@@ -24902,7 +24932,7 @@ function scanSpindleMarkup(text) {
24902
24932
  if (attribute.valueStart === void 0 || attribute.valueEnd === void 0) continue;
24903
24933
  const value = text.slice(attribute.valueStart, attribute.valueEnd);
24904
24934
  if (!value.includes("{")) continue;
24905
- for (const inner of tokenizeMarkupTolerant(value, { text: true }).tokens) if (inner.type === "macro" || inner.type === "variable" || inner.type === "expression") push({
24935
+ for (const inner of tokenizeMarkupTolerant$1(value, { text: true }).tokens) if (inner.type === "macro" || inner.type === "variable" || inner.type === "expression") push({
24906
24936
  start: attribute.valueStart + inner.start,
24907
24937
  end: attribute.valueStart + inner.end
24908
24938
  }, true);
@@ -25101,7 +25131,7 @@ var DEFAULT_DEDENTING = /* @__PURE__ */ new Set([
25101
25131
  /** The names of the macros the passage closes with `{/name}`: block macros it defines itself. */
25102
25132
  function closedMacroNames(text) {
25103
25133
  const found = /* @__PURE__ */ new Set();
25104
- for (const t of tokenizeMarkupTolerant(text).tokens) if (t.type === "macro" && t.isClose) found.add(t.name.toLowerCase());
25134
+ for (const t of tokenizeMarkupTolerant$1(text).tokens) if (t.type === "macro" && t.isClose) found.add(t.name.toLowerCase());
25105
25135
  return found;
25106
25136
  }
25107
25137
  /** The built-in block macros (and the block widgets the runtime has defined) plus those the passage closes. */
@@ -25424,7 +25454,7 @@ function macroTokensByLine(lines) {
25424
25454
  offset += line.length + 1;
25425
25455
  }
25426
25456
  let line = 0;
25427
- for (const token of tokenizeMarkupTolerant(lines.join("\n")).tokens) {
25457
+ for (const token of tokenizeMarkupTolerant$1(lines.join("\n")).tokens) {
25428
25458
  if (token.type !== "macro") continue;
25429
25459
  while (line + 1 < lines.length && starts[line + 1] <= token.start) line++;
25430
25460
  const lineStart = starts[line];
@@ -25517,7 +25547,7 @@ function wordWrap(text, maxLen, indent) {
25517
25547
  const lines = [];
25518
25548
  /** Whether the character is inside a token whose text must stay as written. */
25519
25549
  const inToken = new Array(text.length).fill(false);
25520
- for (const token of tokenizeMarkupTolerant(text).tokens) {
25550
+ for (const token of tokenizeMarkupTolerant$1(text).tokens) {
25521
25551
  if (token.type === "text") continue;
25522
25552
  inToken.fill(true, token.start, token.end);
25523
25553
  }
@@ -25644,6 +25674,28 @@ var transformCache = /* @__PURE__ */ new Map();
25644
25674
  * validator, with no Preact or DOM dependencies. pkg/tooling.js re-exports
25645
25675
  * them, binding the validator to its macro metadata.
25646
25676
  */
25677
+ function tokenizationOptions(options) {
25678
+ if (!options.macros) return options;
25679
+ const macros = [...options.macros];
25680
+ const parametersOf = parameterLookup(macros);
25681
+ return {
25682
+ ...options,
25683
+ hooks: {
25684
+ isRaw: (name) => isRawMacro(name, parametersOf.widget?.(name) !== void 0),
25685
+ isBlock: blockLookup(macros),
25686
+ branchParents: (name) => branchParents(name, macros),
25687
+ ...options.hooks
25688
+ }
25689
+ };
25690
+ }
25691
+ /** Flat tokens with optional effective macro/widget metadata. */
25692
+ function tokenizeMarkup(source, options = {}) {
25693
+ return tokenizeMarkup$1(source, tokenizationOptions(options));
25694
+ }
25695
+ /** Metadata-aware flat tokens and recovery errors, in original source offsets. */
25696
+ function tokenizeMarkupTolerant(source, options = {}) {
25697
+ return tokenizeMarkupTolerant$1(source, tokenizationOptions(options));
25698
+ }
25647
25699
  /**
25648
25700
  * Validate the markup of a story's passages as Spindle does when the story
25649
25701
  * starts, against the given macros (built-in and user-defined): malformed
@@ -25739,10 +25791,12 @@ var NOT_MARKUP_TAGS = ["script", "stylesheet"];
25739
25791
  */
25740
25792
  function referenceOptions(macros, all = false, passages = []) {
25741
25793
  const list = [...macros];
25794
+ const effective = new NameMap();
25795
+ for (const macro of list) effective.set(macro.name, macro);
25742
25796
  const widgets = effectiveWidgetDefinitions(widgetDefinitions$1(passages, parameterLookup(list)));
25743
25797
  return {
25744
25798
  all,
25745
- storeVarMacros: new Set(list.filter((m) => m.storeVar).map((m) => m.name.toLowerCase())),
25799
+ storeVarMacros: new Set([...effective.values()].filter((m) => m.storeVar).map((m) => m.name.toLowerCase())),
25746
25800
  parametersOf: parameterLookup([...list, ...definedWidgetMacros(widgets)]),
25747
25801
  tolerant: true
25748
25802
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rohal12/spindle",
3
- "version": "0.62.5",
3
+ "version": "0.62.6",
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
@@ -233,17 +233,25 @@ export interface ParametersOf {
233
233
  source?(name: string): 'builtin' | 'user' | undefined;
234
234
  }
235
235
 
236
- /** A raw body is executable only while the builtin implementation owns it. */
237
- export function executesRawBody(
236
+ /** Whether builtin-only interpretation still belongs to this implementation. */
237
+ export function usesBuiltinSemantics(
238
238
  name: string,
239
239
  parametersOf: ParametersOf,
240
240
  ): boolean {
241
241
  return (
242
- isRawMacro(name, parametersOf.widget?.(name) !== undefined) &&
242
+ parametersOf.widget?.(name) === undefined &&
243
243
  parametersOf.source?.(name) !== 'user'
244
244
  );
245
245
  }
246
246
 
247
+ /** A raw body is executable only while the builtin implementation owns it. */
248
+ export function executesRawBody(
249
+ name: string,
250
+ parametersOf: ParametersOf,
251
+ ): boolean {
252
+ return isRawMacro(name, false) && usesBuiltinSemantics(name, parametersOf);
253
+ }
254
+
247
255
  /** What the argument check needs to know of a macro (see MacroMetadata). */
248
256
  export interface MacroParameters {
249
257
  name: string;
@@ -298,13 +306,14 @@ export function parameterLookup(
298
306
  sources.set(name, source ?? 'user');
299
307
  if (params)
300
308
  parameters.set(name, withHolds(params, holdsByDefault(interpolate)));
309
+ else parameters.delete(name);
301
310
  if (widget)
302
311
  widgets.set(name, widgetParameters(widget.params, widget.holds) ?? []);
303
312
  }
304
313
  const lookup: ParametersOf = (name) => {
305
314
  if (widgets.has(name)) return undefined;
306
315
  let params = parameters.get(name);
307
- if (!params) {
316
+ if (!params && sources.get(name) !== 'user') {
308
317
  const sub = subMacroParameters(name);
309
318
  if (sub) parameters.set(name, (params = withHolds(sub, 'text')));
310
319
  }
@@ -434,7 +443,10 @@ export function* codeAndText(
434
443
  continue;
435
444
  }
436
445
  const args = token.rawArgs;
437
- if (PASSAGE_BODIES.has(name) && !widget) {
446
+ if (
447
+ PASSAGE_BODIES.has(name) &&
448
+ usesBuiltinSemantics(name, parametersOf)
449
+ ) {
438
450
  // The body names the passage when it is only text
439
451
  const close = closeOfTextBody(t, name);
440
452
  if (close > t + 1) {
@@ -453,7 +465,10 @@ export function* codeAndText(
453
465
  if (!args) continue;
454
466
  const label = `{${token.name} ${args}}`;
455
467
  const argsAt = token.argsStart;
456
- if (CONDITION_MACROS.has(name) && !widget) {
468
+ if (
469
+ CONDITION_MACROS.has(name) &&
470
+ usesBuiltinSemantics(name, parametersOf)
471
+ ) {
457
472
  yield {
458
473
  kind: 'code',
459
474
  code: args,