dommy 0.15.0 → 0.16.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 (171) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dommy/backend.rb +16 -15
  3. data/lib/dommy/blob.rb +17 -0
  4. data/lib/dommy/bridge/values.rb +29 -2
  5. data/lib/dommy/bridge/wire_tags.rb +8 -4
  6. data/lib/dommy/browser.rb +324 -49
  7. data/lib/dommy/character_data.rb +20 -27
  8. data/lib/dommy/class_list.rb +24 -9
  9. data/lib/dommy/cookie_jar.rb +408 -0
  10. data/lib/dommy/cookie_store.rb +104 -49
  11. data/lib/dommy/css.rb +34 -8
  12. data/lib/dommy/custom_elements.rb +362 -114
  13. data/lib/dommy/data_transfer.rb +22 -2
  14. data/lib/dommy/document.rb +1071 -200
  15. data/lib/dommy/dom_exception.rb +6 -0
  16. data/lib/dommy/dom_parser.rb +46 -14
  17. data/lib/dommy/dom_string_list.rb +36 -0
  18. data/lib/dommy/element.rb +217 -102
  19. data/lib/dommy/element_internals.rb +215 -0
  20. data/lib/dommy/encodings.rb +10 -0
  21. data/lib/dommy/event.rb +378 -122
  22. data/lib/dommy/event_source.rb +4 -30
  23. data/lib/dommy/file_reader.rb +5 -29
  24. data/lib/dommy/form_data.rb +40 -6
  25. data/lib/dommy/form_entry_list.rb +106 -137
  26. data/lib/dommy/fragment.rb +11 -10
  27. data/lib/dommy/history.rb +247 -90
  28. data/lib/dommy/html_canvas_element.rb +15 -35
  29. data/lib/dommy/html_collection.rb +49 -30
  30. data/lib/dommy/html_elements/base.rb +111 -69
  31. data/lib/dommy/html_elements/embedded.rb +349 -105
  32. data/lib/dommy/html_elements/forms/form.rb +407 -368
  33. data/lib/dommy/html_elements/forms/input.rb +333 -174
  34. data/lib/dommy/html_elements/forms/select.rb +90 -74
  35. data/lib/dommy/html_elements/interactive.rb +274 -64
  36. data/lib/dommy/html_elements/media.rb +71 -30
  37. data/lib/dommy/html_elements/metadata.rb +215 -71
  38. data/lib/dommy/html_elements/miscellaneous.rb +58 -14
  39. data/lib/dommy/html_elements/tables.rb +7 -9
  40. data/lib/dommy/html_elements.rb +23 -1
  41. data/lib/dommy/hyperlink_utils.rb +15 -4
  42. data/lib/dommy/interaction/drag_and_drop.rb +187 -0
  43. data/lib/dommy/interaction/event_synthesis.rb +182 -24
  44. data/lib/dommy/interaction/field_interactor.rb +17 -5
  45. data/lib/dommy/interaction/form_submission.rb +10 -4
  46. data/lib/dommy/interaction/key_sender.rb +101 -16
  47. data/lib/dommy/interaction.rb +1 -0
  48. data/lib/dommy/internal/accessibility_tree.rb +15 -3
  49. data/lib/dommy/internal/accessible_description.rb +1 -1
  50. data/lib/dommy/internal/accessible_name.rb +10 -17
  51. data/lib/dommy/internal/aria_role.rb +1 -1
  52. data/lib/dommy/internal/autofill.rb +2 -2
  53. data/lib/dommy/internal/backend_prefilter.rb +1 -5
  54. data/lib/dommy/internal/ce_reactions.rb +285 -0
  55. data/lib/dommy/internal/ce_reactions_table.rb +296 -0
  56. data/lib/dommy/internal/child_list.rb +7 -43
  57. data/lib/dommy/internal/child_navigable.rb +155 -0
  58. data/lib/dommy/internal/child_node.rb +44 -41
  59. data/lib/dommy/internal/close_watcher_manager.rb +238 -0
  60. data/lib/dommy/internal/complex_match.rb +3 -1
  61. data/lib/dommy/internal/constraint_validation.rb +130 -0
  62. data/lib/dommy/internal/css/ancestor_filter.rb +107 -0
  63. data/lib/dommy/internal/css/cascade.rb +7 -7
  64. data/lib/dommy/internal/css/cascaded_declarations.rb +4 -1
  65. data/lib/dommy/internal/css/computed_style_builder.rb +9 -3
  66. data/lib/dommy/internal/css/custom_properties.rb +41 -29
  67. data/lib/dommy/internal/css/parser.rb +0 -22
  68. data/lib/dommy/internal/css/renderability.rb +6 -0
  69. data/lib/dommy/internal/css/rule_index.rb +149 -98
  70. data/lib/dommy/internal/css/rule_index_inputs.rb +135 -0
  71. data/lib/dommy/internal/css/selector_dependencies.rb +8 -2
  72. data/lib/dommy/internal/css/style_cache.rb +7 -4
  73. data/lib/dommy/internal/css/ua_stylesheet.rb +34 -0
  74. data/lib/dommy/internal/declarative_shadow_roots.rb +182 -0
  75. data/lib/dommy/internal/document_generations.rb +55 -2
  76. data/lib/dommy/internal/document_interaction_state.rb +293 -9
  77. data/lib/dommy/internal/dom_matching.rb +2 -2
  78. data/lib/dommy/internal/element_aria.rb +25 -19
  79. data/lib/dommy/internal/element_popover.rb +226 -25
  80. data/lib/dommy/internal/element_shadow.rb +97 -26
  81. data/lib/dommy/internal/element_state.rb +98 -5
  82. data/lib/dommy/internal/element_top_layer.rb +15 -1
  83. data/lib/dommy/internal/enumerated_keyword_sets.rb +40 -0
  84. data/lib/dommy/internal/event_handler_tables.rb +241 -0
  85. data/lib/dommy/internal/event_handlers.rb +149 -0
  86. data/lib/dommy/internal/exception_report.rb +1 -1
  87. data/lib/dommy/internal/focusability.rb +267 -0
  88. data/lib/dommy/internal/form_associated_custom_elements.rb +127 -0
  89. data/lib/dommy/internal/html_or_svg_or_mathml_element.rb +34 -23
  90. data/lib/dommy/internal/html_serialization.rb +292 -0
  91. data/lib/dommy/internal/idl_reflection.rb +202 -0
  92. data/lib/dommy/internal/idl_reflection_table.rb +589 -0
  93. data/lib/dommy/internal/infra.rb +9 -0
  94. data/lib/dommy/internal/input_type.rb +76 -18
  95. data/lib/dommy/internal/insertion_point.rb +27 -3
  96. data/lib/dommy/internal/js_number.rb +60 -0
  97. data/lib/dommy/internal/light_dismiss.rb +178 -0
  98. data/lib/dommy/internal/mime_type.rb +8 -0
  99. data/lib/dommy/internal/mutation_coordinator.rb +94 -62
  100. data/lib/dommy/internal/node_adopter.rb +52 -4
  101. data/lib/dommy/internal/node_factory.rb +111 -7
  102. data/lib/dommy/internal/node_identity.rb +1 -1
  103. data/lib/dommy/internal/node_traversal.rb +22 -4
  104. data/lib/dommy/internal/node_wrapper_cache.rb +6 -38
  105. data/lib/dommy/internal/observer_options.rb +24 -14
  106. data/lib/dommy/internal/origin.rb +86 -0
  107. data/lib/dommy/internal/parent_node.rb +10 -8
  108. data/lib/dommy/internal/popover_invoker_element.rb +67 -0
  109. data/lib/dommy/internal/popover_stack.rb +120 -0
  110. data/lib/dommy/internal/post_insertion_steps.rb +140 -71
  111. data/lib/dommy/internal/reflected_attributes.rb +128 -18
  112. data/lib/dommy/internal/rendered_text/collector.rb +1 -5
  113. data/lib/dommy/internal/rendered_text/fragment.rb +6 -2
  114. data/lib/dommy/internal/selector_arguments.rb +2 -1
  115. data/lib/dommy/internal/selector_match.rb +1 -1
  116. data/lib/dommy/internal/selector_matcher.rb +11 -6
  117. data/lib/dommy/internal/selector_parser.rb +14 -1
  118. data/lib/dommy/internal/sequential_focus_navigation.rb +210 -0
  119. data/lib/dommy/internal/shadow_root_registry.rb +7 -0
  120. data/lib/dommy/internal/template_content_registry.rb +7 -1
  121. data/lib/dommy/internal/text_selection.rb +185 -32
  122. data/lib/dommy/internal/toggle_events.rb +6 -4
  123. data/lib/dommy/internal/unsafe_html.rb +107 -0
  124. data/lib/dommy/internal/url_parser.rb +84 -8
  125. data/lib/dommy/internal/user_activation.rb +192 -0
  126. data/lib/dommy/internal/web_idl.rb +149 -16
  127. data/lib/dommy/internal/window_constructors.rb +26 -6
  128. data/lib/dommy/internal/xml_serialization.rb +3 -3
  129. data/lib/dommy/js/constructor_resolver.rb +18 -0
  130. data/lib/dommy/js/custom_element_bridge.rb +57 -82
  131. data/lib/dommy/js/dom_interfaces.rb +50 -15
  132. data/lib/dommy/js/host_bridge.rb +189 -40
  133. data/lib/dommy/js/host_runtime.js +1897 -415
  134. data/lib/dommy/js/marshaller.rb +13 -4
  135. data/lib/dommy/js/module_loader.rb +37 -6
  136. data/lib/dommy/js/module_preload.rb +107 -29
  137. data/lib/dommy/js/script_boot.rb +197 -115
  138. data/lib/dommy/js/serialized_record.rb +32 -0
  139. data/lib/dommy/js/webidl_event_handlers.js +45 -0
  140. data/lib/dommy/js/webidl_members.js +196 -0
  141. data/lib/dommy/js/webidl_signatures.js +2055 -0
  142. data/lib/dommy/js/webidl_tables.js +47 -100
  143. data/lib/dommy/js/window_wiring.rb +9 -5
  144. data/lib/dommy/location.rb +129 -76
  145. data/lib/dommy/media_query_list.rb +4 -13
  146. data/lib/dommy/message_channel.rb +137 -64
  147. data/lib/dommy/mutation_observer.rb +2 -0
  148. data/lib/dommy/navigation.rb +59 -9
  149. data/lib/dommy/navigator.rb +131 -5
  150. data/lib/dommy/node.rb +19 -11
  151. data/lib/dommy/notification.rb +9 -1
  152. data/lib/dommy/promise.rb +8 -0
  153. data/lib/dommy/range.rb +5 -34
  154. data/lib/dommy/resources.rb +38 -3
  155. data/lib/dommy/scheduler.rb +176 -48
  156. data/lib/dommy/shadow_root.rb +117 -19
  157. data/lib/dommy/storage.rb +135 -25
  158. data/lib/dommy/structured_data.rb +45 -0
  159. data/lib/dommy/style_declaration.rb +16 -0
  160. data/lib/dommy/text_codec.rb +2 -14
  161. data/lib/dommy/tree_walker.rb +10 -18
  162. data/lib/dommy/url.rb +11 -4
  163. data/lib/dommy/validity_state.rb +109 -107
  164. data/lib/dommy/version.rb +1 -1
  165. data/lib/dommy/web_socket.rb +5 -28
  166. data/lib/dommy/window.rb +811 -99
  167. data/lib/dommy/worker.rb +4 -28
  168. data/lib/dommy/xml_http_request.rb +32 -45
  169. data/lib/dommy.rb +35 -1
  170. metadata +35 -4
  171. data/lib/dommy/internal/cookie_jar.rb +0 -29
@@ -9,9 +9,9 @@ module Dommy
9
9
  # Document's public API. The element behaviours an insertion also sets off —
10
10
  # a script running, a details group settling — are PostInsertionSteps'.
11
11
  #
12
- # A custom element reaction that throws is the page's exception, so it is
13
- # REPORTED at the window ("report an exception"), the same seam an event
14
- # listener's exception takes. It used to be discarded.
12
+ # The custom element reactions are enqueued (Internal::CEReactions), not
13
+ # run: they run when the [CEReactions] member that made the mutation
14
+ # returns, or from the backup element queue.
15
15
  class MutationCoordinator
16
16
  def initialize(document, observer_manager)
17
17
  @document = document
@@ -30,24 +30,30 @@ module Dommy
30
30
  nil
31
31
  end
32
32
 
33
- # Fire CustomElement lifecycle: connected (synchronous, before mutation delivery)
33
+ # DOM insert step 7.7 for one connected element: a custom element gets
34
+ # a connectedCallback reaction; any other is tried for an upgrade.
34
35
  def notify_connected(element)
35
- return unless element&.respond_to?(:connected_callback)
36
+ return unless element.respond_to?(:__internal_ce_custom__?)
36
37
 
37
- element.connected_callback
38
- rescue StandardError => e
39
- report_exception(e)
38
+ registry = element.__internal_ce_registry__
39
+ registry.__internal_note_scoped_document__(element.owner_document) if registry&.scoped?
40
+
41
+ if element.__internal_ce_custom__?
42
+ CEReactions.enqueue_callback(element, "connectedCallback", [])
43
+ else
44
+ @document.__internal_try_to_upgrade__(element)
45
+ end
40
46
  end
41
47
 
48
+ # DOM remove step 15: a custom element gets a disconnectedCallback
49
+ # reaction.
42
50
  def notify_disconnected(element)
43
- return unless element&.respond_to?(:disconnected_callback)
51
+ return unless element.respond_to?(:__internal_ce_custom__?) && element.__internal_ce_custom__?
44
52
 
45
- element.disconnected_callback
46
- rescue StandardError => e
47
- report_exception(e)
53
+ CEReactions.enqueue_callback(element, "disconnectedCallback", [])
48
54
  end
49
55
 
50
- # Connected callbacks, connected scripts and blank-iframe loads for every
56
+ # Connected callbacks, connected scripts and iframe navigables for every
51
57
  # element among the subtree's shadow-including inclusive descendants, in
52
58
  # shadow-including tree order — so the custom elements in an inserted
53
59
  # host's shadow tree connect too.
@@ -68,38 +74,35 @@ module Dommy
68
74
  each_shadow_including_element(nk) { |element| notify_moved(element) }
69
75
  end
70
76
 
71
- # Disconnected callbacks, over the same shadow-including walk.
77
+ # HTML's removing steps — the focus fixup, a popover hiding, a dialog
78
+ # leaving the top layer — and the disconnected callbacks, over the same
79
+ # shadow-including walk.
72
80
  def notify_disconnected_subtree(nk)
73
- each_shadow_including_element(nk) { |element| notify_disconnected(element) }
81
+ root = @document.wrap_node(nk)
82
+ @document.__internal_focused_subtree_removed__(root) if root
83
+ each_shadow_including_element(nk) do |element|
84
+ element.__internal_popover_removed__ if element.respond_to?(:__internal_popover_removed__)
85
+ element.__internal_dialog_removed__ if element.respond_to?(:__internal_dialog_removed__)
86
+ # The iframe removing steps: destroy its child navigable.
87
+ element.__internal_destroy_child_navigable__ if element.is_a?(HTMLIFrameElement)
88
+ notify_disconnected(element)
89
+ end
74
90
  end
75
91
 
92
+ # DOM "handle attribute changes" step 2: a custom element gets an
93
+ # attributeChangedCallback reaction (enqueued only when its definition
94
+ # observes the attribute — matched exactly, so a `fooBar` made by
95
+ # setAttributeNS is observed as "fooBar" alone).
76
96
  def notify_attribute_changed(element, name, old_value, new_value, namespace = nil)
77
- return unless element&.respond_to?(:attribute_changed_callback)
78
-
79
- klass = element.class
80
- return unless klass.respond_to?(:observed_attributes)
81
- # The local name as it is: observedAttributes is matched exactly, so a
82
- # `fooBar` made by setAttributeNS is observed as "fooBar" alone.
83
- return unless Array(klass.observed_attributes).map(&:to_s).include?(name.to_s)
84
-
85
- # attributeChangedCallback's 4th arg is the attribute's namespace (null
86
- # for a plain HTML attribute). Pass it only to callbacks that accept it
87
- # (the JS bridge, or a 4-arg Ruby callback) so existing 3-arg Ruby custom
88
- # elements keep working.
89
- cb = element.method(:attribute_changed_callback)
90
- if cb.arity.negative? || cb.arity >= 4
91
- element.attribute_changed_callback(name, old_value, new_value, namespace)
92
- else
93
- element.attribute_changed_callback(name, old_value, new_value)
94
- end
95
- rescue StandardError => e
96
- report_exception(e)
97
+ return unless element.respond_to?(:__internal_ce_custom__?) && element.__internal_ce_custom__?
98
+
99
+ CEReactions.enqueue_callback(element, "attributeChangedCallback", [name.to_s, old_value, new_value, namespace])
97
100
  end
98
101
 
99
102
  # Fire MutationObserver childList records
100
103
  # `moving:` marks the records of a move (moveBefore). A move runs neither
101
104
  # the insertion nor the removing steps, so the connected / disconnected
102
- # walk below — lifecycle callbacks, script execution, blank-iframe load —
105
+ # walk below — lifecycle callbacks, script execution, iframe navigables —
103
106
  # is skipped for it; its custom element reactions are the move's own
104
107
  # (notify_moved_subtree).
105
108
  def notify_child_list_mutation(
@@ -115,15 +118,31 @@ module Dommy
115
118
  return nil unless target
116
119
  return nil if added_nodes.empty? && removed_nodes.empty?
117
120
 
121
+ # The form-associated custom elements moved in or out reset their form
122
+ # owner and disabled state (their insertion / removing steps), ahead
123
+ # of their connected / disconnected reactions.
124
+ if FormAssociatedCustomElements.any?
125
+ removed_nodes.each { |nk| FormAssociatedCustomElements.refresh_subtree(@document, nk) }
126
+ added_nodes.each { |nk| FormAssociatedCustomElements.refresh_subtree(@document, nk) }
127
+ end
128
+
118
129
  # Custom Element connected/disconnected callbacks, script execution, and
119
- # blank-iframe load all require the subtree to be connected to the
130
+ # iframe child navigables all require the subtree to be connected to the
120
131
  # document (the script/iframe paths already check is_connected?, and
121
132
  # connectedCallback fires only when connected). So skip the O(subtree)
122
133
  # walk for mutations within a still-detached tree — the common case
123
134
  # during bulk DOM construction, where nothing in the walk can fire.
124
135
  if !moving && (!target.respond_to?(:is_connected?) || target.is_connected?)
125
- added_nodes.each { |nk| notify_connected_subtree(nk) }
136
+ # A script's children changed steps run before the post-connection
137
+ # steps of what was inserted into it (DOM "insert" orders them so).
138
+ # Only for an insertion: DOM runs them for a removal too, but no
139
+ # browser prepares a script because a child left it, and WPT's
140
+ # script-does-not-run-on-child-removal holds them to that.
141
+ @post_insertion_steps.script_children_changed(target) unless added_nodes.empty?
142
+ # A replacement removes before it inserts (DOM "replace all"), so the
143
+ # disconnected reactions are enqueued ahead of the connected ones.
126
144
  removed_nodes.each { |nk| notify_disconnected_subtree(nk) }
145
+ added_nodes.each { |nk| notify_connected_subtree(nk) }
127
146
  end
128
147
 
129
148
  # HTML's details insertion steps run wherever the element lands, not only
@@ -131,6 +150,12 @@ module Dommy
131
150
  # already consistent by the time it is attached.
132
151
  @post_insertion_steps.details_inserted(added_nodes)
133
152
  @post_insertion_steps.select_mutated(target_node, added_nodes, removed_nodes)
153
+ connected = target.respond_to?(:is_connected?) && target.is_connected?
154
+ @post_insertion_steps.radios_moved(added_nodes, connected: connected && !moving)
155
+ @post_insertion_steps.radios_moved(removed_nodes, connected: false)
156
+ # HTML's "children changed steps" for the one element that has its own
157
+ # (a pristine textarea's raw value follows its child text content).
158
+ target.__internal_children_changed__(added_nodes, removed_nodes) if target.is_a?(HTMLTextAreaElement)
134
159
 
135
160
  # MutationRecords are only needed when something is observing; skip the
136
161
  # eager wrapping + record entirely when no observer is registered.
@@ -190,18 +215,6 @@ module Dommy
190
215
  # answer for a prefixed attribute with the same one.
191
216
  new_value = Backend.get_attribute_ns(target_node, namespace, attr)
192
217
 
193
- # HTML "attribute change steps": an element that reacts to one of its own
194
- # attributes (a details to `open`, an option to `selected`, a select to
195
- # `multiple` / `size`) does it here — on EVERY write path, since they all
196
- # end up announcing the mutation, where overriding `setAttribute` and
197
- # `removeAttribute` in each class missed `setAttributeNS` and its
198
- # removal counterpart.
199
- target.__internal_attribute_changed__(attr, old_value, new_value, namespace) if
200
- target.respond_to?(:__internal_attribute_changed__)
201
-
202
- # Custom Element attributeChangedCallback (synchronous)
203
- notify_attribute_changed(target, attr, old_value, new_value, namespace)
204
-
205
218
  # The attributeFilter is part of the per-registration condition, so it is
206
219
  # applied inside `entry_wants?` rather than to the observer as a whole:
207
220
  # a filtered registration must not hide another one of the same observer
@@ -222,6 +235,20 @@ module Dommy
222
235
  )
223
236
  end
224
237
 
238
+ # Queue this mutation before running attribute change steps: those may
239
+ # mutate another attribute (input type changes can copy value to its
240
+ # content attribute), whose record must follow the originating write.
241
+ # Every attribute API reaches this hook, including the NS variants.
242
+ target.__internal_attribute_changed__(attr, old_value, new_value, namespace) if
243
+ target.respond_to?(:__internal_attribute_changed__)
244
+
245
+ notify_attribute_changed(target, attr, old_value, new_value, namespace)
246
+ form_association_attribute_changed(target, target_node, attr) if namespace.nil? && FormAssociatedCustomElements.any?
247
+ @post_insertion_steps.script_attribute_changed(target, attr, new_value, namespace)
248
+ # An event handler content attribute (`onclick="…"`) sets or removes
249
+ # its handler.
250
+ EventHandlers.attribute_changed(target, attr, new_value, namespace) if attr.start_with?("on") && target.is_a?(Element)
251
+
225
252
  nil
226
253
  end
227
254
 
@@ -268,19 +295,24 @@ module Dommy
268
295
  end
269
296
 
270
297
 
271
- # HTML "enqueue a custom element callback reaction": a definition without
272
- # connectedMoveCallback runs disconnectedCallback and then connectedCallback
273
- # in its place. (A JS-defined element always answers the Ruby method; the
274
- # bridge falls back on the JS side.)
275
- def notify_moved(element)
276
- if element.respond_to?(:connected_move_callback)
277
- element.connected_move_callback
278
- else
279
- notify_disconnected(element)
280
- notify_connected(element)
298
+ # A `form` or `disabled` attribute of a form-associated custom element,
299
+ # a fieldset's `disabled`, or any `id` (a form's, which a `form`
300
+ # attribute names) can change the form owner or the disabled state.
301
+ def form_association_attribute_changed(target, target_node, attr)
302
+ case attr
303
+ when "form" then FormAssociatedCustomElements.refresh(target)
304
+ when "disabled" then FormAssociatedCustomElements.refresh_subtree(@document, target_node)
305
+ when "id" then FormAssociatedCustomElements.refresh_subtree(@document, @document.backend_doc)
281
306
  end
282
- rescue StandardError => e
283
- report_exception(e)
307
+ end
308
+
309
+ # DOM move: a custom element gets a connectedMoveCallback reaction
310
+ # (CEReactions turns it into disconnected + connected for a definition
311
+ # without one).
312
+ def notify_moved(element)
313
+ return unless element.respond_to?(:__internal_ce_custom__?) && element.__internal_ce_custom__?
314
+
315
+ CEReactions.enqueue_callback(element, "connectedMoveCallback", [])
284
316
  end
285
317
  end
286
318
  end
@@ -36,18 +36,26 @@ module Dommy
36
36
  # An Attr has no parent to be removed from, so it stays on its
37
37
  # element; only its node document changes.
38
38
  return node.__internal_adopt__(@document) if node.is_a?(Attr)
39
- return nil unless node.respond_to?(:__dommy_backend_node__)
39
+ src = node.__dommy_backend_node__ if node.is_a?(Node)
40
+ return nil unless src
40
41
 
41
- src = node.__dommy_backend_node__
42
42
  # WHATWG adopt removes the node from its parent first — a full remove, so
43
43
  # the old parent gets its removing steps AND its childList record.
44
- @document.remove_node_with_notify(src) if src.parent
44
+ # The removal is the old document's: its custom elements get their
45
+ # disconnected reactions there.
46
+ if src.parent
47
+ source = node.respond_to?(:document) && node.document.respond_to?(:remove_node_with_notify) ? node.document : @document
48
+ source.remove_node_with_notify(src)
49
+ end
45
50
 
46
51
  # Same document: just return the wrapper after the detach above.
47
52
  return @document.wrap_node(src) if src.document == backend_doc
48
53
  return adopt_doctype(node, src) if node.is_a?(DocumentType)
49
54
 
50
- adopt_across_documents(node, src)
55
+ old_document = node.respond_to?(:document) ? node.document : nil
56
+ adopted = adopt_across_documents(node, src)
57
+ enqueue_adopted_callbacks(adopted.__dommy_backend_node__, old_document)
58
+ adopted
51
59
  end
52
60
 
53
61
  # Adopt a raw backend node that has no wrapper of its own to re-bind — a
@@ -61,6 +69,7 @@ module Dommy
61
69
  adopted = Backend.adopt(node, backend_doc)
62
70
  if source_document && !source_document.equal?(@document)
63
71
  Transfer.new(source_document, @document).carry_over(node, adopted)
72
+ enqueue_adopted_callbacks(adopted, source_document)
64
73
  end
65
74
  adopted
66
75
  end
@@ -69,6 +78,45 @@ module Dommy
69
78
 
70
79
  def backend_doc = @document.backend_doc
71
80
 
81
+ # DOM adopt step 3.2.2.2: an element without a scoped registry gets the
82
+ # new document's (its parent's, under a parent) effective global one —
83
+ # what an unset registry stands for — or keeps null under a parent
84
+ # whose registry is scoped or null.
85
+ def adopt_registry(element)
86
+ shadow = element.respond_to?(:__internal_shadow_root__) ? element.__internal_shadow_root__ : nil
87
+ shadow&.__internal_adopt_registry__
88
+ data = element.__internal_peek_ce_data__
89
+ return unless data&.registry_set?
90
+
91
+ registry = data.registry
92
+ return if registry&.scoped?
93
+
94
+ parent = element.parent_node
95
+ if registry || parent.nil? || parent.is_a?(Fragment)
96
+ data.reset_registry!
97
+ else
98
+ parent_registry = CustomElementRegistry.for_node(parent)
99
+ parent_registry && !parent_registry.scoped? ? data.reset_registry! : (data.registry = nil)
100
+ end
101
+ end
102
+
103
+ # DOM adopt step 3.3: each custom element among the adopted node's
104
+ # shadow-including inclusive descendants gets an adoptedCallback
105
+ # reaction, given the old and the new document.
106
+ def enqueue_adopted_callbacks(root, old_document)
107
+ return unless root && old_document && !old_document.equal?(@document)
108
+
109
+ @document.__internal_each_shadow_including_element__(root) do |node|
110
+ element = @document.__internal_peek_wrapper__(node)
111
+ next unless element.respond_to?(:__internal_ce_custom__?)
112
+
113
+ adopt_registry(element)
114
+ next unless element.__internal_ce_custom__?
115
+
116
+ CEReactions.enqueue_callback(element, "adoptedCallback", [old_document, @document])
117
+ end
118
+ end
119
+
72
120
  # Cross-document DocumentType: Makiri can't import a doctype node between
73
121
  # arenas, so re-create it in the destination's backend from its name /
74
122
  # publicId / systemId (as createDocument does), then re-bind the caller's
@@ -14,7 +14,7 @@ module Dommy
14
14
  @wrappers = wrappers
15
15
  end
16
16
 
17
- def create_element(name)
17
+ def create_element(name, options = nil)
18
18
  str = domstring(name)
19
19
  raise DOMException::InvalidCharacterError, "name must not be empty" if str.empty?
20
20
  raise DOMException::InvalidCharacterError, "invalid element name: #{str.inspect}" unless Namespaces.valid_element_local_name?(str)
@@ -32,9 +32,22 @@ module Dommy
32
32
  namespace = @document.content_type == "application/xhtml+xml" ? Element::HTML_NAMESPACE : nil
33
33
  end
34
34
 
35
- node = Backend.create_element(local, namespace, @document.backend_doc)
35
+ registry, is_value = flatten_options(options)
36
+ create_an_element(local, namespace, is_value, registry) do
37
+ Backend.create_element(local, namespace, @document.backend_doc)
38
+ end
39
+ end
36
40
 
37
- @wrappers.wrap(node)
41
+ # The HTML element constructor run for `new MyElement()` (its
42
+ # definition's construction stack empty): a new element that is custom
43
+ # from the start.
44
+ def create_custom_element(definition)
45
+ node = Backend.create_element(definition.local_name, Element::HTML_NAMESPACE, @document.backend_doc)
46
+ element = @wrappers.wrap(node)
47
+ data = element.__internal_init_ce_data__(definition.autonomous? ? nil : definition.name)
48
+ element.__internal_set_ce_registry__(definition.registry) if definition.registry&.scoped?
49
+ definition.mark_custom(data)
50
+ element
38
51
  end
39
52
 
40
53
  def create_text_node(text)
@@ -83,7 +96,7 @@ module Dommy
83
96
  Attr.new(qualified_name, namespace_uri: ns, prefix: prefix, local_name: local, document: @document)
84
97
  end
85
98
 
86
- def create_element_ns(namespace_uri, qualified_name)
99
+ def create_element_ns(namespace_uri, qualified_name, options = nil)
87
100
  # WHATWG "validate and extract": QName-validate the qualifiedName
88
101
  # (InvalidCharacterError) and apply the prefix/namespace rules
89
102
  # (NamespaceError), then build the element with its prefix bound.
@@ -93,15 +106,106 @@ module Dommy
93
106
  qualified_name = domstring(qualified_name)
94
107
  ns, = Namespaces.validate_and_extract(namespace_uri, qualified_name, context: :element)
95
108
 
96
- el = Backend.create_element_ns(ns, qualified_name, @document.backend_doc)
97
-
98
- @wrappers.build_element_wrapper(el)
109
+ local = qualified_name.include?(":") ? qualified_name.split(":", 2).last : qualified_name
110
+ registry, is_value = flatten_options(options)
111
+ create_an_element(local, ns, is_value, registry) do
112
+ Backend.create_element_ns(ns, qualified_name, @document.backend_doc)
113
+ end
99
114
  end
100
115
 
101
116
  # Query methods
102
117
 
103
118
  private
104
119
 
120
+ # DOM "create an element" with the synchronous custom elements flag set
121
+ # (createElement / createElementNS). `make_node` mints the backend node
122
+ # when no autonomous definition constructs the element.
123
+ def create_an_element(local, namespace, is_value, registry)
124
+ definition = CustomElementRegistry.lookup(registry, namespace, local, is_value)
125
+ if definition&.autonomous?
126
+ element = create_custom_element_synchronously(definition, local)
127
+ element.__internal_set_ce_registry__(registry)
128
+ return element
129
+ end
130
+
131
+ element = @wrappers.build_element_wrapper(yield)
132
+ data = element.__internal_init_ce_data__(definition ? definition.name : is_value)
133
+ element.__internal_set_ce_registry__(registry)
134
+ upgrade_synchronously(definition, data) if definition
135
+ element
136
+ end
137
+
138
+ # Step 5 (a customized built-in): upgrade the new element now; what that
139
+ # throws is reported, and the element's state is "failed".
140
+ def upgrade_synchronously(definition, data)
141
+ definition.upgrade(data)
142
+ rescue StandardError => e
143
+ definition.report(e)
144
+ data.state = "failed"
145
+ end
146
+
147
+ # DOM "flatten element creation options": the registry (the document's,
148
+ # or a dictionary's `customElementRegistry`) and the is value (a
149
+ # dictionary's `is`; a string argument is the legacy form, which carries
150
+ # none). Both at once, or a global registry other than the document's,
151
+ # is a NotSupportedError.
152
+ def flatten_options(options)
153
+ registry = CustomElementRegistry.for_document(@document)
154
+ is_value = nil
155
+ if options.is_a?(Hash)
156
+ value = options.key?("is") ? options["is"] : options[:is]
157
+ is_value = value.to_s unless value.nil? || value.equal?(Bridge::UNDEFINED)
158
+ given = options.key?("customElementRegistry") ? options["customElementRegistry"] : Bridge::UNDEFINED
159
+ unless given.equal?(Bridge::UNDEFINED)
160
+ raise DOMException::NotSupportedError, "is and customElementRegistry are exclusive" unless is_value.nil?
161
+ unless given.nil? || given.is_a?(CustomElementRegistry)
162
+ raise Bridge::TypeError, "customElementRegistry is not a CustomElementRegistry"
163
+ end
164
+
165
+ registry = given
166
+ end
167
+ end
168
+ if registry && !registry.scoped? && !registry.equal?(CustomElementRegistry.for_document(@document))
169
+ raise DOMException::NotSupportedError, "a global registry other than the document's"
170
+ end
171
+
172
+ [registry, is_value]
173
+ end
174
+
175
+ # DOM "create an element" step 6.2, the synchronous custom elements flag
176
+ # set (createElement / createElementNS): run the definition's
177
+ # constructor now. What it returns must be a new, empty, parentless HTML
178
+ # element of this document with the definition's local name; otherwise
179
+ # — or when it throws — the exception is reported and the element is an
180
+ # HTMLUnknownElement whose custom element state is "failed".
181
+ def create_custom_element_synchronously(definition, local)
182
+ result = definition.construct_synchronously(@document)
183
+ check_constructed_element!(result, local)
184
+ result
185
+ rescue StandardError => e
186
+ definition.report(e)
187
+ node = Backend.create_element(local, Element::HTML_NAMESPACE, @document.backend_doc)
188
+ element = HTMLUnknownElement.new(@document, node)
189
+ @wrappers.register(node, element)
190
+ element.__internal_set_custom_element_state__("failed")
191
+ element
192
+ end
193
+
194
+ def check_constructed_element!(result, local)
195
+ unless result.is_a?(HTMLElement) && result.namespace_uri == Element::HTML_NAMESPACE
196
+ raise Bridge::TypeError, "the custom element constructor did not return an HTMLElement"
197
+ end
198
+
199
+ problem =
200
+ if result.has_attributes? then "has attributes"
201
+ elsif result.__dommy_backend_node__.children.any? then "has children"
202
+ elsif result.__dommy_backend_node__.parent then "has a parent"
203
+ elsif !result.owner_document.equal?(@document) then "belongs to another document"
204
+ elsif result.local_name != local then "has the local name #{result.local_name.inspect}"
205
+ end
206
+ raise DOMException::NotSupportedError, "the constructed custom element #{problem}" if problem
207
+ end
208
+
105
209
  # WebIDL DOMString coercion for a name/qualifiedName argument: JS
106
210
  # `undefined` → "undefined", JS `null` (Ruby nil) → "null", else #to_s.
107
211
  def domstring(value)
@@ -23,7 +23,7 @@ module Dommy
23
23
  end
24
24
 
25
25
  def key_for(node)
26
- backend = node.respond_to?(:__dommy_backend_node__) ? node.__dommy_backend_node__ : node
26
+ backend = node.is_a?(Node) ? node.__dommy_backend_node__ : node
27
27
  backend && Backend.identity_key(backend)
28
28
  end
29
29
  end
@@ -18,11 +18,29 @@ module Dommy
18
18
 
19
19
  # The root of the tree a backend node is in: its topmost inclusive
20
20
  # ancestor, the backend document when it is attached.
21
- def self.root_of(node)
22
- while (parent = node.parent)
23
- node = parent
21
+ def self.root_of(node) = node.root_node
22
+
23
+ # More shadow trees nested in one another than any real page has.
24
+ MAX_SHADOW_DEPTH = 100_000
25
+
26
+ # Whether `node` (a backend node of `document`) is connected: its
27
+ # shadow-including root is a document. From each tree's root to the
28
+ # host of its shadow root, if it is one. Shadow trees do not nest in a
29
+ # cycle; the cap only keeps a malformed chain from hanging.
30
+ def self.connected?(node, document)
31
+ current = node
32
+ MAX_SHADOW_DEPTH.times do
33
+ root = root_of(current)
34
+ return true if root.is_a?(Backend.document_class)
35
+
36
+ # Only a fragment can be a shadow root's.
37
+ shadow_root = root.document_fragment? && document.__internal_shadow_root_for_fragment__(root)
38
+ host = shadow_root && shadow_root.host
39
+ return false unless host
40
+
41
+ current = host.__dommy_backend_node__
24
42
  end
25
- node
43
+ false
26
44
  end
27
45
 
28
46
  # Check if ancestor is an ancestor of node.
@@ -147,30 +147,13 @@ module Dommy
147
147
  # local name (an XML node's name holds its prefix).
148
148
  def build_element_wrapper(node)
149
149
  ns = Backend.namespace_uri(node)
150
- # A JS-defined custom element (`customElements.define(name, classExpr)`
151
- # from page script) registers its JS constructor — a HostCallback — not a
152
- # Ruby class, so we cannot `.new(@document, node)` it. Wrap such a node as
153
- # its plain built-in element instead (its server-rendered light-DOM
154
- # content still displays; the JS upgrade is simply not run). Only a Ruby
155
- # class definition routes a custom Ruby wrapper + #construct.
156
- custom_klass = custom_element_class_for(node.name)
157
- ruby_custom = custom_klass if custom_klass.is_a?(::Class)
158
- klass = ruby_custom || Dommy.element_class_for(node.local_name, ns)
159
- instance = klass.new(@document, node)
160
-
150
+ # The element's own interface: a custom element's class is applied by
151
+ # its definition when it is constructed or upgraded (see
152
+ # CustomElementDefinition), never by wrapping — wrapping a node is no
153
+ # creation, and an element its definition has not reached yet is
154
+ # still undefined.
155
+ instance = Dommy.element_class_for(node.local_name, ns).new(@document, node)
161
156
  @wrappers[node] = instance
162
-
163
- # A custom element's constructor is the page's code, so an exception in
164
- # it is reported at the window rather than discarded — the wrapper still
165
- # exists either way, which is what the caller is owed.
166
- if ruby_custom && instance.respond_to?(:construct)
167
- begin
168
- instance.construct
169
- rescue StandardError => e
170
- report_construct_exception(e)
171
- end
172
- end
173
-
174
157
  instance
175
158
  end
176
159
  private
@@ -219,21 +202,6 @@ module Dommy
219
202
  DocumentType.new(backend_node: node, document: @document)
220
203
  end
221
204
  end
222
-
223
-
224
- def report_construct_exception(error)
225
- window = (@document.default_view if @document.respond_to?(:default_view))
226
- return unless window.respond_to?(:__internal_report_exception__)
227
-
228
- Internal::ExceptionReport.report_at(window, error)
229
- end
230
-
231
- def custom_element_class_for(tag_name)
232
- # Custom elements are registered on window, not document.
233
- # Access via default_view if available.
234
- default_view = @document.default_view
235
- default_view&.custom_elements&.get(tag_name)
236
- end
237
205
  end
238
206
  end
239
207
  end
@@ -25,8 +25,11 @@ module Dommy
25
25
  end
26
26
 
27
27
  # The spec's steps 1-6, in their order: the implications are in the
28
- # readers below, then "at least one", then the contradictions.
28
+ # readers below, then "at least one", then the contradictions. WebIDL
29
+ # converted the dictionary before them, so a member that cannot convert
30
+ # throws first.
29
31
  def to_registration(target)
32
+ filter = attribute_filter
30
33
  unless child_list? || attributes? || character_data?
31
34
  raise Bridge::TypeError,
32
35
  "MutationObserver.observe: at least one of childList, attributes, characterData must be true"
@@ -38,7 +41,7 @@ module Dommy
38
41
  child_list: child_list?,
39
42
  subtree: flag("subtree"),
40
43
  attributes: attributes?,
41
- attribute_filter: attribute_filter,
44
+ attribute_filter: filter,
42
45
  attribute_old_value: flag("attributeOldValue"),
43
46
  character_data: character_data?,
44
47
  character_data_old_value: flag("characterDataOldValue")
@@ -47,9 +50,18 @@ module Dommy
47
50
 
48
51
  private
49
52
 
53
+ # `sequence<DOMString>`: an array (the iterable the bridge hands over),
54
+ # each element through ToString — null is "null". Anything else, null
55
+ # and a string included, cannot convert and is a TypeError. The names
56
+ # stay as given: the filter is matched against an attribute's local name
57
+ # case-sensitively ("queue a mutation record" step 2.3).
50
58
  def attribute_filter
51
- filter = @opts["attributeFilter"] || @opts[:attributeFilter]
52
- filter.is_a?(Array) ? filter.map { |name| name.to_s.downcase } : filter
59
+ return nil unless given?("attributeFilter")
60
+
61
+ filter = member("attributeFilter")
62
+ raise Bridge::TypeError, "MutationObserver.observe: attributeFilter is not a sequence" unless filter.is_a?(Array)
63
+
64
+ filter.map { |name| WebIDL.dom_string(name) }
53
65
  end
54
66
 
55
67
  def child_list? = flag("childList")
@@ -76,18 +88,16 @@ module Dommy
76
88
  raise Bridge::TypeError, "characterDataOldValue requires characterData to be true"
77
89
  end
78
90
 
79
- def given?(name) = @opts.key?(name) || @opts.key?(name.to_sym)
91
+ # Whether the dictionary has the member: an undefined value is as if it
92
+ # were missing (WebIDL dictionary conversion skips it).
93
+ def given?(name)
94
+ (@opts.key?(name) || @opts.key?(name.to_sym)) && !member(name).equal?(Bridge::UNDEFINED)
95
+ end
80
96
 
81
- # JS truthiness for a dictionary member, which is what WebIDL's `boolean`
82
- # conversion amounts to here.
83
- def flag(name)
84
- value = @opts.key?(name) ? @opts[name] : @opts[name.to_sym]
85
- return false if value.nil? || value == false || value == 0 || value == ""
86
- return false if defined?(Bridge::UNDEFINED) && value.equal?(Bridge::UNDEFINED)
87
- return false if value.is_a?(Float) && value.nan?
97
+ def member(name) = @opts.key?(name) ? @opts[name] : @opts[name.to_sym]
88
98
 
89
- true
90
- end
99
+ # A boolean member through WebIDL's conversion (ToBoolean).
100
+ def flag(name) = WebIDL.boolean(member(name))
91
101
  end
92
102
  end
93
103
  end