stimeo-ui 0.18.1 → 0.18.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.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +21 -0
  3. data/dist/cable/index.js +6 -5
  4. data/dist/controllers/alert_dialog_controller.js +89 -45
  5. data/dist/controllers/announcer_controller.js +22 -9
  6. data/dist/controllers/auto_submit_controller.js +77 -11
  7. data/dist/controllers/bulk_select_controller.js +6 -9
  8. data/dist/controllers/character_counter_controller.js +77 -11
  9. data/dist/controllers/clipboard_controller.js +6 -5
  10. data/dist/controllers/combobox_controller.js +71 -6
  11. data/dist/controllers/command_palette_controller.js +160 -51
  12. data/dist/controllers/conditional_fields_controller.js +84 -44
  13. data/dist/controllers/confirm_controller.js +89 -45
  14. data/dist/controllers/context_menu_controller.js +1 -1
  15. data/dist/controllers/countdown_controller.js +6 -5
  16. data/dist/controllers/currency_input_controller.js +71 -6
  17. data/dist/controllers/data_grid_controller.js +77 -42
  18. data/dist/controllers/date_range_picker_controller.js +2 -1
  19. data/dist/controllers/dialog_controller.js +89 -45
  20. data/dist/controllers/direct_upload_controller.js +6 -5
  21. data/dist/controllers/dismissible_controller.js +11 -7
  22. data/dist/controllers/drawer_controller.js +89 -45
  23. data/dist/controllers/dropdown_controller.js +1 -1
  24. data/dist/controllers/editable_controller.js +71 -6
  25. data/dist/controllers/empty_state_controller.js +6 -5
  26. data/dist/controllers/file_dropzone_controller.js +13 -11
  27. data/dist/controllers/flash_controller.js +6 -5
  28. data/dist/controllers/focus_controller.js +89 -45
  29. data/dist/controllers/form_field_controller.js +6 -5
  30. data/dist/controllers/form_validation_controller.js +11 -7
  31. data/dist/controllers/frame_loading_controller.js +50 -16
  32. data/dist/controllers/hover_card_controller.js +1 -1
  33. data/dist/controllers/input_mask_controller.js +94 -36
  34. data/dist/controllers/intersection_controller.js +61 -29
  35. data/dist/controllers/lazy_frame_controller.js +61 -29
  36. data/dist/controllers/listbox_controller.js +80 -47
  37. data/dist/controllers/local_time_controller.js +1 -7
  38. data/dist/controllers/menu_controller.js +1 -1
  39. data/dist/controllers/menubar_controller.js +4 -6
  40. data/dist/controllers/meter_controller.js +6 -5
  41. data/dist/controllers/multi_select_controller.js +86 -21
  42. data/dist/controllers/navigation_menu_controller.js +1 -1
  43. data/dist/controllers/nested_form_controller.js +17 -12
  44. data/dist/controllers/network_status_controller.js +6 -5
  45. data/dist/controllers/number_input_controller.js +154 -66
  46. data/dist/controllers/otp_controller.js +71 -6
  47. data/dist/controllers/overflow_menu_controller.js +7 -2
  48. data/dist/controllers/pagination_controller.js +7 -2
  49. data/dist/controllers/password_strength_controller.js +6 -5
  50. data/dist/controllers/popover_controller.js +12 -8
  51. data/dist/controllers/progress_controller.js +6 -5
  52. data/dist/controllers/radio_group_controller.js +85 -49
  53. data/dist/controllers/range_slider_controller.js +72 -54
  54. data/dist/controllers/relative_time_controller.js +1 -7
  55. data/dist/controllers/roving_controller.js +7 -2
  56. data/dist/controllers/scroll_area_controller.js +11 -7
  57. data/dist/controllers/scrollspy_controller.js +61 -29
  58. data/dist/controllers/separator_controller.js +72 -54
  59. data/dist/controllers/sidebar_controller.js +89 -45
  60. data/dist/controllers/skeleton_controller.js +50 -16
  61. data/dist/controllers/slider_controller.js +72 -54
  62. data/dist/controllers/sortable_controller.js +6 -5
  63. data/dist/controllers/spinner_controller.js +50 -16
  64. data/dist/controllers/sticky_observer_controller.js +61 -29
  65. data/dist/controllers/submit_once_controller.js +6 -5
  66. data/dist/controllers/switch_controller.js +84 -44
  67. data/dist/controllers/tags_input_controller.js +83 -20
  68. data/dist/controllers/time_picker_controller.js +77 -42
  69. data/dist/controllers/toggle_group_controller.js +84 -44
  70. data/dist/controllers/toolbar_controller.js +78 -8
  71. data/dist/controllers/tooltip_controller.js +1 -1
  72. data/dist/controllers/tree_view_controller.js +87 -49
  73. data/dist/index.js +503 -286
  74. data/lib/stimeo/ui/version.rb +1 -1
  75. metadata +2 -2
@@ -54,6 +54,7 @@ var EscapeLayer = class _EscapeLayer {
54
54
  );
55
55
  #active = null;
56
56
  activate(ownerDocument = document, options) {
57
+ const entry = { onDismiss: options.onDismiss, claims: options.claims ?? null };
57
58
  this.deactivate();
58
59
  let registry = _EscapeLayer.#registries.get(ownerDocument);
59
60
  if (!registry) {
@@ -61,7 +62,6 @@ var EscapeLayer = class _EscapeLayer {
61
62
  _EscapeLayer.#registries.set(ownerDocument, registry);
62
63
  ownerDocument.addEventListener("keydown", registry.onKeydown);
63
64
  }
64
- const entry = { onDismiss: options.onDismiss, claims: options.claims ?? null };
65
65
  registry.stack.push(entry);
66
66
  this.#active = { document: ownerDocument, entry };
67
67
  }
@@ -110,11 +110,16 @@ var EscapeLayer = class _EscapeLayer {
110
110
  }
111
111
  };
112
112
 
113
+ var HTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
113
114
  function inheritsFieldsetDisabled(control) {
114
115
  let fieldset = control.closest("fieldset[disabled]");
115
116
  while (fieldset) {
116
- const legend = Array.from(fieldset.children).find((child) => child.tagName === "LEGEND");
117
- if (!legend?.contains(control)) return true;
117
+ if (fieldset.namespaceURI === HTML_NAMESPACE && fieldset.localName === "fieldset") {
118
+ const legend = Array.from(fieldset.children).find(
119
+ (child) => child.namespaceURI === HTML_NAMESPACE && child.localName === "legend"
120
+ );
121
+ if (!legend?.contains(control)) return true;
122
+ }
118
123
  fieldset = fieldset.parentElement?.closest("fieldset[disabled]") ?? null;
119
124
  }
120
125
  return false;
@@ -187,11 +192,10 @@ function hasNativeTabStop(element) {
187
192
  if (element.tagName === "AUDIO" || element.tagName === "VIDEO") {
188
193
  return element.hasAttribute("controls");
189
194
  }
190
- if (element instanceof HTMLElement && element.tagName === "SUMMARY") {
191
- const details = element.parentElement;
192
- return details instanceof HTMLDetailsElement && Array.from(details.children).find((child) => child.tagName === "SUMMARY") === element;
193
- }
194
- return false;
195
+ const details = element.parentElement;
196
+ return details instanceof HTMLDetailsElement && Array.from(details.children).find(
197
+ (child) => child.namespaceURI === HTML_NAMESPACE && child.localName === "summary"
198
+ ) === element;
195
199
  }
196
200
  function hasEditableTabStop(element) {
197
201
  const value = element.getAttribute("contenteditable")?.toLowerCase();
@@ -254,8 +258,7 @@ function isSequentialStop(element) {
254
258
  function ownedScope(element) {
255
259
  if (element.shadowRoot) return Array.from(element.shadowRoot.children);
256
260
  if (element instanceof HTMLSlotElement) {
257
- const assigned = element.assignedElements();
258
- return assigned.length > 0 ? assigned : Array.from(element.children);
261
+ return element.assignedNodes().length > 0 ? element.assignedElements() : Array.from(element.children);
259
262
  }
260
263
  return null;
261
264
  }
@@ -264,12 +267,19 @@ function orderScope(elements, walk) {
264
267
  const visit = (element) => {
265
268
  if (element === walk.from) walk.fromAt = walk.tree.length;
266
269
  const tabindex = authoredTabindex(element) ?? 0;
267
- const stop = (element instanceof HTMLElement || element instanceof SVGElement) && isSequentialStop(element) ? element : null;
268
- if (stop) walk.tree.push(stop);
270
+ let stop = null;
271
+ if ((element instanceof HTMLElement || element instanceof SVGElement) && isSequentialStop(element)) {
272
+ stop = element;
273
+ walk.tree.push(element);
274
+ }
269
275
  const owned = ownedScope(element);
270
276
  if (owned) {
271
277
  if (tabindex >= 0) {
272
278
  entries.push({ tabindex, stops: [...stop ? [stop] : [], ...orderScope(owned, walk)] });
279
+ } else if (walk.from && flatTreeContains(element, walk.from)) {
280
+ walk.fromAt = walk.tree.length;
281
+ walk.excludedScope = element;
282
+ orderScope(owned, { from: walk.from, tree: walk.tree, fromAt: -1, excludedScope: null });
273
283
  }
274
284
  return;
275
285
  }
@@ -297,16 +307,38 @@ function startIndex(order, walk, backward, isStop) {
297
307
  if (at >= 0) return backward ? at - 1 : at + 1;
298
308
  const neighbour = backward ? walk.tree.slice(0, walk.fromAt).reverse().find(isStop) : walk.tree.slice(walk.fromAt).find(isStop);
299
309
  if (neighbour) return order.indexOf(neighbour);
300
- return backward ? order.length - 1 : 0;
310
+ return backward ? -1 : order.length;
301
311
  }
302
- function sequentialTabStops(root, from = null, backward = false) {
303
- const walk = { from, tree: [], fromAt: from === root ? 0 : -1 };
312
+ function movesWithin(root, from, backward, wrap, radioContext) {
313
+ const walk = {
314
+ from,
315
+ tree: [],
316
+ fromAt: from === root ? 0 : -1,
317
+ excludedScope: null
318
+ };
304
319
  const order = orderScope(ownedScope(root) ?? Array.from(root.children), walk);
305
- const focusedRadio = walk.fromAt >= 0 && isRadio(from) ? from : null;
306
- const isStop = (element) => passesRadioRule(element, walk.tree, focusedRadio);
307
- const start = startIndex(order, walk, backward, isStop);
308
- const visited = backward ? [...order.slice(0, start + 1).reverse(), ...order.slice(start + 1).reverse()] : [...order.slice(start), ...order.slice(0, start)];
309
- return visited.filter((element) => element !== from && isStop(element));
320
+ const radios = radioContext ?? {
321
+ tree: walk.tree,
322
+ focusedRadio: walk.fromAt >= 0 && isRadio(from) ? from : null
323
+ };
324
+ const isStop = (element) => passesRadioRule(element, radios.tree, radios.focusedRadio);
325
+ const start = startIndex(
326
+ order,
327
+ walk,
328
+ backward,
329
+ (element) => order.includes(element) && isStop(element)
330
+ );
331
+ const remaining = backward ? order.slice(0, start + 1).reverse() : order.slice(start);
332
+ const wrapped = wrap ? backward ? order.slice(start + 1).reverse() : order.slice(0, start) : [];
333
+ const excluded = walk.excludedScope;
334
+ const prefix = excluded && !(backward && from === excluded) ? movesWithin(excluded, from === excluded ? null : from, backward, false, radios) : [];
335
+ return [
336
+ ...prefix,
337
+ ...[...remaining, ...wrapped].filter((element) => element !== from && isStop(element))
338
+ ];
339
+ }
340
+ function sequentialTabStops(root, from = null, backward = false) {
341
+ return movesWithin(root, from, backward, true);
310
342
  }
311
343
  function deepActiveElement(ownerDocument) {
312
344
  let active = ownerDocument.activeElement;
@@ -500,7 +532,10 @@ var FocusTrap = class _FocusTrap {
500
532
  constructor(getContainer, options = {}) {
501
533
  this.#getContainer = getContainer;
502
534
  this.#options = options;
503
- this.#entry = {
535
+ this.#entry = this.#createEntry();
536
+ }
537
+ #createEntry() {
538
+ return {
504
539
  container: () => this.#readContainer(),
505
540
  moveFocus: (backward) => this.#moveFocus(backward),
506
541
  refocus: () => this.#refocus(),
@@ -516,6 +551,7 @@ var FocusTrap = class _FocusTrap {
516
551
  activate() {
517
552
  if (this.#activeState) return;
518
553
  _FocusTrap.#sweep();
554
+ this.#entry = this.#createEntry();
519
555
  this.#activeState = true;
520
556
  this.#openers = _FocusTrap.#focusPath();
521
557
  this.#entry.openedFrom = _FocusTrap.#trapHolding(this.#openers[0] ?? null);
@@ -532,14 +568,20 @@ var FocusTrap = class _FocusTrap {
532
568
  this.#returnTabindexes();
533
569
  return;
534
570
  }
571
+ const entry = this.#entry;
572
+ const openers = this.#openers;
573
+ this.#openers = [];
535
574
  this.#activeState = false;
536
575
  this.#escapeLayer.deactivate();
537
- const above = this.#leaveStack();
538
- this.#unlockScroll();
576
+ const above = this.#leaveStack(entry);
577
+ this.#unlockScroll(entry);
539
578
  _FocusTrap.#reconcileBackground(document);
540
- const wayBack = this.#handOver(above);
541
- if (restoreFocus) this.#restore(wayBack, above);
542
- this.#returnTabindexes();
579
+ try {
580
+ const wayBack = this.#handOver(entry, openers, above);
581
+ if (restoreFocus) this.#restore(wayBack, above);
582
+ } finally {
583
+ this.#returnTabindexes();
584
+ }
543
585
  }
544
586
  connect() {
545
587
  this.#gate.cancel();
@@ -578,9 +620,9 @@ var FocusTrap = class _FocusTrap {
578
620
  }
579
621
  registry.stack.push(this.#entry);
580
622
  }
581
- #leaveStack() {
623
+ #leaveStack(entry) {
582
624
  const registry = _FocusTrap.#registry(document);
583
- const index = registry.stack.indexOf(this.#entry);
625
+ const index = registry.stack.indexOf(entry);
584
626
  const above = registry.stack.slice(index + 1);
585
627
  registry.stack.splice(index, 1);
586
628
  if (registry.stack.length === 0) {
@@ -604,21 +646,23 @@ var FocusTrap = class _FocusTrap {
604
646
  }
605
647
  return null;
606
648
  }
607
- #handOver(above) {
608
- const entry = this.#entry;
609
- const wayBack = [
610
- ...this.#openers,
611
- this.#options.fallbackFocus?.() ?? null,
612
- ...entry.handedBack
613
- ].filter((element) => element !== null);
614
- for (const trap of above) {
615
- if (trap.openedFrom !== entry) continue;
616
- trap.handedBack = [...trap.handedBack, ...wayBack];
617
- trap.openedFrom = entry.openedFrom;
618
- }
619
- this.#openers = [];
649
+ #handOver(entry, openers, above) {
650
+ const wayBack = [...openers];
651
+ const parent = entry.openedFrom;
652
+ const handedBack = entry.handedBack;
620
653
  entry.openedFrom = null;
621
654
  entry.handedBack = [];
655
+ try {
656
+ const fallback = this.#options.fallbackFocus?.();
657
+ if (fallback) wayBack.push(fallback);
658
+ } finally {
659
+ wayBack.push(...handedBack);
660
+ for (const trap of above) {
661
+ if (trap.openedFrom !== entry) continue;
662
+ trap.handedBack = [...trap.handedBack, ...wayBack];
663
+ trap.openedFrom = parent;
664
+ }
665
+ }
622
666
  return wayBack;
623
667
  }
624
668
  #restore(wayBack, above) {
@@ -668,11 +712,11 @@ var FocusTrap = class _FocusTrap {
668
712
  this.#entry.lockedBody = body;
669
713
  body.style.overflow = "hidden";
670
714
  }
671
- #unlockScroll() {
715
+ #unlockScroll(entry) {
672
716
  const registry = _FocusTrap.#registry(document);
673
- const body = this.#entry.lockedBody;
674
- this.#entry.lockedBody = null;
675
- if (!registry.scrollLocks.delete(this.#entry) || !body) return;
717
+ const body = entry.lockedBody;
718
+ entry.lockedBody = null;
719
+ if (!registry.scrollLocks.delete(entry) || !body) return;
676
720
  if (!_FocusTrap.#isLocked(registry, body)) _FocusTrap.#unlock(body);
677
721
  }
678
722
  static #isLocked(registry, body) {
@@ -1,13 +1,14 @@
1
1
  import { Controller } from '@hotwired/stimulus';
2
2
 
3
+ var afterFocus = /* @__PURE__ */ Symbol.for("stimeo--announcer:after-focus");
3
4
  function announce(message, options = {}) {
4
5
  const text = message.trim();
5
6
  if (text.length === 0) return;
6
- window.dispatchEvent(
7
- new CustomEvent("stimeo--announcer:announce", {
8
- detail: { message: text, assertive: options.assertive === true }
9
- })
10
- );
7
+ const event = new CustomEvent("stimeo--announcer:announce", {
8
+ detail: { message: text, assertive: options.assertive === true }
9
+ });
10
+ if (options.afterFocus) event[afterFocus] = true;
11
+ window.dispatchEvent(event);
11
12
  }
12
13
 
13
14
  function sharedRegistry(key, create) {
@@ -6,11 +6,16 @@ function setDefaultAttribute(element, name, value) {
6
6
  return true;
7
7
  }
8
8
 
9
+ var HTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
9
10
  function inheritsFieldsetDisabled(control) {
10
11
  let fieldset = control.closest("fieldset[disabled]");
11
12
  while (fieldset) {
12
- const legend = Array.from(fieldset.children).find((child) => child.tagName === "LEGEND");
13
- if (!legend?.contains(control)) return true;
13
+ if (fieldset.namespaceURI === HTML_NAMESPACE && fieldset.localName === "fieldset") {
14
+ const legend = Array.from(fieldset.children).find(
15
+ (child) => child.namespaceURI === HTML_NAMESPACE && child.localName === "legend"
16
+ );
17
+ if (!legend?.contains(control)) return true;
18
+ }
14
19
  fieldset = fieldset.parentElement?.closest("fieldset[disabled]") ?? null;
15
20
  }
16
21
  return false;
@@ -68,11 +73,10 @@ function hasNativeTabStop(element) {
68
73
  if (element.tagName === "AUDIO" || element.tagName === "VIDEO") {
69
74
  return element.hasAttribute("controls");
70
75
  }
71
- if (element instanceof HTMLElement && element.tagName === "SUMMARY") {
72
- const details = element.parentElement;
73
- return details instanceof HTMLDetailsElement && Array.from(details.children).find((child) => child.tagName === "SUMMARY") === element;
74
- }
75
- return false;
76
+ const details = element.parentElement;
77
+ return details instanceof HTMLDetailsElement && Array.from(details.children).find(
78
+ (child) => child.namespaceURI === HTML_NAMESPACE && child.localName === "summary"
79
+ ) === element;
76
80
  }
77
81
  function hasEditableTabStop(element) {
78
82
  const value = element.getAttribute("contenteditable")?.toLowerCase();
@@ -7,14 +7,15 @@ function fillTemplate(template, values) {
7
7
  });
8
8
  }
9
9
 
10
+ var afterFocus = /* @__PURE__ */ Symbol.for("stimeo--announcer:after-focus");
10
11
  function announce(message, options = {}) {
11
12
  const text = message.trim();
12
13
  if (text.length === 0) return;
13
- window.dispatchEvent(
14
- new CustomEvent("stimeo--announcer:announce", {
15
- detail: { message: text, assertive: options.assertive === true }
16
- })
17
- );
14
+ const event = new CustomEvent("stimeo--announcer:announce", {
15
+ detail: { message: text, assertive: options.assertive === true }
16
+ });
17
+ if (options.afterFocus) event[afterFocus] = true;
18
+ window.dispatchEvent(event);
18
19
  }
19
20
 
20
21
  function setAttributeIfChanged(element, name, value, beforeWrite) {
@@ -220,7 +221,7 @@ var ListenerSet = class {
220
221
 
221
222
  var MinDurationFloor = class {
222
223
  #timers;
223
- #timerId = null;
224
+ #task = null;
224
225
  #since = 0;
225
226
  constructor(timers) {
226
227
  this.#timers = timers;
@@ -229,25 +230,19 @@ var MinDurationFloor = class {
229
230
  this.#since = Date.now();
230
231
  }
231
232
  get pending() {
232
- return this.#timerId !== null;
233
+ return this.#task?.pending ?? false;
233
234
  }
234
235
  schedule(minDuration, finish) {
235
236
  this.cancel();
236
237
  const remaining = minDuration - (Date.now() - this.#since);
237
238
  if (remaining > 0) {
238
- this.#timerId = this.#timers.set(() => {
239
- this.#timerId = null;
240
- finish();
241
- }, remaining);
239
+ this.#task = this.#timers.setTask(finish, remaining);
242
240
  } else {
243
241
  finish();
244
242
  }
245
243
  }
246
244
  cancel() {
247
- if (this.#timerId !== null) {
248
- this.#timers.clear(this.#timerId);
249
- this.#timerId = null;
250
- }
245
+ this.#task?.cancel();
251
246
  }
252
247
  };
253
248
 
@@ -314,6 +309,22 @@ var TimerRegistry = class {
314
309
  return this.ids.size;
315
310
  }
316
311
  };
312
+ var TimeoutRegistration = class {
313
+ #registration;
314
+ constructor(owner, id) {
315
+ this.#registration = { owner, id };
316
+ }
317
+ get pending() {
318
+ return this.#registration !== null;
319
+ }
320
+ cancel() {
321
+ const registration = this.#registration;
322
+ if (registration) registration.owner.clear(registration.id);
323
+ }
324
+ expire() {
325
+ this.#registration = null;
326
+ }
327
+ };
317
328
  var SafeTimeout = class extends TimerRegistry {
318
329
  set(callback, delay) {
319
330
  const id = this.schedule(() => {
@@ -330,6 +341,29 @@ var SafeTimeout = class extends TimerRegistry {
330
341
  window.clearTimeout(id);
331
342
  }
332
343
  };
344
+ var TaskTimeout = class extends SafeTimeout {
345
+ #tasks = /* @__PURE__ */ new Map();
346
+ setTask(callback, delay) {
347
+ const id = this.set(() => {
348
+ this.#tasks.delete(id);
349
+ registration.expire();
350
+ callback();
351
+ }, delay);
352
+ const registration = new TimeoutRegistration(this, id);
353
+ this.#tasks.set(id, registration);
354
+ return registration;
355
+ }
356
+ clear(id) {
357
+ this.#tasks.get(id)?.expire();
358
+ this.#tasks.delete(id);
359
+ super.clear(id);
360
+ }
361
+ clearAll() {
362
+ for (const registration of this.#tasks.values()) registration.expire();
363
+ this.#tasks.clear();
364
+ super.clearAll();
365
+ }
366
+ };
333
367
 
334
368
  var FrameLoadingController = class _FrameLoadingController extends Controller {
335
369
  #numbers = new NumberValueReader();
@@ -344,7 +378,7 @@ var FrameLoadingController = class _FrameLoadingController extends Controller {
344
378
  minDuration: NUMBER_BOUNDS.timer
345
379
  };
346
380
  static events = ["start", "end", "reconcile"];
347
- #timeouts = new SafeTimeout();
381
+ #timeouts = new TaskTimeout();
348
382
  #floor = new MinDurationFloor(this.#timeouts);
349
383
  #gate = new DetachGate();
350
384
  #listeners = new ListenerSet();
@@ -160,6 +160,7 @@ var EscapeLayer = class _EscapeLayer {
160
160
  );
161
161
  #active = null;
162
162
  activate(ownerDocument = document, options) {
163
+ const entry = { onDismiss: options.onDismiss, claims: options.claims ?? null };
163
164
  this.deactivate();
164
165
  let registry2 = _EscapeLayer.#registries.get(ownerDocument);
165
166
  if (!registry2) {
@@ -167,7 +168,6 @@ var EscapeLayer = class _EscapeLayer {
167
168
  _EscapeLayer.#registries.set(ownerDocument, registry2);
168
169
  ownerDocument.addEventListener("keydown", registry2.onKeydown);
169
170
  }
170
- const entry = { onDismiss: options.onDismiss, claims: options.claims ?? null };
171
171
  registry2.stack.push(entry);
172
172
  this.#active = { document: ownerDocument, entry };
173
173
  }
@@ -10,7 +10,50 @@ function isCompositionInputType(inputType) {
10
10
  return inputType !== void 0 && COMPOSITION_INPUT_TYPES.has(inputType);
11
11
  }
12
12
  var BEFORE_MORPH_ATTRIBUTE = "turbo:before-morph-attribute";
13
- var textOf = (element) => element.value;
13
+ var textOf = (element) => {
14
+ if ("value" in element) return element.value;
15
+ return element instanceof Element ? element.textContent : void 0;
16
+ };
17
+ var nodesOf = (element) => {
18
+ const nodes = [];
19
+ if ("value" in element || !(element instanceof Element)) return nodes;
20
+ const walker = element.ownerDocument.createTreeWalker(element);
21
+ for (let node = walker.nextNode(); node !== null; node = walker.nextNode()) nodes.push(node);
22
+ return nodes;
23
+ };
24
+ var folded = (text) => text.replaceAll("\xA0", " ");
25
+ var startOf = (node, data, replaced, caret) => {
26
+ const text = folded(node.data);
27
+ const composed = folded(data);
28
+ if (replaced?.startContainer === node && text.startsWith(composed, replaced.startOffset)) {
29
+ return replaced.startOffset;
30
+ }
31
+ const start = text.lastIndexOf(composed, caret);
32
+ return start !== -1 && start + composed.length >= caret ? start : -1;
33
+ };
34
+ var spotOf = (element, replaced, data) => {
35
+ if ("value" in element || !(element instanceof Element)) return null;
36
+ const selection = element.ownerDocument.getSelection();
37
+ const node = selection?.focusNode;
38
+ if (!(node instanceof Text) || node.data === "" || !element.contains(node)) return null;
39
+ const range = element.ownerDocument.createRange();
40
+ const start = typeof data === "string" && data !== "" ? startOf(node, data, replaced, selection?.focusOffset ?? 0) : -1;
41
+ if (start === -1) {
42
+ range.selectNodeContents(node);
43
+ } else {
44
+ range.setStart(node, start);
45
+ range.setEnd(node, start + data.length);
46
+ }
47
+ return range;
48
+ };
49
+ var replacedBy = (event) => typeof event.getTargetRanges === "function" ? event.getTargetRanges()[0] ?? null : null;
50
+ var rewritten = ({ element, text, nodes, spot }) => {
51
+ if (spot !== null) {
52
+ return spot.collapsed || !(element instanceof Node && element.contains(spot.startContainer));
53
+ }
54
+ const current = nodesOf(element);
55
+ return textOf(element) !== text || current.length !== nodes.length || current.some((node, index) => node !== nodes[index]);
56
+ };
14
57
  var holdsFocus = (element) => element instanceof Node && element.getRootNode().activeElement === element;
15
58
  var CompositionTracker = class {
16
59
  #observedTargets = /* @__PURE__ */ new Set();
@@ -101,11 +144,11 @@ var CompositionTracker = class {
101
144
  }
102
145
  #settle() {
103
146
  const composition = this.#composition;
104
- if (composition === null) return;
105
- const { target, element, text } = composition;
147
+ if (composition === null || !this.#observedTargets.has(composition.target)) return;
148
+ const { target, element } = composition;
106
149
  if (target instanceof Node && element instanceof Node && !target.contains(element)) {
107
150
  this.#composition = null;
108
- } else if (textOf(element) !== text) {
151
+ } else if (rewritten(composition)) {
109
152
  this.#abandon(false);
110
153
  }
111
154
  }
@@ -130,7 +173,14 @@ var CompositionTracker = class {
130
173
  this.#owe(true);
131
174
  const target = event.currentTarget;
132
175
  const element = event.target;
133
- this.#composition = { target, element, text: textOf(element) };
176
+ this.#composition = {
177
+ target,
178
+ element,
179
+ text: textOf(element),
180
+ nodes: nodesOf(element),
181
+ spot: spotOf(element, null, void 0),
182
+ replaced: null
183
+ };
134
184
  this.#runAbandon();
135
185
  this.#onStart?.(event);
136
186
  };
@@ -154,6 +204,10 @@ var CompositionTracker = class {
154
204
  if (this.#composition !== null && !keyComposing && !isComposing && inputType && !isCompositionInputType(inputType)) {
155
205
  this.#abandon(true);
156
206
  }
207
+ const composition = this.#composition;
208
+ if (composition !== null && event.target === composition.element) {
209
+ composition.replaced = replacedBy(event);
210
+ }
157
211
  this.#runAbandon();
158
212
  };
159
213
  #handleInput = (event) => {
@@ -161,6 +215,15 @@ var CompositionTracker = class {
161
215
  const { isTrusted, isComposing, inputType } = event;
162
216
  if (composition !== null && event.target === composition.element && (isTrusted || isComposing || isCompositionInputType(inputType))) {
163
217
  composition.text = textOf(composition.element);
218
+ composition.nodes = nodesOf(composition.element);
219
+ if (isCompositionInputType(inputType)) {
220
+ composition.spot = spotOf(
221
+ composition.element,
222
+ composition.replaced,
223
+ event.data
224
+ );
225
+ }
226
+ composition.replaced = null;
164
227
  } else {
165
228
  this.#settle();
166
229
  }
@@ -181,7 +244,9 @@ var CompositionTracker = class {
181
244
  const composition = this.#composition;
182
245
  if (composition?.target !== event.currentTarget || event.target !== composition.element) return;
183
246
  const detail = event.detail;
184
- if (detail?.attributeName === "value") event.preventDefault();
247
+ if (detail?.attributeName === "value" && "value" in composition.element) {
248
+ event.preventDefault();
249
+ }
185
250
  };
186
251
  };
187
252
 
@@ -245,11 +310,16 @@ var DetachGate = class _DetachGate {
245
310
  }
246
311
  };
247
312
 
313
+ var HTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
248
314
  function inheritsFieldsetDisabled(control) {
249
315
  let fieldset = control.closest("fieldset[disabled]");
250
316
  while (fieldset) {
251
- const legend = Array.from(fieldset.children).find((child) => child.tagName === "LEGEND");
252
- if (!legend?.contains(control)) return true;
317
+ if (fieldset.namespaceURI === HTML_NAMESPACE && fieldset.localName === "fieldset") {
318
+ const legend = Array.from(fieldset.children).find(
319
+ (child) => child.namespaceURI === HTML_NAMESPACE && child.localName === "legend"
320
+ );
321
+ if (!legend?.contains(control)) return true;
322
+ }
253
323
  fieldset = fieldset.parentElement?.closest("fieldset[disabled]") ?? null;
254
324
  }
255
325
  return false;
@@ -524,7 +594,8 @@ function applyMask(value, pattern, tokens) {
524
594
  } else {
525
595
  masked += patternChar;
526
596
  tokenFlags.push(false);
527
- if (halfWidthChar(value[valueIndex] ?? "") === halfWidthChar(patternChar)) valueIndex += 1;
597
+ const literal = value.slice(valueIndex, valueIndex + patternChar.length);
598
+ if (halfWidthChar(literal) === halfWidthChar(patternChar)) valueIndex += patternChar.length;
528
599
  }
529
600
  }
530
601
  return {
@@ -560,14 +631,16 @@ function deletionOf(event) {
560
631
  if (!inputType.startsWith("delete")) return null;
561
632
  return inputType.endsWith("Forward") ? "forward" : "backward";
562
633
  }
563
- function tokenCharOffset(tokenFlags, n) {
564
- let seen = 0;
565
- for (let i = 0; i < tokenFlags.length; i += 1) {
566
- if (!tokenFlags[i]) continue;
567
- seen += 1;
568
- if (seen === n) return i;
569
- }
570
- return -1;
634
+ function tokenBoundary(pattern, tokenFlags, tokensBefore) {
635
+ const characters = pattern[Symbol.iterator]();
636
+ let offset = 0;
637
+ for (const token of tokenFlags) {
638
+ if (token && tokensBefore === 0) break;
639
+ const character = characters.next().value ?? "";
640
+ if (token) tokensBefore -= 1;
641
+ offset += token ? 1 : character.length;
642
+ }
643
+ return offset;
571
644
  }
572
645
  var InputMaskController = class extends Controller {
573
646
  get #unmaskAttribute() {
@@ -691,8 +764,8 @@ var InputMaskController = class extends Controller {
691
764
  let result = applyMask(raw, this.patternValue, this.#tokens);
692
765
  if (deletion !== null && result.masked === this.#lastValue) {
693
766
  const target = deletion === "backward" ? anchor : anchor + 1;
694
- const offset = tokenCharOffset(result.tokenFlags, target);
695
- if (offset >= 0) {
767
+ const offset = tokenBoundary(this.patternValue, result.tokenFlags, target - 1);
768
+ if (offset < result.masked.length) {
696
769
  anchor = target - 1;
697
770
  result = applyMask(
698
771
  result.masked.slice(0, offset) + result.masked.slice(offset + 1),
@@ -708,7 +781,7 @@ var InputMaskController = class extends Controller {
708
781
  }
709
782
  if (input.value !== result.masked) {
710
783
  input.value = result.masked;
711
- this.#restoreCaret(result.tokenFlags, anchor);
784
+ this.#restoreCaret(tokenBoundary(this.patternValue, result.tokenFlags, anchor));
712
785
  }
713
786
  this.#publish(result, result.masked);
714
787
  if (result.masked === this.#lastValue) return;
@@ -770,22 +843,7 @@ var InputMaskController = class extends Controller {
770
843
  #significantBefore(prefix) {
771
844
  return applyMask(prefix, this.patternValue, this.#tokens).unmasked.length;
772
845
  }
773
- #restoreCaret(tokenFlags, n) {
774
- let position;
775
- if (n <= 0) {
776
- let i = 0;
777
- while (i < tokenFlags.length && !tokenFlags[i]) i += 1;
778
- position = i;
779
- } else {
780
- const offset = tokenCharOffset(tokenFlags, n);
781
- if (offset < 0) {
782
- position = tokenFlags.length;
783
- } else {
784
- let j = offset + 1;
785
- while (j < tokenFlags.length && !tokenFlags[j]) j += 1;
786
- position = j;
787
- }
788
- }
846
+ #restoreCaret(position) {
789
847
  try {
790
848
  this.element.setSelectionRange(position, position);
791
849
  } catch {