@rohal12/spindle 0.63.0 → 0.64.1

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.
@@ -13248,6 +13248,18 @@ var NameSet = class {
13248
13248
  }
13249
13249
  };
13250
13250
  //#endregion
13251
+ //#region src/components/macros/option-clauses.ts
13252
+ /** The direct value clauses read by the built-in choice controls. */
13253
+ var OPTION_CLAUSE = "option";
13254
+ function isOptionClause(name) {
13255
+ return name.toLowerCase() === OPTION_CLAUSE;
13256
+ }
13257
+ /** Replacements render their own children and do not inherit this contract. */
13258
+ function consumesOptionClauses(macro) {
13259
+ const name = macro.name.toLowerCase();
13260
+ return macro.source === "builtin" && !macro.widget && (name === "listbox" || name === "cycle");
13261
+ }
13262
+ //#endregion
13251
13263
  //#region src/registry.ts
13252
13264
  var registry = new NameMap();
13253
13265
  function registerMacro(name, component) {
@@ -13353,16 +13365,32 @@ function registerMacroMetadata(name, metadata) {
13353
13365
  if (parents?.size === 0) companionParents.delete(sub);
13354
13366
  }
13355
13367
  metadataRegistry.set(name, metadata);
13356
- for (const sub of metadata.subMacros) {
13357
- const parents = companionParents.get(sub) ?? /* @__PURE__ */ new Set();
13358
- parents.add(metadata.name.toLowerCase());
13359
- companionParents.set(sub, parents);
13368
+ indexCompanionParents(companionParents, metadata);
13369
+ }
13370
+ /** Share index construction between live registrations and stateless tooling. */
13371
+ function indexCompanionParents(index, macro) {
13372
+ const name = macro.name.toLowerCase();
13373
+ for (const sub of macro.subMacros) {
13374
+ const parents = index.get(sub) ?? /* @__PURE__ */ new Set();
13375
+ parents.add(name);
13376
+ index.set(sub, parents);
13360
13377
  }
13361
13378
  }
13362
13379
  /** The parents declaring a branching companion (input options are values). */
13363
13380
  function branchParents(name, macros) {
13364
- if (!macros) return [...companionParents.get(name) ?? []].filter((parent) => parent !== "listbox" && parent !== "cycle");
13365
- return effectiveNamedDefinitions(macros).filter((macro) => macro.name.toLowerCase() !== "listbox" && macro.name.toLowerCase() !== "cycle" && macro.subMacros.some((sub) => sub.toLowerCase() === name.toLowerCase())).map((macro) => macro.name.toLowerCase());
13381
+ if (!macros) return [...companionParents.get(name) ?? []].filter((parent) => !consumesOptionClauses(metadataRegistry.get(parent)));
13382
+ return [...branchParentLookup(macros)(name)];
13383
+ }
13384
+ /** Build once per effective metadata snapshot, then look up each token. */
13385
+ function branchParentLookup(macros) {
13386
+ const parents = new NameMap();
13387
+ for (const macro of effectiveNamedDefinitions(macros)) {
13388
+ if (consumesOptionClauses(macro)) continue;
13389
+ indexCompanionParents(parents, macro);
13390
+ }
13391
+ const index = new NameMap();
13392
+ for (const [sub, names] of parents) index.set(sub, [...names]);
13393
+ return (name) => index.get(name) ?? [];
13366
13394
  }
13367
13395
  /** The declared parameters of the macro `name`, if it declares any. */
13368
13396
  function getMacroParameters(name) {
@@ -13427,6 +13455,10 @@ function isBlockMacro(name) {
13427
13455
  if (REGISTERED_BLOCK_MACROS.has(name)) return true;
13428
13456
  if (WIDGETS.has(name)) return BLOCK_WIDGETS.has(name);
13429
13457
  if (DEFINED_MACROS.has(name)) return DEFINED_BLOCK_MACROS.has(name);
13458
+ return isBuiltinBlockMacro(name);
13459
+ }
13460
+ /** Built-in defaults without any process-global registrations or widgets. */
13461
+ function isBuiltinBlockMacro(name) {
13430
13462
  return BLOCK_MACROS.has(name);
13431
13463
  }
13432
13464
  /**
@@ -13593,9 +13625,11 @@ function pairMarkup(tokens, options = {}) {
13593
13625
  const isBlock = options.isBlock ?? isBlockMacro;
13594
13626
  const isRaw = options.isRaw ?? isRawMacro;
13595
13627
  const parentsOf = (name) => {
13628
+ if (options.branchParents) return options.branchParents(name);
13629
+ const registered = branchParents(name);
13596
13630
  const lower = name.toLowerCase();
13597
13631
  const builtin = Object.prototype.hasOwnProperty.call(BRANCH_PARENT, lower) ? BRANCH_PARENT[lower] : void 0;
13598
- return builtin ? [builtin] : options.branchParents?.(name) ?? branchParents(name);
13632
+ return builtin && getMacroParameters.source(builtin) === void 0 ? [...registered, builtin] : registered;
13599
13633
  };
13600
13634
  const errors = [];
13601
13635
  const noticedAtEnd = (tokens[tokens.length - 1]?.end ?? 0) + 1;
@@ -14882,7 +14916,7 @@ var OPTION_PARAMETERS = [{
14882
14916
  required: true
14883
14917
  }];
14884
14918
  /** The parameters of the built-in sub-macros that take arguments. */
14885
- var SUB_MACRO_PARAMETERS = /* @__PURE__ */ new Map([["option", OPTION_PARAMETERS]]);
14919
+ var SUB_MACRO_PARAMETERS = /* @__PURE__ */ new Map([[OPTION_CLAUSE, OPTION_PARAMETERS]]);
14886
14920
  /** The parameters of the built-in sub-macro `name`, if it takes any. */
14887
14921
  function subMacroParameters(name) {
14888
14922
  return SUB_MACRO_PARAMETERS.get(name.toLowerCase());
@@ -14892,7 +14926,7 @@ function subMacroParameters(name) {
14892
14926
  */
14893
14927
  function extractOptions(children) {
14894
14928
  const options = [];
14895
- for (const node of children) if (node.type === "macro" && node.name === "option") {
14929
+ for (const node of children) if (node.type === "macro" && isOptionClause(node.name)) {
14896
14930
  const { value } = parseMacroArgs(node.rawArgs, OPTION_PARAMETERS);
14897
14931
  options.push(value ?? "");
14898
14932
  }
@@ -14978,6 +15012,30 @@ function widgetParameters(params, holds = {}) {
14978
15012
  ...hasOwn(holds, name) ? { holds: holds[name] } : {}
14979
15013
  }));
14980
15014
  }
15015
+ /** A consumed branch uses its parent's argument contract, not standalone metadata. */
15016
+ function macroArgumentLookup(tokens, parametersOf) {
15017
+ let owners;
15018
+ const parentsOf = parametersOf.pairing?.branchParents ?? branchParents;
15019
+ return (token) => {
15020
+ const name = token.name.toLowerCase();
15021
+ if (parentsOf(name).length > 0 && !owners) {
15022
+ owners = /* @__PURE__ */ new Map();
15023
+ const pending = [...pairMarkup(tokens, parametersOf.pairing).nodes];
15024
+ while (pending.length) {
15025
+ const node = pending.pop();
15026
+ if (!node.body) continue;
15027
+ for (const branch of node.body.branches) {
15028
+ if (node.token.type === "macro") owners.set(branch.tag, node.token.name.toLowerCase());
15029
+ for (const child of branch.children) pending.push(child);
15030
+ }
15031
+ for (const child of node.body.children) pending.push(child);
15032
+ }
15033
+ }
15034
+ const owner = owners?.get(token);
15035
+ if (owner !== void 0) return CONDITION_MACROS.has(name) && owner === BRANCH_PARENT[name] && usesBuiltinSemantics(owner, parametersOf) ? "condition" : "branch";
15036
+ return CONDITION_MACROS.has(name) && usesBuiltinSemantics(name, parametersOf) ? "condition" : "invocation";
15037
+ };
15038
+ }
14981
15039
  /** Whether builtin-only interpretation still belongs to this implementation. */
14982
15040
  function usesBuiltinSemantics(name, parametersOf) {
14983
15041
  return parametersOf.widget?.(name) === void 0 && parametersOf.source?.(name) !== "user";
@@ -15011,10 +15069,11 @@ function withHolds(params, holds) {
15011
15069
  * A widget among `macros` says what its parameters hold (see ParametersOf).
15012
15070
  */
15013
15071
  function parameterLookup(macros) {
15072
+ const list = [...macros];
15014
15073
  const parameters = new NameMap();
15015
15074
  const widgets = new NameMap();
15016
15075
  const sources = new NameMap();
15017
- for (const { name, parameters: params, interpolate, widget, source } of macros) {
15076
+ for (const { name, parameters: params, interpolate, widget, source } of list) {
15018
15077
  sources.set(name, source ?? "user");
15019
15078
  if (params) parameters.set(name, withHolds(params, holdsByDefault(interpolate)));
15020
15079
  else parameters.delete(name);
@@ -15031,6 +15090,15 @@ function parameterLookup(macros) {
15031
15090
  };
15032
15091
  if (widgets.size > 0) lookup.widget = (name) => widgets.get(name);
15033
15092
  lookup.source = (name) => sources.get(name);
15093
+ const definitions = new NameMap(effectiveNamedDefinitions([...list.filter((macro) => !macro.widget), ...list.filter((macro) => macro.widget)]).map((macro) => [macro.name, macro]));
15094
+ lookup.pairing = {
15095
+ isBlock: (name) => definitions.get(name)?.block ?? isBuiltinBlockMacro(name),
15096
+ isRaw: (name) => isRawMacro(name, widgets.has(name)),
15097
+ branchParents: branchParentLookup([...definitions.values()].map((macro) => ({
15098
+ ...macro,
15099
+ subMacros: [...macro.subMacros ?? []]
15100
+ })))
15101
+ };
15034
15102
  return lookup;
15035
15103
  }
15036
15104
  /** What a `string` or `text` argument holds when its parameter doesn't say. */
@@ -15053,6 +15121,7 @@ function stringHolds(param) {
15053
15121
  * `parametersOf` gives the declared parameters of a macro, if it has any.
15054
15122
  */
15055
15123
  function* codeAndText(src, tokens, parametersOf) {
15124
+ const argumentRole = macroArgumentLookup(tokens, parametersOf);
15056
15125
  const closeOfTextBody = (t, name) => {
15057
15126
  for (let k = t + 1; k < tokens.length; k++) {
15058
15127
  const c = tokens[k];
@@ -15096,6 +15165,8 @@ function* codeAndText(src, tokens, parametersOf) {
15096
15165
  }
15097
15166
  else if (token.type === "macro" && !token.isClose) {
15098
15167
  const name = token.name.toLowerCase();
15168
+ const role = argumentRole(token);
15169
+ if (role === "branch") continue;
15099
15170
  const widget = parametersOf.widget?.(name);
15100
15171
  if (executesRawBody(name, parametersOf)) {
15101
15172
  const close = closeOfTextBody(t, name);
@@ -15129,7 +15200,7 @@ function* codeAndText(src, tokens, parametersOf) {
15129
15200
  if (!args) continue;
15130
15201
  const label = `{${token.name} ${args}}`;
15131
15202
  const argsAt = token.argsStart;
15132
- if (CONDITION_MACROS.has(name) && usesBuiltinSemantics(name, parametersOf)) {
15203
+ if (role === "condition") {
15133
15204
  yield {
15134
15205
  kind: "code",
15135
15206
  code: args,
@@ -15185,6 +15256,7 @@ function* expand(root, src, tokens, parametersOf, options, toRoot, where, locate
15185
15256
  const inner = analyzeMarkup(flat.text, {
15186
15257
  text: true,
15187
15258
  hooks: {
15259
+ ...parametersOf.pairing,
15188
15260
  ...options.isBlock && { isBlock: options.isBlock },
15189
15261
  isRaw: (name) => isRawMacro(name, parametersOf.widget?.(name) !== void 0)
15190
15262
  }
@@ -16737,7 +16809,7 @@ function PlaceholderAttributes({ tag, attributes, placeholders, svg, children })
16737
16809
  return withAttributeErrors(errors, h(tag, ref ? {
16738
16810
  ...props,
16739
16811
  ref
16740
- } : props, ...children));
16812
+ } : props, children));
16741
16813
  }
16742
16814
  /** An element of markdown output with attributes to set directly. */
16743
16815
  function DirectAttributes({ tag, props, direct, children }) {
@@ -16745,7 +16817,7 @@ function DirectAttributes({ tag, props, direct, children }) {
16745
16817
  return h(tag, {
16746
16818
  ...props,
16747
16819
  ref
16748
- }, ...children);
16820
+ }, children);
16749
16821
  }
16750
16822
  /**
16751
16823
  * Show the errors met in an element's attributes in front of it, the way a
@@ -16786,10 +16858,9 @@ function parseHtmlInert(html) {
16786
16858
  */
16787
16859
  function htmlToPreact(html, ph, unwrapParagraphs = false) {
16788
16860
  const content = parseHtmlInert(html.trim());
16789
- if (unwrapParagraphs) {
16790
- for (const child of Array.from(content.childNodes)) if (child.nodeType === Node.ELEMENT_NODE && child.nodeName === "P") child.replaceWith(...Array.from(child.childNodes));
16791
- }
16792
- const children = Array.from(content.childNodes).map((child, i) => convertDomNode(child, i, ph));
16861
+ const children = [];
16862
+ for (const child of Array.from(content.childNodes)) if (unwrapParagraphs && child.nodeType === Node.ELEMENT_NODE && child.nodeName === "P") for (const node of Array.from(child.childNodes)) children.push(convertDomNode(node, children.length, ph));
16863
+ else children.push(convertDomNode(child, children.length, ph));
16793
16864
  return /* @__PURE__ */ jsx(Fragment$1, { children });
16794
16865
  }
16795
16866
  function convertDomNode(node, key, ph) {
@@ -16840,7 +16911,7 @@ function convertDomNode(node, key, ph) {
16840
16911
  return h(tag, {
16841
16912
  ...props,
16842
16913
  key
16843
- }, ...children);
16914
+ }, children);
16844
16915
  }
16845
16916
  return null;
16846
16917
  }
@@ -17326,7 +17397,7 @@ function trimLineEdges(text) {
17326
17397
  */
17327
17398
  function buildPlainTextVnodes(core, ph, unwrapParagraphs) {
17328
17399
  const children = expandPlaceholderText(trimLineEdges(core), ph).filter((part) => part !== "");
17329
- return unwrapParagraphs ? /* @__PURE__ */ jsx(Fragment$1, { children }) : h("p", null, ...children);
17400
+ return unwrapParagraphs ? /* @__PURE__ */ jsx(Fragment$1, { children }) : h("p", null, children);
17330
17401
  }
17331
17402
  /**
17332
17403
  * Render AST nodes with full CommonMark markdown support.
@@ -17353,19 +17424,23 @@ function renderNodes(nodes, options) {
17353
17424
  components,
17354
17425
  nodes: []
17355
17426
  };
17356
- let combined = "";
17427
+ const chunks = [];
17428
+ let trailingBackslash = false;
17357
17429
  for (let i = 0; i < nodes.length; i++) {
17358
17430
  const node = nodes[i];
17359
17431
  if (node.type === "text") {
17360
- combined += node.value;
17432
+ chunks.push(node.value);
17433
+ if (node.value) trailingBackslash = node.value.endsWith("\\");
17361
17434
  continue;
17362
17435
  }
17363
17436
  const phIdx = components.length;
17364
17437
  components.push(renderSingleNode(node));
17365
17438
  ph.nodes.push(node);
17366
- if (combined.endsWith("\\")) combined += ESCAPE_GUARD;
17367
- combined += placeholderHtml(ph.nonce, phIdx);
17439
+ if (trailingBackslash) chunks.push(ESCAPE_GUARD);
17440
+ chunks.push(placeholderHtml(ph.nonce, phIdx));
17441
+ trailingBackslash = false;
17368
17442
  }
17443
+ const combined = chunks.join("");
17369
17444
  const unwrapParagraphs = !!(options?.nobr || options?.inline);
17370
17445
  const lead = combined.slice(0, skipChars(combined, 0, 1, EDGE_WS));
17371
17446
  const trail = lead.length === combined.length ? "" : combined.slice(skipChars(combined, combined.length, -1, EDGE_WS));
@@ -23368,6 +23443,7 @@ function boundReference(token, at, all) {
23368
23443
  /** Report the `$var` references in the tokens of `content`. */
23369
23444
  function collectTokenRefs(content, tokens, at, emit, options) {
23370
23445
  const { storeVarMacros, parametersOf } = options;
23446
+ const argumentRole = macroArgumentLookup(tokens, parametersOf);
23371
23447
  for (let t = 0; t < tokens.length; t++) {
23372
23448
  const token = tokens[t];
23373
23449
  if (options.all && token.type !== "macro" && token.type !== "html" && token.type !== "text") scanSelectors(content, token, at, emit, options);
@@ -23393,13 +23469,15 @@ function collectTokenRefs(content, tokens, at, emit, options) {
23393
23469
  else scanInterpolations(value, valueIn, emit, options);
23394
23470
  }
23395
23471
  else if (token.type === "macro" && !token.isClose) {
23396
- const binding = storeVarMacros.has(token.name.toLowerCase()) && !parametersOf.widget?.(token.name) ? boundReference(token, at, options.all) : void 0;
23397
- scanArgs(token, parametersOf, at, (ref) => {
23472
+ const role = argumentRole(token);
23473
+ const binding = role === "invocation" && storeVarMacros.has(token.name.toLowerCase()) && !parametersOf.widget?.(token.name) ? boundReference(token, at, options.all) : void 0;
23474
+ if (role === "condition") scanCode(token.rawArgs, (i) => at(token.argsStart + i), emit);
23475
+ else if (role === "invocation") scanArgs(token, parametersOf, at, (ref) => {
23398
23476
  if (!binding || ref.start < binding.start || ref.start >= binding.end) emit(ref);
23399
23477
  }, options);
23400
23478
  scanSelectors(content, token, at, emit, options);
23401
23479
  if (binding) emit(binding);
23402
- if (executesRawBody(token.name, parametersOf)) {
23480
+ if (role === "invocation" && executesRawBody(token.name, parametersOf)) {
23403
23481
  let close = t + 1;
23404
23482
  while (close < tokens.length) {
23405
23483
  const c = tokens[close];
@@ -24540,6 +24618,7 @@ function selectIfBranch(branches, evaluate) {
24540
24618
  defineMacro({
24541
24619
  name: "if",
24542
24620
  block: true,
24621
+ subMacros: ["elseif", "else"],
24543
24622
  interpolate: true,
24544
24623
  merged: true,
24545
24624
  render({ branches = [] }, ctx) {
@@ -26097,7 +26176,7 @@ defineMacro({
26097
26176
  defineMacro({
26098
26177
  name: "listbox",
26099
26178
  interpolate: true,
26100
- subMacros: ["option"],
26179
+ subMacros: [OPTION_CLAUSE],
26101
26180
  storeVar: true,
26102
26181
  parameters: [VARIABLE_PARAMETER],
26103
26182
  render({ children = [] }, ctx) {
@@ -26126,7 +26205,7 @@ defineMacro({
26126
26205
  defineMacro({
26127
26206
  name: "cycle",
26128
26207
  block: true,
26129
- subMacros: ["option"],
26208
+ subMacros: [OPTION_CLAUSE],
26130
26209
  interpolate: true,
26131
26210
  storeVar: true,
26132
26211
  parameters: [VARIABLE_PARAMETER],
@@ -78,7 +78,10 @@ export default [
78
78
  {
79
79
  "name": "if",
80
80
  "block": true,
81
- "subMacros": [],
81
+ "subMacros": [
82
+ "elseif",
83
+ "else"
84
+ ],
82
85
  "interpolate": true,
83
86
  "merged": true,
84
87
  "source": "builtin"
@@ -78,7 +78,10 @@
78
78
  {
79
79
  "name": "if",
80
80
  "block": true,
81
- "subMacros": [],
81
+ "subMacros": [
82
+ "elseif",
83
+ "else"
84
+ ],
82
85
  "interpolate": true,
83
86
  "merged": true,
84
87
  "source": "builtin"