@rohal12/spindle 0.62.5 → 0.62.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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", {
@@ -11768,6 +11787,53 @@ function readBoundVariable(rawArgs) {
11768
11787
  const first = terms(src)[0];
11769
11788
  return first ? slice(src, first) : "";
11770
11789
  }
11790
+ /**
11791
+ * Read the first term as the runtime's story-variable binding. Quotes are
11792
+ * stripped, but this is an identifier path, not a JavaScript string literal:
11793
+ * only identifier escapes are decoded. Returns null for a missing binding;
11794
+ * throws for rejected namespaces, invalid escapes and reserved path names.
11795
+ * The full span excludes quotes and includes the sigil when written.
11796
+ */
11797
+ function parseBoundVariable(rawArgs) {
11798
+ const term = readBoundVariable(rawArgs);
11799
+ if (!term) return null;
11800
+ const lead = rawArgs.length - rawArgs.trimStart().length;
11801
+ const offsets = [];
11802
+ for (let i = 0; i < term.length; i++) if (term[i] !== "\"" && term[i] !== "'") offsets.push(lead + i);
11803
+ const firstToken = term.replace(/["']/g, "");
11804
+ const kind = {
11805
+ "%": "transient variables",
11806
+ _: "temporary variables",
11807
+ "@": "locals"
11808
+ }[firstToken.charAt(0)];
11809
+ if (kind) throw new Error(`${kind} (${firstToken}) cannot be bound to input macros`);
11810
+ const explicitSigil = firstToken.startsWith("$");
11811
+ if (!explicitSigil && firstToken.split(".")[0].includes("\\")) throw new Error("Escaped binding identifiers require a literal $ story-variable sigil");
11812
+ const segments = firstToken.replace(/^\$/, "").split(".").map((segment, index) => {
11813
+ if (!segment.includes("\\")) return segment;
11814
+ if (index > 0) {
11815
+ const name = parseMemberIdentifier(segment);
11816
+ if (name === null) throw new Error("Invalid bound identifier");
11817
+ return name;
11818
+ }
11819
+ const reference = parseReferencePath(`$${segment}`);
11820
+ if (!reference || reference.nested) throw new Error("Invalid bound identifier");
11821
+ return reference.name;
11822
+ });
11823
+ if (segments.includes("__proto__")) throw new Error(`"$${segments.join(".")}" cannot be bound: ${RESERVED_NAME} is reserved`);
11824
+ const rootStart = explicitSigil ? 1 : 0;
11825
+ const rootEnd = firstToken.indexOf(".", rootStart);
11826
+ const nameEnd = rootEnd < 0 ? firstToken.length : rootEnd;
11827
+ return {
11828
+ name: segments[0],
11829
+ path: segments.slice(1),
11830
+ explicitSigil,
11831
+ start: offsets[0] ?? lead,
11832
+ end: offsets.length ? offsets[offsets.length - 1] + 1 : lead,
11833
+ nameStart: offsets[rootStart] ?? lead + rootStart,
11834
+ nameEnd: offsets[nameEnd - 1] === void 0 ? lead : offsets[nameEnd - 1] + 1
11835
+ };
11836
+ }
11771
11837
  //#endregion
11772
11838
  //#region src/components/macros/option-utils.ts
11773
11839
  /** `$var "placeholder"`, the arguments of the input macros. */
@@ -11867,9 +11933,13 @@ function widgetParameters(params, holds = {}) {
11867
11933
  ...hasOwn(holds, name) ? { holds: holds[name] } : {}
11868
11934
  }));
11869
11935
  }
11936
+ /** Whether builtin-only interpretation still belongs to this implementation. */
11937
+ function usesBuiltinSemantics(name, parametersOf) {
11938
+ return parametersOf.widget?.(name) === void 0 && parametersOf.source?.(name) !== "user";
11939
+ }
11870
11940
  /** A raw body is executable only while the builtin implementation owns it. */
11871
11941
  function executesRawBody(name, parametersOf) {
11872
- return isRawMacro(name, parametersOf.widget?.(name) !== void 0) && parametersOf.source?.(name) !== "user";
11942
+ return isRawMacro(name, false) && usesBuiltinSemantics(name, parametersOf);
11873
11943
  }
11874
11944
  /**
11875
11945
  * `params` with each `string` and `text` parameter (options too) saying
@@ -11902,12 +11972,13 @@ function parameterLookup(macros) {
11902
11972
  for (const { name, parameters: params, interpolate, widget, source } of macros) {
11903
11973
  sources.set(name, source ?? "user");
11904
11974
  if (params) parameters.set(name, withHolds(params, holdsByDefault(interpolate)));
11975
+ else parameters.delete(name);
11905
11976
  if (widget) widgets.set(name, widgetParameters(widget.params, widget.holds) ?? []);
11906
11977
  }
11907
11978
  const lookup = (name) => {
11908
11979
  if (widgets.has(name)) return void 0;
11909
11980
  let params = parameters.get(name);
11910
- if (!params) {
11981
+ if (!params && sources.get(name) !== "user") {
11911
11982
  const sub = subMacroParameters(name);
11912
11983
  if (sub) parameters.set(name, params = withHolds(sub, "text"));
11913
11984
  }
@@ -11994,7 +12065,7 @@ function* codeAndText(src, tokens, parametersOf) {
11994
12065
  continue;
11995
12066
  }
11996
12067
  const args = token.rawArgs;
11997
- if (PASSAGE_BODIES.has(name) && !widget) {
12068
+ if (PASSAGE_BODIES.has(name) && usesBuiltinSemantics(name, parametersOf)) {
11998
12069
  const close = closeOfTextBody(t, name);
11999
12070
  if (close > t + 1) {
12000
12071
  const text = src.slice(token.end, tokens[close].start);
@@ -12012,7 +12083,7 @@ function* codeAndText(src, tokens, parametersOf) {
12012
12083
  if (!args) continue;
12013
12084
  const label = `{${token.name} ${args}}`;
12014
12085
  const argsAt = token.argsStart;
12015
- if (CONDITION_MACROS.has(name) && !widget) {
12086
+ if (CONDITION_MACROS.has(name) && usesBuiltinSemantics(name, parametersOf)) {
12016
12087
  yield {
12017
12088
  kind: "code",
12018
12089
  code: args,
@@ -14490,7 +14561,7 @@ function renderInlineNodes(nodes) {
14490
14561
  * line ending (`\r\n` or `\r`) also takes the full pipeline, which reads
14491
14562
  * it as micromark does.
14492
14563
  */
14493
- var MARKDOWN_SYNTAX_RE = /[*_`#|~\[>\\\-+=\r]|!\[|\d+[.)]|&#?[a-zA-Z0-9]+;| {2}\n|<[!?]/;
14564
+ var MARKDOWN_SYNTAX_RE = /[*_`#|~\[>\\\-+=\r]|!\[|\d{1,9}[.)]|&#?[a-zA-Z0-9]+;| {2}\n|<[!?]/;
14494
14565
  /** Two line endings (LF, CRLF or CR) with only whitespace between them. */
14495
14566
  var BLANK_LINE_RE = /(?:\r\n|\r(?!\n)|\n)\s*[\r\n]/;
14496
14567
  var PLACEHOLDER_STRIP_RE = /<span data-tw=[0-9a-z]+:\d+><\/span>/g;
@@ -20298,10 +20369,6 @@ var FOCUSABLE = [
20298
20369
  "[contenteditable]",
20299
20370
  "[tabindex]"
20300
20371
  ].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
20372
  function composedChildren(root) {
20306
20373
  if (root.localName === "slot") return root.assignedNodes({ flatten: true }).filter((node) => node.nodeType === 1);
20307
20374
  return Array.from(root.children);
@@ -21332,39 +21399,17 @@ function defineMacro(config, source = "builtin") {
21332
21399
  ctx.evaluate = (expr) => evaluateDisplay(expr, [...merged], displayOnly);
21333
21400
  }
21334
21401
  if (config.storeVar) {
21335
- const firstToken = readBoundVariable(props.rawArgs).replace(/["']/g, "");
21336
- const kind = {
21337
- "%": "transient variables",
21338
- _: "temporary variables",
21339
- "@": "locals"
21340
- }[firstToken.charAt(0)];
21341
- if (kind) return h("span", { class: "error" }, `{${config.name}}: ${kind} (${firstToken}) cannot be bound to input macros`);
21342
- if (!firstToken.startsWith("$") && firstToken.split(".")[0].includes("\\")) return h(MacroError, {
21343
- macro: config.name,
21344
- error: /* @__PURE__ */ new Error("Escaped binding identifiers require a literal $ story-variable sigil")
21345
- });
21346
21402
  let segments;
21347
21403
  try {
21348
- segments = firstToken.replace(/^\$/, "").split(".").map((segment, index) => {
21349
- if (!segment.includes("\\")) return segment;
21350
- if (index > 0) {
21351
- const name = parseMemberIdentifier(segment);
21352
- if (name === null) throw new Error("Invalid bound identifier");
21353
- return name;
21354
- }
21355
- const reference = parseReferencePath(`$${segment}`);
21356
- if (!reference || reference.nested) throw new Error("Invalid bound identifier");
21357
- return reference.name;
21358
- });
21404
+ const binding = parseBoundVariable(props.rawArgs);
21405
+ segments = binding ? [binding.name, ...binding.path] : [""];
21359
21406
  } catch (error) {
21360
21407
  return h(MacroError, {
21361
21408
  macro: config.name,
21362
21409
  error
21363
21410
  });
21364
21411
  }
21365
- const varExpr = segments.join(".");
21366
- if (segments.includes("__proto__")) return h("span", { class: "error" }, `{${config.name}}: "$${varExpr}" cannot be bound: ${RESERVED_NAME} is reserved`);
21367
- ctx.varName = varExpr;
21412
+ ctx.varName = segments.join(".");
21368
21413
  ctx.value = getByPath(useStoryFields("variables").variables, segments);
21369
21414
  ctx.getValue = () => getByPath(readState().variables, segments);
21370
21415
  ctx.setValue = displayOnly ? refuse : (value) => {
@@ -22774,8 +22819,6 @@ function declarationRegex(sigil) {
22774
22819
  }
22775
22820
  /** A `$name` or `%name` reference with its dot path, at a sigil the lexer found. */
22776
22821
  var VAR_PATH_RE = /([$%])(\w+(?:\.\w+)*)/y;
22777
- /** Quoted first argument of an input macro naming a story variable. */
22778
- var QUOTED_VAR_ARG_RE = /^["']\$(\w+(?:\.\w+)*)["']?$/;
22779
22822
  var VALID_VAR_TYPES = /* @__PURE__ */ new Set([
22780
22823
  "number",
22781
22824
  "string",
@@ -23155,6 +23198,30 @@ function scanSelectors(content, token, at, emit, options) {
23155
23198
  storeVarMacros: NO_STORE_VAR_MACROS
23156
23199
  });
23157
23200
  }
23201
+ /** Read a runtime binding and map its editable spans into the passage. */
23202
+ function boundReference(token, at, all) {
23203
+ try {
23204
+ const binding = parseBoundVariable(token.rawArgs);
23205
+ if (!binding || !all && !binding.explicitSigil) return void 0;
23206
+ const mapped = {
23207
+ ...binding,
23208
+ start: at(token.argsStart + binding.start),
23209
+ end: at(token.argsStart + binding.end),
23210
+ nameStart: at(token.argsStart + binding.nameStart),
23211
+ nameEnd: at(token.argsStart + binding.nameEnd)
23212
+ };
23213
+ return {
23214
+ sigil: "$",
23215
+ name: binding.name,
23216
+ path: binding.path,
23217
+ start: mapped.start,
23218
+ end: mapped.end,
23219
+ binding: mapped
23220
+ };
23221
+ } catch {
23222
+ return;
23223
+ }
23224
+ }
23158
23225
  /** Report the `$var` references in the tokens of `content`. */
23159
23226
  function collectTokenRefs(content, tokens, at, emit, options) {
23160
23227
  const { storeVarMacros, parametersOf } = options;
@@ -23183,24 +23250,12 @@ function collectTokenRefs(content, tokens, at, emit, options) {
23183
23250
  else scanInterpolations(value, valueIn, emit, options);
23184
23251
  }
23185
23252
  else if (token.type === "macro" && !token.isClose) {
23186
- scanArgs(token, parametersOf, at, emit, options);
23253
+ const binding = storeVarMacros.has(token.name.toLowerCase()) && !parametersOf.widget?.(token.name) ? boundReference(token, at, options.all) : void 0;
23254
+ scanArgs(token, parametersOf, at, (ref) => {
23255
+ if (!binding || ref.start < binding.start || ref.start >= binding.end) emit(ref);
23256
+ }, options);
23187
23257
  scanSelectors(content, token, at, emit, options);
23188
- if (storeVarMacros.has(token.name.toLowerCase()) && !parametersOf.widget?.(token.name)) {
23189
- const lead = token.rawArgs.length - token.rawArgs.trimStart().length;
23190
- const first = token.rawArgs.trim().split(/\s+/)[0] ?? "";
23191
- const quoted = QUOTED_VAR_ARG_RE.exec(first);
23192
- if (quoted) {
23193
- const [name, ...path] = quoted[1].split(".");
23194
- const start = token.argsStart + lead + 1;
23195
- emit({
23196
- sigil: "$",
23197
- name,
23198
- path,
23199
- start: at(start),
23200
- end: at(start + 1 + quoted[1].length)
23201
- });
23202
- } else if (!/^["'`]/.test(first)) scanCode(first, (i) => at(token.argsStart + lead + i), emit);
23203
- }
23258
+ if (binding) emit(binding);
23204
23259
  if (executesRawBody(token.name, parametersOf)) {
23205
23260
  let close = t + 1;
23206
23261
  while (close < tokens.length) {
@@ -24555,8 +24610,9 @@ var SETTLING_MACROS = new NameSet([
24555
24610
  function settlesAfterRender(nodes) {
24556
24611
  return nodes.every((n) => {
24557
24612
  switch (n.type) {
24558
- case "macro": return SETTLING_MACROS.has(n.name) && settlesAfterRender(n.children) && (n.branches ?? []).every((b) => settlesAfterRender(b.children));
24613
+ 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
24614
  case "html": return settlesAfterRender(n.children);
24615
+ case "variable": return n.scope !== "local" || n.name !== "children";
24560
24616
  default: return true;
24561
24617
  }
24562
24618
  });