dommy 0.16.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dommy/animation.rb +1 -0
  3. data/lib/dommy/backend.rb +3 -0
  4. data/lib/dommy/blob.rb +6 -0
  5. data/lib/dommy/browser.rb +12 -4
  6. data/lib/dommy/character_data.rb +1 -13
  7. data/lib/dommy/cookie_store.rb +2 -1
  8. data/lib/dommy/document.rb +67 -30
  9. data/lib/dommy/element.rb +11 -0
  10. data/lib/dommy/element_internals.rb +12 -0
  11. data/lib/dommy/encodings/legacy_multibyte.rb +800 -0
  12. data/lib/dommy/encodings/multibyte_overrides.rb +76 -0
  13. data/lib/dommy/encodings/prescan.rb +234 -0
  14. data/lib/dommy/encodings.rb +121 -57
  15. data/lib/dommy/event.rb +107 -54
  16. data/lib/dommy/event_source.rb +1 -0
  17. data/lib/dommy/fetch.rb +244 -53
  18. data/lib/dommy/file_reader.rb +1 -0
  19. data/lib/dommy/form_data.rb +22 -9
  20. data/lib/dommy/form_entry_list.rb +3 -3
  21. data/lib/dommy/html_canvas_element.rb +85 -12
  22. data/lib/dommy/html_elements/base.rb +1 -0
  23. data/lib/dommy/html_elements/forms/input.rb +3 -0
  24. data/lib/dommy/html_elements/interactive.rb +56 -48
  25. data/lib/dommy/html_elements/metadata.rb +23 -0
  26. data/lib/dommy/interaction/form_submission.rb +24 -42
  27. data/lib/dommy/internal/accessibility_tree.rb +149 -162
  28. data/lib/dommy/internal/accessibility_visibility.rb +16 -17
  29. data/lib/dommy/internal/accessible_name.rb +231 -246
  30. data/lib/dommy/internal/aria_role.rb +182 -195
  31. data/lib/dommy/internal/aria_snapshot.rb +35 -38
  32. data/lib/dommy/internal/aria_state.rb +102 -118
  33. data/lib/dommy/internal/autofill.rb +75 -75
  34. data/lib/dommy/internal/backend_prefilter.rb +127 -130
  35. data/lib/dommy/internal/child_navigable.rb +86 -83
  36. data/lib/dommy/internal/close_watcher_manager.rb +1 -0
  37. data/lib/dommy/internal/css/ancestor_filter.rb +64 -64
  38. data/lib/dommy/internal/css/calc.rb +90 -71
  39. data/lib/dommy/internal/css/cascade.rb +69 -67
  40. data/lib/dommy/internal/css/color.rb +249 -187
  41. data/lib/dommy/internal/css/computed_style_builder.rb +34 -0
  42. data/lib/dommy/internal/css/computed_style_declaration.rb +50 -11
  43. data/lib/dommy/internal/css/counters.rb +67 -67
  44. data/lib/dommy/internal/css/custom_properties.rb +114 -114
  45. data/lib/dommy/internal/css/parser.rb +277 -239
  46. data/lib/dommy/internal/css/property_registry.rb +321 -302
  47. data/lib/dommy/internal/css/renderability.rb +37 -35
  48. data/lib/dommy/internal/css/rule_index.rb +2 -2
  49. data/lib/dommy/internal/css/rule_index_inputs.rb +2 -2
  50. data/lib/dommy/internal/css/supports.rb +115 -113
  51. data/lib/dommy/internal/css/ua_stylesheet.rb +55 -53
  52. data/lib/dommy/internal/css_rule_text.rb +121 -119
  53. data/lib/dommy/internal/css_source.rb +1 -3
  54. data/lib/dommy/internal/css_syntax.rb +157 -92
  55. data/lib/dommy/internal/declarative_shadow_roots.rb +1 -0
  56. data/lib/dommy/internal/directionality.rb +120 -123
  57. data/lib/dommy/internal/document_live_ranges.rb +26 -1
  58. data/lib/dommy/internal/dom_matching.rb +185 -184
  59. data/lib/dommy/internal/element_geometry.rb +8 -1
  60. data/lib/dommy/internal/element_matching.rb +71 -69
  61. data/lib/dommy/internal/element_shadow.rb +1 -13
  62. data/lib/dommy/internal/element_state.rb +255 -265
  63. data/lib/dommy/internal/event_handlers.rb +66 -64
  64. data/lib/dommy/internal/exception_report.rb +88 -86
  65. data/lib/dommy/internal/focusability.rb +205 -205
  66. data/lib/dommy/internal/global_functions.rb +49 -47
  67. data/lib/dommy/internal/idl_reflection.rb +99 -97
  68. data/lib/dommy/internal/js_number.rb +35 -33
  69. data/lib/dommy/internal/light_dismiss.rb +130 -132
  70. data/lib/dommy/internal/literal_lookup.rb +75 -75
  71. data/lib/dommy/internal/mime_type.rb +54 -49
  72. data/lib/dommy/internal/mutation_coordinator.rb +46 -0
  73. data/lib/dommy/internal/mutation_observer_microtask.rb +60 -0
  74. data/lib/dommy/internal/namespaces.rb +51 -49
  75. data/lib/dommy/internal/node_adopter.rb +25 -1
  76. data/lib/dommy/internal/node_equality.rb +61 -59
  77. data/lib/dommy/internal/post_insertion_steps.rb +1 -0
  78. data/lib/dommy/internal/referrer.rb +144 -0
  79. data/lib/dommy/internal/rendered_text/collector.rb +22 -3
  80. data/lib/dommy/internal/rendered_text/computed_box.rb +3 -2
  81. data/lib/dommy/internal/rendered_text/fragment.rb +37 -38
  82. data/lib/dommy/internal/rendered_text/items.rb +7 -5
  83. data/lib/dommy/internal/selector_matcher.rb +361 -359
  84. data/lib/dommy/internal/selector_parser.rb +74 -62
  85. data/lib/dommy/internal/sequential_focus_navigation.rb +150 -151
  86. data/lib/dommy/internal/shadow_root_registry.rb +7 -0
  87. data/lib/dommy/internal/slot_assignment.rb +117 -0
  88. data/lib/dommy/internal/slot_signals.rb +125 -0
  89. data/lib/dommy/internal/unsafe_html.rb +79 -77
  90. data/lib/dommy/internal/url_parser.rb +5 -29
  91. data/lib/dommy/internal/web_idl.rb +158 -153
  92. data/lib/dommy/internal/window_bound.rb +14 -0
  93. data/lib/dommy/internal/window_constructors.rb +3 -0
  94. data/lib/dommy/internal/xml_serialization.rb +298 -296
  95. data/lib/dommy/js/dom_interfaces.rb +13 -1
  96. data/lib/dommy/js/host_bridge.rb +23 -1
  97. data/lib/dommy/js/host_callbacks.rb +7 -0
  98. data/lib/dommy/js/host_runtime.js +625 -61
  99. data/lib/dommy/js/import_map.rb +1 -1
  100. data/lib/dommy/js/observable_runtime.js +295 -128
  101. data/lib/dommy/js/webidl_members.js +1 -1
  102. data/lib/dommy/js/webidl_signatures.js +35 -6
  103. data/lib/dommy/media_query_list.rb +1 -0
  104. data/lib/dommy/message_channel.rb +2 -0
  105. data/lib/dommy/mutation_observer.rb +25 -29
  106. data/lib/dommy/navigator.rb +4 -1
  107. data/lib/dommy/node.rb +4 -0
  108. data/lib/dommy/notification.rb +1 -0
  109. data/lib/dommy/promise.rb +12 -0
  110. data/lib/dommy/range.rb +20 -3
  111. data/lib/dommy/shadow_root.rb +25 -1
  112. data/lib/dommy/streams.rb +18 -0
  113. data/lib/dommy/style_declaration.rb +15 -4
  114. data/lib/dommy/text_codec.rb +7 -5
  115. data/lib/dommy/tree_walker.rb +68 -61
  116. data/lib/dommy/version.rb +1 -1
  117. data/lib/dommy/web_socket.rb +1 -0
  118. data/lib/dommy/window.rb +22 -4
  119. data/lib/dommy/worker.rb +1 -0
  120. data/lib/dommy/xml_http_request.rb +12 -2
  121. data/lib/dommy.rb +17 -2
  122. metadata +11 -3
@@ -53,60 +53,6 @@ module Dommy
53
53
  # a race can cause, matching the existing per-document caches.
54
54
  AST_CACHE_CAP = 2048
55
55
 
56
- module_function
57
-
58
- # `namespaces` maps a prefix String to its URI (with the symbol key
59
- # :default for the default namespace), letting `svg|rect` resolve in a
60
- # stylesheet that declared `@namespace`. nil/empty (the DOM querySelector
61
- # path) keeps any named prefix undeclared — a SyntaxError, as before.
62
- def parse!(selector, namespaces: nil)
63
- key = [selector.to_s, namespaces]
64
- cache = (@ast_cache ||= {})
65
- if cache.key?(key)
66
- cached = cache[key]
67
- # An invalid selector is memoized as its SyntaxError so a repeat still
68
- # throws (the message embeds the selector, so re-raising is exact).
69
- raise cached if cached.is_a?(::Dommy::DOMException::SyntaxError)
70
-
71
- return cached
72
- end
73
-
74
- ast =
75
- begin
76
- new_parser(selector.to_s, namespaces).parse_selector_list!
77
- rescue InvalidSelector => e
78
- error = ::Dommy::DOMException::SyntaxError.new("'#{selector}' is not a valid selector: #{e.message}")
79
- ast_cache_store(cache, key, error)
80
- raise error
81
- end
82
- ast_cache_store(cache, key, ast)
83
- ast
84
- end
85
-
86
- def ast_cache_store(cache, key, value)
87
- cache.clear if cache.size >= AST_CACHE_CAP
88
- cache[key] = value
89
- end
90
-
91
- # Validate `selector`; raise DOMException::SyntaxError if it is not a valid
92
- # selector list, else return the original string.
93
- def validate!(selector)
94
- parse!(selector)
95
- selector
96
- end
97
-
98
- # True when `selector` parses cleanly (no raise).
99
- def valid?(selector)
100
- validate!(selector)
101
- true
102
- rescue ::Dommy::DOMException::SyntaxError
103
- false
104
- end
105
-
106
- def new_parser(string, namespaces = nil)
107
- Parser.new(string, namespaces: namespaces)
108
- end
109
-
110
56
  # Recursive-descent parser over a character buffer. Methods raise
111
57
  # InvalidSelector on the first grammar violation.
112
58
  class Parser
@@ -506,23 +452,33 @@ module Dommy
506
452
  end
507
453
  end
508
454
 
455
+ # The arguments run to the `)` closing the function (css-syntax-3
456
+ # "consume a function"); a block in them closes only at its own mirror
457
+ # ("consume a simple block"), and the end of the input closes
458
+ # everything still open. Whatever the
459
+ # function's grammar, its arguments are at least <any-value>, which
460
+ # holds no bad string, no bad url and no closing bracket that closes
461
+ # nothing — so any of those makes the whole selector invalid, even in
462
+ # :is()'s forgiving list, which drops its items only after that.
509
463
  def consume_function_argument_source(allow_empty: false)
510
464
  skip_ws
511
465
  start = @i
512
- depth = 0
466
+ closers = []
513
467
  until eof?
514
468
  c = peek
515
- break if c == ")" && depth.zero?
469
+ break if c == ")" && closers.empty?
516
470
 
517
471
  if (j = CssSyntax.atom_end(@s, @i))
472
+ fail!("bad string or url in function arguments") if CssSyntax.bad_token_at?(@s, @i)
518
473
  @i = j
519
474
  next
520
475
  end
521
476
 
522
- if c == "(" || c == "["
523
- depth += 1
524
- elsif c == ")" || c == "]"
525
- depth -= 1
477
+ if (closer = CssSyntax::CLOSER[c])
478
+ closers.push(closer)
479
+ elsif CssSyntax::CLOSING_BRACKETS.include?(c)
480
+ fail!("unmatched #{c.inspect} in function arguments") unless closers.last == c
481
+ closers.pop
526
482
  end
527
483
  advance
528
484
  end
@@ -578,9 +534,9 @@ module Dommy
578
534
 
579
535
  ch = source[i]
580
536
  i += 1
581
- if ch == "(" || ch == "["
537
+ if CssSyntax::OPENING_BRACKETS.include?(ch)
582
538
  depth += 1
583
- elsif ch == ")" || ch == "]"
539
+ elsif CssSyntax::CLOSING_BRACKETS.include?(ch)
584
540
  depth -= 1 if depth.positive?
585
541
  elsif ch == "," && depth.zero?
586
542
  out << current.strip
@@ -961,6 +917,62 @@ module Dommy
961
917
  raise InvalidSelector, message
962
918
  end
963
919
  end
920
+
921
+ class << self
922
+ # `namespaces` maps a prefix String to its URI (with the symbol key
923
+ # :default for the default namespace), letting `svg|rect` resolve in a
924
+ # stylesheet that declared `@namespace`. nil/empty (the DOM querySelector
925
+ # path) keeps any named prefix undeclared — a SyntaxError, as before.
926
+ def parse!(selector, namespaces: nil)
927
+ key = [selector.to_s, namespaces]
928
+ cache = (@ast_cache ||= {})
929
+ if cache.key?(key)
930
+ cached = cache[key]
931
+ # An invalid selector is memoized as its SyntaxError so a repeat still
932
+ # throws (the message embeds the selector, so re-raising is exact).
933
+ raise cached if cached.is_a?(::Dommy::DOMException::SyntaxError)
934
+
935
+ return cached
936
+ end
937
+
938
+ ast =
939
+ begin
940
+ new_parser(selector.to_s, namespaces).parse_selector_list!
941
+ rescue InvalidSelector => e
942
+ error = ::Dommy::DOMException::SyntaxError.new("'#{selector}' is not a valid selector: #{e.message}")
943
+ ast_cache_store(cache, key, error)
944
+ raise error
945
+ end
946
+ ast_cache_store(cache, key, ast)
947
+ ast
948
+ end
949
+
950
+ # Validate `selector`; raise DOMException::SyntaxError if it is not a valid
951
+ # selector list, else return the original string.
952
+ def validate!(selector)
953
+ parse!(selector)
954
+ selector
955
+ end
956
+
957
+ # True when `selector` parses cleanly (no raise).
958
+ def valid?(selector)
959
+ validate!(selector)
960
+ true
961
+ rescue ::Dommy::DOMException::SyntaxError
962
+ false
963
+ end
964
+
965
+ private
966
+
967
+ def ast_cache_store(cache, key, value)
968
+ cache.clear if cache.size >= AST_CACHE_CAP
969
+ cache[key] = value
970
+ end
971
+
972
+ def new_parser(string, namespaces = nil)
973
+ Parser.new(string, namespaces: namespaces)
974
+ end
975
+ end
964
976
  end
965
977
  end
966
978
  end
@@ -23,188 +23,187 @@ module Dommy
23
23
  # navigables are not entered: an iframe is a focusable area like any
24
24
  # other, since Dommy keeps a frame's focus out of its parent's.
25
25
  module SequentialFocusNavigation
26
- module_function
27
-
28
- # Move the focus as the user's Tab (`direction` :forward) or Shift+Tab
29
- # (:backward) in `document` does. Answers the element focused, or nil.
30
- def navigate(document, direction)
31
- focused = document.__internal_focused_element__
32
- starting_point = focused || document
33
- point = document.__internal_sequential_focus_navigation_starting_point__
34
- if point && point_usable?(point, document) &&
35
- (focused.nil? || Retargeting.shadow_including_inclusive_ancestor?(focused, point))
36
- starting_point = point
37
- end
26
+ class << self
27
+ # Move the focus as the user's Tab (`direction` :forward) or Shift+Tab
28
+ # (:backward) in `document` does. Answers the element focused, or nil.
29
+ def navigate(document, direction)
30
+ focused = document.__internal_focused_element__
31
+ starting_point = focused || document
32
+ point = document.__internal_sequential_focus_navigation_starting_point__
33
+ if point && point_usable?(point, document) &&
34
+ (focused.nil? || Retargeting.shadow_including_inclusive_ancestor?(focused, point))
35
+ starting_point = point
36
+ end
38
37
 
39
- restarted = false
40
- loop do
41
- order = navigation_order(document)
42
- mechanism = starting_point.equal?(document) || order.any? { |e| e.equal?(starting_point) } ? :sequential : :dom
43
- candidate = search(document, order, starting_point, direction, mechanism)
44
- if candidate
45
- document.__internal_with_focus_type__(:keyboard) { Focusability.run_focusing_steps(candidate) }
46
- return candidate
38
+ restarted = false
39
+ loop do
40
+ order = navigation_order(document)
41
+ mechanism = starting_point.equal?(document) || order.any? { |e| e.equal?(starting_point) } ? :sequential : :dom
42
+ candidate = search(document, order, starting_point, direction, mechanism)
43
+ if candidate
44
+ document.__internal_with_focus_type__(:keyboard) { Focusability.run_focusing_steps(candidate) }
45
+ return candidate
46
+ end
47
+
48
+ document.__internal_sequential_focus_navigation_starting_point__ = nil
49
+ # No controls of our own to move to: start over from the document.
50
+ return nil if restarted || starting_point.equal?(document)
51
+
52
+ restarted = true
53
+ starting_point = document
47
54
  end
55
+ end
48
56
 
49
- document.__internal_sequential_focus_navigation_starting_point__ = nil
50
- # No controls of our own to move to: start over from the document.
51
- return nil if restarted || starting_point.equal?(document)
57
+ private
52
58
 
53
- restarted = true
54
- starting_point = document
55
- end
56
- end
59
+ # The sequential navigation search algorithm over `order` (the
60
+ # document's sequential focus navigation order).
61
+ def search(document, order, starting_point, direction, mechanism)
62
+ suitable = order.select { |element| suitable?(element) }
63
+ if starting_point.equal?(document)
64
+ return direction == :forward ? suitable.first : suitable.last
65
+ end
57
66
 
58
- # The sequential navigation search algorithm over `order` (the
59
- # document's sequential focus navigation order).
60
- def search(document, order, starting_point, direction, mechanism)
61
- suitable = order.select { |element| suitable?(element) }
62
- if starting_point.equal?(document)
63
- return direction == :forward ? suitable.first : suitable.last
64
- end
67
+ if mechanism == :sequential
68
+ index = order.index { |e| e.equal?(starting_point) }
69
+ before = order[0...index]
70
+ after = order[(index + 1)..]
71
+ return direction == :forward ? after.find { |e| suitable?(e) } : before.reverse.find { |e| suitable?(e) }
72
+ end
65
73
 
66
- if mechanism == :sequential
67
- index = order.index { |e| e.equal?(starting_point) }
68
- before = order[0...index]
69
- after = order[(index + 1)..]
70
- return direction == :forward ? after.find { |e| suitable?(e) } : before.reverse.find { |e| suitable?(e) }
71
- end
74
+ # DOM: the suitable area nearest the starting point in shadow-including
75
+ # tree order.
76
+ positions = tree_positions(document)
77
+ start = positions[starting_point]
78
+ return nil if start.nil?
72
79
 
73
- # DOM: the suitable area nearest the starting point in shadow-including
74
- # tree order.
75
- positions = tree_positions(document)
76
- start = positions[starting_point]
77
- return nil if start.nil?
78
-
79
- ranked = suitable.filter_map { |e| (pos = positions[e]) && [pos, e] }
80
- if direction == :forward
81
- ranked.select { |pos, _| pos > start }.min_by(&:first)&.last
82
- else
83
- ranked.select { |pos, _| pos < start }.max_by(&:first)&.last
80
+ ranked = suitable.filter_map { |e| (pos = positions[e]) && [pos, e] }
81
+ if direction == :forward
82
+ ranked.select { |pos, _| pos > start }.min_by(&:first)&.last
83
+ else
84
+ ranked.select { |pos, _| pos < start }.max_by(&:first)&.last
85
+ end
84
86
  end
85
- end
86
87
 
87
- # HTML "suitable sequentially focusable area": not inert, and
88
- # sequentially focusable.
89
- def suitable?(element) = !Focusability.inert?(element) && Focusability.sequentially_focusable?(element)
88
+ # HTML "suitable sequentially focusable area": not inert, and
89
+ # sequentially focusable.
90
+ def suitable?(element) = !Focusability.inert?(element) && Focusability.sequentially_focusable?(element)
90
91
 
91
- # The document's sequential focus navigation order: its flattened
92
- # tabindex-ordered focus navigation scope.
93
- def navigation_order(document)
94
- root = document.document_element
95
- return [] if root.nil?
92
+ # The document's sequential focus navigation order: its flattened
93
+ # tabindex-ordered focus navigation scope.
94
+ def navigation_order(document)
95
+ root = document.document_element
96
+ return [] if root.nil?
96
97
 
97
- triggers = {}.compare_by_identity
98
- each_element(root) do |element|
99
- triggers[element.__internal_popover_trigger__] = true if popover_with_trigger?(element)
98
+ triggers = {}.compare_by_identity
99
+ each_element(root) do |element|
100
+ triggers[element.__internal_popover_trigger__] = true if popover_with_trigger?(element)
101
+ end
102
+ scopes = {}.compare_by_identity
103
+ collect(root, document, scopes)
104
+ flatten(document, scopes, {}.compare_by_identity, triggers)
100
105
  end
101
- scopes = {}.compare_by_identity
102
- collect(root, document, scopes)
103
- flatten(document, scopes, {}.compare_by_identity, triggers)
104
- end
105
106
 
106
- # Every element of the shadow-including tree under `element`.
107
- def each_element(element, &block)
108
- yield element
109
- shadow = element.respond_to?(:__internal_shadow_root__) ? element.__internal_shadow_root__ : nil
110
- shadow&.children&.to_a&.each { |child| each_element(child, &block) }
111
- element.children.to_a.each { |child| each_element(child, &block) }
112
- end
107
+ # Every element of the shadow-including tree under `element`.
108
+ def each_element(element, &block)
109
+ yield element
110
+ shadow = element.respond_to?(:__internal_shadow_root__) ? element.__internal_shadow_root__ : nil
111
+ shadow&.children&.to_a&.each { |child| each_element(child, &block) }
112
+ element.children.to_a.each { |child| each_element(child, &block) }
113
+ end
113
114
 
114
- # Walk the shadow-including tree, filing each element under its
115
- # associated focus navigation owner (HTML's algorithm), in tree order.
116
- def collect(element, owner, scopes)
117
- owner = element.__internal_popover_trigger__ if popover_with_trigger?(element)
118
- (scopes[owner] ||= []) << element
119
-
120
- shadow = element.respond_to?(:__internal_shadow_root__) ? element.__internal_shadow_root__ : nil
121
- if shadow
122
- shadow.children.to_a.each { |child| collect(child, element, scopes) }
123
- element.children.to_a.each do |child|
124
- slot = child.respond_to?(:assigned_slot) ? child.assigned_slot : nil
125
- collect(child, slot, scopes) if slot
115
+ # Walk the shadow-including tree, filing each element under its
116
+ # associated focus navigation owner (HTML's algorithm), in tree order.
117
+ def collect(element, owner, scopes)
118
+ owner = element.__internal_popover_trigger__ if popover_with_trigger?(element)
119
+ (scopes[owner] ||= []) << element
120
+
121
+ shadow = element.respond_to?(:__internal_shadow_root__) ? element.__internal_shadow_root__ : nil
122
+ if shadow
123
+ shadow.children.to_a.each { |child| collect(child, element, scopes) }
124
+ element.children.to_a.each do |child|
125
+ slot = child.respond_to?(:assigned_slot) ? child.assigned_slot : nil
126
+ collect(child, slot, scopes) if slot
127
+ end
128
+ elsif slot?(element) && in_shadow_tree?(element)
129
+ # Fallback content is not rendered while the slot has assigned
130
+ # nodes; otherwise it is the slot's scope. (HTML's owner algorithm
131
+ # would file it under the slot's own owner; browsers and WPT's
132
+ # shadow-dom/focus-navigation tests navigate it as the slot's.)
133
+ element.children.to_a.each { |child| collect(child, element, scopes) } if element.assigned_nodes.to_a.empty?
134
+ else
135
+ element.children.to_a.each { |child| collect(child, owner, scopes) }
126
136
  end
127
- elsif slot?(element) && in_shadow_tree?(element)
128
- # Fallback content is not rendered while the slot has assigned
129
- # nodes; otherwise it is the slot's scope. (HTML's owner algorithm
130
- # would file it under the slot's own owner; browsers and WPT's
131
- # shadow-dom/focus-navigation tests navigate it as the slot's.)
132
- element.children.to_a.each { |child| collect(child, element, scopes) } if element.assigned_nodes.to_a.empty?
133
- else
134
- element.children.to_a.each { |child| collect(child, owner, scopes) }
135
137
  end
136
- end
137
138
 
138
- # HTML "flattened tabindex-ordered focus navigation scope" of `owner`.
139
- def flatten(owner, scopes, visited, triggers)
140
- visited[owner] = true
141
- result = []
142
- tabindex_ordered(scopes[owner] || [], triggers).each do |item|
143
- if scope_owner?(item, triggers) && !visited[item]
144
- result << item if Focusability.focusable_area?(item)
145
- result.concat(flatten(item, scopes, visited, triggers))
146
- else
147
- result << item
139
+ # HTML "flattened tabindex-ordered focus navigation scope" of `owner`.
140
+ def flatten(owner, scopes, visited, triggers)
141
+ visited[owner] = true
142
+ result = []
143
+ tabindex_ordered(scopes[owner] || [], triggers).each do |item|
144
+ if scope_owner?(item, triggers) && !visited[item]
145
+ result << item if Focusability.focusable_area?(item)
146
+ result.concat(flatten(item, scopes, visited, triggers))
147
+ else
148
+ result << item
149
+ end
148
150
  end
151
+ result
149
152
  end
150
- result
151
- end
152
153
 
153
- # HTML "tabindex-ordered focus navigation scope": the scope owners and
154
- # focusable areas of a scope, minus those with a negative tabindex,
155
- # positive tabindex values first in increasing order, then the rest;
156
- # tree order otherwise.
157
- def tabindex_ordered(members, triggers)
158
- kept = members.select do |element|
159
- next false unless scope_owner?(element, triggers) || Focusability.focusable_area?(element)
154
+ # HTML "tabindex-ordered focus navigation scope": the scope owners and
155
+ # focusable areas of a scope, minus those with a negative tabindex,
156
+ # positive tabindex values first in increasing order, then the rest;
157
+ # tree order otherwise.
158
+ def tabindex_ordered(members, triggers)
159
+ kept = members.select do |element|
160
+ next false unless scope_owner?(element, triggers) || Focusability.focusable_area?(element)
160
161
 
161
- value = Focusability.tabindex_value(element)
162
- value.nil? || value >= 0
162
+ value = Focusability.tabindex_value(element)
163
+ value.nil? || value >= 0
164
+ end
165
+ kept.each_with_index.sort_by do |element, index|
166
+ value = Focusability.tabindex_value(element) || 0
167
+ value.positive? ? [0, value, index] : [1, 0, index]
168
+ end.map(&:first)
163
169
  end
164
- kept.each_with_index.sort_by do |element, index|
165
- value = Focusability.tabindex_value(element) || 0
166
- value.positive? ? [0, value, index] : [1, 0, index]
167
- end.map(&:first)
168
- end
169
170
 
170
- # HTML "focus navigation scope owner" (an element one: the Document is
171
- # the root's owner).
172
- def scope_owner?(element, triggers)
173
- return true if element.respond_to?(:__internal_shadow_root__) && element.__internal_shadow_root__
174
- return true if slot?(element)
171
+ # HTML "focus navigation scope owner" (an element one: the Document is
172
+ # the root's owner).
173
+ def scope_owner?(element, triggers)
174
+ return true if element.respond_to?(:__internal_shadow_root__) && element.__internal_shadow_root__
175
+ return true if slot?(element)
175
176
 
176
- triggers.key?(element)
177
- end
177
+ triggers.key?(element)
178
+ end
178
179
 
179
- def slot?(element) = element.is_a?(HTMLSlotElement)
180
+ def slot?(element) = element.is_a?(HTMLSlotElement)
180
181
 
181
- def in_shadow_tree?(element) = element.get_root_node.is_a?(ShadowRoot)
182
+ def in_shadow_tree?(element) = element.get_root_node.is_a?(ShadowRoot)
182
183
 
183
- def popover_with_trigger?(element)
184
- element.respond_to?(:__internal_popover_trigger__) && element.__internal_popover_showing__? &&
185
- !element.__internal_popover_trigger__.nil?
186
- end
184
+ def popover_with_trigger?(element)
185
+ element.respond_to?(:__internal_popover_trigger__) && element.__internal_popover_showing__? &&
186
+ !element.__internal_popover_trigger__.nil?
187
+ end
187
188
 
188
- def point_usable?(point, document)
189
- point.respond_to?(:owner_document) && point.owner_document.equal?(document) && point.is_connected?
190
- end
189
+ def point_usable?(point, document)
190
+ point.respond_to?(:owner_document) && point.owner_document.equal?(document) && point.is_connected?
191
+ end
191
192
 
192
- # Each node's position in shadow-including tree order.
193
- def tree_positions(document)
194
- positions = {}.compare_by_identity
195
- counter = 0
196
- walk = lambda do |node|
197
- positions[node] = (counter += 1)
198
- shadow = node.respond_to?(:__internal_shadow_root__) ? node.__internal_shadow_root__ : nil
199
- walk.call(shadow) if shadow
200
- node.child_nodes.to_a.each { |child| walk.call(child) } if node.respond_to?(:child_nodes)
193
+ # Each node's position in shadow-including tree order.
194
+ def tree_positions(document)
195
+ positions = {}.compare_by_identity
196
+ counter = 0
197
+ walk = lambda do |node|
198
+ positions[node] = (counter += 1)
199
+ shadow = node.respond_to?(:__internal_shadow_root__) ? node.__internal_shadow_root__ : nil
200
+ walk.call(shadow) if shadow
201
+ node.child_nodes.to_a.each { |child| walk.call(child) } if node.respond_to?(:child_nodes)
202
+ end
203
+ walk.call(document)
204
+ positions
201
205
  end
202
- walk.call(document)
203
- positions
204
206
  end
205
-
206
- private_class_method :search, :suitable?, :each_element, :collect, :flatten, :tabindex_ordered,
207
- :scope_owner?, :slot?, :in_shadow_tree?, :popover_with_trigger?, :point_usable?, :tree_positions
208
207
  end
209
208
  end
210
209
  end
@@ -27,6 +27,13 @@ module Dommy
27
27
  @by_host[Backend.identity_key(host_node)]
28
28
  end
29
29
 
30
+ # Forget a shadow root that moved to another document with its host
31
+ # (`fragment_node` and `host_node` are what it was registered under).
32
+ def unregister(fragment_node, host_node)
33
+ @shadow_roots.delete(Backend.identity_key(fragment_node))
34
+ @by_host.delete(Backend.identity_key(host_node))
35
+ end
36
+
30
37
  # Find the ShadowRoot for a given fragment (if any)
31
38
  def find_for_fragment(fragment_node)
32
39
  return nil unless fragment_node
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dommy
4
+ module Internal
5
+ # DOM slot assignment, read from the tree when asked: "find a slot" gives
6
+ # the slot a slottable goes to, "find slottables" the slottables a slot
7
+ # gets. Only an HTML `<slot>` is a slot (an SVG or MathML one is an
8
+ # ordinary element), and only an Element or a Text node is a slottable.
9
+ #
10
+ # Spec: https://dom.spec.whatwg.org/#find-a-slot
11
+ module SlotAssignment
12
+ class << self
13
+ # Whether backend node `node` is an HTML slot element.
14
+ def slot_node?(node)
15
+ node.element? && node.local_name == "slot" && Backend.namespace_uri(node) == Namespaces::HTML
16
+ end
17
+
18
+ # DOM "slottable": an Element or a Text node.
19
+ def slottable?(node)
20
+ node.element? || node.text?
21
+ end
22
+
23
+ # The HTML slots of the subtree rooted at backend node `root` (an
24
+ # element or a fragment, itself included), in tree order, found by the
25
+ # backend rather than a walk in Ruby. In an XML document the backend's
26
+ # selector engine matches `slot` only where a default namespace is
27
+ # declared, which createElementNS's elements lack, so the descendants
28
+ # are found by local name there.
29
+ def slot_nodes(root)
30
+ return [] unless root.element? || root.is_a?(Backend.document_fragment_class)
31
+
32
+ candidates = Backend.xml_node?(root) ? root.xpath(".//*[local-name()='slot']") : root.css("slot")
33
+ slots = candidates.select { |node| slot_node?(node) }
34
+ slots.unshift(root) if slot_node?(root)
35
+ slots
36
+ end
37
+
38
+ # A slottable's name: an element's `slot` attribute, a Text node's "".
39
+ def slottable_name(node)
40
+ node.element? ? Backend.no_namespace_attribute_value(node, "slot").to_s : ""
41
+ end
42
+
43
+ # The first slot of each name in `shadow`'s tree, as name => backend node.
44
+ def first_slots_by_name(shadow)
45
+ firsts = {}
46
+ slot_nodes(shadow.__dommy_backend_node__).each { |node| firsts[slot_name(node)] ||= node }
47
+ firsts
48
+ end
49
+
50
+ # DOM "find a slot" for backend node `node`: the wrapped slot, or nil.
51
+ # `open` is the spec's open flag: a closed shadow tree's slot is nil.
52
+ def find_slot(document, node, open: false)
53
+ parent = node.parent
54
+ return nil unless parent&.element? && slottable?(node)
55
+
56
+ shadow = document.__internal_shadow_root_for_host__(parent)
57
+ return nil unless shadow
58
+ return nil if open && shadow.mode != "open"
59
+
60
+ if shadow.slot_assignment == "manual"
61
+ slot = document.__internal_peek_wrapper__(node)&.__internal_manual_slot__
62
+ return slot if slot && document.__internal_shadow_root_containing__(slot.__dommy_backend_node__).equal?(shadow)
63
+
64
+ return nil
65
+ end
66
+
67
+ name = slottable_name(node)
68
+ found = slot_nodes(shadow.__dommy_backend_node__).find { |slot| slot_name(slot) == name }
69
+ found && document.wrap_node(found)
70
+ end
71
+
72
+ # DOM "find slottables" for a wrapped slot: its slottables, wrapped, in
73
+ # tree order (named) or assignment order (manual).
74
+ def find_slottables(document, slot)
75
+ slot_node = slot.__dommy_backend_node__
76
+ shadow = document.__internal_shadow_root_containing__(slot_node)
77
+ host_node = shadow&.host&.__dommy_backend_node__
78
+ return [] unless host_node
79
+ return manual_slottables(slot, host_node) if shadow.slot_assignment == "manual"
80
+
81
+ name = slot_name(slot_node)
82
+ first = slot_nodes(shadow.__dommy_backend_node__).find { |node| slot_name(node) == name }
83
+ return [] unless first && same_node?(first, slot_node)
84
+
85
+ host_node.children.filter_map do |child|
86
+ document.wrap_node(child) if slottable?(child) && slottable_name(child) == name
87
+ end
88
+ end
89
+
90
+ # A manual slot's assigned nodes that are `host_node`'s children.
91
+ def manual_slottables(slot, host_node)
92
+ host_key = Backend.identity_key(host_node)
93
+ slot.__internal_manual_assignment__.select do |node|
94
+ parent = node.__dommy_backend_node__&.parent
95
+ parent && Backend.identity_key(parent) == host_key
96
+ end
97
+ end
98
+
99
+ # Whether `host_node` has a slottable child named `name`.
100
+ def slottable_named?(host_node, name)
101
+ host_node.children.any? { |child| slottable?(child) && slottable_name(child) == name }
102
+ end
103
+
104
+ def same_node?(a, b)
105
+ a.nil? ? b.nil? : (!b.nil? && Backend.identity_key(a) == Backend.identity_key(b))
106
+ end
107
+
108
+ private
109
+
110
+ # A slot's name: its `name` attribute.
111
+ def slot_name(node)
112
+ Backend.no_namespace_attribute_value(node, "name").to_s
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end