Class: Dommy::Fragment
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
Instance Attribute Summary collapse
Instance Method Summary
collapse
included
#append, #append_child, #prepend, #replace_children
Methods included from Node
#compare_document_position, #get_root_node, #is_default_namespace, #is_equal_node, #is_same_node, #lookup_namespace_uri, #lookup_prefix
#__internal_deliver_event__, #add_event_listener, capture_flag, #deliver_at, #dispatch_event, js_truthy?, #remove_event_listener
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
#document ⇒ Object
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
|
# File 'lib/dommy/element.rb', line 113
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.nokogiri_doc)) : @document
.wrap_node(Parser.fragment("", owner_doc: @document.nokogiri_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"
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/dommy/element.rb', line 80
def __js_get__(key)
case key
when "nodeType"
11
when "nodeName"
"#document-fragment"
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 "ownerDocument"
@document
end
end
|
#child_element_count ⇒ Object
28
29
30
|
# File 'lib/dommy/element.rb', line 28
def child_element_count
@__node__.element_children.size
end
|
#child_nodes ⇒ Object
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
|
#children ⇒ Object
Public Ruby API (DocumentFragment surface)
24
25
26
|
# File 'lib/dommy/element.rb', line 24
def children
element_children
end
|
#contains?(other) ⇒ Boolean
206
207
208
209
210
211
|
# File 'lib/dommy/element.rb', line 206
def contains?(other)
return false unless other.respond_to?(:__dommy_backend_node__)
on = other.__dommy_backend_node__
on == @__node__ || on.ancestors.include?(@__node__)
end
|
170
171
172
173
174
|
# File 'lib/dommy/element.rb', line 170
def
nodes = @__node__.children.to_a
nodes.each(&:unlink)
nodes
end
|
#first_child ⇒ Object
40
41
42
|
# File 'lib/dommy/element.rb', line 40
def first_child
@document.wrap_node(@__node__.children.first)
end
|
#first_element_child ⇒ Object
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
74
75
76
77
78
|
# File 'lib/dommy/element.rb', line 74
def get_element_by_id(id)
return nil if id.nil?
@document.wrap_node(@__node__.at_css("##{id}"))
end
|
#insert_before(node, ref) ⇒ Object
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/dommy/element.rb', line 186
def insert_before(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_child ⇒ Object
44
45
46
|
# File 'lib/dommy/element.rb', line 44
def last_child
@document.wrap_node(@__node__.children.last)
end
|
#last_element_child ⇒ Object
52
53
54
|
# File 'lib/dommy/element.rb', line 52
def last_element_child
@document.wrap_node(@__node__.element_children.last)
end
|
#normalize ⇒ Object
213
214
215
216
|
# File 'lib/dommy/element.rb', line 213
def normalize
@__node__.children.each { |n| n.unlink if n.text? && n.content.empty? }
nil
end
|
#query_selector(selector) ⇒ Object
#query_selector_all(selector) ⇒ Object
#remove_child(node) ⇒ Object
Node mutation on the fragment’s children (ParentNode covers append/prepend/ replaceChildren; these are the remaining Node methods).
178
179
180
181
182
183
184
|
# File 'lib/dommy/element.rb', line 178
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
197
198
199
200
201
202
203
204
|
# File 'lib/dommy/element.rb', line 197
def replace_child(new_child, old_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__
detach_dom_nodes(new_child).each { |n| old_bn.add_previous_sibling(n) }
old_bn.unlink
old_child
end
|
#text_content ⇒ Object
56
57
58
|
# File 'lib/dommy/element.rb', line 56
def text_content
@__node__.text
end
|