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
@@ -1,13 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "uri"
4
+ require "time"
4
5
 
5
6
  require_relative "internal/node_wrapper_cache"
6
7
  require_relative "internal/directionality"
7
8
  require_relative "internal/node_factory"
8
9
  require_relative "internal/mutation_coordinator"
9
10
  require_relative "internal/shadow_root_registry"
10
- require_relative "internal/cookie_jar"
11
+ require_relative "cookie_jar"
11
12
  require_relative "internal/node_traversal"
12
13
  require_relative "internal/node_adopter"
13
14
  require_relative "internal/observer_manager"
@@ -35,9 +36,8 @@ module Dommy
35
36
  # insert steps, neither of which the old doctype-specific path did.
36
37
  include Internal::ChildNode
37
38
 
38
- # Mixed into a node-backed doctype only, so a synthetic one does NOT respond
39
- # to `__dommy_backend_node__` — leaving the Node mixin's guards (which key off
40
- # `respond_to?(:__dommy_backend_node__)`) to treat it as disconnected.
39
+ # Mixed into a node-backed doctype only, so a synthetic one keeps Node's nil
40
+ # `__dommy_backend_node__` and is treated as disconnected.
41
41
  module NodeBacked
42
42
  def __dommy_backend_node__ = @__node__
43
43
  end
@@ -178,6 +178,14 @@ module Dommy
178
178
  nil
179
179
  when "isConnected"
180
180
  is_connected?
181
+ when "textContent", "nodeValue"
182
+ nil
183
+ when "baseURI"
184
+ @owner_document&.base_uri
185
+ else
186
+ # A doctype is a Node and a ChildNode only: Element's and ParentNode's
187
+ # getters (tagName, children, ...) are not its members.
188
+ Bridge::ABSENT
181
189
  end
182
190
  end
183
191
 
@@ -322,7 +330,18 @@ module Dommy
322
330
  # with, in tree order, the doctype (when given) then a document element
323
331
  # (namespace, qualifiedName) when qualifiedName is non-empty.
324
332
  def create_document(namespace, qualified_name, doctype = nil)
333
+ # WebIDL converts the interface argument before running the DOM steps
334
+ # (including qualified-name validation).
335
+ unless doctype.nil? || doctype.equal?(Bridge::UNDEFINED) || doctype.is_a?(DocumentType)
336
+ raise Bridge::TypeError, "doctype must be a DocumentType"
337
+ end
338
+
325
339
  doc = Document.new(nil, backend_doc: Backend.empty_xml_document)
340
+ # The result is an XMLDocument (DOM's createDocument), unlike a DOMParser
341
+ # result of the same content type — so the interface is pinned here.
342
+ doc.__internal_xml_document__ = true
343
+ # Its origin is the associated document's (DOM createDocument step 7).
344
+ doc.__internal_set_creator__(@document)
326
345
  # createDocument's content type is keyed off the namespace. None is
327
346
  # "text/html", so tagName keeps its case; xhtml+xml still routes
328
347
  # createElement to the HTML namespace (so an XHTML document isEqualNode
@@ -342,34 +361,14 @@ module Dommy
342
361
  doc
343
362
  end
344
363
 
345
- private
346
-
347
- # Place `doctype` (a DocumentType passed to createDocument) as `doc`'s first
348
- # child. createDocument appends the doctype node itself (step 5, before
349
- # step 6 appends the element), so it goes through the ordinary insert: the
350
- # adoption re-binds the caller's wrapper, and `xmlDoc.firstChild ===
351
- # doctype`, its parentNode and ownerDocument all follow.
352
- #
353
- # No-op for nil/undefined or a non-DocumentType. A doctype the XML backend
354
- # cannot hold is left unplaced rather than thrown: createDocument itself
355
- # validates none of it — only an XML serialization would.
356
- def adopt_doctype_into(doc, doctype)
357
- return if doctype.nil? || doctype.equal?(Bridge::UNDEFINED)
358
- return unless doctype.is_a?(DocumentType)
359
-
360
- doc.insert_before(doctype, doc.document_element)
361
- rescue DOMException::NotSupportedError
362
- nil
363
- end
364
-
365
- public
366
-
367
364
  # createHTMLDocument(title?) — a fresh HTML document (doctype + html > head,
368
365
  # body), with an optional <title>. A given title is a title element in the
369
366
  # head holding one Text node of exactly that data, "" included — which
370
367
  # the title setter's string-replace-all would leave empty.
371
368
  def create_html_document(title = nil)
372
369
  doc = Document.new(nil, backend_doc: Backend.parse("<!DOCTYPE html><html><head></head><body></body></html>"))
370
+ # Its origin is the associated document's (DOM createHTMLDocument step 8).
371
+ doc.__internal_set_creator__(@document)
373
372
  unless title.nil? || title.equal?(Bridge::UNDEFINED)
374
373
  element = doc.head.append_child(doc.create_element("title"))
375
374
  element.append_child(doc.create_text_node(title.to_s))
@@ -386,6 +385,10 @@ module Dommy
386
385
  when "createDocumentType"
387
386
  create_document_type(args[0], args[1], args[2])
388
387
  when "createDocument"
388
+ # namespace and qualifiedName are required (either may be null, but
389
+ # must be present); doctype is optional.
390
+ raise Bridge::TypeError, "createDocument requires 2 arguments." if args.length < 2
391
+
389
392
  create_document(args[0], args[1], args[2])
390
393
  when "createHTMLDocument"
391
394
  # title is an OPTIONAL DOMString: a missing or undefined argument leaves
@@ -399,6 +402,27 @@ module Dommy
399
402
  has_feature
400
403
  end
401
404
  end
405
+
406
+ private
407
+
408
+ # Place `doctype` (a DocumentType passed to createDocument) as `doc`'s first
409
+ # child. createDocument appends the doctype node itself (step 5, before
410
+ # step 6 appends the element), so it goes through the ordinary insert: the
411
+ # adoption re-binds the caller's wrapper, and `xmlDoc.firstChild ===
412
+ # doctype`, its parentNode and ownerDocument all follow.
413
+ #
414
+ # No-op for nil/undefined. Any other non-DocumentType is a TypeError: the
415
+ # IDL converts `doctype` to `DocumentType?`, so `createDocument(ns, name,
416
+ # false)` throws rather than ignoring the value. A doctype the XML backend
417
+ # cannot hold is left unplaced rather than thrown: only an XML
418
+ # serialization would validate its contents.
419
+ def adopt_doctype_into(doc, doctype)
420
+ return if doctype.nil? || doctype.equal?(Bridge::UNDEFINED)
421
+
422
+ doc.insert_before(doctype, doc.document_element)
423
+ rescue DOMException::NotSupportedError
424
+ nil
425
+ end
402
426
  end
403
427
 
404
428
  # `document` — the entry point for DOM construction and querying.
@@ -414,6 +438,7 @@ module Dommy
414
438
  include Internal::DocumentLiveRanges
415
439
  include Internal::DocumentInteractionState
416
440
  include EventTarget
441
+ extend Internal::EventHandlers::AnswersIdlAttributes
417
442
  include Node
418
443
 
419
444
  attr_reader :backend_doc
@@ -468,11 +493,12 @@ module Dommy
468
493
 
469
494
 
470
495
 
471
- # A by-id/class/tag index of the backend element tree, memoized per DOM
472
- # generation, for SelectorMatcher's document-scoped fast path (or nil to tell
473
- # the caller to walk). Rebuilt lazily only after a mutation bumps
474
- # dom_generation, so it costs one tree walk per generation and pays off when
475
- # several queries run before the next mutation.
496
+ # A by-id/class/tag index of the backend element tree, memoized per
497
+ # #__internal_selector_index_generation__, for SelectorMatcher's
498
+ # document-scoped fast path (or nil to tell the caller to walk). Rebuilt
499
+ # lazily only after a mutation that can change it (the tree, or an `id` /
500
+ # `class`), so it costs one tree walk per such mutation and pays off when
501
+ # several queries run before the next one.
476
502
  #
477
503
  # Adaptive bypass: if the index keeps getting invalidated after serving only a
478
504
  # handful of queries (a mutation-between-every-query workload, where building
@@ -484,7 +510,7 @@ module Dommy
484
510
  SELECTOR_INDEX_RETEST_GAP = 64 # generations to wait before re-testing a bypass
485
511
 
486
512
  def __internal_selector_index__
487
- gen = dom_generation
513
+ gen = __internal_selector_index_generation__
488
514
  if @__sel_idx_gen != gen
489
515
  if @__sel_idx
490
516
  if @__sel_idx_served.to_i < SELECTOR_INDEX_MIN_REUSE
@@ -576,6 +602,25 @@ module Dommy
576
602
  nil
577
603
  end
578
604
 
605
+ # A `->(element, name, body, window_handler) {}` set by the JS layer that
606
+ # compiles an event handler content attribute's body into a function (the
607
+ # element, its form owner and the document in its scope; none for a
608
+ # Window's handler, whose `onerror` takes five arguments), raising the
609
+ # SyntaxError of a body that does not parse. nil = no engine, nothing runs.
610
+ attr_accessor :event_handler_compiler
611
+
612
+ # Activate the event handler content attributes of every element the
613
+ # parser built (it runs no attribute change steps), as though each had
614
+ # just been set. Run at boot, before any script, and safe to repeat.
615
+ def __internal_activate_parsed_event_handlers__
616
+ selector = (Internal::EventHandlers::GLOBAL | Internal::EventHandlers::WINDOW).map { |name| "[#{name}]" }.join(",")
617
+ parsed_css(selector).each do |node|
618
+ element = wrap_node(node)
619
+ element.__internal_wire_inline_handler__(nil) if element.respond_to?(:__internal_wire_inline_handler__)
620
+ end
621
+ nil
622
+ end
623
+
579
624
  def initialize(host = nil, backend_doc: nil, default_view: nil)
580
625
  @host = host
581
626
  @default_view = default_view
@@ -583,7 +628,6 @@ module Dommy
583
628
  @node_factory = Internal::NodeFactory.new(self, @node_wrapper_cache)
584
629
  @observer_manager = Internal::ObserverManager.new
585
630
  @shadow_registry = Internal::ShadowRootRegistry.new
586
- @cookie_jar = Internal::CookieJar.new
587
631
  @template_content_registry = Internal::TemplateContentRegistry.new(self)
588
632
  @mutation_coordinator = Internal::MutationCoordinator.new(self, @observer_manager)
589
633
  # Weak, like @live_ranges: a NodeIterator is consulted by every removal for
@@ -603,14 +647,28 @@ module Dommy
603
647
  @__current_script__ = nil
604
648
  end
605
649
 
606
- # Whether this is an "HTML document" in the DOM sense (created by the HTML
607
- # parser / `text/html`), as opposed to an XML document. It drives the
608
- # case-folding rules: `createElement` lowercases names and `Element#tagName`
609
- # uppercases HTML-namespace names only in an HTML document. An XML or XHTML
610
- # document (e.g. an `application/xhtml+xml` / `text/xml` resource) preserves
611
- # case.
650
+ # Whether this is an "HTML document" in the DOM sense (DOM's type "html"),
651
+ # as opposed to an XML document. It drives the case-folding rules:
652
+ # `createElement` lowercases names and `Element#tagName` uppercases
653
+ # HTML-namespace names only in an HTML document. An XML or XHTML document
654
+ # (e.g. an `application/xhtml+xml` / `text/xml` resource) preserves case.
655
+ #
656
+ # The type is not the content type: a text document (a text/plain or
657
+ # text/css response shown in a <pre>) is an HTML document too. Every way of
658
+ # making an XML document gives it an XML MIME type, so that is what decides.
612
659
  def html_document?
613
- @content_type == "text/html"
660
+ !Internal::MimeType.xml?(@content_type)
661
+ end
662
+
663
+ # Whether this document is DOM's XMLDocument — the interface
664
+ # `implementation.createDocument` returns (and a clone of one keeps). A
665
+ # DOMParser result is the base Document even when parsed from XML, so this
666
+ # rides on the instance rather than the content type. Together with
667
+ # #html_document? it decides the document's most-derived interface.
668
+ def xml_document? = @xml_document == true
669
+
670
+ def __internal_xml_document__=(value)
671
+ @xml_document = value
614
672
  end
615
673
 
616
674
  # `document.compatMode` — "BackCompat" in quirks mode, "CSS1Compat" in
@@ -661,6 +719,29 @@ module Dommy
661
719
  html_element&.__internal_set_attribute_value__("dir", value.to_s)
662
720
  end
663
721
 
722
+ # HTML §16 (obsolete features), the partial Document interface: fgColor,
723
+ # linkColor, vlinkColor, alinkColor and bgColor reflect the body element's
724
+ # text, link, vlink, alink and bgcolor content attributes — "if the body
725
+ # element is a body element (as opposed to a frameset element). When there
726
+ # is no body element or if it is a frameset element, the attributes must
727
+ # instead return the empty string on getting and do nothing on setting."
728
+ # The setters are [LegacyNullToEmptyString] (null writes "").
729
+ LEGACY_BODY_COLORS = {
730
+ fg_color: "text", link_color: "link", vlink_color: "vlink", alink_color: "alink", bg_color: "bgcolor"
731
+ }.freeze
732
+
733
+ LEGACY_BODY_COLORS.each do |name, attr|
734
+ define_method(name) do
735
+ element = body
736
+ element.is_a?(HTMLBodyElement) ? element.__internal_attribute_value__(attr).to_s : ""
737
+ end
738
+
739
+ define_method(:"#{name}=") do |value|
740
+ element = body
741
+ element.__internal_set_attribute_value__(attr, value.to_s) if element.is_a?(HTMLBodyElement)
742
+ end
743
+ end
744
+
664
745
  # Whether designMode is "on", which makes the whole document editable.
665
746
  def __internal_design_mode__? = @design_mode == "on"
666
747
 
@@ -759,7 +840,18 @@ module Dommy
759
840
  # "about:blank", not the empty string.
760
841
  def url
761
842
  view = @default_view
762
- view&.location ? view.location.href : "about:blank"
843
+ return view.location.href if view&.location
844
+
845
+ @creator_url || "about:blank"
846
+ end
847
+
848
+ # A document made by a parsing or creation API (DOMParser,
849
+ # DOMImplementation) takes its origin from `document` — the relevant
850
+ # global object's associated Document — and, for DOMParser, its URL too.
851
+ def __internal_set_creator__(document, url: nil)
852
+ @origin_document = document
853
+ @creator_url = url
854
+ nil
763
855
  end
764
856
 
765
857
  alias document_uri url
@@ -792,32 +884,88 @@ module Dommy
792
884
  nil
793
885
  end
794
886
 
795
- private def creator_base_url
796
- @creator_base_url if @creator_base_url && FALLBACK_BASE_URLS.include?(url)
887
+ # `document.domain` — the effective domain of the document's origin
888
+ # serialized: the domain a `document.domain =` set, or else the origin's
889
+ # host; "" for an opaque origin.
890
+ def domain
891
+ return @domain_override if @domain_override
892
+
893
+ host = Internal::Origin.host_of(origin)
894
+ host.nil? ? "" : host.to_s
797
895
  end
798
896
 
799
- # `document.domain` — host portion of the URL. Real browsers
800
- # restrict cross-origin reads of this; we just return the bare host.
801
- def domain
897
+ # The `document.domain` setter. It needs a browsing context and a tuple
898
+ # origin, and accepts only the current effective domain or a registrable
899
+ # suffix of it (everything else is a SecurityError). The agent cluster is
900
+ # site-keyed (no Origin-Agent-Cluster header), so the domain is then set.
901
+ #
902
+ # Without a public suffix list, a single label (a bare TLD such as "com")
903
+ # stands in for the public suffix.
904
+ def domain=(value)
802
905
  view = @default_view
803
- return "" unless view&.location
906
+ raise DOMException::SecurityError, "The document has no browsing context" unless view&.navigable?
907
+
908
+ current = Internal::Origin.host_of(origin)
909
+ raise DOMException::SecurityError, "The document's origin is opaque" if current.nil?
910
+
911
+ candidate = value.to_s
912
+ unless registrable_domain_suffix_or_equal?(candidate, current.to_s)
913
+ raise DOMException::SecurityError, "'#{candidate}' is not a suffix of '#{current}'"
914
+ end
804
915
 
805
- view.location.__js_get__("hostname").to_s
916
+ @domain_override = Internal::UrlParser.parse("http://#{candidate}/").host.to_s
806
917
  end
807
918
 
808
- # `document.origin` — serialized origin of the document URL, mirroring
809
- # `window.location.origin`. Empty when there is no associated window.
919
+ # `document.origin` — this document's origin, serialized (the same answer as
920
+ # `self.origin`). Empty when there is no associated window.
810
921
  def origin
811
922
  view = @default_view
812
- return "" unless view&.location
923
+ return @origin_document&.origin.to_s unless view&.location
813
924
 
814
925
  view.origin
815
926
  end
816
927
 
817
- # `document.referrer` — Dommy never has a referring page, so this
818
- # is always empty.
928
+ # `document.lastModified` — the source's last modification (the response's
929
+ # Last-Modified header, when the embedder passed one on), else the current
930
+ # time, in local time as "MM/DD/YYYY hh:mm:ss".
931
+ #
932
+ # Spec: https://html.spec.whatwg.org/#dom-document-lastmodified
933
+ def last_modified
934
+ (@last_modified_time || Time.now).getlocal.strftime("%m/%d/%Y %H:%M:%S")
935
+ end
936
+
937
+ # Record the document's source last-modified time: a Time, or an HTTP date
938
+ # string (a Last-Modified header value). One that does not parse leaves it
939
+ # unknown.
940
+ def __internal_set_last_modified__(value)
941
+ @last_modified_time =
942
+ case value
943
+ when Time then value
944
+ when nil then nil
945
+ else
946
+ begin
947
+ Time.httpdate(value.to_s.strip)
948
+ rescue ArgumentError
949
+ nil
950
+ end
951
+ end
952
+ nil
953
+ end
954
+
955
+ # `document.referrer` — the URL of the document that navigated here, as the
956
+ # embedder reports it (`__internal_referrer__=`); empty when there was none.
819
957
  def referrer
820
- ""
958
+ @referrer.to_s
959
+ end
960
+
961
+ def __internal_referrer__=(url)
962
+ @referrer = url
963
+ end
964
+
965
+ # Whether the document has completely finished loading: its `load` event
966
+ # has been fired. A document whose lifecycle no one replays is born loaded.
967
+ def __internal_completely_loaded__?
968
+ !@loading_lifecycle
821
969
  end
822
970
 
823
971
  # Live HTMLCollection helpers — each call re-queries the
@@ -889,7 +1037,7 @@ module Dommy
889
1037
  # document expose). Per spec, false for null / a non-Node.
890
1038
  def contains?(other)
891
1039
  return true if other.equal?(self)
892
- return false unless other.respond_to?(:__dommy_backend_node__)
1040
+ return false unless other.is_a?(Node)
893
1041
 
894
1042
  # Whose root the backend document node is. (The backend's #ancestors
895
1043
  # stops below the document, so it can't test document membership; the
@@ -954,39 +1102,47 @@ module Dommy
954
1102
  # it here with the same qualified name, namespace, prefix and value, owned
955
1103
  # by no element (importNode never attaches the copy to anything).
956
1104
  return import_attribute(node) if node.is_a?(Attr)
957
- return nil unless node.respond_to?(:__dommy_backend_node__)
1105
+ return nil unless node.is_a?(Node) && node.__dommy_backend_node__
1106
+
1107
+ # `(boolean or ImportNodeOptions) options = false`: missing or undefined
1108
+ # is the default, false. Null and an object convert to the dictionary,
1109
+ # whose `selfOnly` negates `deep` (so null clones deep) and whose
1110
+ # `customElementRegistry` (a scoped one, or this document's) is the
1111
+ # registry the copies fall back to. Anything else is a boolean, read
1112
+ # with ToBoolean.
1113
+ registry = nil
1114
+ deep = false if deep.equal?(Bridge::UNDEFINED)
1115
+ if deep.nil? || deep.is_a?(Hash)
1116
+ options = deep || {}
1117
+ deep = !Internal::WebIDL.boolean(options["selfOnly"])
1118
+ given = options.fetch("customElementRegistry", Bridge::UNDEFINED)
1119
+ unless given.equal?(Bridge::UNDEFINED)
1120
+ raise Bridge::TypeError, "customElementRegistry is not a CustomElementRegistry" unless given.is_a?(CustomElementRegistry)
1121
+ if !given.scoped? && !given.equal?(__internal_custom_element_registry__)
1122
+ raise DOMException::NotSupportedError, "a global registry other than the document's"
1123
+ end
958
1124
 
959
- # WebIDL `optional boolean deep = false`: a missing / undefined argument
960
- # is the default (false / shallow), not a truthy sentinel.
961
- deep = false if deep.nil? || deep.equal?(Bridge::UNDEFINED)
1125
+ registry = given
1126
+ end
1127
+ else
1128
+ deep = Internal::WebIDL.boolean(deep)
1129
+ end
1130
+ # "Clone a single node": the node's own registry — a global one standing
1131
+ # for this document's — and the fallback only for a node without one.
1132
+ fallback = registry || __internal_custom_element_registry__
1133
+ source = node.is_a?(Element) ? node.__internal_ce_registry__ : fallback
1134
+ registry = if source.nil? then fallback
1135
+ elsif source.scoped? then source
1136
+ else __internal_custom_element_registry__
1137
+ end
962
1138
  source_document = node.respond_to?(:document) ? node.document : self
963
1139
  copy = clone_into_doc(node.__dommy_backend_node__, deep, source_document)
964
1140
  apply_imported_cloning_steps(node.__dommy_backend_node__, copy, deep, source_document)
1141
+ __internal_enqueue_created_upgrades__(copy, registry) if copy.respond_to?(:element?)
1142
+ __internal_clone_shadow_roots__(node.__dommy_backend_node__, copy, deep, source_document) if copy.respond_to?(:element?)
965
1143
  wrap_node(copy)
966
1144
  end
967
1145
 
968
- # importNode is a clone, so the HTML cloning steps run for it as they do
969
- # for cloneNode (#__internal_apply_cloning_steps__): the live state Dommy
970
- # keeps on a wrapper — an input's dirty value, a script's "already
971
- # started" — is copied onto the new node. The originals' wrappers belong
972
- # to the source document, so they are looked up there.
973
- def apply_imported_cloning_steps(src_root, copy_root, deep, source_document)
974
- return unless source_document.respond_to?(:__internal_peek_wrapper__)
975
-
976
- src_nodes = deep ? Internal::NodeTraversal.subtree_nodes(src_root) : [src_root]
977
- copy_nodes = deep ? Internal::NodeTraversal.subtree_nodes(copy_root) : [copy_root]
978
- return unless src_nodes.length == copy_nodes.length
979
-
980
- src_nodes.zip(copy_nodes).each do |orig, copy|
981
- state = source_document.__internal_peek_wrapper__(orig)&.then { |w| w.respond_to?(:__internal_cloning_state__) && w.__internal_cloning_state__ }
982
- next unless state
983
-
984
- wrapper = wrap_node(copy)
985
- wrapper.__internal_apply_cloning_state__(state) if wrapper.respond_to?(:__internal_apply_cloning_state__)
986
- end
987
- end
988
- private :apply_imported_cloning_steps
989
-
990
1146
  def import_attribute(attr)
991
1147
  Attr.new(
992
1148
  attr.name,
@@ -1023,11 +1179,6 @@ module Dommy
1023
1179
  node_adopter.adopt_backend_node(node, source_document)
1024
1180
  end
1025
1181
 
1026
- def node_adopter
1027
- @node_adopter ||= Internal::NodeAdopter.new(self)
1028
- end
1029
- private :node_adopter
1030
-
1031
1182
  # HTML "cloning steps": a cloned node copies interface-specific live state
1032
1183
  # that the content attributes don't capture — an input's dirty value and
1033
1184
  # checkedness, a textarea's dirty value, etc. Dommy keeps that state on the
@@ -1057,6 +1208,60 @@ module Dommy
1057
1208
  end
1058
1209
  end
1059
1210
 
1211
+ # DOM "clone a node" step 6, for `copy_root` — a clone made in this
1212
+ # document of `src_root` (of `source_document`), with or without its
1213
+ # subtree — and, when `deep`, every element of the copy: the clone of a
1214
+ # shadow host whose shadow root is clonable gets a shadow root of its own
1215
+ # with the same mode, delegates focus, serializable, slot assignment,
1216
+ # declarative and keep-custom-element-registry-null, clonable, holding
1217
+ # clones of the shadow root's children. A shallow clone still does this
1218
+ # for the host itself. The backend copied the subtree (and template
1219
+ # contents) in one go, so the two trees are walked in lockstep.
1220
+ def __internal_clone_shadow_roots__(src_root, copy_root, deep, source_document = self)
1221
+ return unless source_document.__internal_any_shadow_roots__?
1222
+
1223
+ clone_shadow_roots_walk(src_root, copy_root, deep, source_document)
1224
+ end
1225
+
1226
+ def clone_shadow_roots_walk(src, copy, deep, source_document)
1227
+ return unless src.element? || src.is_a?(Backend.document_fragment_class)
1228
+
1229
+ if src.element?
1230
+ shadow = source_document.__internal_shadow_root_for_host__(src)
1231
+ clone_shadow_root(shadow, copy, source_document) if shadow&.clonable
1232
+ end
1233
+ return unless deep
1234
+
1235
+ src_children = src.children.to_a
1236
+ copy_children = copy.children.to_a
1237
+ return unless src_children.length == copy_children.length
1238
+
1239
+ src_children.zip(copy_children).each { |s, c| clone_shadow_roots_walk(s, c, true, source_document) }
1240
+ return unless src.element? && @template_content_registry.template_node?(src)
1241
+
1242
+ src_contents = source_document.__internal_template_registry__.existing_contents(src)
1243
+ copy_contents = @template_content_registry.existing_contents(copy)
1244
+ clone_shadow_roots_walk(src_contents, copy_contents, true, source_document) if src_contents && copy_contents
1245
+ end
1246
+
1247
+ def clone_shadow_root(shadow, copy, source_document)
1248
+ host = wrap_node(copy)
1249
+ # The shadow root's registry; a global one stands for this document's.
1250
+ registry = shadow.__internal_registry_set__? ? shadow.__internal_custom_element_registry__ : :document
1251
+ registry = :document if registry && registry != :document && !registry.scoped?
1252
+ root = host.__internal_attach_shadow_root__(
1253
+ mode: shadow.mode, delegates_focus: shadow.delegates_focus, serializable: shadow.serializable,
1254
+ slot_assignment: shadow.slot_assignment, clonable: true, registry: registry
1255
+ )
1256
+ root.__internal_declarative__ = shadow.__internal_declarative__?
1257
+ root.__internal_keep_registry_null__ = shadow.__internal_keep_registry_null__?
1258
+ shadow.child_nodes.to_a.each do |child|
1259
+ child_copy = source_document.equal?(self) ? child.clone_node(true) : import_node(child, true)
1260
+ root.append_child(child_copy)
1261
+ end
1262
+ end
1263
+ private :clone_shadow_roots_walk, :clone_shadow_root
1264
+
1060
1265
  # Legacy `document.createEvent("EventName")` factory. The DOM Standard
1061
1266
  # matches the type ASCII case-insensitively against a fixed alias table, and
1062
1267
  # throws NotSupportedError for anything else — including the plural forms it
@@ -1101,8 +1306,22 @@ module Dommy
1101
1306
  # don't apply to a layout-less DOM. They exist so callers don't
1102
1307
  # hit NoMethodError; semantics are documented as no-op.
1103
1308
 
1309
+ # `document.hasFocus()` — HTML's "has focus steps", with the top-level
1310
+ # page always holding system focus: a document with no browsing context
1311
+ # has no focus, a top-level one has it, and a nested one has it when its
1312
+ # frame is the focused element of a parent document that has it.
1313
+ #
1314
+ # Spec: https://html.spec.whatwg.org/#has-focus-steps
1104
1315
  def has_focus?
1105
- true
1316
+ view = @default_view
1317
+ return false unless view
1318
+
1319
+ frame = view.frame_element
1320
+ return true unless frame
1321
+ return false unless frame.is_connected?
1322
+
1323
+ parent = frame.owner_document
1324
+ parent.has_focus? && parent.__internal_focused_element__.equal?(frame)
1106
1325
  end
1107
1326
 
1108
1327
  alias has_focus has_focus?
@@ -1119,6 +1338,12 @@ module Dommy
1119
1338
  Range.new(self)
1120
1339
  end
1121
1340
 
1341
+ # The showing popovers and the state that orders their showing and
1342
+ # hiding (Internal::PopoverStack).
1343
+ def __internal_popover_stack__
1344
+ @popover_stack ||= Internal::PopoverStack.new
1345
+ end
1346
+
1122
1347
  # Fullscreen API — no actual fullscreen mode, just track which
1123
1348
  # element claimed it. `element.requestFullscreen()` sets it; this
1124
1349
  # is the read side.
@@ -1129,6 +1354,8 @@ module Dommy
1129
1354
  @fullscreen_element = element
1130
1355
  return if previous == element
1131
1356
 
1357
+ # :fullscreen and :modal match the fullscreen element.
1358
+ __internal_note_selector_state_change__
1132
1359
  dispatch_event(Event.new("fullscreenchange"))
1133
1360
  end
1134
1361
 
@@ -1136,6 +1363,7 @@ module Dommy
1136
1363
  return PromiseValue.resolve(@default_view, nil) if @fullscreen_element.nil?
1137
1364
 
1138
1365
  @fullscreen_element = nil
1366
+ __internal_note_selector_state_change__
1139
1367
  dispatch_event(Event.new("fullscreenchange"))
1140
1368
  PromiseValue.resolve(@default_view, nil)
1141
1369
  end
@@ -1254,10 +1482,18 @@ module Dommy
1254
1482
  end
1255
1483
  end
1256
1484
 
1485
+ # Pre-insert refuses an element before a doctype `child` or one that
1486
+ # follows it; replace (`exclude` is the child replaced) only one that
1487
+ # follows it, since the doctype being replaced goes.
1488
+ doctype_in_the_way =
1489
+ if exclude
1490
+ doctype_after_child?(existing, child_bn, inclusive: false)
1491
+ else
1492
+ (child_bn && child_bn.node_type == 10) || doctype_after_child?(existing, child_bn)
1493
+ end
1257
1494
  if elements > 1
1258
1495
  raise DOMException::HierarchyRequestError, "Only one element may be a child of a document."
1259
- elsif elements == 1 &&
1260
- (has_element || (child_bn && child_bn.node_type == 10) || doctype_after_child?(existing, child_bn))
1496
+ elsif elements == 1 && (has_element || doctype_in_the_way)
1261
1497
  raise DOMException::HierarchyRequestError, "An element cannot be inserted here."
1262
1498
  end
1263
1499
 
@@ -1272,7 +1508,7 @@ module Dommy
1272
1508
  # createDocument, which the DOM never lets throw here, catches this and
1273
1509
  # leaves the doctype out.
1274
1510
  def ensure_doctypes_have_nodes!(args)
1275
- return unless args.any? { |a| a.is_a?(Dommy::DocumentType) && !a.respond_to?(:__dommy_backend_node__) }
1511
+ return unless args.any? { |a| a.is_a?(Dommy::DocumentType) && a.__dommy_backend_node__.nil? }
1276
1512
 
1277
1513
  raise DOMException::NotSupportedError, "This doctype cannot be inserted: the backend could not create it."
1278
1514
  end
@@ -1310,19 +1546,20 @@ module Dommy
1310
1546
  existing[0...idx].any? { |c| c.node_type == 1 }
1311
1547
  end
1312
1548
 
1313
- # Whether any doctype child follows `child_bn` in the document's child list.
1314
- def doctype_after_child?(existing, child_bn)
1549
+ # Whether a doctype child is at `child_bn` (when `inclusive`) or follows
1550
+ # it in the document's child list.
1551
+ def doctype_after_child?(existing, child_bn, inclusive: true)
1315
1552
  idx = child_bn && existing.index { |c| c == child_bn }
1316
1553
  return false unless idx
1317
1554
 
1318
- existing[idx..].any? { |c| c.node_type == 10 }
1555
+ existing[(inclusive ? idx : idx + 1)..].any? { |c| c.node_type == 10 }
1319
1556
  end
1320
1557
 
1321
1558
  # Append a node as a child of the document itself (e.g. a comment alongside
1322
1559
  # the document element). Adopts the node into this document.
1323
1560
  def append_child(node)
1324
1561
  ensure_document_insertion_validity!([node], nil)
1325
- return node unless node.respond_to?(:__dommy_backend_node__)
1562
+ return node unless node.is_a?(Node) && node.__dommy_backend_node__
1326
1563
 
1327
1564
  # An append has a null reference child, so insert step 5 shifts nothing.
1328
1565
  nodes = document_insertion_nodes([node])
@@ -1338,6 +1575,8 @@ module Dommy
1338
1575
  # a doctype only ahead of the document element), so each forwards to the
1339
1576
  # `document_*` implementation that carries them.
1340
1577
  def insert_before(node, reference)
1578
+ Internal::WebIDL.node!(node)
1579
+ Internal::WebIDL.nullable_node!(reference)
1341
1580
  document_insert_before(node, reference)
1342
1581
  end
1343
1582
 
@@ -1382,7 +1621,7 @@ module Dommy
1382
1621
  detach_node(bn, moving: true) # steps 10-11, 14
1383
1622
  ref_bn = nil if ref_bn && ref_bn.parent != @backend_doc
1384
1623
  __internal_ranges_will_insert__(@backend_doc, ref_bn, 1) # step 16
1385
- new_previous = ref_bn ? ref_bn.previous_sibling : @backend_doc.children.to_a.last
1624
+ new_previous = ref_bn ? ref_bn.previous_sibling : @backend_doc.last_child
1386
1625
  ref_bn ? ref_bn.add_previous_sibling(bn) : @backend_doc.add_child(bn) # step 18
1387
1626
  if old_parent
1388
1627
  notify_child_list_mutation(
@@ -1397,14 +1636,13 @@ module Dommy
1397
1636
  nil
1398
1637
  end
1399
1638
 
1400
- # The backend node an argument to `moveBefore` stands for. A Dommy::Document
1401
- # has no `__dommy_backend_node__` of its own, but it is a node the algorithm
1402
- # has to see (step 2 rejects it, step 3 measures its parentage).
1639
+ # The backend node an argument to `moveBefore` stands for. A
1640
+ # Dommy::Document's `__dommy_backend_node__` is nil, but it is a node the
1641
+ # algorithm has to see (step 2 rejects it, step 3 measures its parentage).
1403
1642
  def move_backend_node(value)
1404
1643
  return value.backend_doc if value.is_a?(Dommy::Document)
1405
- return nil unless value.respond_to?(:__dommy_backend_node__)
1406
1644
 
1407
- value.__dommy_backend_node__
1645
+ value.__dommy_backend_node__ if value.is_a?(Node)
1408
1646
  end
1409
1647
 
1410
1648
  # "Move" steps 1-6 with a document new parent.
@@ -1454,11 +1692,12 @@ module Dommy
1454
1692
  # ParentNode / Node mutation on the document's direct children (the doctype
1455
1693
  # and the document element).
1456
1694
  def document_insert(args, prepend:)
1457
- ref_bn = prepend ? @backend_doc.children.first : nil
1695
+ args = Internal::InsertionPoint.convert_nodes_into_a_node(self, args)
1696
+ ref_bn = prepend ? @backend_doc.first_child : nil
1458
1697
  ensure_document_insertion_validity!(args, ref_bn)
1459
1698
  __internal_ranges_will_insert__(@backend_doc, ref_bn, document_insertion_count(args))
1460
1699
  nodes = document_insertion_nodes(args)
1461
- if prepend && (first = @backend_doc.children.first)
1700
+ if prepend && (first = @backend_doc.first_child)
1462
1701
  nodes.reverse_each { |n| first.add_previous_sibling(n) }
1463
1702
  else
1464
1703
  nodes.each { |n| @backend_doc.add_child(n) }
@@ -1468,6 +1707,7 @@ module Dommy
1468
1707
  end
1469
1708
 
1470
1709
  def document_replace_children(args)
1710
+ args = Internal::InsertionPoint.convert_nodes_into_a_node(self, args)
1471
1711
  # replaceChildren removes the current children first, so the validity
1472
1712
  # checks ignore them (whatwg/dom#1045).
1473
1713
  ensure_document_insertion_validity!(args, nil, ignore_existing: true)
@@ -1480,7 +1720,13 @@ module Dommy
1480
1720
  end
1481
1721
 
1482
1722
  def document_remove_child(node)
1483
- return __internal_remove_doctype__(node) if node.is_a?(DocumentType)
1723
+ if node.is_a?(DocumentType)
1724
+ bn = backend_node(node)
1725
+ raise DOMException::NotFoundError, "node is not a child of this document" if bn && bn.parent != @backend_doc
1726
+
1727
+ __internal_remove_doctype__(node)
1728
+ return node
1729
+ end
1484
1730
 
1485
1731
  bn = backend_node(node)
1486
1732
  raise DOMException::NotFoundError, "node is not a child of this document" unless bn && bn.parent == @backend_doc
@@ -1594,7 +1840,7 @@ module Dommy
1594
1840
  # (at the document start) or after it (just before the document element).
1595
1841
  def __internal_insert_at_doctype__(nodes, after:)
1596
1842
  bns = nodes.filter_map { |n| backend_node(n) }
1597
- anchor = after ? backend_document_element : @backend_doc.children.first
1843
+ anchor = after ? backend_document_element : @backend_doc.first_child
1598
1844
  __internal_ranges_will_insert__(@backend_doc, anchor, bns.size)
1599
1845
  if after
1600
1846
  anchor ? bns.each { |n| anchor.add_previous_sibling(n) } : bns.each { |n| @backend_doc.add_child(n) }
@@ -1609,10 +1855,13 @@ module Dommy
1609
1855
  # deep clone then copies the original's children into it — so nothing
1610
1856
  # sprouts an html/head/body of its own, and a document with no children
1611
1857
  # clones to one with no children.
1612
- def clone_node(deep)
1858
+ def clone_node(deep = false)
1859
+ deep = Internal::WebIDL.boolean(deep)
1613
1860
  copy = Document.new(nil, backend_doc: Backend.empty_document_like(@backend_doc))
1614
1861
  copy.content_type = @content_type
1862
+ copy.__internal_xml_document__ = @xml_document
1615
1863
  copy.__internal_quirks_mode__ = quirks_mode?
1864
+ copy.__internal_allow_declarative_shadow_roots__ = __internal_allow_declarative_shadow_roots__?
1616
1865
  return copy unless deep
1617
1866
 
1618
1867
  child_nodes.each { |child| copy.append_child(copy.import_node(child, true)) }
@@ -1620,7 +1869,7 @@ module Dommy
1620
1869
  end
1621
1870
 
1622
1871
  def backend_node(node)
1623
- node.respond_to?(:__dommy_backend_node__) ? node.__dommy_backend_node__ : nil
1872
+ node.__dommy_backend_node__ if node.is_a?(Node)
1624
1873
  end
1625
1874
 
1626
1875
  # Like `backend_node`, but first adopts a node that belongs to another
@@ -1662,7 +1911,7 @@ module Dommy
1662
1911
  def document_insertion_count(args) = Internal::InsertionPoint.count(args)
1663
1912
 
1664
1913
  def adopted_backend_node(node)
1665
- return nil unless node.respond_to?(:__dommy_backend_node__)
1914
+ return nil unless node.is_a?(Node) && node.__dommy_backend_node__
1666
1915
 
1667
1916
  if node.respond_to?(:document) && !node.document.equal?(self)
1668
1917
  return adopt_node(node)&.__dommy_backend_node__
@@ -1673,19 +1922,42 @@ module Dommy
1673
1922
  bn
1674
1923
  end
1675
1924
 
1676
- # Delegate to CookieJar
1677
-
1925
+ # `document.cookie` (HTML §3.1.5): the cookie-string of the window's
1926
+ # cookie jar for this document's URL, as a "non-HTTP" API sees it (no
1927
+ # HttpOnly cookies). A cookie-averse document — no browsing context, or a
1928
+ # URL that is not http(s) — reads "" and ignores writes; one with an
1929
+ # opaque origin throws SecurityError.
1678
1930
  def cookie
1679
- @cookie_jar.to_cookie_string
1931
+ return "" if __internal_cookie_averse__?
1932
+
1933
+ raise_if_opaque_for_cookies!
1934
+ @default_view.cookie_jar.cookie_string(url, http: false)
1680
1935
  end
1681
1936
 
1682
1937
  def cookie=(value)
1683
- @cookie_jar.set_cookie(value)
1938
+ return nil if __internal_cookie_averse__?
1939
+
1940
+ raise_if_opaque_for_cookies!
1941
+ @default_view.cookie_jar.store(value.to_s, url, http: false)
1684
1942
  nil
1685
1943
  end
1686
1944
 
1687
- def create_element_ns(namespace_uri, qualified_name)
1688
- @node_factory.create_element_ns(namespace_uri, qualified_name)
1945
+ # HTML "cookie-averse Document object".
1946
+ def __internal_cookie_averse__?
1947
+ return true unless @default_view.respond_to?(:cookie_jar)
1948
+
1949
+ !url.to_s.match?(/\Ahttps?:/i)
1950
+ end
1951
+
1952
+ def raise_if_opaque_for_cookies!
1953
+ return unless @default_view.origin == "null"
1954
+
1955
+ raise DOMException::SecurityError, "the document's origin is opaque"
1956
+ end
1957
+ private :raise_if_opaque_for_cookies!
1958
+
1959
+ def create_element_ns(namespace_uri, qualified_name, options = nil)
1960
+ @node_factory.create_element_ns(namespace_uri, qualified_name, options)
1689
1961
  end
1690
1962
 
1691
1963
  def get_elements_by_tag_name(name)
@@ -1700,27 +1972,68 @@ module Dommy
1700
1972
  HTMLCollection.elements_by_tag_name_ns(@backend_doc, self, namespace, local_name)
1701
1973
  end
1702
1974
 
1703
- # `document.write(html)` — legacy API. Appends parsed nodes to the
1704
- # body. Real browsers only re-stream the DOM during initial parse;
1705
- # this stub is enough for tests that fire write() during teardown.
1975
+ # ----- Dynamic markup insertion (document.open / write / close) -----
1976
+ #
1977
+ # Dommy has no incremental parser, so this models the spec's input stream
1978
+ # without one:
1979
+ #
1980
+ # - While the page's own scripts boot (readyState "loading" and a script
1981
+ # running) the parser counts as active with a script nesting level above
1982
+ # 0: `open()` is a no-op that returns the document, and `write()` inserts
1983
+ # the parsed markup right after the running script (where the insertion
1984
+ # point would be), or at the end of the body when that script is not in
1985
+ # the body.
1986
+ # - Otherwise `write()` with no script-created parser runs the document open
1987
+ # steps first (the page is gone), and every write re-parses the whole
1988
+ # input written since `open()` as a fresh document, whose children replace
1989
+ # the document's. Earlier writes' nodes are therefore not the same objects
1990
+ # after a later write, and scripts in written markup do not run.
1991
+ #
1992
+ # Spec: https://html.spec.whatwg.org/#dynamic-markup-insertion
1993
+
1994
+ # `document.write(...text)` — the document write steps with lineFeed false.
1706
1995
  def write(*args)
1707
- html = args.join
1708
- fragment = Parser.fragment(html, owner_doc: @backend_doc)
1709
- removed = []
1710
- added = fragment.children.to_a
1711
- body_node = body.__dommy_backend_node__
1712
- added.each { |node| body_node.add_child(node) }
1713
- notify_child_list_mutation(target_node: body_node, added_nodes: added, removed_nodes: removed)
1714
- nil
1996
+ document_write(args, line_feed: false)
1715
1997
  end
1716
1998
 
1717
- # No-ops — real browsers reset the DOM on `open()` and flush
1718
- # pending writes on `close()`. We don't model the parse pipeline.
1719
- def open
1720
- nil
1999
+ # `document.writeln(...text)` — the document write steps with lineFeed true.
2000
+ def writeln(*args)
2001
+ document_write(args, line_feed: true)
1721
2002
  end
1722
2003
 
2004
+ # `document.open()` — the document open steps; returns the document. With
2005
+ # three arguments it is `window.open(url, name, features)` instead.
2006
+ #
2007
+ # Spec: https://html.spec.whatwg.org/#dom-document-open
2008
+ def open(*args)
2009
+ return open_window(args) if args.length >= 3
2010
+
2011
+ document_open_steps
2012
+ end
2013
+
2014
+ # `document.close()` — closes the input stream a script-created parser
2015
+ # reads: the parser reaches EOF and "stops parsing", so the document goes
2016
+ # "interactive" (DOMContentLoaded) then "complete" (load at the window,
2017
+ # when there is one). Dispatched synchronously rather than as queued tasks.
2018
+ #
2019
+ # Spec: https://html.spec.whatwg.org/#dom-document-close
1723
2020
  def close
2021
+ raise DOMException::InvalidStateError, "close() is not supported on an XML document" unless html_document?
2022
+ raise_if_markup_insertion_forbidden!("close")
2023
+ return nil if @script_created_input.nil?
2024
+
2025
+ # The parser reaches EOF. With nothing written it has seen no doctype
2026
+ # (quirks mode) and, into an empty document, builds html/head/body.
2027
+ if @script_created_input.empty?
2028
+ if @backend_doc.children.to_a.empty?
2029
+ reparse_script_created_input
2030
+ else
2031
+ @quirks_mode = true
2032
+ end
2033
+ end
2034
+ @script_created_input = nil
2035
+ __internal_set_ready_state__("interactive")
2036
+ __internal_set_ready_state__("complete")
1724
2037
  nil
1725
2038
  end
1726
2039
 
@@ -1760,12 +2073,11 @@ module Dommy
1760
2073
  end
1761
2074
 
1762
2075
  def __js_get__(key)
1763
- if key.start_with?("on") && key.length > 2
1764
- # An event handler IDL attribute (GlobalEventHandlers /
1765
- # DocumentAndElementEventHandlers, plus onreadystatechange and
1766
- # onvisibilitychange): the registered handler, or null when unset —
1767
- # matching Element's on* getter.
1768
- return @on_handlers&.[](event_name_from_on(key))
2076
+ if Internal::EventHandlers.idl_attribute?(self, key)
2077
+ # An event handler IDL attribute Document declares (GlobalEventHandlers,
2078
+ # plus onreadystatechange, onvisibilitychange, onfullscreenchange, …):
2079
+ # the registered handler, or null when unset — matching Element's.
2080
+ return on_handler(event_name_from_on(key))
1769
2081
  end
1770
2082
 
1771
2083
  case key
@@ -1791,10 +2103,17 @@ module Dommy
1791
2103
  read_title
1792
2104
  when "dir"
1793
2105
  dir
2106
+ when "fgColor" then fg_color
2107
+ when "linkColor" then link_color
2108
+ when "vlinkColor" then vlink_color
2109
+ when "alinkColor" then alink_color
2110
+ when "bgColor" then bg_color
1794
2111
  when "cookie"
1795
2112
  cookie
1796
2113
  when "nodeType"
1797
2114
  9
2115
+ when "customElementRegistry"
2116
+ __internal_custom_element_registry__
1798
2117
  when "isConnected"
1799
2118
  # A document is its own shadow-including root, so it is always connected.
1800
2119
  true
@@ -1822,7 +2141,7 @@ module Dommy
1822
2141
  when "designMode"
1823
2142
  @design_mode || "off"
1824
2143
  when "lastModified"
1825
- @last_modified || "01/01/1970 00:00:00"
2144
+ last_modified
1826
2145
  when "readyState"
1827
2146
  # "complete" by default (the document is fully parsed before scripts
1828
2147
  # run); an embedder can replay "loading" → "interactive" → "complete"
@@ -1892,6 +2211,15 @@ module Dommy
1892
2211
  end
1893
2212
  end
1894
2213
 
2214
+ # The elements of this document tree that can give its Window a named
2215
+ # property (HTML §7.2.2.3 "Named access on the Window object"), in tree
2216
+ # order: navigable containers, elements with an id, and embed / form /
2217
+ # img / object elements with a name. Window#__js_named_props__ applies the
2218
+ # spec's rules to them.
2219
+ def __internal_window_named_candidates__
2220
+ @backend_doc.css("[id], [name], iframe, frame").filter_map { |node| wrap_node(node) }
2221
+ end
2222
+
1895
2223
  # The document's supported property names (for `"name" in document`): for
1896
2224
  # each exposed element in tree order, its id when it is a named element with
1897
2225
  # that name, then its name.
@@ -1906,6 +2234,13 @@ module Dommy
1906
2234
  names.uniq
1907
2235
  end
1908
2236
 
2237
+ # The named getter on its own, for the bridge to read a supported name
2238
+ # BEFORE the builtins ([LegacyOverrideBuiltIns]: `<form name=body>` makes
2239
+ # `document.body` that form).
2240
+ def __js_named_get__(name)
2241
+ document_named_property(name)
2242
+ end
2243
+
1909
2244
  # Resolve a document named-getter property: nil when unsupported, a single
1910
2245
  # element (a named iframe yields its content window), or an HTMLCollection
1911
2246
  # when several elements share the name.
@@ -1922,40 +2257,8 @@ module Dommy
1922
2257
  el.local_name.to_s.casecmp?("iframe") && el.respond_to?(:content_window) ? (el.content_window || el) : el
1923
2258
  end
1924
2259
 
1925
- private
1926
-
1927
- # "The html element": the document element when it is an HTML <html>.
1928
- def html_element
1929
- root = document_element
1930
- root if root.is_a?(HTMLElement) && root.local_name == "html"
1931
- end
1932
-
1933
- # An element is a named element with the name `name` when it is one of the
1934
- # exposed kinds and either carries that `name`, or is an object with that
1935
- # `id`, or is an img whose id it is and which also has a non-empty name.
1936
- def named_element?(node, name)
1937
- return false if name.empty?
1938
- return false unless %w[embed form iframe img object].include?(node.name.to_s.downcase)
1939
- own_name = Backend.no_namespace_attribute_value(node, "name")
1940
- return true if own_name == name
1941
- return true if node.name.to_s.casecmp?("object") && Backend.no_namespace_attribute_value(node, "id") == name
1942
-
1943
- node.name.to_s.casecmp?("img") && Backend.no_namespace_attribute_value(node, "id") == name && !own_name.to_s.empty?
1944
- end
1945
-
1946
- # Elements the document's named getter exposes, in tree order.
1947
- def named_getter_nodes
1948
- @backend_doc.css("embed, form, iframe, img, object")
1949
- end
1950
-
1951
- def document_named_property_nodes(name)
1952
- named_getter_nodes.select { |node| named_element?(node, name) }.map { |node| wrap_node(node) }.compact
1953
- end
1954
-
1955
- public
1956
-
1957
2260
  def __js_set__(key, value)
1958
- if key.start_with?("on") && key.length > 2
2261
+ if Internal::EventHandlers.idl_attribute?(self, key)
1959
2262
  # `document.onXxx = fn` registers fn as a single named handler; nil
1960
2263
  # removes it. Without this the assignment became a plain JS expando and
1961
2264
  # the handler never fired (e.g. `document.onreadystatechange`).
@@ -1970,10 +2273,17 @@ module Dommy
1970
2273
  self.cookie = value.to_s
1971
2274
  when "dir"
1972
2275
  self.dir = value
2276
+ when "fgColor" then self.fg_color = value
2277
+ when "linkColor" then self.link_color = value
2278
+ when "vlinkColor" then self.vlink_color = value
2279
+ when "alinkColor" then self.alink_color = value
2280
+ when "bgColor" then self.bg_color = value
1973
2281
  when "designMode"
1974
2282
  # Enumerated: only "on"/"off" (case-insensitive), else ignored.
1975
2283
  v = value.to_s.downcase
1976
2284
  @design_mode = v if %w[on off].include?(v)
2285
+ when "domain"
2286
+ self.domain = value.to_s
1977
2287
  when "location"
1978
2288
  # `document.location = url` navigates, same as `location.href = url`.
1979
2289
  loc = @default_view&.__js_get__("location")
@@ -2043,9 +2353,8 @@ module Dommy
2043
2353
  document_remove_child(args[0])
2044
2354
  when "insertBefore"
2045
2355
  raise Bridge::TypeError, "insertBefore requires 2 arguments." if args.length < 2
2046
- Internal::WebIDL.nullable_node!(args[1])
2047
2356
 
2048
- document_insert_before(args[0], args[1])
2357
+ insert_before(args[0], args[1])
2049
2358
  when "replaceChild"
2050
2359
  document_replace_child(args[0], args[1])
2051
2360
  when "moveBefore"
@@ -2058,7 +2367,8 @@ module Dommy
2058
2367
  when "normalize"
2059
2368
  normalize
2060
2369
  when "writeln"
2061
- write(*(args + ["\n"]))
2370
+ writeln(*args)
2371
+ Bridge::UNDEFINED
2062
2372
  when "exitFullscreen"
2063
2373
  exit_fullscreen
2064
2374
  when "startViewTransition"
@@ -2070,9 +2380,9 @@ module Dommy
2070
2380
 
2071
2381
  ViewTransition.new(@default_view)
2072
2382
  when "createElement"
2073
- create_element(args[0])
2383
+ create_element(args[0], args[1])
2074
2384
  when "createElementNS"
2075
- create_element_ns(args[0], args[1])
2385
+ create_element_ns(args[0], args[1], args[2])
2076
2386
  when "createTextNode"
2077
2387
  create_text_node(args[0])
2078
2388
  when "createComment"
@@ -2110,7 +2420,8 @@ module Dommy
2110
2420
  when "createEvent"
2111
2421
  create_event(args[0])
2112
2422
  when "importNode"
2113
- import_node(args[0], args[1])
2423
+ # A missing options argument is undefined (the default), not null.
2424
+ import_node(args[0], args.fetch(1, Bridge::UNDEFINED))
2114
2425
  when "adoptNode"
2115
2426
  adopt_node(args[0])
2116
2427
  when "hasFocus"
@@ -2129,10 +2440,12 @@ module Dommy
2129
2440
  dispatch_event(args[0])
2130
2441
  when "write"
2131
2442
  write(*args)
2443
+ Bridge::UNDEFINED
2132
2444
  when "open"
2133
- open
2445
+ open(*args)
2134
2446
  when "close"
2135
2447
  close
2448
+ Bridge::UNDEFINED
2136
2449
  else
2137
2450
  nil
2138
2451
  end
@@ -2153,16 +2466,113 @@ module Dommy
2153
2466
  return if @ready_state == state
2154
2467
 
2155
2468
  @ready_state = state
2156
- dispatch_event(Event.new("readystatechange"))
2469
+ @loading_lifecycle = true if state == "loading"
2470
+ __internal_fire_event__("readystatechange")
2157
2471
  case state
2158
2472
  when "interactive"
2159
- dispatch_event(Event.new("DOMContentLoaded", "bubbles" => true))
2473
+ __internal_fire_event__("DOMContentLoaded", {"bubbles" => true})
2160
2474
  when "complete"
2161
- @default_view&.dispatch_event(Event.new("load"))
2475
+ fire_load_and_pageshow
2162
2476
  end
2163
2477
  nil
2164
2478
  end
2165
2479
 
2480
+ # The document's current readiness ("loading" / "interactive" /
2481
+ # "complete").
2482
+ def __internal_ready_state__ = @ready_state
2483
+
2484
+ # HTML "update the current document readiness" alone: readyState becomes
2485
+ # `state` and `readystatechange` fires — without the milestone event
2486
+ # __internal_set_ready_state__ adds. The parser's "the end" fires
2487
+ # DOMContentLoaded and load from tasks of their own (see ScriptBoot).
2488
+ def __internal_update_readiness__(state)
2489
+ state = state.to_s
2490
+ return nil if @ready_state == state
2491
+
2492
+ @ready_state = state
2493
+ @loading_lifecycle = true if state == "loading"
2494
+ __internal_fire_event__("readystatechange")
2495
+ nil
2496
+ end
2497
+
2498
+ # "The end", its DOMContentLoaded task: fire a trusted, bubbling
2499
+ # `DOMContentLoaded` at the document.
2500
+ def __internal_fire_dom_content_loaded__
2501
+ __internal_fire_event__("DOMContentLoaded", {"bubbles" => true})
2502
+ nil
2503
+ end
2504
+
2505
+ # "The end", its load task: readiness becomes "complete", then `load` at
2506
+ # the window and `pageshow`, and the document is completely loaded.
2507
+ def __internal_finish_loading__
2508
+ return nil unless @loading_lifecycle
2509
+
2510
+ __internal_update_readiness__("complete")
2511
+ fire_load_and_pageshow
2512
+ nil
2513
+ end
2514
+
2515
+ # Whether something delays this document's load event: a child navigable
2516
+ # still navigating (HTML "potentially delays the load event", iframe).
2517
+ def __internal_load_delayed__?
2518
+ @backend_doc.css("iframe").any? do |node|
2519
+ frame = wrap_node(node)
2520
+ frame.is_a?(HTMLIFrameElement) && frame.__internal_navigation_pending__?
2521
+ end
2522
+ end
2523
+
2524
+ # The end of loading, from "update the current document readiness to
2525
+ # complete": fire a trusted `load` at the window with the legacy target
2526
+ # override (so `event.target` is this document), then `pageshow` (persisted
2527
+ # false), and mark the document completely loaded — ready for post-load
2528
+ # tasks such as a print() requested while it loaded.
2529
+ def fire_load_and_pageshow
2530
+ view = @default_view
2531
+ if view
2532
+ load = Event.new("load")
2533
+ load.__internal_set_target__(self)
2534
+ view.dispatch_event(load.__internal_mark_trusted__)
2535
+ view.dispatch_event(__internal_page_transition_event__("pageshow"))
2536
+ end
2537
+ @loading_lifecycle = false
2538
+ view.__internal_ready_for_post_load_tasks__ if view.respond_to?(:__internal_ready_for_post_load_tasks__)
2539
+ run_after_load_tasks
2540
+ end
2541
+
2542
+ # Queue `block` as a task once this document has completely loaded (at
2543
+ # once when it already has).
2544
+ def __internal_after_load__(&block)
2545
+ if __internal_completely_loaded__?
2546
+ queue_after_load_task(block)
2547
+ else
2548
+ (@after_load_tasks ||= []) << block
2549
+ end
2550
+ nil
2551
+ end
2552
+
2553
+ def run_after_load_tasks
2554
+ tasks = @after_load_tasks || []
2555
+ @after_load_tasks = nil
2556
+ tasks.each { |task| queue_after_load_task(task) }
2557
+ end
2558
+ private :run_after_load_tasks
2559
+
2560
+ def queue_after_load_task(block)
2561
+ scheduler = __internal_scheduler__
2562
+ scheduler ? scheduler.set_timeout(block, 0) : block.call
2563
+ end
2564
+ private :queue_after_load_task
2565
+ private :fire_load_and_pageshow
2566
+
2567
+ # A trusted page transition event (`pageshow` / `pagehide`) for this
2568
+ # document's window: bubbles, cancelable, `persisted` false (there is no
2569
+ # back/forward cache), targeted at the document (legacy target override).
2570
+ def __internal_page_transition_event__(type, persisted: false)
2571
+ event = PageTransitionEvent.new(type, "persisted" => persisted, "bubbles" => true, "cancelable" => true)
2572
+ event.__internal_set_target__(self)
2573
+ event.__internal_mark_trusted__
2574
+ end
2575
+
2166
2576
  # Set `document.currentScript` to the <script> element being executed (and
2167
2577
  # back to nil afterward). The host (script boot) brackets each classic
2168
2578
  # script run with this so code reading `document.currentScript` sees its own
@@ -2172,6 +2582,21 @@ module Dommy
2172
2582
  nil
2173
2583
  end
2174
2584
 
2585
+ # HTML "execute the script element", for a classic script: currentScript
2586
+ # is the element while its script runs (null when the element's root is a
2587
+ # shadow root — not "in a document tree", since a script removed before it
2588
+ # runs still points at itself), then goes back to whatever it was before,
2589
+ # so a script inserted and run from inside another script leaves the outer
2590
+ # one current again. A caller reports the script's exception from inside
2591
+ # the block: the report is part of the run, while currentScript is set.
2592
+ def __internal_with_current_script__(element)
2593
+ old = @__current_script__
2594
+ @__current_script__ = element.root_node.is_a?(ShadowRoot) ? nil : element
2595
+ yield
2596
+ ensure
2597
+ @__current_script__ = old
2598
+ end
2599
+
2175
2600
  # Delegate node wrapping to NodeWrapperCache
2176
2601
  def wrap_node(node)
2177
2602
  @node_wrapper_cache.wrap(node)
@@ -2199,19 +2624,55 @@ module Dommy
2199
2624
  # HTML-namespace only: a `css` query matches on local name, so a
2200
2625
  # `<details>` the parser put inside `<svg>` answers it too, as an
2201
2626
  # SVGElement that has none of these steps.
2202
- elements = @backend_doc.css("details").filter_map { |node| __internal_html_element_wrapper__(node) }
2627
+ elements = parsed_css("details").filter_map { |node| __internal_html_element_wrapper__(node) }
2203
2628
  HTMLDetailsElement.run_insertion_steps(elements) unless elements.empty?
2204
- @backend_doc.css("select").each { |node| __internal_html_element_wrapper__(node)&.__internal_settle_selectedness_once__ }
2205
- @backend_doc.css("script").each { |node| __internal_html_element_wrapper__(node)&.__internal_mark_parser_inserted__ }
2629
+ parsed_css("select").each { |node| __internal_html_element_wrapper__(node)&.__internal_settle_selectedness_once__ }
2630
+ parsed_css("script").each do |node|
2631
+ script = __internal_html_element_wrapper__(node)
2632
+ next unless script
2633
+
2634
+ script.__internal_mark_parser_inserted__
2635
+ script.__internal_mark_parser_document__
2636
+ end
2637
+ parsed_css("meta[http-equiv]").each { |node| __internal_html_element_wrapper__(node)&.__internal_run_pragma__ }
2638
+ # An open dialog the parser inserted runs its dialog setup steps.
2639
+ parsed_css("dialog[open]").each do |node|
2640
+ dialog = __internal_html_element_wrapper__(node)
2641
+ dialog.__internal_dialog_inserted__ if dialog.respond_to?(:__internal_dialog_inserted__)
2642
+ end
2643
+ # Each element the parser inserted with an autofocus attribute is an
2644
+ # autofocus candidate (only in a document with a browsing context).
2645
+ parsed_css("[autofocus]").each do |node|
2646
+ element = wrap_node(node)
2647
+ __internal_autofocus_inserted__(element) if element.respond_to?(:autofocus) && element.is_connected?
2648
+ end
2206
2649
  nil
2207
2650
  end
2208
2651
 
2652
+ # Script boot replays the parser's insertion of each `<iframe>` (in tree
2653
+ # order): its post-connection steps create its child navigable and process
2654
+ # its attributes, so a srcless one fires `load` and one with a `src` starts
2655
+ # navigating. Only a document with a browsing context has child navigables.
2656
+ def __internal_process_parsed_iframes__
2657
+ return nil unless @default_view
2658
+
2659
+ parsed_css("iframe").each do |node|
2660
+ frame = __internal_html_element_wrapper__(node)
2661
+ frame.__internal_parser_inserted__ if frame.is_a?(HTMLIFrameElement)
2662
+ end
2663
+ nil
2664
+ end
2665
+
2666
+ # HTML's pragma-set default language (`<meta http-equiv=content-language
2667
+ # content=…>`): nil until such a pragma has been processed.
2668
+ attr_accessor :__internal_pragma_default_language__
2669
+
2209
2670
  # DOMParser parses with scripting disabled (HTML) or XML scripting support
2210
2671
  # disabled, so every script it makes is "already started": moved or cloned
2211
2672
  # into a document that runs scripts, it still does not run. Found by local
2212
2673
  # name, so an XML document's prefixed `h:script` counts too.
2213
- def __internal_mark_scripts_already_started__
2214
- Internal::NodeTraversal.subtree_nodes(@backend_doc).each do |node|
2674
+ def __internal_mark_scripts_already_started__(root = @backend_doc)
2675
+ Internal::NodeTraversal.subtree_nodes(root).each do |node|
2215
2676
  next unless node.element?
2216
2677
  next unless node.local_name == "script"
2217
2678
 
@@ -2281,6 +2742,69 @@ module Dommy
2281
2742
  @shadow_registry.find_for_host(host_node)
2282
2743
  end
2283
2744
 
2745
+ # DOM "allow declarative shadow roots": whether the document's own parser
2746
+ # (the page parse, document.write) attaches declarative shadow roots. On
2747
+ # for a browsing context's documents and parseHTMLUnsafe()'s; off for
2748
+ # createHTMLDocument(), DOMParser and `new Document()`; a clone keeps it.
2749
+ attr_writer :__internal_allow_declarative_shadow_roots__
2750
+
2751
+ def __internal_allow_declarative_shadow_roots__? = @__internal_allow_declarative_shadow_roots__ ? true : false
2752
+
2753
+ # The document's parser has built `root_bn`'s subtree: attach its
2754
+ # declarative shadow roots, when the document allows them. The shadow
2755
+ # roots are the document's parsed roots from then on (their scripts run
2756
+ # at boot, their elements get the parser's insertion steps). Returns them.
2757
+ def __internal_attach_declarative_shadow_roots__(root_bn, top_host: nil)
2758
+ return [] unless __internal_allow_declarative_shadow_roots__? && Backend.html_backed?(@backend_doc)
2759
+
2760
+ roots = Internal::DeclarativeShadowRoots.attach(self, root_bn, top_host: top_host)
2761
+ (@parser_shadow_roots ||= []).concat(roots)
2762
+ roots
2763
+ end
2764
+
2765
+ # The roots of what the document's parser built: the document, and the
2766
+ # declarative shadow roots it attached.
2767
+ def __internal_parsed_roots__
2768
+ return [@backend_doc] unless @parser_shadow_roots
2769
+
2770
+ [@backend_doc, *@parser_shadow_roots.map(&:__dommy_backend_node__)]
2771
+ end
2772
+
2773
+ # The document's parser-inserted scripts, in the order the parser met
2774
+ # them: tree order, where a declarative shadow tree's contents come at
2775
+ # the place its template was. With no declarative shadow roots, that is
2776
+ # `document.scripts`.
2777
+ def __internal_parser_scripts__
2778
+ return scripts.to_a unless @parser_shadow_roots
2779
+
2780
+ parsed_shadows = @parser_shadow_roots.to_h { |root| [Backend.identity_key(root.host.__dommy_backend_node__), root] }
2781
+ list = []
2782
+ collect_parser_scripts(@backend_doc, parsed_shadows, list)
2783
+ list
2784
+ end
2785
+
2786
+ # Whether any shadow root was ever attached in this document — the HTML
2787
+ # serializer's fast path asks before looking for shadow hosts.
2788
+ def __internal_any_shadow_roots__? = @shadow_registry.any?
2789
+
2790
+ # Whether an element of this document has an is value it does not carry
2791
+ # as an `is` attribute (createElement's `{is}`, a customized built-in's
2792
+ # constructor, a clone of either) — the serializer's other reason to
2793
+ # leave its fast path. The elements given an is value are held weakly.
2794
+ def __internal_any_is_values__?
2795
+ return false unless @is_value_elements
2796
+
2797
+ @is_value_elements.keys.any? do |element|
2798
+ node = element.__dommy_backend_node__
2799
+ current = @node_wrapper_cache.peek(node) || element
2800
+ current.__internal_is_value__ && !Backend.has_attribute_ns?(node, nil, "is")
2801
+ end
2802
+ end
2803
+
2804
+ def __internal_note_is_value__(element)
2805
+ (@is_value_elements ||= ObjectSpace::WeakMap.new)[element] = true
2806
+ end
2807
+
2284
2808
  # Every element among `root`'s shadow-including inclusive descendants, as
2285
2809
  # backend nodes, in shadow-including tree order: an element, then the shadow
2286
2810
  # tree it hosts, then its children. The whole list, shadow trees included, is
@@ -2292,18 +2816,6 @@ module Dommy
2292
2816
  nil
2293
2817
  end
2294
2818
 
2295
- def shadow_including_elements(root, list = [])
2296
- elements = root.element? ? [root] : []
2297
- elements.concat(root.css("*").to_a)
2298
- elements.each do |element|
2299
- list << element
2300
- shadow = @shadow_registry.find_for_host(element)
2301
- shadow_including_elements(shadow.__dommy_backend_node__, list) if shadow
2302
- end
2303
- list
2304
- end
2305
- private :shadow_including_elements
2306
-
2307
2819
  def __internal_shadow_root_containing__(node)
2308
2820
  @shadow_registry.find_enclosing(node)
2309
2821
  end
@@ -2341,6 +2853,65 @@ module Dommy
2341
2853
  @mutation_coordinator.notify_disconnected_subtree(nk)
2342
2854
  end
2343
2855
 
2856
+ # DOM "custom element registry" of the document: the one initialize()
2857
+ # set, else its window's (none without a browsing context).
2858
+ attr_writer :__internal_custom_element_registry__
2859
+
2860
+ def __internal_custom_element_registry__
2861
+ @__internal_custom_element_registry__ || (@default_view.custom_elements if @default_view.respond_to?(:custom_elements))
2862
+ end
2863
+
2864
+ # The element the HTML element constructor makes for `new MyElement()`.
2865
+ def __internal_create_custom_element__(definition)
2866
+ @node_factory.create_custom_element(definition)
2867
+ end
2868
+
2869
+ # HTML "try to upgrade an element": enqueue an upgrade reaction when the
2870
+ # registry `element` looks definitions up in has one for it.
2871
+ def __internal_try_to_upgrade__(element)
2872
+ registry = element.__internal_ce_registry__
2873
+ return unless registry&.any_definitions?
2874
+
2875
+ definition = CustomElementRegistry.lookup(registry, element.namespace_uri, element.local_name,
2876
+ element.__internal_is_value__)
2877
+ Internal::CEReactions.enqueue_upgrade(element, definition) if definition
2878
+ end
2879
+
2880
+ # The elements a parser or a clone just created in this document without
2881
+ # the synchronous custom elements flag (DOM "create an element" step 6.3),
2882
+ # with `registry` (the parser's intended parent's, the original's): each
2883
+ # one gets the registry when it is a scoped one, and each one a definition
2884
+ # applies to an upgrade reaction. `nodes` are backend nodes, each walked
2885
+ # in tree order (a template's contents are not its children, and belong
2886
+ # to a document with no registry).
2887
+ def __internal_enqueue_created_upgrades__(nodes, registry = CustomElementRegistry.for_document(self))
2888
+ explicit = !registry.equal?(CustomElementRegistry.effective_global_for(self))
2889
+ return unless explicit || registry&.any_definitions?
2890
+
2891
+ (nodes.is_a?(Array) ? nodes : [nodes]).each do |root|
2892
+ Internal::NodeTraversal.subtree_nodes(root).each do |node|
2893
+ next unless node.element?
2894
+
2895
+ known = __internal_peek_wrapper__(node)
2896
+ if explicit
2897
+ known ||= wrap_node(node)
2898
+ known.__internal_ce_data__.registry = registry
2899
+ end
2900
+ next if registry.nil?
2901
+ next unless Backend.namespace_uri(node) == Element::HTML_NAMESPACE
2902
+
2903
+ # The is value a wrapper was made with (a clone's), else the one
2904
+ # the parser gives it: its `is` attribute.
2905
+ is_value = known ? known.__internal_is_value__ : Backend.get_attribute_ns(node, nil, "is")
2906
+ definition = registry.lookup_definition(node.name, is_value)
2907
+ next unless definition
2908
+
2909
+ element = known || wrap_node(node)
2910
+ Internal::CEReactions.enqueue_upgrade(element, definition) if element
2911
+ end
2912
+ end
2913
+ end
2914
+
2344
2915
  def __internal_notify_attribute_changed__(element, name, old_value, new_value, namespace = nil)
2345
2916
  @mutation_coordinator.notify_attribute_changed(element, name, old_value, new_value, namespace)
2346
2917
  end
@@ -2461,6 +3032,184 @@ module Dommy
2461
3032
  end
2462
3033
  end
2463
3034
 
3035
+ # The document write steps (without Trusted Types).
3036
+ #
3037
+ # Spec: https://html.spec.whatwg.org/#document-write-steps
3038
+ def document_write(args, line_feed:)
3039
+ string = args.map { |a| a.nil? ? "null" : a.to_s }.join
3040
+ string += "\n" if line_feed
3041
+ raise DOMException::InvalidStateError, "write() is not supported on an XML document" unless html_document?
3042
+ raise_if_markup_insertion_forbidden!("write")
3043
+
3044
+ return boot_script_write(string) if @script_created_input.nil? && boot_script_running?
3045
+
3046
+ document_open_steps if @script_created_input.nil?
3047
+ # The open steps return early (no script-created parser) only when the
3048
+ # parser is active, which the branch above already took.
3049
+ return nil if @script_created_input.nil?
3050
+
3051
+ @script_created_input << string
3052
+ reparse_script_created_input
3053
+ nil
3054
+ end
3055
+
3056
+ # Whether a page script is running while the page boots: the closest Dommy
3057
+ # has to "an active parser whose script nesting level is greater than 0".
3058
+ def boot_script_running?
3059
+ @ready_state == "loading" && !@__current_script__.nil?
3060
+ end
3061
+
3062
+ # The document open steps, minus what Dommy does not model (the origin
3063
+ # check against the entry document, unload counters, stopping a
3064
+ # navigation, and the URL and history update steps).
3065
+ #
3066
+ # Spec: https://html.spec.whatwg.org/#document-open-steps
3067
+ def document_open_steps
3068
+ raise DOMException::InvalidStateError, "open() is not supported on an XML document" unless html_document?
3069
+ raise_if_markup_insertion_forbidden!("open")
3070
+ return self if boot_script_running?
3071
+ # A second open() while a script-created parser is open keeps it (the
3072
+ # spec's steps re-run, but the written input so far is already gone with
3073
+ # the children they replace).
3074
+ erase_all_event_listeners_and_handlers
3075
+ document_replace_children([])
3076
+ @quirks_mode = false
3077
+ @script_created_input = +""
3078
+ unless @ready_state == "loading"
3079
+ @ready_state = "loading"
3080
+ __internal_fire_event__("readystatechange")
3081
+ end
3082
+ self
3083
+ end
3084
+
3085
+ # Insert what a boot-time script wrote at the insertion point: right after
3086
+ # the running script when it sits in the body, else at the end of the body
3087
+ # (or of the document element when there is no body).
3088
+ def boot_script_write(string)
3089
+ script_bn = @__current_script__&.__dommy_backend_node__
3090
+ parent_bn = script_bn&.parent
3091
+ in_body = parent_bn && body && parent_bn != @backend_doc &&
3092
+ Internal::NodeTraversal.subtree_nodes(body.__dommy_backend_node__).include?(parent_bn)
3093
+ target_bn = in_body ? parent_bn : (body || document_element)&.__dommy_backend_node__
3094
+ return nil unless target_bn
3095
+
3096
+ context = target_bn.element? ? target_bn : nil
3097
+ fragment = Parser.fragment(string, owner_doc: @backend_doc, context: context)
3098
+ # The document's parser reads the markup: a top-level declarative
3099
+ # template's adjusted current node is the element it is written into.
3100
+ __internal_attach_declarative_shadow_roots__(fragment, top_host: target_bn)
3101
+ added = fragment.children.to_a
3102
+ return nil if added.empty?
3103
+
3104
+ # The document's parser creates a defined element by running its
3105
+ # constructor; Dommy's parsed the markup already, so each one is
3106
+ # upgraded instead, before it is inserted.
3107
+ __internal_enqueue_created_upgrades__(added)
3108
+
3109
+ if in_body
3110
+ reference = script_bn
3111
+ added.each do |node|
3112
+ reference.add_next_sibling(node)
3113
+ reference = node
3114
+ end
3115
+ else
3116
+ added.each { |node| target_bn.add_child(node) }
3117
+ end
3118
+ @__document_writing = true
3119
+ begin
3120
+ notify_child_list_mutation(target_node: target_bn, added_nodes: added, removed_nodes: [])
3121
+ ensure
3122
+ @__document_writing = false
3123
+ end
3124
+ nil
3125
+ end
3126
+
3127
+ # Whether the insertion under way comes from document.write: an external
3128
+ # script it writes is the parser's pending parsing-blocking script, which
3129
+ # runs as soon as the writing script returns — before the parser goes on.
3130
+ def __internal_document_writing__ = @__document_writing == true
3131
+
3132
+ # Re-parse everything written since open() as a whole document and make
3133
+ # its children the document's (see "Dynamic markup insertion" above).
3134
+ # HTML's throw-on-dynamic-markup-insertion counter: while the parser runs
3135
+ # custom element constructors and reactions (see #parser_runs_scripts),
3136
+ # open(), write() and close() are an InvalidStateError.
3137
+ def raise_if_markup_insertion_forbidden!(operation)
3138
+ return unless @throw_on_dynamic_markup_insertion_counter.to_i.positive?
3139
+
3140
+ raise DOMException::InvalidStateError, "#{operation}() is not allowed while the parser constructs a custom element"
3141
+ end
3142
+
3143
+ # The parser's custom element work for markup it inserts: the
3144
+ # constructors ("create an element for a token" with willExecuteScript)
3145
+ # and the reactions they enqueue run before it continues, with the
3146
+ # throw-on-dynamic-markup-insertion counter raised.
3147
+ def parser_runs_scripts
3148
+ @throw_on_dynamic_markup_insertion_counter = @throw_on_dynamic_markup_insertion_counter.to_i + 1
3149
+ Internal::CEReactions.scope { yield }
3150
+ ensure
3151
+ @throw_on_dynamic_markup_insertion_counter -= 1
3152
+ end
3153
+
3154
+ def reparse_script_created_input
3155
+ parser_runs_scripts { reparse_script_created_input_now }
3156
+ end
3157
+
3158
+ def reparse_script_created_input_now
3159
+ parsed = Document.new(nil, backend_doc: Backend.parse(@script_created_input))
3160
+ @quirks_mode = parsed.quirks_mode?
3161
+ parsed.__internal_mark_scripts_already_started__
3162
+ # What the parser builds, not what a script inserts: a doctype and an
3163
+ # element go in together, which replaceChildren's checks would refuse.
3164
+ removed = @backend_doc.children.to_a
3165
+ removed.each { |child| detach_node(child) }
3166
+ added = document_insertion_nodes(parsed.child_nodes.to_a)
3167
+ added.each { |n| @backend_doc.add_child(n) }
3168
+ # The written markup is this document's parser's: its declarative
3169
+ # shadow roots, when the document allows them, replace the last parse's.
3170
+ @parser_shadow_roots = nil
3171
+ __internal_attach_declarative_shadow_roots__(@backend_doc).each do |root|
3172
+ __internal_mark_scripts_already_started__(root.__dommy_backend_node__)
3173
+ end
3174
+ notify_document_child_list(added: added, removed: removed)
3175
+ end
3176
+
3177
+ # "Erase all event listeners and handlers" for the document's
3178
+ # shadow-including inclusive descendants and, for a window's document, the
3179
+ # window. Only nodes that have a wrapper can hold listeners.
3180
+ def erase_all_event_listeners_and_handlers
3181
+ targets = [self]
3182
+ collect_listener_targets(@backend_doc, targets)
3183
+ targets << @default_view if @default_view
3184
+ targets.each { |t| t.__internal_erase_event_listeners_and_handlers__ if t.respond_to?(:__internal_erase_event_listeners_and_handlers__) }
3185
+ end
3186
+
3187
+ def collect_listener_targets(root_bn, targets)
3188
+ Internal::NodeTraversal.subtree_nodes(root_bn).each do |bn|
3189
+ wrapper = @node_wrapper_cache.peek(bn)
3190
+ next unless wrapper
3191
+
3192
+ targets << wrapper unless wrapper.equal?(self)
3193
+ shadow = wrapper.respond_to?(:__internal_shadow_root__) ? wrapper.__internal_shadow_root__ : nil
3194
+ next unless shadow
3195
+
3196
+ targets << shadow
3197
+ collect_listener_targets(shadow.__dommy_backend_node__, targets) if shadow.__dommy_backend_node__
3198
+ end
3199
+ end
3200
+
3201
+ # document.open(url, name, features) is window.open; it needs a fully
3202
+ # active document (one with a window, here).
3203
+ def open_window(args)
3204
+ raise DOMException::InvalidAccessError, "the document is not fully active" unless @default_view
3205
+
3206
+ @default_view.__js_call__("open", args)
3207
+ end
3208
+
3209
+ private :document_write, :boot_script_running?, :document_open_steps, :boot_script_write,
3210
+ :reparse_script_created_input, :erase_all_event_listeners_and_handlers,
3211
+ :collect_listener_targets, :open_window
3212
+
2464
3213
  # Unlink a backend node from its parent and queue a childList removal record
2465
3214
  # capturing the node's position (previous/next sibling) BEFORE the unlink, so
2466
3215
  # the record's previousSibling/nextSibling are correct (the coordinator can't
@@ -2615,8 +3364,8 @@ module Dommy
2615
3364
 
2616
3365
  # Delegate factory methods to NodeWrapperCache
2617
3366
 
2618
- def create_element(name)
2619
- @node_factory.create_element(name)
3367
+ def create_element(name, options = nil)
3368
+ @node_factory.create_element(name, options)
2620
3369
  end
2621
3370
 
2622
3371
  def create_text_node(text)
@@ -2674,6 +3423,123 @@ module Dommy
2674
3423
 
2675
3424
  private
2676
3425
 
3426
+ # HTML "is a registrable domain suffix of or is equal to" (a single label
3427
+ # stands in for the public suffix; see #domain=).
3428
+ def registrable_domain_suffix_or_equal?(suffix_string, original_host)
3429
+ return false if suffix_string.empty?
3430
+
3431
+ suffix = Internal::UrlParser.parse("http://#{suffix_string}/").host.to_s
3432
+ return true if suffix == original_host
3433
+ return false if ip_host?(suffix) || ip_host?(original_host)
3434
+ return false unless original_host.end_with?(".#{suffix}")
3435
+ # The suffix may not itself be a public suffix.
3436
+ return false unless suffix.include?(".")
3437
+
3438
+ true
3439
+ rescue Internal::UrlParser::Failure
3440
+ false
3441
+ end
3442
+
3443
+ def ip_host?(host)
3444
+ host.start_with?("[") || host.match?(/\A\d+\.\d+\.\d+\.\d+\z/)
3445
+ end
3446
+
3447
+
3448
+ def creator_base_url
3449
+ @creator_base_url if @creator_base_url && FALLBACK_BASE_URLS.include?(url)
3450
+ end
3451
+
3452
+ # importNode is a clone, so the HTML cloning steps run for it as they do
3453
+ # for cloneNode (#__internal_apply_cloning_steps__): the live state Dommy
3454
+ # keeps on a wrapper — an input's dirty value, a script's "already
3455
+ # started" — is copied onto the new node. The originals' wrappers belong
3456
+ # to the source document, so they are looked up there.
3457
+ def apply_imported_cloning_steps(src_root, copy_root, deep, source_document)
3458
+ return unless source_document.respond_to?(:__internal_peek_wrapper__)
3459
+
3460
+ src_nodes = deep ? Internal::NodeTraversal.subtree_nodes(src_root) : [src_root]
3461
+ copy_nodes = deep ? Internal::NodeTraversal.subtree_nodes(copy_root) : [copy_root]
3462
+ return unless src_nodes.length == copy_nodes.length
3463
+
3464
+ src_nodes.zip(copy_nodes).each do |orig, copy|
3465
+ state = source_document.__internal_peek_wrapper__(orig)&.then { |w| w.respond_to?(:__internal_cloning_state__) && w.__internal_cloning_state__ }
3466
+ next unless state
3467
+
3468
+ wrapper = wrap_node(copy)
3469
+ wrapper.__internal_apply_cloning_state__(state) if wrapper.respond_to?(:__internal_apply_cloning_state__)
3470
+ end
3471
+ end
3472
+
3473
+ def node_adopter
3474
+ @node_adopter ||= Internal::NodeAdopter.new(self)
3475
+ end
3476
+
3477
+ def collect_parser_scripts(node, parsed_shadows, list)
3478
+ shadow = node.element? ? parsed_shadows[Backend.identity_key(node)] : nil
3479
+ shadow = nil unless shadow&.host&.__dommy_backend_node__ == node
3480
+ pending = shadow
3481
+ node.children.each do |child|
3482
+ if pending && (before = pending.__internal_parsed_before__) && Backend.identity_key(child) == Backend.identity_key(before)
3483
+ collect_parser_scripts(pending.__dommy_backend_node__, parsed_shadows, list)
3484
+ pending = nil
3485
+ end
3486
+ next unless child.element?
3487
+
3488
+ if child.local_name == "script" && (script = __internal_html_element_wrapper__(child))
3489
+ list << script
3490
+ end
3491
+ collect_parser_scripts(child, parsed_shadows, list)
3492
+ end
3493
+ collect_parser_scripts(pending.__dommy_backend_node__, parsed_shadows, list) if pending
3494
+ end
3495
+
3496
+ # A `css` query over every root the parser built.
3497
+ def parsed_css(selector)
3498
+ roots = __internal_parsed_roots__
3499
+ return @backend_doc.css(selector) if roots.size == 1
3500
+
3501
+ roots.flat_map { |root| root.css(selector).to_a }
3502
+ end
3503
+
3504
+ def shadow_including_elements(root, list = [])
3505
+ elements = root.element? ? [root] : []
3506
+ elements.concat(root.css("*").to_a)
3507
+ elements.each do |element|
3508
+ list << element
3509
+ shadow = @shadow_registry.find_for_host(element)
3510
+ shadow_including_elements(shadow.__dommy_backend_node__, list) if shadow
3511
+ end
3512
+ list
3513
+ end
3514
+
3515
+ # "The html element": the document element when it is an HTML <html>.
3516
+ def html_element
3517
+ root = document_element
3518
+ root if root.is_a?(HTMLElement) && root.local_name == "html"
3519
+ end
3520
+
3521
+ # An element is a named element with the name `name` when it is one of the
3522
+ # exposed kinds and either carries that `name`, or is an object with that
3523
+ # `id`, or is an img whose id it is and which also has a non-empty name.
3524
+ def named_element?(node, name)
3525
+ return false if name.empty?
3526
+ return false unless %w[embed form iframe img object].include?(node.name.to_s.downcase)
3527
+ own_name = Backend.no_namespace_attribute_value(node, "name")
3528
+ return true if own_name == name
3529
+ return true if node.name.to_s.casecmp?("object") && Backend.no_namespace_attribute_value(node, "id") == name
3530
+
3531
+ node.name.to_s.casecmp?("img") && Backend.no_namespace_attribute_value(node, "id") == name && !own_name.to_s.empty?
3532
+ end
3533
+
3534
+ # Elements the document's named getter exposes, in tree order.
3535
+ def named_getter_nodes
3536
+ @backend_doc.css("embed, form, iframe, img, object")
3537
+ end
3538
+
3539
+ def document_named_property_nodes(name)
3540
+ named_getter_nodes.select { |node| named_element?(node, name) }.map { |node| wrap_node(node) }.compact
3541
+ end
3542
+
2677
3543
  # Build a Nokogiri copy of the given node inside our @backend_doc.
2678
3544
  # `deep: true` recurses into children. Used by importNode and
2679
3545
  # adoptNode for cross-document transfer.
@@ -2685,7 +3551,7 @@ module Dommy
2685
3551
  # A <template>'s contents live in a separate content fragment, not its
2686
3552
  # child list, so the pass over `children` misses them. It still runs: an
2687
3553
  # XML document's <template> keeps its children in the child list.
2688
- clone_template_content(source, copy, source_document) if source.element? && source.name == "template"
3554
+ clone_template_content(source, copy, source_document) if @template_content_registry.template_node?(source)
2689
3555
  source.children.each do |child|
2690
3556
  copy.add_child(clone_into_doc(child, true, source_document))
2691
3557
  end
@@ -2788,7 +3654,7 @@ module Dommy
2788
3654
  root = backend_document_element
2789
3655
  if svg_root?(root)
2790
3656
  title = svg_title_child(root)
2791
- title = insert_title(root, Internal::Namespaces::SVG, before: root.children.first) unless title
3657
+ title = insert_title(root, Internal::Namespaces::SVG, before: root.first_child) unless title
2792
3658
  elsif root && Backend.namespace_uri(root) == Internal::Namespaces::HTML
2793
3659
  title = html_title_element
2794
3660
  unless title
@@ -2804,8 +3670,13 @@ module Dommy
2804
3670
  end
2805
3671
 
2806
3672
  # The title element: the first HTML `title` in the document, in tree order.
3673
+ # Matched by namespace and local name: an XML document's `css("title")`
3674
+ # does not find an HTML-namespace title, so there the tree is walked.
2807
3675
  def html_title_element
2808
- @backend_doc.css("title").find { |node| Backend.namespace_uri(node) == Internal::Namespaces::HTML }
3676
+ candidates = html_document? ? @backend_doc.css("title") : Internal::NodeTraversal.subtree_nodes(@backend_doc)
3677
+ candidates.find do |node|
3678
+ node.element? && node.local_name == "title" && Backend.namespace_uri(node) == Internal::Namespaces::HTML
3679
+ end
2809
3680
  end
2810
3681
 
2811
3682
  def svg_root?(root)