Class: Dommy::Fragment

Inherits:
Object
  • Object
show all
Includes:
Bridge::Methods, EventTarget, Internal::ParentNode, Node
Defined in:
lib/dommy/element.rb

Constant Summary

Constants included from Node

Node::ATTRIBUTE_NODE, Node::CDATA_SECTION_NODE, Node::COMMENT_NODE, Node::DOCUMENT_FRAGMENT_NODE, Node::DOCUMENT_NODE, Node::DOCUMENT_POSITION_CONTAINED_BY, Node::DOCUMENT_POSITION_CONTAINS, Node::DOCUMENT_POSITION_DISCONNECTED, Node::DOCUMENT_POSITION_FOLLOWING, Node::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, Node::DOCUMENT_POSITION_PRECEDING, Node::DOCUMENT_TYPE_NODE, Node::ELEMENT_NODE, Node::HTML_NAMESPACE, Node::PROCESSING_INSTRUCTION_NODE, Node::TEXT_NODE, Node::XMLNS_NAMESPACE, Node::XML_NAMESPACE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Bridge::Methods

included

Methods included from Internal::ParentNode

#append, #append_child, #normalize, #prepend, #replace_children

Methods included from Internal::ChildNode

#child_node_after, #child_node_before, #child_node_replace_with, #nullable_dom_string, #validate_insert_before_ref!

Methods included from Node

#compare_document_position, #get_root_node, #is_default_namespace, #is_equal_node, #is_same_node, #lookup_namespace_uri, #lookup_prefix

Methods included from EventTarget

#__dommy_dump_event_failure__, #__internal_deliver_event__, #__internal_process_event_handler_return__, #add_event_listener, capture_flag, #deliver_at, #dispatch_event, #event_name_from_on, #invoke_listener_isolated, js_truthy?, #on_handler, #remove_event_listener, #set_on_handler

Constructor Details

#initialize(document, nokogiri_node) ⇒ Fragment

Returns a new instance of Fragment.



17
18
19
20
# File 'lib/dommy/element.rb', line 17

def initialize(document, nokogiri_node)
  @document = document
  @__node__ = nokogiri_node
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



13
14
15
# File 'lib/dommy/element.rb', line 13

def document
  @document
end

Instance Method Details

#__dommy_backend_node__Object



15
# File 'lib/dommy/element.rb', line 15

def __dommy_backend_node__ = @__node__

#__js_call__(method, args) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/dommy/element.rb', line 144

def __js_call__(method, args)
  case method
  when "hasChildNodes"
    @__node__.children.any?
  when "compareDocumentPosition"
    compare_document_position(args[0])
  when "lookupNamespaceURI"
    lookup_namespace_uri(args[0])
  when "lookupPrefix"
    lookup_prefix(args[0])
  when "isDefaultNamespace"
    is_default_namespace(args[0])
  when "cloneNode"
    deep = args.empty? ? false : !!args[0]
    deep ? @document.wrap_node(Parser.fragment(@__node__.to_html, owner_doc: @document.backend_doc)) : @document
      .wrap_node(Parser.fragment("", owner_doc: @document.backend_doc))
  when "querySelector"
    query_selector(Internal.css_query_arg!(args))
  when "querySelectorAll"
    query_selector_all(Internal.css_query_arg!(args))
  when "getElementById"
    get_element_by_id(args[0])
  when "appendChild"
    append_child(args[0])
  when "append"
    append(*args)
  when "prepend"
    prepend(*args)
  when "replaceChildren"
    replace_children(*args)
  when "removeChild"
    remove_child(args[0])
  when "insertBefore"
    validate_insert_before_ref!(args)
    insert_before(args[0], args[1])
  when "replaceChild"
    replace_child(args[0], args[1])
  when "isEqualNode"
    is_equal_node(args[0])
  when "isSameNode"
    is_same_node(args[0])
  when "getRootNode"
    get_root_node(args[0])
  when "contains"
    contains?(args[0])
  when "normalize"
    normalize
  when "addEventListener"
    add_event_listener(args[0], args[1], args[2])
  when "removeEventListener"
    remove_event_listener(args[0], args[1], args[2])
  when "dispatchEvent"
    dispatch_event(args[0])
  else
    nil
  end
end

#__js_get__(key) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/dommy/element.rb', line 102

def __js_get__(key)
  case key
  when "nodeType"
    11
  when "nodeName"
    "#document-fragment"
  when "nodeValue"
    # A DocumentFragment's nodeValue is null (not undefined).
    nil
  when "children"
    element_children
  when "childNodes"
    child_nodes
  when "childElementCount"
    child_element_count
  when "firstChild"
    first_child
  when "lastChild"
    last_child
  when "firstElementChild"
    first_element_child
  when "lastElementChild"
    last_element_child
  when "textContent"
    @__node__.text
  when "parentNode", "parentElement"
    # A DocumentFragment is never inserted, so it has no parent (null, not
    # undefined).
    nil
  when "ownerDocument"
    @document
  else
    Bridge::ABSENT
  end
end

#__js_set__(key, value) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/dommy/element.rb', line 71

def __js_set__(key, value)
  case key
  when "textContent"
    self.text_content = value
    nil
  else
    Bridge::UNHANDLED
  end
end

#child_element_countObject



28
29
30
# File 'lib/dommy/element.rb', line 28

def child_element_count
  @__node__.element_children.size
end

#child_nodesObject

Live, cached childNodes so fragment.childNodes === fragment.childNodes and later mutations are reflected (WHATWG live NodeList).



34
35
36
37
38
# File 'lib/dommy/element.rb', line 34

def child_nodes
  @live_child_nodes ||= LiveNodeList.new do
    @__node__.children.map { |n| @document.wrap_node(n) }.compact
  end
end

#childrenObject

Public Ruby API (DocumentFragment surface)



24
25
26
# File 'lib/dommy/element.rb', line 24

def children
  element_children
end

#contains?(other) ⇒ Boolean

Returns:

  • (Boolean)


247
248
249
250
251
252
253
254
255
# File 'lib/dommy/element.rb', line 247

def contains?(other)
  return false unless other.respond_to?(:__dommy_backend_node__)

  on = other.__dommy_backend_node__
  # Walk parents rather than the backend's `ancestors`: Makiri omits a
  # DocumentFragment parent from `ancestors`, so a fragment never appears
  # to contain its own children. `parent` is consistent across backends.
  on == @__node__ || Internal::NodeTraversal.ancestor_of?(@__node__, on)
end

#extract_childrenObject



202
203
204
205
206
207
208
209
210
211
# File 'lib/dommy/element.rb', line 202

def extract_children
  nodes = @__node__.children.to_a
  return nodes if nodes.empty?

  nodes.each(&:unlink)
  # Inserting a DocumentFragment removes all its children first; the spec
  # queues a single childList record on the fragment for that removal.
  @document.notify_child_list_mutation(target_node: @__node__, added_nodes: [], removed_nodes: nodes)
  nodes
end

#first_childObject



40
41
42
# File 'lib/dommy/element.rb', line 40

def first_child
  @document.wrap_node(@__node__.children.first)
end

#first_element_childObject



48
49
50
# File 'lib/dommy/element.rb', line 48

def first_element_child
  @document.wrap_node(@__node__.children.find(&:element?))
end

#get_element_by_id(id) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/dommy/element.rb', line 93

def get_element_by_id(id)
  return nil if id.nil? || id.to_s.empty?

  # getElementById matches the `id` attribute literally, not as a CSS
  # selector, so escape special characters (e.g. React `useId` `:rjm:`) to a
  # valid id-selector ident — a raw "##{id}" would be an invalid selector.
  @document.wrap_node(@__node__.at_css("##{Dommy::CSSNamespace.escape(id)}"))
end

#insert_before(node, ref) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/dommy/element.rb', line 223

def insert_before(node, ref)
  coerce_node_argument!(node)
  ensure_pre_insertion_validity!(node, ref)
  nodes = detach_dom_nodes(node)
  ref_bn = ref.respond_to?(:__dommy_backend_node__) ? ref.__dommy_backend_node__ : nil
  if ref_bn && ref_bn.parent == @__node__
    nodes.each { |n| ref_bn.add_previous_sibling(n) }
  else
    nodes.each { |n| @__node__.add_child(n) }
  end
  node
end

#last_childObject



44
45
46
# File 'lib/dommy/element.rb', line 44

def last_child
  @document.wrap_node(@__node__.children.last)
end

#last_element_childObject



52
53
54
# File 'lib/dommy/element.rb', line 52

def last_element_child
  @document.wrap_node(@__node__.element_children.last)
end

#query_selector(selector) ⇒ Object



81
82
83
84
85
# File 'lib/dommy/element.rb', line 81

def query_selector(selector)
  return nil if selector.nil?
  ast = Internal::SelectorParser.parse!(selector)
  Internal::SelectorMatcher.query_first(self, ast, scope: self)
end

#query_selector_all(selector) ⇒ Object



87
88
89
90
91
# File 'lib/dommy/element.rb', line 87

def query_selector_all(selector)
  return NodeList.new if selector.nil?
  ast = Internal::SelectorParser.parse!(selector)
  NodeList.new(Internal::SelectorMatcher.query(self, ast, scope: self))
end

#remove_child(node) ⇒ Object

Node mutation on the fragment's children (ParentNode covers append/prepend/ replaceChildren; these are the remaining Node methods).



215
216
217
218
219
220
221
# File 'lib/dommy/element.rb', line 215

def remove_child(node)
  bn = node.respond_to?(:__dommy_backend_node__) ? node.__dommy_backend_node__ : nil
  raise DOMException::NotFoundError, "node is not a child of this fragment" unless bn && bn.parent == @__node__

  bn.unlink
  node
end

#replace_child(new_child, old_child) ⇒ Object



236
237
238
239
240
241
242
243
244
245
# File 'lib/dommy/element.rb', line 236

def replace_child(new_child, old_child)
  coerce_node_argument!(new_child)
  old_bn = old_child.respond_to?(:__dommy_backend_node__) ? old_child.__dommy_backend_node__ : nil
  raise DOMException::NotFoundError, "node is not a child of this fragment" unless old_bn && old_bn.parent == @__node__

  ensure_pre_insertion_validity!(new_child, old_child)
  detach_dom_nodes(new_child).each { |n| old_bn.add_previous_sibling(n) }
  old_bn.unlink
  old_child
end

#text_contentObject



56
57
58
# File 'lib/dommy/element.rb', line 56

def text_content
  @__node__.text
end

#text_content=(value) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/dommy/element.rb', line 60

def text_content=(value)
  # Replace all children with a single Text node (nullable: null/undefined
  # clear with no replacement). Unlink old children so a removed node keeps
  # its own descendants.
  removed = @__node__.children.to_a
  str = nullable_dom_string(value)
  removed.each(&:unlink)
  @__node__.add_child(@document.create_text_node(str).__dommy_backend_node__) unless str.empty?
  notify_child_list(added: @__node__.children.to_a, removed: removed)
end