dommy 0.16.0 → 0.17.0

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 (122) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dommy/animation.rb +1 -0
  3. data/lib/dommy/backend.rb +3 -0
  4. data/lib/dommy/blob.rb +6 -0
  5. data/lib/dommy/browser.rb +12 -4
  6. data/lib/dommy/character_data.rb +1 -13
  7. data/lib/dommy/cookie_store.rb +2 -1
  8. data/lib/dommy/document.rb +67 -30
  9. data/lib/dommy/element.rb +11 -0
  10. data/lib/dommy/element_internals.rb +12 -0
  11. data/lib/dommy/encodings/legacy_multibyte.rb +800 -0
  12. data/lib/dommy/encodings/multibyte_overrides.rb +76 -0
  13. data/lib/dommy/encodings/prescan.rb +234 -0
  14. data/lib/dommy/encodings.rb +121 -57
  15. data/lib/dommy/event.rb +107 -54
  16. data/lib/dommy/event_source.rb +1 -0
  17. data/lib/dommy/fetch.rb +244 -53
  18. data/lib/dommy/file_reader.rb +1 -0
  19. data/lib/dommy/form_data.rb +22 -9
  20. data/lib/dommy/form_entry_list.rb +3 -3
  21. data/lib/dommy/html_canvas_element.rb +85 -12
  22. data/lib/dommy/html_elements/base.rb +1 -0
  23. data/lib/dommy/html_elements/forms/input.rb +3 -0
  24. data/lib/dommy/html_elements/interactive.rb +56 -48
  25. data/lib/dommy/html_elements/metadata.rb +23 -0
  26. data/lib/dommy/interaction/form_submission.rb +24 -42
  27. data/lib/dommy/internal/accessibility_tree.rb +149 -162
  28. data/lib/dommy/internal/accessibility_visibility.rb +16 -17
  29. data/lib/dommy/internal/accessible_name.rb +231 -246
  30. data/lib/dommy/internal/aria_role.rb +182 -195
  31. data/lib/dommy/internal/aria_snapshot.rb +35 -38
  32. data/lib/dommy/internal/aria_state.rb +102 -118
  33. data/lib/dommy/internal/autofill.rb +75 -75
  34. data/lib/dommy/internal/backend_prefilter.rb +127 -130
  35. data/lib/dommy/internal/child_navigable.rb +86 -83
  36. data/lib/dommy/internal/close_watcher_manager.rb +1 -0
  37. data/lib/dommy/internal/css/ancestor_filter.rb +64 -64
  38. data/lib/dommy/internal/css/calc.rb +90 -71
  39. data/lib/dommy/internal/css/cascade.rb +69 -67
  40. data/lib/dommy/internal/css/color.rb +249 -187
  41. data/lib/dommy/internal/css/computed_style_builder.rb +34 -0
  42. data/lib/dommy/internal/css/computed_style_declaration.rb +50 -11
  43. data/lib/dommy/internal/css/counters.rb +67 -67
  44. data/lib/dommy/internal/css/custom_properties.rb +114 -114
  45. data/lib/dommy/internal/css/parser.rb +277 -239
  46. data/lib/dommy/internal/css/property_registry.rb +321 -302
  47. data/lib/dommy/internal/css/renderability.rb +37 -35
  48. data/lib/dommy/internal/css/rule_index.rb +2 -2
  49. data/lib/dommy/internal/css/rule_index_inputs.rb +2 -2
  50. data/lib/dommy/internal/css/supports.rb +115 -113
  51. data/lib/dommy/internal/css/ua_stylesheet.rb +55 -53
  52. data/lib/dommy/internal/css_rule_text.rb +121 -119
  53. data/lib/dommy/internal/css_source.rb +1 -3
  54. data/lib/dommy/internal/css_syntax.rb +157 -92
  55. data/lib/dommy/internal/declarative_shadow_roots.rb +1 -0
  56. data/lib/dommy/internal/directionality.rb +120 -123
  57. data/lib/dommy/internal/document_live_ranges.rb +26 -1
  58. data/lib/dommy/internal/dom_matching.rb +185 -184
  59. data/lib/dommy/internal/element_geometry.rb +8 -1
  60. data/lib/dommy/internal/element_matching.rb +71 -69
  61. data/lib/dommy/internal/element_shadow.rb +1 -13
  62. data/lib/dommy/internal/element_state.rb +255 -265
  63. data/lib/dommy/internal/event_handlers.rb +66 -64
  64. data/lib/dommy/internal/exception_report.rb +88 -86
  65. data/lib/dommy/internal/focusability.rb +205 -205
  66. data/lib/dommy/internal/global_functions.rb +49 -47
  67. data/lib/dommy/internal/idl_reflection.rb +99 -97
  68. data/lib/dommy/internal/js_number.rb +35 -33
  69. data/lib/dommy/internal/light_dismiss.rb +130 -132
  70. data/lib/dommy/internal/literal_lookup.rb +75 -75
  71. data/lib/dommy/internal/mime_type.rb +54 -49
  72. data/lib/dommy/internal/mutation_coordinator.rb +46 -0
  73. data/lib/dommy/internal/mutation_observer_microtask.rb +60 -0
  74. data/lib/dommy/internal/namespaces.rb +51 -49
  75. data/lib/dommy/internal/node_adopter.rb +25 -1
  76. data/lib/dommy/internal/node_equality.rb +61 -59
  77. data/lib/dommy/internal/post_insertion_steps.rb +1 -0
  78. data/lib/dommy/internal/referrer.rb +144 -0
  79. data/lib/dommy/internal/rendered_text/collector.rb +22 -3
  80. data/lib/dommy/internal/rendered_text/computed_box.rb +3 -2
  81. data/lib/dommy/internal/rendered_text/fragment.rb +37 -38
  82. data/lib/dommy/internal/rendered_text/items.rb +7 -5
  83. data/lib/dommy/internal/selector_matcher.rb +361 -359
  84. data/lib/dommy/internal/selector_parser.rb +74 -62
  85. data/lib/dommy/internal/sequential_focus_navigation.rb +150 -151
  86. data/lib/dommy/internal/shadow_root_registry.rb +7 -0
  87. data/lib/dommy/internal/slot_assignment.rb +117 -0
  88. data/lib/dommy/internal/slot_signals.rb +125 -0
  89. data/lib/dommy/internal/unsafe_html.rb +79 -77
  90. data/lib/dommy/internal/url_parser.rb +5 -29
  91. data/lib/dommy/internal/web_idl.rb +158 -153
  92. data/lib/dommy/internal/window_bound.rb +14 -0
  93. data/lib/dommy/internal/window_constructors.rb +3 -0
  94. data/lib/dommy/internal/xml_serialization.rb +298 -296
  95. data/lib/dommy/js/dom_interfaces.rb +13 -1
  96. data/lib/dommy/js/host_bridge.rb +23 -1
  97. data/lib/dommy/js/host_callbacks.rb +7 -0
  98. data/lib/dommy/js/host_runtime.js +625 -61
  99. data/lib/dommy/js/import_map.rb +1 -1
  100. data/lib/dommy/js/observable_runtime.js +295 -128
  101. data/lib/dommy/js/webidl_members.js +1 -1
  102. data/lib/dommy/js/webidl_signatures.js +35 -6
  103. data/lib/dommy/media_query_list.rb +1 -0
  104. data/lib/dommy/message_channel.rb +2 -0
  105. data/lib/dommy/mutation_observer.rb +25 -29
  106. data/lib/dommy/navigator.rb +4 -1
  107. data/lib/dommy/node.rb +4 -0
  108. data/lib/dommy/notification.rb +1 -0
  109. data/lib/dommy/promise.rb +12 -0
  110. data/lib/dommy/range.rb +20 -3
  111. data/lib/dommy/shadow_root.rb +25 -1
  112. data/lib/dommy/streams.rb +18 -0
  113. data/lib/dommy/style_declaration.rb +15 -4
  114. data/lib/dommy/text_codec.rb +7 -5
  115. data/lib/dommy/tree_walker.rb +68 -61
  116. data/lib/dommy/version.rb +1 -1
  117. data/lib/dommy/web_socket.rb +1 -0
  118. data/lib/dommy/window.rb +22 -4
  119. data/lib/dommy/worker.rb +1 -0
  120. data/lib/dommy/xml_http_request.rb +12 -2
  121. data/lib/dommy.rb +17 -2
  122. metadata +11 -3
@@ -6,7 +6,8 @@
6
6
  // values before the member sees them. An operation, static operation or
7
7
  // constructor has `required`, the number of arguments a call must pass
8
8
  // (fewer is a TypeError), and `arguments`, one string conversion per
9
- // position; a writable attribute has its value's string conversion. A
9
+ // position; a writable or [PutForwards] attribute has its value's string
10
+ // conversion (for [PutForwards], the forwarded-to attribute's). A
10
11
  // conversion is a string type ("DOMString", "USVString", "ByteString"),
11
12
  // "?" after it when nullable, "[LegacyNullToEmptyString] " before it when
12
13
  // null means "", "optional " before it when undefined is left for the
@@ -73,12 +74,16 @@ globalThis.__rbIdlSignatures = {
73
74
  }
74
75
  },
75
76
  "CSSImportRule": {
76
- "inherits": "CSSRule"
77
+ "inherits": "CSSRule",
78
+ "attributes": {
79
+ "media": "[LegacyNullToEmptyString] DOMString"
80
+ }
77
81
  },
78
82
  "CSSKeyframeRule": {
79
83
  "inherits": "CSSRule",
80
84
  "attributes": {
81
- "keyText": "DOMString"
85
+ "keyText": "DOMString",
86
+ "style": "DOMString"
82
87
  }
83
88
  },
84
89
  "CSSKeyframesRule": {
@@ -93,7 +98,10 @@ globalThis.__rbIdlSignatures = {
93
98
  }
94
99
  },
95
100
  "CSSMarginRule": {
96
- "inherits": "CSSRule"
101
+ "inherits": "CSSRule",
102
+ "attributes": {
103
+ "style": "DOMString"
104
+ }
97
105
  },
98
106
  "CSSNamespaceRule": {
99
107
  "inherits": "CSSRule"
@@ -120,7 +128,8 @@ globalThis.__rbIdlSignatures = {
120
128
  "CSSPageRule": {
121
129
  "inherits": "CSSGroupingRule",
122
130
  "attributes": {
123
- "selectorText": "DOMString"
131
+ "selectorText": "DOMString",
132
+ "style": "DOMString"
124
133
  }
125
134
  },
126
135
  "CSSRule": {
@@ -154,7 +163,8 @@ globalThis.__rbIdlSignatures = {
154
163
  "CSSStyleRule": {
155
164
  "inherits": "CSSGroupingRule",
156
165
  "attributes": {
157
- "selectorText": "DOMString"
166
+ "selectorText": "DOMString",
167
+ "style": "DOMString"
158
168
  }
159
169
  },
160
170
  "CSSStyleSheet": {
@@ -391,6 +401,7 @@ globalThis.__rbIdlSignatures = {
391
401
  "domain": "USVString",
392
402
  "fgColor": "[LegacyNullToEmptyString] DOMString",
393
403
  "linkColor": "[LegacyNullToEmptyString] DOMString",
404
+ "location": "USVString",
394
405
  "title": "DOMString",
395
406
  "vlinkColor": "[LegacyNullToEmptyString] DOMString"
396
407
  }
@@ -495,6 +506,7 @@ globalThis.__rbIdlSignatures = {
495
506
  "ariaValueMin": "DOMString?",
496
507
  "ariaValueNow": "DOMString?",
497
508
  "ariaValueText": "DOMString?",
509
+ "classList": "DOMString",
498
510
  "className": "DOMString",
499
511
  "id": "DOMString",
500
512
  "innerHTML": "[LegacyNullToEmptyString] DOMString",
@@ -650,6 +662,7 @@ globalThis.__rbIdlSignatures = {
650
662
  "protocol": "USVString",
651
663
  "referrerPolicy": "DOMString",
652
664
  "rel": "DOMString",
665
+ "relList": "DOMString",
653
666
  "rev": "DOMString",
654
667
  "search": "USVString",
655
668
  "shape": "DOMString",
@@ -677,6 +690,7 @@ globalThis.__rbIdlSignatures = {
677
690
  "protocol": "USVString",
678
691
  "referrerPolicy": "DOMString",
679
692
  "rel": "DOMString",
693
+ "relList": "DOMString",
680
694
  "search": "USVString",
681
695
  "shape": "DOMString",
682
696
  "target": "DOMString",
@@ -794,6 +808,7 @@ globalThis.__rbIdlSignatures = {
794
808
  "nonce": "DOMString",
795
809
  "outerText": "[LegacyNullToEmptyString] DOMString",
796
810
  "popover": "DOMString?",
811
+ "style": "DOMString",
797
812
  "title": "DOMString",
798
813
  "writingSuggestions": "DOMString"
799
814
  }
@@ -843,6 +858,7 @@ globalThis.__rbIdlSignatures = {
843
858
  "method": "DOMString",
844
859
  "name": "DOMString",
845
860
  "rel": "DOMString",
861
+ "relList": "DOMString",
846
862
  "target": "DOMString"
847
863
  }
848
864
  },
@@ -902,6 +918,7 @@ globalThis.__rbIdlSignatures = {
902
918
  "marginWidth": "[LegacyNullToEmptyString] DOMString",
903
919
  "name": "DOMString",
904
920
  "referrerPolicy": "DOMString",
921
+ "sandbox": "DOMString",
905
922
  "scrolling": "DOMString",
906
923
  "src": "USVString",
907
924
  "srcdoc": "DOMString",
@@ -983,6 +1000,7 @@ globalThis.__rbIdlSignatures = {
983
1000
  "inherits": "HTMLElement",
984
1001
  "attributes": {
985
1002
  "as": "DOMString",
1003
+ "blocking": "DOMString",
986
1004
  "charset": "DOMString",
987
1005
  "crossOrigin": "DOMString?",
988
1006
  "fetchPriority": "DOMString",
@@ -994,7 +1012,9 @@ globalThis.__rbIdlSignatures = {
994
1012
  "media": "DOMString",
995
1013
  "referrerPolicy": "DOMString",
996
1014
  "rel": "DOMString",
1015
+ "relList": "DOMString",
997
1016
  "rev": "DOMString",
1017
+ "sizes": "DOMString",
998
1018
  "target": "DOMString",
999
1019
  "type": "DOMString"
1000
1020
  }
@@ -1107,6 +1127,7 @@ globalThis.__rbIdlSignatures = {
1107
1127
  },
1108
1128
  "attributes": {
1109
1129
  "defaultValue": "DOMString",
1130
+ "htmlFor": "DOMString",
1110
1131
  "name": "DOMString",
1111
1132
  "value": "DOMString"
1112
1133
  }
@@ -1147,6 +1168,7 @@ globalThis.__rbIdlSignatures = {
1147
1168
  "supports": {"required":1,"arguments":["DOMString"]}
1148
1169
  },
1149
1170
  "attributes": {
1171
+ "blocking": "DOMString",
1150
1172
  "charset": "DOMString",
1151
1173
  "crossOrigin": "DOMString?",
1152
1174
  "event": "DOMString",
@@ -1198,6 +1220,7 @@ globalThis.__rbIdlSignatures = {
1198
1220
  "HTMLStyleElement": {
1199
1221
  "inherits": "HTMLElement",
1200
1222
  "attributes": {
1223
+ "blocking": "DOMString",
1201
1224
  "media": "DOMString",
1202
1225
  "type": "DOMString"
1203
1226
  }
@@ -1779,6 +1802,11 @@ globalThis.__rbIdlSignatures = {
1779
1802
  "initStorageEvent": {"required":1,"arguments":["DOMString",null,null,"optional DOMString?","optional DOMString?","optional DOMString?","optional USVString"]}
1780
1803
  }
1781
1804
  },
1805
+ "StyleSheet": {
1806
+ "attributes": {
1807
+ "media": "[LegacyNullToEmptyString] DOMString"
1808
+ }
1809
+ },
1782
1810
  "StyleSheetList": {
1783
1811
  "operations": {
1784
1812
  "item": {"required":1}
@@ -1963,6 +1991,7 @@ globalThis.__rbIdlSignatures = {
1963
1991
  "structuredClone": {"required":1}
1964
1992
  },
1965
1993
  "attributes": {
1994
+ "location": "USVString",
1966
1995
  "name": "DOMString",
1967
1996
  "status": "DOMString"
1968
1997
  }
@@ -17,6 +17,7 @@ module Dommy
17
17
  # Spec: https://drafts.csswg.org/cssom-view/#mediaquerylist
18
18
  class MediaQueryList
19
19
  include EventTarget
20
+ include Internal::WindowBound
20
21
  include Internal::EventHandlers::IdlAttributeBridge
21
22
 
22
23
  attr_reader :media
@@ -40,6 +40,7 @@ module Dommy
40
40
  # receiver gets (see #__internal_transfer__).
41
41
  class MessagePort
42
42
  include EventTarget
43
+ include Internal::WindowBound
43
44
  include Internal::EventHandlers::IdlAttributeBridge
44
45
 
45
46
  def initialize(window)
@@ -265,6 +266,7 @@ module Dommy
265
266
  # name within the same Window.
266
267
  class BroadcastChannel
267
268
  include EventTarget
269
+ include Internal::WindowBound
268
270
  include Internal::EventHandlers::IdlAttributeBridge
269
271
 
270
272
  @@registries = Hash.new { |h, w| h[w] = Hash.new { |c, n| c[n] = [] } }
@@ -76,7 +76,6 @@ module Dommy
76
76
  @callback = callback
77
77
  @observed = []
78
78
  @records = []
79
- @scheduled = false
80
79
  @registered_docs = []
81
80
  # Transient registered observers (WHATWG DOM): when a node is removed from
82
81
  # an observed subtree, its subtree keeps being observed until the next
@@ -228,10 +227,31 @@ module Dommy
228
227
 
229
228
  def enqueue(record)
230
229
  @records << record
231
- return nil if @scheduled
230
+ @window.__internal_mutation_observer_microtask__.observer_pending(self)
231
+ nil
232
+ end
233
+
234
+ # This observer's part of "notify mutation observers": its transient
235
+ # registered observers end, and its records, if it has any, go to its
236
+ # callback.
237
+ def __internal_notify__
238
+ @transients.clear
239
+ return if @records.empty?
232
240
 
233
- @scheduled = true
234
- @window.scheduler.queue_microtask(proc { flush })
241
+ records = @records.dup
242
+ @records.clear
243
+ # Per spec the callback receives (mutationRecords, observer) and is invoked
244
+ # with `this` set to the observer.
245
+ invoke_observer_callback(records)
246
+ rescue StandardError => e
247
+ # A throwing observer callback MUST NOT escape its dispatch — WHATWG says to
248
+ # report the exception and keep notifying the other observers, and a page's
249
+ # JS must not be derailed by it. The engine swallows this for callbacks
250
+ # (HostBridge#invoke_callback, raising:false), but that has proven
251
+ # engine/Ruby-version-dependent, so guard here too. Without this, one broken
252
+ # observer (e.g. an image lazy-loader that throws on a batch of <img>
253
+ # mutations) cascades into a blank / "something went wrong" page.
254
+ __dommy_dump_mo_failure__(records, e) if ENV["DOMMY_MO_DEBUG"]
235
255
  nil
236
256
  end
237
257
 
@@ -274,7 +294,6 @@ module Dommy
274
294
 
275
295
  def disconnect
276
296
  @records.clear
277
- @scheduled = false
278
297
  @observed.clear
279
298
  @registered_docs.each { |doc| doc.unregister_observer(self) }
280
299
  @registered_docs.clear
@@ -291,37 +310,14 @@ module Dommy
291
310
 
292
311
  # WHATWG takeRecords(): clone the record queue, empty it, return the clone.
293
312
  # It does NOT end the transient registered observers' lifetime — only the
294
- # microtask checkpoint ("notify mutation observers", `flush` below) does, so
313
+ # microtask checkpoint ("notify mutation observers", #__internal_notify__) does, so
295
314
  # a caller that drains records by hand keeps observing a removed subtree.
296
315
  def take_records
297
316
  out = @records.dup
298
317
  @records.clear
299
- @scheduled = false
300
318
  out
301
319
  end
302
320
 
303
- def flush
304
- @scheduled = false
305
- @transients.clear
306
- return if @records.empty?
307
-
308
- records = @records.dup
309
- @records.clear
310
- # Per spec the callback receives (mutationRecords, observer) and is invoked
311
- # with `this` set to the observer.
312
- invoke_observer_callback(records)
313
- rescue StandardError => e
314
- # A throwing observer callback MUST NOT escape its dispatch — WHATWG says to
315
- # report the exception and keep notifying the other observers, and a page's
316
- # JS must not be derailed by it. The engine swallows this for callbacks
317
- # (HostBridge#invoke_callback, raising:false), but that has proven
318
- # engine/Ruby-version-dependent, so guard here too. Without this, one broken
319
- # observer (e.g. an image lazy-loader that throws on a batch of <img>
320
- # mutations) cascades into a blank / "something went wrong" page.
321
- __dommy_dump_mo_failure__(records, e) if ENV["DOMMY_MO_DEBUG"]
322
- nil
323
- end
324
-
325
321
  def invoke_observer_callback(records)
326
322
  CallableInvoker.invoke(@callback, records, self, this: self)
327
323
  end
@@ -99,7 +99,7 @@ module Dommy
99
99
  # Dommy does not emit the request (no background egress); it just reports
100
100
  # success so callers that feature-detect `sendBeacon` take the beacon path.
101
101
  def send_beacon(url, _data = nil)
102
- raise Bridge::TypeError, "Invalid URL #{url.to_s.inspect}" if @window.__internal_parse_url__(url).nil?
102
+ raise Bridge::TypeError, "Invalid URL #{url.to_s.inspect}" if @window.__internal_parse_url__(url, utf8: true).nil?
103
103
 
104
104
  true
105
105
  end
@@ -286,6 +286,7 @@ module Dommy
286
286
  # PromiseValue so callers' `.await` chains keep working.
287
287
  class Clipboard
288
288
  include EventTarget
289
+ include Internal::WindowBound
289
290
 
290
291
  def initialize(window)
291
292
  @window = window
@@ -434,6 +435,7 @@ module Dommy
434
435
  # permission).
435
436
  class PermissionStatus
436
437
  include EventTarget
438
+ include Internal::WindowBound
437
439
 
438
440
  attr_reader :name, :state
439
441
 
@@ -611,6 +613,7 @@ module Dommy
611
613
 
612
614
  class WakeLockSentinel
613
615
  include EventTarget
616
+ include Internal::WindowBound
614
617
 
615
618
  attr_reader :type
616
619
 
data/lib/dommy/node.rb CHANGED
@@ -325,6 +325,10 @@ module Dommy
325
325
  Internal::NodeEquality.equal?(self, other)
326
326
  end
327
327
 
328
+ # The slot this slottable was last assign()ed to (DOM "manual slot
329
+ # assignment"), or nil.
330
+ attr_accessor :__internal_manual_slot__
331
+
328
332
  # WHATWG Node.ownerDocument — null when this is a document, this node's
329
333
  # node document otherwise. The IDL puts the attribute on Node, so every node
330
334
  # class needs it; only Element and Attr defined it before, which left
@@ -8,6 +8,7 @@ module Dommy
8
8
  # Spec: https://notifications.spec.whatwg.org/
9
9
  class Notification
10
10
  include EventTarget
11
+ include Internal::WindowBound
11
12
  include Internal::EventHandlers::IdlAttributeBridge
12
13
 
13
14
  @permission = "default"
data/lib/dommy/promise.rb CHANGED
@@ -100,6 +100,16 @@ module Dommy
100
100
  @state != :pending
101
101
  end
102
102
 
103
+ # Call `callback` as this promise settles (at once when it has), before any
104
+ # reaction runs: how a host algorithm drops what it no longer needs once
105
+ # the promise has its outcome.
106
+ def __internal_when_settled__(&callback)
107
+ return callback.call if settled?
108
+
109
+ (@settle_callbacks ||= []) << callback
110
+ nil
111
+ end
112
+
103
113
  # Call `on_fulfilled` / `on_rejected` with the outcome once settled (from a
104
114
  # microtask), without a chained promise: how a JS realm Promise standing
105
115
  # for this one learns its outcome.
@@ -187,6 +197,8 @@ module Dommy
187
197
 
188
198
  @state = state
189
199
  @value = value
200
+ @settle_callbacks&.each(&:call)
201
+ @settle_callbacks = nil
190
202
  schedule_flush
191
203
  self
192
204
  end
data/lib/dommy/range.rb CHANGED
@@ -536,9 +536,11 @@ module Dommy
536
536
 
537
537
  fragment = extract_contents
538
538
  # "Replace all" with null: newParent is emptied before it takes the
539
- # contents. Insert it first, then fill it — inserting a populated wrapper
540
- # would place its children relative to the wrong boundary.
541
- new_parent.child_nodes.to_a.each { |child| child.remove }
539
+ # contents, under ONE childList record naming every removed child (only
540
+ # a ParentNode can have any). Insert it first, then fill it — inserting a
541
+ # populated wrapper would place its children relative to the wrong
542
+ # boundary.
543
+ new_parent.__internal_replace_all__([]) if new_parent.is_a?(Internal::ParentNode)
542
544
  insert_node(new_parent)
543
545
  new_parent.append_child(fragment)
544
546
  select_node(new_parent)
@@ -564,6 +566,12 @@ module Dommy
564
566
  # Steps 2-5: what the node goes in front of, and whose child it becomes.
565
567
  reference = splitting ? start_node : child_at(start_node, @start_offset)
566
568
  parent = reference.nil? ? start_node : parent_of(reference)
569
+ # Pre-insert's first check: only a Document, DocumentFragment or Element
570
+ # takes children, so an Attr-rooted range has nowhere to put the node.
571
+ unless parent.respond_to?(:__internal_ensure_insertion_validity__)
572
+ raise DOMException::HierarchyRequestError, "the range's start node cannot have children"
573
+ end
574
+
567
575
  # Step 6, ahead of the split: an insertion pre-insert would reject must
568
576
  # not leave the Text node split behind it.
569
577
  ref_bn = reference&.__dommy_backend_node__
@@ -847,9 +855,18 @@ module Dommy
847
855
  end
848
856
 
849
857
  def boundaries_moved
858
+ track_in_document_of(@start_container)
850
859
  @selection&.__internal_range_moved__(self)
851
860
  end
852
861
 
862
+ # A range is live wherever its boundaries are: one set into another
863
+ # document's tree has to hear that document's mutations, which only its
864
+ # registry reports. (Registration is weak and idempotent.)
865
+ def track_in_document_of(node)
866
+ document = node.is_a?(Document) ? node : (node.owner_document if node.respond_to?(:owner_document))
867
+ document.__internal_register_range__(self) if document.respond_to?(:__internal_register_range__)
868
+ end
869
+
853
870
  # A Text node in the spec's sense, which includes a CDATASection.
854
871
  def text_node?(node)
855
872
  [3, 4].include?(node_type_of(node))
@@ -78,6 +78,13 @@ module Dommy
78
78
 
79
79
  def __dommy_backend_node__ = @__node__
80
80
 
81
+ # The first slot of each name in this tree, as name => backend node: what
82
+ # slotchange compares with when the tree's slots change
83
+ # (Internal::SlotSignals). Kept current wherever the tree's slots change
84
+ # without a mutation record: empty for a new root, set again after a
85
+ # declarative root is filled and after an adopt moves the tree.
86
+ attr_accessor :__internal_first_slots__
87
+
81
88
  def initialize(host, mode:, delegates_focus: false, slot_assignment: "named", clonable: false, serializable: false)
82
89
  @host = host
83
90
  @mode = mode.to_s
@@ -86,6 +93,7 @@ module Dommy
86
93
  @clonable = clonable ? true : false
87
94
  @serializable = serializable ? true : false
88
95
  @__internal_declarative__ = false
96
+ @__internal_first_slots__ = {}
89
97
  @document = host.document
90
98
  @__node__ = Parser.fragment("", owner_doc: @document.backend_doc)
91
99
  @document.__internal_register_shadow_fragment__(@__node__, self)
@@ -99,6 +107,12 @@ module Dommy
99
107
  raise DOMException::NotSupportedError, "a shadow root cannot be cloned"
100
108
  end
101
109
 
110
+ # DocumentOrShadowRoot's elementFromPoint: with no layout, no element is
111
+ # at any point (as for the document).
112
+ def element_from_point(_x, _y)
113
+ nil
114
+ end
115
+
102
116
  def inner_html
103
117
  return Internal::XmlSerialization.serialize_children_of(self) unless @document.html_document?
104
118
 
@@ -344,6 +358,12 @@ module Dommy
344
358
  11
345
359
  when "nodeName"
346
360
  "#document-fragment"
361
+ when "ownerDocument"
362
+ @document
363
+ when "isConnected"
364
+ is_connected?
365
+ when "baseURI"
366
+ @document.base_uri
347
367
  when "parentNode", "parentElement", "nextSibling", "previousSibling", "nodeValue"
348
368
  # A ShadowRoot is a DocumentFragment attached to its host, never
349
369
  # inserted as a child of it: no parent, no siblings, and a null
@@ -380,7 +400,7 @@ module Dommy
380
400
  querySelector querySelectorAll getElementById append prepend replaceChildren moveBefore appendChild
381
401
  insertBefore removeChild replaceChild
382
402
  getRootNode contains addEventListener removeEventListener dispatchEvent
383
- isEqualNode isSameNode hasChildNodes normalize compareDocumentPosition getHTML setHTMLUnsafe
403
+ isEqualNode isSameNode hasChildNodes normalize compareDocumentPosition getHTML setHTMLUnsafe cloneNode elementFromPoint
384
404
  ]
385
405
  def __js_call__(method, args)
386
406
  case method
@@ -395,6 +415,10 @@ module Dommy
395
415
  when "setHTMLUnsafe"
396
416
  set_html_unsafe(args[0], args[1])
397
417
  Bridge::UNDEFINED
418
+ when "cloneNode"
419
+ clone_node(args[0])
420
+ when "elementFromPoint"
421
+ element_from_point(args[0], args[1])
398
422
  when "isEqualNode"
399
423
  is_equal_node(args[0])
400
424
  when "isSameNode"
data/lib/dommy/streams.rb CHANGED
@@ -315,6 +315,7 @@ module Dommy
315
315
  @read_requests = []
316
316
  requests.each { |request| request.close_steps.call }
317
317
  @reader&.__internal_stream_closed__
318
+ run_done_callbacks
318
319
  end
319
320
 
320
321
  def __internal_error_stream__(reason)
@@ -326,6 +327,15 @@ module Dommy
326
327
  @read_requests = []
327
328
  requests.each { |request| request.error_steps.call(reason) }
328
329
  @reader&.__internal_stream_errored__(reason)
330
+ run_done_callbacks
331
+ end
332
+
333
+ # Run `block` once the stream stops being readable (closed or errored),
334
+ # at once if it already has.
335
+ def __internal_when_done__(&block)
336
+ return block.call unless @state == :readable
337
+
338
+ (@done_callbacks ||= []) << block
329
339
  end
330
340
 
331
341
  def __internal_release_reader__
@@ -354,6 +364,14 @@ module Dommy
354
364
 
355
365
  class Error < StandardError
356
366
  end
367
+
368
+ private
369
+
370
+ def run_done_callbacks
371
+ callbacks = @done_callbacks
372
+ @done_callbacks = nil
373
+ callbacks&.each(&:call)
374
+ end
357
375
  end
358
376
 
359
377
  # The controller an underlying source's `start` and `pull` receive: the
@@ -104,7 +104,7 @@ module Dommy
104
104
  # An unset CSS property reads as "" (per CSSStyleDeclaration), not nil —
105
105
  # `el.style.display` is "" until assigned, which `v-show` and other
106
106
  # display-toggling code compares against.
107
- properties[method_to_css_name(key)] || ""
107
+ properties[property_key(method_to_css_name(key))] || ""
108
108
  end
109
109
  end
110
110
  end
@@ -205,10 +205,9 @@ module Dommy
205
205
  s.match?(/\A[a-z][a-zA-Z0-9_]*=?\z/) && !s.start_with?("to_") && !NOT_PROPERTY_ACCESSORS.include?(s)
206
206
  end
207
207
 
208
+ # The property an attribute name (or its Ruby setter) stands for.
208
209
  def method_to_css_name(name)
209
- s = name.to_s.sub(/=\z/, "")
210
- # snake_case (Ruby idiomatic) → kebab; camelCase (JS idiomatic) → kebab.
211
- s.include?("_") ? s.tr("_", "-") : s.gsub(/[A-Z]/) { |m| "-#{m.downcase}" }
210
+ Internal::CSS::Parser.attribute_property_name(name.to_s.sub(/=\z/, ""))
212
211
  end
213
212
 
214
213
  # The declaration block as an ordered { property => [value, priority] } hash,
@@ -269,6 +268,18 @@ module Dommy
269
268
  @height = height
270
269
  end
271
270
 
271
+ # `new DOMRect(x, y, width, height)`, each an optional unrestricted
272
+ # double that defaults to 0.
273
+ #
274
+ # Spec: https://drafts.fxtf.org/geometry/#dom-domrect-domrect
275
+ def self.construct(args)
276
+ x, y, width, height = Array.new(4) do |i|
277
+ arg = args[i]
278
+ arg.equal?(Bridge::UNDEFINED) ? 0.0 : Internal::WebIDL.unrestricted_double(arg)
279
+ end
280
+ new(x: x, y: y, width: width, height: height)
281
+ end
282
+
272
283
  def top
273
284
  @y
274
285
  end
@@ -71,11 +71,17 @@ module Dommy
71
71
  # invalid sequence throws a TypeError; otherwise it is replaced with U+FFFD.
72
72
  # A leading byte-order mark is stripped unless `ignoreBOM` was set. With
73
73
  # `stream: true` a partial sequence at the end is kept for the next call.
74
+ # A call that does not continue a stream starts a new decoder — at its
75
+ # start, so one a fatal error left mid-sequence is not reused.
74
76
  #
75
77
  # Spec: https://encoding.spec.whatwg.org/#dom-textdecoder-decode
76
78
  def decode(input = nil, options = nil)
77
79
  stream = options.is_a?(Hash) && truthy?(options["stream"] || options[:stream])
78
- @decoder ||= Encodings.decoder_for(@name, fatal: @fatal)
80
+ unless @do_not_flush
81
+ @decoder = Encodings.decoder_for(@name, fatal: @fatal)
82
+ @bom_seen = false
83
+ end
84
+ @do_not_flush = stream
79
85
  code_points = @decoder.decode(extract_bytes(input), flush: !stream)
80
86
 
81
87
  # The byte-order mark is dropped at the code-point level, so one split
@@ -85,10 +91,6 @@ module Dommy
85
91
  @bom_seen = true
86
92
  end
87
93
 
88
- unless stream
89
- @decoder = nil
90
- @bom_seen = false
91
- end
92
94
  code_points.pack("U*")
93
95
  end
94
96