@rohal12/spindle 0.62.1 → 0.62.2

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.
@@ -4045,6 +4045,8 @@ var BLOCK_MACROS = new NameSet([
4045
4045
  * definition is, whereas the macros in BLOCK_MACROS stay blocks.
4046
4046
  */
4047
4047
  var BLOCK_WIDGETS = new NameSet();
4048
+ /** All widgets, including inline ones, override built-in parsing semantics. */
4049
+ var WIDGETS = new NameSet();
4048
4050
  /** Classification owned by defineMacro, independent of explicit registrations. */
4049
4051
  var DEFINED_BLOCK_MACROS = new NameSet();
4050
4052
  function setDefinedBlockMacro(name, isBlock) {
@@ -4058,8 +4060,8 @@ function setClassification(names, name, block) {
4058
4060
  /** Macros whose body is JavaScript source, kept verbatim. */
4059
4061
  var RAW_BODY_MACROS = /* @__PURE__ */ new Set(["do"]);
4060
4062
  /** Whether a macro's body is JavaScript, kept verbatim (`{do}`). */
4061
- function isRawMacro(name) {
4062
- return RAW_BODY_MACROS.has(name);
4063
+ function isRawMacro(name, widget = WIDGETS.has(name)) {
4064
+ return RAW_BODY_MACROS.has(name.toLowerCase()) && !widget;
4063
4065
  }
4064
4066
  /** Whether a macro takes a body closed by `{/name}`. */
4065
4067
  function isBlockMacro(name) {
@@ -4070,6 +4072,7 @@ function isBlockMacro(name) {
4070
4072
  * does. A block macro registered under the same name stays a block.
4071
4073
  */
4072
4074
  function setBlockWidget(name, isBlock) {
4075
+ WIDGETS.add(name);
4073
4076
  setClassification(BLOCK_WIDGETS, name, isBlock);
4074
4077
  }
4075
4078
  /** Macros whose branches render as one: the node holds one per branch. */
@@ -4125,7 +4128,8 @@ function astNode({ token, body }) {
4125
4128
  };
4126
4129
  if (!body) return withSelectors$1(node, token);
4127
4130
  const children = buildAst(body.children);
4128
- if (!BRANCHING.has(name) && body.branches.length === 0) {
4131
+ const branching = BRANCHING.has(name) && !WIDGETS.has(name);
4132
+ if (!branching && body.branches.length === 0) {
4129
4133
  node.children = children;
4130
4134
  return withSelectors$1(node, token);
4131
4135
  }
@@ -4133,9 +4137,9 @@ function astNode({ token, body }) {
4133
4137
  rawArgs: tag.rawArgs,
4134
4138
  children: branchChildren
4135
4139
  }, tag);
4136
- if (!BRANCHING.has(name)) node.children = children;
4137
- node.branches = [...BRANCHING.has(name) ? [branch(token, children)] : [], ...body.branches.map(({ tag, children: c }) => branch(tag, buildAst(c)))];
4138
- return BRANCHING.has(name) ? node : withSelectors$1(node, token);
4140
+ if (!branching) node.children = children;
4141
+ node.branches = [...branching ? [branch(token, children)] : [], ...body.branches.map(({ tag, children: c }) => branch(tag, buildAst(c)))];
4142
+ return branching ? node : withSelectors$1(node, token);
4139
4143
  }
4140
4144
  }
4141
4145
  }
@@ -4279,6 +4283,25 @@ function lineColumn(text, offset) {
4279
4283
  column: offset - lineStart + 1
4280
4284
  };
4281
4285
  }
4286
+ /** Index one source once; offsets and columns count UTF-16 code units. */
4287
+ function sourcePositions(text) {
4288
+ const starts = [0];
4289
+ for (let i = 0; i < text.length; i++) if (text[i] === "\n") starts.push(i + 1);
4290
+ return (offset) => {
4291
+ let low = 0;
4292
+ let high = starts.length;
4293
+ while (low < high) {
4294
+ const mid = low + high >>> 1;
4295
+ if (starts[mid] <= offset) low = mid + 1;
4296
+ else high = mid;
4297
+ }
4298
+ const line = Math.max(1, low);
4299
+ return {
4300
+ line,
4301
+ column: offset - starts[line - 1] + 1
4302
+ };
4303
+ };
4304
+ }
4282
4305
  //#endregion
4283
4306
  //#region src/markup/pair.ts
4284
4307
  /**
@@ -4330,9 +4353,11 @@ function pairMarkup(tokens, options = {}) {
4330
4353
  };
4331
4354
  const errors = [];
4332
4355
  const noticedAtEnd = (tokens[tokens.length - 1]?.end ?? 0) + 1;
4356
+ let position;
4333
4357
  const where = (offset) => {
4334
4358
  if (options.source === void 0) return `offset ${offset}`;
4335
- const { line, column } = lineColumn(options.source, offset);
4359
+ position ??= sourcePositions(options.source);
4360
+ const { line, column } = position(offset);
4336
4361
  return `line ${line}, column ${column}`;
4337
4362
  };
4338
4363
  const report = (code, token, message, noticedAt, data) => {
@@ -10778,7 +10803,8 @@ function analyzeMarkup(source, options = {}) {
10778
10803
  };
10779
10804
  const inDamage = (start) => errors.some((e, i) => start >= damage[i] && start <= e.offset);
10780
10805
  const noticed = errors.map((e, i) => [damage[i] + .5, e]);
10781
- for (const error of paired.errors) if (!inDamage(error.start)) noticed.push([error.noticedAt, pairingError(source, error)]);
10806
+ const position = paired.errors.length ? sourcePositions(source) : void 0;
10807
+ for (const error of paired.errors) if (!inDamage(error.start)) noticed.push([error.noticedAt, pairingError(position, error)]);
10782
10808
  noticed.sort((a, b) => a[0] - b[0]);
10783
10809
  return {
10784
10810
  tokens,
@@ -10797,8 +10823,8 @@ function parseMarkup(source, options = {}) {
10797
10823
  return buildAst(nodes);
10798
10824
  }
10799
10825
  /** A pairing problem as the MarkupError it is for `source`. */
10800
- function pairingError(source, error) {
10801
- const { line, column } = lineColumn(source, error.start);
10826
+ function pairingError(position, error) {
10827
+ const { line, column } = position(error.start);
10802
10828
  return new MarkupError(error.message, error.start, line, column, error.code, error.end, error.data);
10803
10829
  }
10804
10830
  /**
@@ -10833,6 +10859,7 @@ function scanMarkupTolerant(source, options = {}) {
10833
10859
  const tokens = [];
10834
10860
  const errors = [];
10835
10861
  const damage = [];
10862
+ let position;
10836
10863
  const lastBrace = source.lastIndexOf("}");
10837
10864
  const { closeBrace } = {
10838
10865
  ...defaultHooks,
@@ -10863,7 +10890,8 @@ function scanMarkupTolerant(source, options = {}) {
10863
10890
  break;
10864
10891
  }
10865
10892
  const at = base + result.offset;
10866
- const { line, column } = lineColumn(source, at);
10893
+ position ??= sourcePositions(source);
10894
+ const { line, column } = position(at);
10867
10895
  errors.push(new MarkupError(result.reason, at, line, column, result.code, base + result.end));
10868
10896
  let cut = result.offset;
10869
10897
  let prefix = attempt(rest.slice(0, cut), base);
@@ -11889,14 +11917,16 @@ function* codeAndText(src, tokens, parametersOf) {
11889
11917
  * `src`. In source order, a nested piece after the text it is in.
11890
11918
  */
11891
11919
  function* passagePiecesOf(src, tokens, parametersOf, options = {}) {
11892
- yield* expand(src, src, tokens, parametersOf, options, (i) => i, void 0);
11920
+ let position;
11921
+ const locate = (offset) => (position ??= sourcePositions(src))(offset);
11922
+ yield* expand(src, src, tokens, parametersOf, options, (i) => i, void 0, locate);
11893
11923
  }
11894
11924
  /**
11895
11925
  * The pieces of `tokens`, the tokens of `src`, which is the text described by
11896
11926
  * `where` (none for `root`, the markup of the whole passage), and whose
11897
11927
  * offsets `toRoot` turns into offsets in `root`.
11898
11928
  */
11899
- function* expand(root, src, tokens, parametersOf, options, toRoot, where) {
11929
+ function* expand(root, src, tokens, parametersOf, options, toRoot, where, locate) {
11900
11930
  for (const flat of codeAndText(src, tokens, parametersOf)) {
11901
11931
  const piece = where ? nestedPiece(flat, toRoot, where) : flat;
11902
11932
  if (flat.kind !== "text" || !flat.text.includes("{")) {
@@ -11906,17 +11936,20 @@ function* expand(root, src, tokens, parametersOf, options, toRoot, where) {
11906
11936
  const toText = (i) => toRoot(pieceOffset(flat, i));
11907
11937
  const inner = analyzeMarkup(flat.text, {
11908
11938
  text: true,
11909
- hooks: options.isBlock && { isBlock: options.isBlock }
11939
+ hooks: {
11940
+ ...options.isBlock && { isBlock: options.isBlock },
11941
+ isRaw: (name) => isRawMacro(name, parametersOf.widget?.(name) !== void 0)
11942
+ }
11910
11943
  });
11911
11944
  yield {
11912
11945
  ...piece,
11913
11946
  tokens: inner.tokens.map((t) => mapOffsets(t, toText)),
11914
11947
  errors: inner.errors.map((e) => {
11915
- const { line, column } = lineColumn(root, toText(e.offset));
11948
+ const { line, column } = locate(toText(e.offset));
11916
11949
  return new MarkupError(e.reason, toText(e.offset), line, column, e.code, toText(e.end), e.data);
11917
11950
  })
11918
11951
  };
11919
- yield* expand(root, flat.text, inner.tokens, parametersOf, options, toText, flat.where);
11952
+ yield* expand(root, flat.text, inner.tokens, parametersOf, options, toText, flat.where, locate);
11920
11953
  }
11921
11954
  }
11922
11955
  /** `piece`, of a text in another, with its offsets turned by `toRoot`. */
@@ -20015,12 +20048,34 @@ var FOCUSABLE = [
20015
20048
  "[contenteditable]",
20016
20049
  "[tabindex]"
20017
20050
  ].join(",");
20051
+ /** The parent in the rendered tree, including slots and shadow hosts. */
20052
+ function composedParent(el) {
20053
+ return el.assignedSlot ?? el.parentElement ?? el.getRootNode().host ?? null;
20054
+ }
20055
+ function composedChildren(root) {
20056
+ if (root.localName === "slot") return root.assignedNodes({ flatten: true }).filter((node) => node.nodeType === 1);
20057
+ return Array.from(root.children);
20058
+ }
20059
+ /** Authored elements in rendered order; unassigned light DOM is not rendered. */
20060
+ function* composedElements(root) {
20061
+ for (const el of composedChildren(root)) {
20062
+ yield el;
20063
+ yield* composedElements(el.shadowRoot ?? el);
20064
+ }
20065
+ }
20018
20066
  /**
20019
20067
  * Whether `el` can be focused at all: shown, rendered, enabled (also by its
20020
20068
  * fieldset) and not inert.
20021
20069
  */
20022
20070
  function isAvailable(el) {
20023
- return !el.hidden && !el.closest("[hidden], [inert]") && !el.matches(":disabled") && isRendered(el);
20071
+ if (!isRendered(el)) return false;
20072
+ for (let at = el; at; at = composedParent(at)) {
20073
+ if (at.hasAttribute("hidden") || at.hasAttribute("inert")) return false;
20074
+ if (at.ownerDocument.defaultView?.getComputedStyle(at).display === "none") return false;
20075
+ const parent = composedParent(at);
20076
+ if (parent?.localName === "details" && !parent.hasAttribute("open") && at !== Array.from(parent.children).find((child) => child.localName === "summary")) return false;
20077
+ }
20078
+ return !el.matches(":disabled");
20024
20079
  }
20025
20080
  /** The `tabindex` of `el` as an integer, or null when absent or invalid. */
20026
20081
  function tabIndexAttr(el) {
@@ -20055,9 +20110,30 @@ function isTabStop(el) {
20055
20110
  * others in document order.
20056
20111
  */
20057
20112
  function focusables(root) {
20058
- const stops = Array.from(root.querySelectorAll(FOCUSABLE)).filter((el) => isAvailable(el) && isTabStop(el));
20113
+ const scopes = [];
20114
+ const collect = (parent) => {
20115
+ for (const element of composedChildren(parent)) {
20116
+ const el = element;
20117
+ const nested = el.shadowRoot ?? (el.localName === "slot" ? el : null);
20118
+ const stop = el.matches(FOCUSABLE) && isAvailable(el) && isTabStop(el);
20119
+ if (nested) {
20120
+ if ((tabIndexAttr(el) ?? 0) < 0) continue;
20121
+ scopes.push({
20122
+ el,
20123
+ stops: [...stop && !el.shadowRoot?.delegatesFocus ? [el] : [], ...focusables(nested)]
20124
+ });
20125
+ } else {
20126
+ if (stop) scopes.push({
20127
+ el,
20128
+ stops: [el]
20129
+ });
20130
+ collect(el);
20131
+ }
20132
+ }
20133
+ };
20134
+ collect(root);
20059
20135
  const positive = (el) => Math.max(tabIndexAttr(el) ?? 0, 0);
20060
- return [...stops.filter((el) => positive(el) > 0).sort((a, b) => positive(a) - positive(b)), ...stops.filter((el) => positive(el) === 0)];
20136
+ return [...scopes.filter(({ el }) => positive(el) > 0).sort((a, b) => positive(a.el) - positive(b.el)), ...scopes.filter(({ el }) => positive(el) === 0)].flatMap(({ stops }) => stops);
20061
20137
  }
20062
20138
  /** Open modal dialogs, innermost last. Only the topmost handles keys. */
20063
20139
  var openModals = [];
@@ -20096,6 +20172,21 @@ function frameDocument(frame) {
20096
20172
  return null;
20097
20173
  }
20098
20174
  }
20175
+ /** Focus an element, entering accessible frames at the appropriate edge. */
20176
+ function enter(el, backwards) {
20177
+ if (isFrame(el)) {
20178
+ const doc = frameDocument(el);
20179
+ const inner = doc ? focusables(doc) : [];
20180
+ const edge = backwards ? inner[inner.length - 1] : inner[0];
20181
+ if (edge) return enter(edge, backwards);
20182
+ }
20183
+ el.focus();
20184
+ }
20185
+ /** document.activeElement exposes a shadow host; follow it to the control. */
20186
+ function activeElement(root) {
20187
+ const el = root.activeElement;
20188
+ return el?.shadowRoot?.activeElement ? activeElement(el.shadowRoot) : el;
20189
+ }
20099
20190
  /** The iframe element that holds `doc`, null for the top document. */
20100
20191
  function frameOf(doc) {
20101
20192
  try {
@@ -20112,8 +20203,10 @@ function frameOf(doc) {
20112
20203
  function nextStop(items, root, from, backwards) {
20113
20204
  const at = from ? items.indexOf(from) : -1;
20114
20205
  if (at !== -1) return items[at + (backwards ? -1 : 1)];
20115
- if (from && from !== root && root.contains(from)) {
20116
- const after = (el) => !!(from.compareDocumentPosition(el) & Node.DOCUMENT_POSITION_FOLLOWING);
20206
+ const order = from ? [...composedElements(root)] : [];
20207
+ const position = order.indexOf(from);
20208
+ if (position !== -1) {
20209
+ const after = (el) => order.indexOf(el) > position;
20117
20210
  return backwards ? items.filter((el) => !after(el)).pop() : items.find(after);
20118
20211
  }
20119
20212
  return backwards ? items[items.length - 1] : items[0];
@@ -20128,13 +20221,13 @@ function nextStop(items, root, from, backwards) {
20128
20221
  */
20129
20222
  function activeChain() {
20130
20223
  const chain = [];
20131
- let doc = document;
20132
- while (doc) {
20133
- const el = doc.activeElement;
20134
- if (!el || chain.length > 0 && el === doc.body) break;
20224
+ let root = document;
20225
+ while (root) {
20226
+ const el = root.activeElement;
20227
+ if (!el || chain.length > 0 && el === root.body) break;
20135
20228
  if (typeof el.focus !== "function") break;
20136
20229
  chain.push(el);
20137
- doc = isFrame(el) ? frameDocument(el) : null;
20230
+ root = el.shadowRoot?.activeElement ? el.shadowRoot : isFrame(el) ? frameDocument(el) : null;
20138
20231
  }
20139
20232
  return chain;
20140
20233
  }
@@ -20162,7 +20255,7 @@ function useModalFocus(panelRef, bodySelector, dismissible, onClose) {
20162
20255
  };
20163
20256
  openModals.push(entry);
20164
20257
  const body = panel.querySelector(bodySelector) ?? panel;
20165
- (Array.from(panel.querySelectorAll("[autofocus]")).find(isAvailable) ?? focusables(body)[0] ?? panel).focus();
20258
+ enter([...composedElements(panel)].find((el) => el.hasAttribute("autofocus") && isAvailable(el)) ?? focusables(body)[0] ?? panel, false);
20166
20259
  return () => {
20167
20260
  const i = openModals.indexOf(entry);
20168
20261
  if (i !== -1) openModals.splice(i, 1);
@@ -20178,16 +20271,6 @@ function useModalFocus(panelRef, bodySelector, dismissible, onClose) {
20178
20271
  const panel = panelRef.current;
20179
20272
  if (!panel) return;
20180
20273
  const top = () => openModals[openModals.length - 1]?.panel === panel;
20181
- /** Focus `el`; a frame is entered at its first (or last) control (#460). */
20182
- const enter = (el, backwards) => {
20183
- if (isFrame(el)) {
20184
- const doc = frameDocument(el);
20185
- const inner = doc ? focusables(doc) : [];
20186
- const edge = backwards ? inner[inner.length - 1] : inner[0];
20187
- if (edge) return enter(edge, backwards);
20188
- }
20189
- el.focus();
20190
- };
20191
20274
  /**
20192
20275
  * Move focus to the next (or previous) modal control from `from`. The
20193
20276
  * order is computed here, never left to the browser: its positive-tabindex
@@ -20212,8 +20295,7 @@ function useModalFocus(panelRef, bodySelector, dismissible, onClose) {
20212
20295
  panel.focus();
20213
20296
  return;
20214
20297
  }
20215
- const next = nextStop(items, panel, from, backwards);
20216
- enter(next ?? (backwards ? last : first), backwards);
20298
+ enter(nextStop(items, panel, from, backwards) ?? (backwards ? last : first), backwards);
20217
20299
  };
20218
20300
  let steppingNative = null;
20219
20301
  const keyHandler = (doc, frame) => {
@@ -20227,7 +20309,7 @@ function useModalFocus(panelRef, bodySelector, dismissible, onClose) {
20227
20309
  return;
20228
20310
  }
20229
20311
  if (e.key !== "Tab") return;
20230
- const active = doc.activeElement;
20312
+ const active = activeElement(doc);
20231
20313
  if (frame) {
20232
20314
  steppingNative = null;
20233
20315
  const inner = focusables(doc);
@@ -20250,20 +20332,20 @@ function useModalFocus(panelRef, bodySelector, dismissible, onClose) {
20250
20332
  steppingNative = null;
20251
20333
  };
20252
20334
  const onFocusIn = (e) => {
20253
- const target = e.target;
20335
+ const target = e.composedPath()[0] ?? e.target;
20254
20336
  if (steppingNative && target !== steppingNative.el && top()) {
20255
20337
  const { from, backwards } = steppingNative;
20256
20338
  steppingNative = null;
20257
20339
  moveFocus(from, backwards);
20258
20340
  return;
20259
20341
  }
20260
- if (!top() || !(target instanceof Node) || panel.contains(target)) return;
20342
+ if (!top() || !(target instanceof Node) || target instanceof Element && [...composedElements(panel)].includes(target) || panel.contains(target)) return;
20261
20343
  if (target === document.body || target === document.documentElement) return;
20262
20344
  moveFocus(null, false);
20263
20345
  };
20264
20346
  const onFocusOut = (e) => {
20265
20347
  const pending = steppingNative;
20266
- if (!pending || e.target !== pending.el || e.relatedTarget || !top()) return;
20348
+ if (!pending || (e.composedPath()[0] ?? e.target) !== pending.el || e.relatedTarget || !top()) return;
20267
20349
  steppingNative = null;
20268
20350
  moveFocus(pending.from, pending.backwards);
20269
20351
  };
@@ -20279,15 +20361,25 @@ function useModalFocus(panelRef, bodySelector, dismissible, onClose) {
20279
20361
  };
20280
20362
  const frameDocs = /* @__PURE__ */ new Map();
20281
20363
  const observers = [];
20364
+ const watched = /* @__PURE__ */ new WeakSet();
20282
20365
  const attachFrame = (frame) => {
20283
20366
  const doc = frameDocument(frame);
20284
20367
  if (!doc || frameDocs.get(frame) === doc) return;
20285
20368
  frameDocs.set(frame, doc);
20286
20369
  listen(doc, frame);
20287
20370
  watchFrames(doc);
20371
+ const chain = activeChain();
20372
+ if (top() && chain[chain.length - 1] === frame) enter(frame, false);
20288
20373
  };
20289
20374
  const watchFrames = (root) => {
20290
- const attachAll = () => root.querySelectorAll("iframe").forEach(attachFrame);
20375
+ if (watched.has(root)) return;
20376
+ watched.add(root);
20377
+ const attachAll = () => {
20378
+ for (const el of composedElements(root)) {
20379
+ if (el.shadowRoot) watchFrames(el.shadowRoot);
20380
+ if (isFrame(el)) attachFrame(el);
20381
+ }
20382
+ };
20291
20383
  root.addEventListener("load", (e) => {
20292
20384
  if (e.target && isFrame(e.target)) attachFrame(e.target);
20293
20385
  }, {
@@ -21824,14 +21916,23 @@ function distance(a, b) {
21824
21916
  }
21825
21917
  /** The known name closest to `name`, if one is close. */
21826
21918
  function closestName(name, names) {
21919
+ if (name.length > 256) return void 0;
21827
21920
  const lower = name.toLowerCase();
21828
21921
  let best;
21829
21922
  let bestDistance = Math.max(2, Math.floor(lower.length / 3)) + 1;
21923
+ let budget = 1e5;
21924
+ let candidates = 1e3;
21830
21925
  for (const candidate of names) {
21926
+ if (--candidates < 0) break;
21927
+ if (candidate.length > 256) continue;
21928
+ if (Math.abs(candidate.length - lower.length) >= bestDistance) continue;
21929
+ budget -= lower.length * candidate.length;
21930
+ if (budget < 0) break;
21831
21931
  const d = distance(lower, candidate.toLowerCase());
21832
21932
  if (d < bestDistance) {
21833
21933
  best = candidate;
21834
21934
  bestDistance = d;
21935
+ if (d === 0) break;
21835
21936
  }
21836
21937
  }
21837
21938
  return best;
@@ -21856,12 +21957,19 @@ function widgetName(rawArgs) {
21856
21957
  */
21857
21958
  function validateMarkup(passages, options) {
21858
21959
  const diagnostics = [];
21960
+ const positions = /* @__PURE__ */ new Map();
21859
21961
  const hooks = {
21860
21962
  ...options.isBlockMacro && { isBlock: options.isBlockMacro },
21861
- ...options.branchParents && { branchParents: options.branchParents }
21963
+ ...options.branchParents && { branchParents: options.branchParents },
21964
+ isRaw: (name) => isRawMacro(name, options.parametersOf ? options.parametersOf.widget?.(name) !== void 0 : void 0)
21862
21965
  };
21863
21966
  const report = (passage, start, end, code, message, data) => {
21864
- const { line, column } = lineColumn(passage.content, start);
21967
+ let position = positions.get(passage);
21968
+ if (!position) {
21969
+ position = sourcePositions(passage.content);
21970
+ positions.set(passage, position);
21971
+ }
21972
+ const { line, column } = position(start);
21865
21973
  const d = {
21866
21974
  passage: passage.name,
21867
21975
  line,
@@ -21907,7 +22015,7 @@ function validateMarkup(passages, options) {
21907
22015
  continue;
21908
22016
  }
21909
22017
  try {
21910
- const tokens = tokenizeMarkup(passage.content);
22018
+ const tokens = tokenizeMarkup(passage.content, { hooks });
21911
22019
  addWidgets(tokens);
21912
22020
  if (!reported) continue;
21913
22021
  parseMarkup(passage.content, { hooks });
@@ -22406,10 +22514,14 @@ var Reader = class {
22406
22514
  * The tokens of markup, or none if it is malformed (validateMarkup reports
22407
22515
  * that, with its position); in tolerant mode, those of the well-formed parts.
22408
22516
  */
22409
- function tokensOf(text, textMode, { tolerant } = {}) {
22410
- if (tolerant) return tokenizeMarkupTolerant(text, { text: textMode }).tokens;
22517
+ function tokensOf(text, textMode, { tolerant, parametersOf } = {}) {
22518
+ const options = {
22519
+ text: textMode,
22520
+ hooks: parametersOf && { isRaw: (name) => isRawMacro(name, parametersOf.widget?.(name) !== void 0) }
22521
+ };
22522
+ if (tolerant) return tokenizeMarkupTolerant(text, options).tokens;
22411
22523
  try {
22412
- return tokenizeMarkup(text, { text: textMode });
22524
+ return tokenizeMarkup(text, options);
22413
22525
  } catch (err) {
22414
22526
  if (err instanceof MarkupError) return [];
22415
22527
  throw err;
@@ -25684,8 +25796,15 @@ function orStop(parse, where = "") {
25684
25796
  * the declared parameters of macros and what the parameters of widgets hold.
25685
25797
  * (The markup and the variable references are read by the same.)
25686
25798
  */
25687
- function knownParameters() {
25688
- return parameterLookup([...getMacroRegistry(), ...widgetMacros()]);
25799
+ function knownParameters(declared = []) {
25800
+ return parameterLookup([
25801
+ ...getMacroRegistry(),
25802
+ ...declared.map((widget) => ({
25803
+ name: widget.name,
25804
+ widget
25805
+ })),
25806
+ ...widgetMacros()
25807
+ ]);
25689
25808
  }
25690
25809
  /** The names of the registered macros that bind a variable (the inputs). */
25691
25810
  function storeVarMacroNames() {
@@ -25696,19 +25815,19 @@ function storeVarMacroNames() {
25696
25815
  * the input macros that bind a variable (registered by author JS or
25697
25816
  * StoryInit) and the roles of their parameters decide what is a reference.
25698
25817
  */
25699
- function variableErrors(storyData, schema, only = () => true) {
25700
- return validatePassages(new Map([...storyData.passages].filter(([, passage]) => only(passage))), schema, storeVarMacroNames(), knownParameters());
25818
+ function variableErrors(storyData, schema, only = () => true, parametersOf = knownParameters()) {
25819
+ return validatePassages(new Map([...storyData.passages].filter(([, passage]) => only(passage))), schema, storeVarMacroNames(), parametersOf);
25701
25820
  }
25702
25821
  /**
25703
25822
  * The markup errors (malformed markup, unknown macros, syntax errors in
25704
25823
  * code) of the passages for which `only` holds, with the macros and widgets
25705
25824
  * known now.
25706
25825
  */
25707
- function markupErrors(storyData, only) {
25826
+ function markupErrors(storyData, only, parametersOf = knownParameters()) {
25708
25827
  return validateMarkup(storyData.passages.values(), {
25709
25828
  isKnownMacro: (name) => !!getMacro(name) || isSubMacro(name) || !!getWidget(name),
25710
25829
  macroNames: getMacroRegistry().map((m) => m.name),
25711
- parametersOf: knownParameters(),
25830
+ parametersOf,
25712
25831
  only
25713
25832
  }).map(formatDiagnostic);
25714
25833
  }
@@ -25747,9 +25866,11 @@ function boot() {
25747
25866
  throw new Error(`spindle: ${msg}`);
25748
25867
  }
25749
25868
  const schema = orStop(() => parseStoryVariables(storyVarsPassage.content));
25750
- for (const { name, block } of orStop(() => effectiveWidgets(storyData.passages.values()))) setBlockWidget(name, block);
25751
- const errors = variableErrors(storyData, schema, (p) => p.name === "StoryInit");
25752
- errors.push(...markupErrors(storyData, (p) => p.name === "StoryInit"));
25869
+ const widgets = orStop(() => effectiveWidgets(storyData.passages.values()));
25870
+ for (const { name, block } of widgets) setBlockWidget(name, block);
25871
+ const startupParameters = knownParameters(widgets);
25872
+ const errors = variableErrors(storyData, schema, (p) => p.name === "StoryInit", startupParameters);
25873
+ errors.push(...markupErrors(storyData, (p) => p.name === "StoryInit", startupParameters));
25753
25874
  let transientDefaults = {};
25754
25875
  const storyTransientsPassage = storyData.passages.get("StoryTransients");
25755
25876
  if (storyTransientsPassage) {