@rohal12/spindle 0.64.12 → 0.64.13

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.
@@ -15543,7 +15543,7 @@ function widgetDefinitions(passages, parametersOf = registeredParameters) {
15543
15543
  });
15544
15544
  }
15545
15545
  } else if (open.length > 0 && (token.type === "variable" && token.scope === "local" && token.name === "children" || tokenTextContainsChildren(token, parametersOf, passage.content, hooks, budget))) open[open.length - 1].isBlock = true;
15546
- done.push(...open);
15546
+ for (const def of open) done.push(def);
15547
15547
  const defs = [];
15548
15548
  for (const { token, isBlock, closeStart } of done) try {
15549
15549
  const [from, to] = nameSpan(token.rawArgs);
@@ -15558,7 +15558,8 @@ function widgetDefinitions(passages, parametersOf = registeredParameters) {
15558
15558
  ...closeStart === void 0 ? {} : { closeStart }
15559
15559
  });
15560
15560
  } catch {}
15561
- found.push(...defs.sort((a, b) => a.start - b.start));
15561
+ defs.sort((a, b) => a.start - b.start);
15562
+ for (const def of defs) found.push(def);
15562
15563
  }
15563
15564
  return found;
15564
15565
  };
@@ -20475,13 +20476,25 @@ function tabIndexAttr(el) {
20475
20476
  if (raw === null || !/^\s*[+-]?\d+\s*$/.test(raw)) return null;
20476
20477
  return parseInt(raw, 10);
20477
20478
  }
20479
+ /** Eligible radio stops in one DOM tree, including external form controls. */
20480
+ function radioTabStops(root) {
20481
+ const groups = /* @__PURE__ */ new Map();
20482
+ for (const radio of root.querySelectorAll("input[type=\"radio\"]")) {
20483
+ if (!radio.name || (tabIndexAttr(radio) ?? 0) < 0 || !isAvailable(radio)) continue;
20484
+ let names = groups.get(radio.form);
20485
+ if (!names) groups.set(radio.form, names = /* @__PURE__ */ new Map());
20486
+ const stop = names.get(radio.name);
20487
+ if (!stop || !stop.checked && radio.checked) names.set(radio.name, radio);
20488
+ }
20489
+ return new Set([...groups.values()].flatMap((names) => [...names.values()]));
20490
+ }
20478
20491
  /**
20479
20492
  * Whether the tab key can reach `el`, an available element (see
20480
20493
  * isAvailable): a negative tabindex only allows scripted focus, and of a
20481
20494
  * radio group the browser tabs to the checked member only (the first one
20482
20495
  * when none is checked), of the members it can reach.
20483
20496
  */
20484
- function isTabStop(el) {
20497
+ function isTabStop(el, radioStops) {
20485
20498
  const tabIndex = tabIndexAttr(el);
20486
20499
  if (tabIndex !== null && tabIndex < 0) return false;
20487
20500
  if (tabIndex === null && el.hasAttribute("contenteditable") && !el.matches("a[href], button, input, select, textarea, iframe, summary") && (!el.isContentEditable || el.parentElement?.isContentEditable)) return false;
@@ -20491,8 +20504,13 @@ function isTabStop(el) {
20491
20504
  }
20492
20505
  const input = el;
20493
20506
  if (el.localName === "input" && input.type === "radio" && input.name) {
20494
- const group = Array.from(el.getRootNode().querySelectorAll("input[type=\"radio\"]")).filter((r) => r.name === input.name && r.form === input.form && (tabIndexAttr(r) ?? 0) >= 0 && isAvailable(r));
20495
- return el === (group.find((r) => r.checked) ?? group[0]);
20507
+ const tree = el.getRootNode();
20508
+ let stops = radioStops.get(tree);
20509
+ if (!stops) {
20510
+ stops = radioTabStops(tree);
20511
+ radioStops.set(tree, stops);
20512
+ }
20513
+ return stops.has(input);
20496
20514
  }
20497
20515
  return true;
20498
20516
  }
@@ -20501,18 +20519,18 @@ function isTabStop(el) {
20501
20519
  * order Tab visits them: positive tabindexes first, ascending, then the
20502
20520
  * others in document order.
20503
20521
  */
20504
- function focusables(root) {
20522
+ function focusables(root, radioStops = /* @__PURE__ */ new Map()) {
20505
20523
  const scopes = [];
20506
20524
  const collect = (parent) => {
20507
20525
  for (const element of composedChildren(parent)) {
20508
20526
  const el = element;
20509
20527
  const nested = el.shadowRoot ?? (el.localName === "slot" ? el : null);
20510
- const stop = el.matches(FOCUSABLE) && isAvailable(el) && isTabStop(el);
20528
+ const stop = el.matches(FOCUSABLE) && isAvailable(el) && isTabStop(el, radioStops);
20511
20529
  if (nested) {
20512
20530
  if ((tabIndexAttr(el) ?? 0) < 0) continue;
20513
20531
  scopes.push({
20514
20532
  el,
20515
- stops: [...stop && !el.shadowRoot?.delegatesFocus ? [el] : [], ...focusables(nested)]
20533
+ stops: [...stop && !el.shadowRoot?.delegatesFocus ? [el] : [], ...focusables(nested, radioStops)]
20516
20534
  });
20517
20535
  } else {
20518
20536
  if (stop) scopes.push({
@@ -12240,7 +12240,7 @@ function widgetDefinitions$1(passages, parametersOf = registeredParameters) {
12240
12240
  });
12241
12241
  }
12242
12242
  } else if (open.length > 0 && (token.type === "variable" && token.scope === "local" && token.name === "children" || tokenTextContainsChildren(token, parametersOf, passage.content, hooks, budget))) open[open.length - 1].isBlock = true;
12243
- done.push(...open);
12243
+ for (const def of open) done.push(def);
12244
12244
  const defs = [];
12245
12245
  for (const { token, isBlock, closeStart } of done) try {
12246
12246
  const [from, to] = nameSpan(token.rawArgs);
@@ -12255,7 +12255,8 @@ function widgetDefinitions$1(passages, parametersOf = registeredParameters) {
12255
12255
  ...closeStart === void 0 ? {} : { closeStart }
12256
12256
  });
12257
12257
  } catch {}
12258
- found.push(...defs.sort((a, b) => a.start - b.start));
12258
+ defs.sort((a, b) => a.start - b.start);
12259
+ for (const def of defs) found.push(def);
12259
12260
  }
12260
12261
  return found;
12261
12262
  };
@@ -25603,13 +25604,13 @@ async function formatLf(text, options) {
25603
25604
  if (region.type !== "spindle") region.lines = region.lines.map((l) => replaceAll(l, sentinel, "\n"));
25604
25605
  if (region.type === "script") {
25605
25606
  if (region.lines.length === 1) {
25606
- lines.push(...region.lines);
25607
+ for (const line of region.lines) lines.push(line);
25607
25608
  continue;
25608
25609
  }
25609
25610
  const innerCode = region.lines.slice(1, -1).join("\n");
25610
25611
  const formatted = await formatJS(innerCode.trim(), prettier, prettierOptions);
25611
25612
  if (formatted === void 0 || !sameSpindleTokens(innerCode, formatted)) {
25612
- lines.push(...region.lines);
25613
+ for (const line of region.lines) lines.push(line);
25613
25614
  continue;
25614
25615
  }
25615
25616
  lines.push(region.lines[0]);
@@ -25618,25 +25619,25 @@ async function formatLf(text, options) {
25618
25619
  continue;
25619
25620
  }
25620
25621
  if (region.type === "svg") {
25621
- lines.push(...region.lines);
25622
+ for (const line of region.lines) lines.push(line);
25622
25623
  continue;
25623
25624
  }
25624
25625
  if (region.type === "html") {
25625
25626
  const htmlText = region.lines.join("\n");
25626
25627
  if (WHITESPACE_AS_WRITTEN.test(htmlText) || region.lines.some((line) => literalLines.has(line.trim())) || hasMarkdownBoundaries(htmlText, isBlock)) {
25627
25628
  for (let i = 0; i < region.lines.length; i++) preservedLines.add(lines.length + i);
25628
- lines.push(...region.lines);
25629
+ for (const line of region.lines) lines.push(line);
25629
25630
  continue;
25630
25631
  }
25631
25632
  const { text: svgPlaceholdered, tokens: svgTokens } = replaceSvgBlocks(htmlText);
25632
25633
  if (!placeholdersIntact(svgPlaceholdered, svgTokens, SVG_PLACEHOLDERS)) {
25633
- lines.push(...region.lines);
25634
+ for (const line of region.lines) lines.push(line);
25634
25635
  continue;
25635
25636
  }
25636
25637
  const { text: placeholdered, tokens } = replaceSpindleTokens(svgPlaceholdered);
25637
25638
  const formatted = await formatHTML(placeholdered, prettier, prettierOptions);
25638
25639
  if (formatted === void 0 || !placeholdersIntact(formatted, tokens, SPINDLE_PLACEHOLDERS) || !placeholdersIntact(formatted, svgTokens, SVG_PLACEHOLDERS)) {
25639
- lines.push(...region.lines);
25640
+ for (const line of region.lines) lines.push(line);
25640
25641
  continue;
25641
25642
  }
25642
25643
  const restored = restoreHtmlTokens(formatted.trim(), tokens.map((t) => replaceAll(t, "\n", sentinel)), svgTokens);
@@ -25889,7 +25890,7 @@ function wrapLines(lines, maxLen, literalLines, isBlock) {
25889
25890
  continue;
25890
25891
  }
25891
25892
  if (line.endsWith(" ")) wrapped[wrapped.length - 1] += " ";
25892
- result.push(...wrapped);
25893
+ for (const line of wrapped) result.push(line);
25893
25894
  }
25894
25895
  return result;
25895
25896
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rohal12/spindle",
3
- "version": "0.64.12",
3
+ "version": "0.64.13",
4
4
  "type": "module",
5
5
  "description": "A Preact-based story format for Twine 2.",
6
6
  "license": "Unlicense",
@@ -246,7 +246,7 @@ async function formatLf(
246
246
  if (region.type === 'script') {
247
247
  // Same-line <script>…</script> — leave as written
248
248
  if (region.lines.length === 1) {
249
- lines.push(...region.lines);
249
+ for (const line of region.lines) lines.push(line);
250
250
  continue;
251
251
  }
252
252
  // Inline <script> — format JS content between tags
@@ -259,7 +259,7 @@ async function formatLf(
259
259
  // Without Prettier, where it fails, or where it changed a Spindle
260
260
  // token (`{$a}` is an object literal to it), the region stays as written
261
261
  if (formatted === undefined || !sameSpindleTokens(innerCode, formatted)) {
262
- lines.push(...region.lines);
262
+ for (const line of region.lines) lines.push(line);
263
263
  continue;
264
264
  }
265
265
  lines.push(region.lines[0]!);
@@ -277,7 +277,7 @@ async function formatLf(
277
277
 
278
278
  if (region.type === 'svg') {
279
279
  // SVG block — leave untouched (Prettier would break rendering)
280
- lines.push(...region.lines);
280
+ for (const line of region.lines) lines.push(line);
281
281
  continue;
282
282
  }
283
283
 
@@ -293,7 +293,7 @@ async function formatLf(
293
293
  ) {
294
294
  for (let i = 0; i < region.lines.length; i++)
295
295
  preservedLines.add(lines.length + i);
296
- lines.push(...region.lines);
296
+ for (const line of region.lines) lines.push(line);
297
297
  continue;
298
298
  }
299
299
  // HTML block — placeholder substitution + Prettier
@@ -302,7 +302,7 @@ async function formatLf(
302
302
  // A later whole-line Spindle token may hide an SVG marker. Check
303
303
  // before that pass so a literal marker cannot stand in for it.
304
304
  if (!placeholdersIntact(svgPlaceholdered, svgTokens, SVG_PLACEHOLDERS)) {
305
- lines.push(...region.lines);
305
+ for (const line of region.lines) lines.push(line);
306
306
  continue;
307
307
  }
308
308
  const { text: placeholdered, tokens } =
@@ -320,7 +320,7 @@ async function formatLf(
320
320
  !placeholdersIntact(formatted, tokens, SPINDLE_PLACEHOLDERS) ||
321
321
  !placeholdersIntact(formatted, svgTokens, SVG_PLACEHOLDERS)
322
322
  ) {
323
- lines.push(...region.lines);
323
+ for (const line of region.lines) lines.push(line);
324
324
  continue;
325
325
  }
326
326
  // A token's line breaks are its payload: keep them out of the line steps below
@@ -722,7 +722,7 @@ function wrapLines(
722
722
  }
723
723
  // Keep a Markdown hard break on the last wrapped line
724
724
  if (line.endsWith(' ')) wrapped[wrapped.length - 1] += ' ';
725
- result.push(...wrapped);
725
+ for (const line of wrapped) result.push(line);
726
726
  }
727
727
 
728
728
  return result;
@@ -55,13 +55,36 @@ function tabIndexAttr(el: HTMLElement): number | null {
55
55
  return parseInt(raw, 10);
56
56
  }
57
57
 
58
+ /** Eligible radio stops in one DOM tree, including external form controls. */
59
+ function radioTabStops(root: ParentNode): Set<HTMLInputElement> {
60
+ const groups = new Map<
61
+ HTMLFormElement | null,
62
+ Map<string, HTMLInputElement>
63
+ >();
64
+ for (const radio of root.querySelectorAll<HTMLInputElement>(
65
+ 'input[type="radio"]',
66
+ )) {
67
+ if (!radio.name || (tabIndexAttr(radio) ?? 0) < 0 || !isAvailable(radio)) {
68
+ continue;
69
+ }
70
+ let names = groups.get(radio.form);
71
+ if (!names) groups.set(radio.form, (names = new Map()));
72
+ const stop = names.get(radio.name);
73
+ // First eligible checked member, otherwise first eligible member (#390).
74
+ if (!stop || (!stop.checked && radio.checked)) names.set(radio.name, radio);
75
+ }
76
+ return new Set([...groups.values()].flatMap((names) => [...names.values()]));
77
+ }
78
+
79
+ type RadioStops = Map<Node, Set<HTMLInputElement>>;
80
+
58
81
  /**
59
82
  * Whether the tab key can reach `el`, an available element (see
60
83
  * isAvailable): a negative tabindex only allows scripted focus, and of a
61
84
  * radio group the browser tabs to the checked member only (the first one
62
85
  * when none is checked), of the members it can reach.
63
86
  */
64
- function isTabStop(el: HTMLElement): boolean {
87
+ function isTabStop(el: HTMLElement, radioStops: RadioStops): boolean {
65
88
  const tabIndex = tabIndexAttr(el);
66
89
  if (tabIndex !== null && tabIndex < 0) return false;
67
90
  // Only an editing host takes focus of its own, not `contenteditable="false"`
@@ -89,22 +112,15 @@ function isTabStop(el: HTMLElement): boolean {
89
112
  // another realm's classes
90
113
  const input = el as HTMLInputElement;
91
114
  if (el.localName === 'input' && input.type === 'radio' && input.name) {
92
- const group = Array.from(
93
- // Form ownership does not imply DOM ancestry: `form="id"` controls
94
- // elsewhere in the same tree belong to the group too (#688).
95
- (el.getRootNode() as ParentNode).querySelectorAll<HTMLInputElement>(
96
- 'input[type="radio"]',
97
- ),
98
- ).filter(
99
- (r) =>
100
- r.name === input.name &&
101
- r.form === input.form &&
102
- // Of the members Tab can reach (#390)
103
- (tabIndexAttr(r) ?? 0) >= 0 &&
104
- isAvailable(r),
105
- );
106
- const stop = group.find((r) => r.checked) ?? group[0];
107
- return el === stop;
115
+ const tree = el.getRootNode();
116
+ let stops = radioStops.get(tree);
117
+ if (!stops) {
118
+ // Form ownership is independent of DOM ancestry (#688). Index the
119
+ // entire tree once, shared across shadow/slot tabindex scopes (#718).
120
+ stops = radioTabStops(tree as ParentNode);
121
+ radioStops.set(tree, stops);
122
+ }
123
+ return stops.has(input);
108
124
  }
109
125
  return true;
110
126
  }
@@ -114,7 +130,10 @@ function isTabStop(el: HTMLElement): boolean {
114
130
  * order Tab visits them: positive tabindexes first, ascending, then the
115
131
  * others in document order.
116
132
  */
117
- function focusables(root: ParentNode): HTMLElement[] {
133
+ function focusables(
134
+ root: ParentNode,
135
+ radioStops: RadioStops = new Map(),
136
+ ): HTMLElement[] {
118
137
  // Each shadow host/slot owns a separate tabindex scope. Sort the owners in
119
138
  // this scope, then expand their contents in place, as native Tab does.
120
139
  const scopes: { el: HTMLElement; stops: HTMLElement[] }[] = [];
@@ -122,7 +141,8 @@ function focusables(root: ParentNode): HTMLElement[] {
122
141
  for (const element of composedChildren(parent)) {
123
142
  const el = element as HTMLElement;
124
143
  const nested = el.shadowRoot ?? (el.localName === 'slot' ? el : null);
125
- const stop = el.matches(FOCUSABLE) && isAvailable(el) && isTabStop(el);
144
+ const stop =
145
+ el.matches(FOCUSABLE) && isAvailable(el) && isTabStop(el, radioStops);
126
146
  if (nested) {
127
147
  // A visibility:hidden host can contain explicitly visible controls.
128
148
  // Each stop checks its own visibility and its ancestors' subtree gates.
@@ -131,7 +151,7 @@ function focusables(root: ParentNode): HTMLElement[] {
131
151
  el,
132
152
  stops: [
133
153
  ...(stop && !el.shadowRoot?.delegatesFocus ? [el] : []),
134
- ...focusables(nested),
154
+ ...focusables(nested, radioStops),
135
155
  ],
136
156
  });
137
157
  } else {
@@ -180,7 +180,7 @@ export function widgetDefinitions(
180
180
  open[open.length - 1]!.isBlock = true;
181
181
  }
182
182
  }
183
- done.push(...open);
183
+ for (const def of open) done.push(def);
184
184
  const defs: WidgetDefinition[] = [];
185
185
  for (const { token, isBlock, closeStart } of done) {
186
186
  try {
@@ -199,7 +199,8 @@ export function widgetDefinitions(
199
199
  // The {widget} macro reports a definition it cannot read
200
200
  }
201
201
  }
202
- found.push(...defs.sort((a, b) => a.start - b.start));
202
+ defs.sort((a, b) => a.start - b.start);
203
+ for (const def of defs) found.push(def);
203
204
  }
204
205
  return found;
205
206
  };