Class: Taurus::XML::Node
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Searchable
#at, #at_css, #at_xpath, #css, #search, #xpath
Constructor Details
#initialize(c_ptr, document, parent: nil) ⇒ Node
Returns a new instance of Node.
6
7
8
9
10
|
# File 'lib/taurus/xml/node.rb', line 6
def initialize(c_ptr, document, parent: nil)
@c_ptr = c_ptr
@document = document
@parent = parent
end
|
Instance Attribute Details
#c_ptr ⇒ Object
Returns the value of attribute c_ptr.
4
5
6
|
# File 'lib/taurus/xml/node.rb', line 4
def c_ptr
@c_ptr
end
|
#document ⇒ Object
Returns the value of attribute document.
4
5
6
|
# File 'lib/taurus/xml/node.rb', line 4
def document
@document
end
|
Class Method Details
.wrap(c_ptr, document, parent: nil) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/taurus/xml/node.rb', line 12
def self.wrap(c_ptr, document, parent: nil)
if document && (cached = document.wrapper_cache[c_ptr.address])
return cached
end
node =
case Taurus::XML::FFI.taurus_node_get_type(c_ptr)
when Taurus::XML::FFI::NODE_ELEMENT
Taurus::XML::Element.new(c_ptr, document, parent: parent)
when Taurus::XML::FFI::NODE_TEXT
Taurus::XML::Text.new(c_ptr, document, parent: parent)
when Taurus::XML::FFI::NODE_COMMENT
Taurus::XML::Comment.new(c_ptr, document, parent: parent)
when Taurus::XML::FFI::NODE_CDATA
Taurus::XML::CDATA.new(c_ptr, document, parent: parent)
when Taurus::XML::FFI::NODE_PI
Taurus::XML::ProcessingInstruction.new(c_ptr, document, parent: parent)
else
new(c_ptr, document, parent: parent)
end
document.wrapper_cache[c_ptr.address] = node if document
node
end
|
Instance Method Details
#<=>(other) ⇒ Object
76
77
78
79
80
|
# File 'lib/taurus/xml/node.rb', line 76
def <=>(other)
return nil unless other.is_a?(Taurus::XML::Node)
return nil unless @document == other.document
Taurus::XML::FFI.taurus_node_compare(@c_ptr, other.c_ptr)
end
|
#==(other) ⇒ Object
196
197
198
199
|
# File 'lib/taurus/xml/node.rb', line 196
def ==(other)
return false unless other.is_a?(Taurus::XML::Node)
@c_ptr == other.c_ptr
end
|
#child ⇒ Object
82
83
84
85
86
|
# File 'lib/taurus/xml/node.rb', line 82
def child
ptr = Taurus::XML::FFI.taurus_node_first_child(@c_ptr)
return nil if ptr.null?
Taurus::XML::Node.wrap(ptr, @document, parent: as_element_or_self)
end
|
#children ⇒ Object
88
89
90
91
92
93
94
95
96
|
# File 'lib/taurus/xml/node.rb', line 88
def children
nodes = []
ptr = Taurus::XML::FFI.taurus_node_first_child(@c_ptr)
until ptr.nil? || ptr.null?
nodes << Taurus::XML::Node.wrap(ptr, @document, parent: as_element_or_self)
ptr = Taurus::XML::FFI.taurus_node_next_sibling(ptr)
end
Taurus::XML::NodeSet.new(@document, nodes)
end
|
#content ⇒ Object
Also known as:
text, inner_text
45
46
47
|
# File 'lib/taurus/xml/node.rb', line 45
def content
raise NotImplementedError, "#{self.class}#content not implemented"
end
|
#css_path ⇒ Object
179
180
181
182
183
184
185
|
# File 'lib/taurus/xml/node.rb', line 179
def css_path
return nil if path.nil?
path.split("/").filter_map do |part|
next nil if part.empty?
part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)')
end.join(" > ")
end
|
#dup ⇒ Object
Also known as:
clone
187
188
189
190
191
192
193
|
# File 'lib/taurus/xml/node.rb', line 187
def dup
elem_ptr = Taurus::XML::FFI.taurus_node_as_element(@c_ptr)
raise Taurus::XML::Error, "dup is only supported for element nodes" if elem_ptr.null?
copy_ptr = Taurus::XML::FFI.taurus_element_copy(elem_ptr, @document.c_ptr)
raise Taurus::XML::Error, "taurus_element_copy failed" if copy_ptr.null?
Taurus::XML::Element.new(copy_ptr, @document)
end
|
#element_children ⇒ Object
Also known as:
elements
126
127
128
|
# File 'lib/taurus/xml/node.rb', line 126
def element_children
children.select(&:element?)
end
|
#first_element_child ⇒ Object
112
113
114
115
116
117
118
119
120
|
# File 'lib/taurus/xml/node.rb', line 112
def first_element_child
ptr = Taurus::XML::FFI.taurus_node_first_child(@c_ptr)
until ptr.nil? || ptr.null?
node = Taurus::XML::Node.wrap(ptr, @document, parent: as_element_or_self)
return node if node.element?
ptr = Taurus::XML::FFI.taurus_node_next_sibling(ptr)
end
nil
end
|
#inspect ⇒ Object
201
202
203
|
# File 'lib/taurus/xml/node.rb', line 201
def inspect
"#<#{self.class.name} ptr=#{c_ptr}>"
end
|
#last_element_child ⇒ Object
122
123
124
|
# File 'lib/taurus/xml/node.rb', line 122
def last_element_child
children.reverse_each.find(&:element?)
end
|
#line ⇒ Object
72
73
74
|
# File 'lib/taurus/xml/node.rb', line 72
def line
Taurus::XML::FFI.taurus_node_line(@c_ptr)
end
|
#name ⇒ Object
41
42
43
|
# File 'lib/taurus/xml/node.rb', line 41
def name
raise NotImplementedError, "#{self.class}#name not implemented"
end
|
#next_element ⇒ Object
131
132
133
134
135
|
# File 'lib/taurus/xml/node.rb', line 131
def next_element
sibling = next_sibling
sibling = sibling.next_sibling until sibling.nil? || sibling.element?
sibling
end
|
#next_sibling ⇒ Object
Also known as:
next
98
99
100
101
102
|
# File 'lib/taurus/xml/node.rb', line 98
def next_sibling
ptr = Taurus::XML::FFI.taurus_node_next_sibling(@c_ptr)
return nil if ptr.null?
Taurus::XML::Node.wrap(ptr, @document, parent: @parent)
end
|
#parent ⇒ Object
65
66
67
68
69
70
|
# File 'lib/taurus/xml/node.rb', line 65
def parent
return @parent if @parent
ptr = Taurus::XML::FFI.taurus_node_parent(@c_ptr)
return nil if ptr.null?
Taurus::XML::Element.new(ptr, @document)
end
|
#path ⇒ Object
173
174
175
176
177
|
# File 'lib/taurus/xml/node.rb', line 173
def path
str_ptr = Taurus::XML::FFI.taurus_node_get_xpath(@c_ptr)
return nil if str_ptr.null?
str_ptr.read_string.tap { Taurus::XML::FFI.taurus_free_string(str_ptr) }
end
|
#previous_element ⇒ Object
137
138
139
140
141
|
# File 'lib/taurus/xml/node.rb', line 137
def previous_element
sibling = previous_sibling
sibling = sibling.previous_sibling until sibling.nil? || sibling.element?
sibling
end
|
#previous_sibling ⇒ Object
Also known as:
previous
105
106
107
108
109
|
# File 'lib/taurus/xml/node.rb', line 105
def previous_sibling
ptr = Taurus::XML::FFI.taurus_node_previous_sibling(@c_ptr)
return nil if ptr.null?
Taurus::XML::Node.wrap(ptr, @document, parent: @parent)
end
|
#processing_instruction? ⇒ Boolean
Also known as:
pi?
60
61
62
|
# File 'lib/taurus/xml/node.rb', line 60
def processing_instruction?
type == Taurus::XML::FFI::NODE_PI
end
|
#traverse ⇒ Object
Walks the subtree in post-order DFS (matches Nokogiri's semantics).
Specialized hot path: skips the intermediate NodeSet allocation that
Element#children would create, walking via raw FFI calls and wrapping
nodes directly. Saves one Array + one NodeSet allocation per parent
node. For a tree of N nodes that's ~N fewer allocations on a full
traversal.
Still pays ~2 FFI calls per visited node (first_child + next_sibling).
Beating Nokogiri on this benchmark needs C-side traverse with a
callback (libtaurus #273); the per-node FFI cost is the floor.
163
164
165
166
167
168
169
170
171
|
# File 'lib/taurus/xml/node.rb', line 163
def traverse
return enum_for(:traverse) unless block_given?
callback = ::FFI::Function.new(:int, [:pointer, :pointer], blocking: true) do |node_ptr, _|
yield Taurus::XML::Node.wrap(node_ptr, @document)
0
end
Taurus::XML::FFI.taurus_node_traverse(
@c_ptr, Taurus::XML::FFI::TRAVERSE_POST_ORDER, callback, nil)
end
|
#type ⇒ Object
Also known as:
node_type
51
52
53
|
# File 'lib/taurus/xml/node.rb', line 51
def type
Taurus::XML::FFI.taurus_node_get_type(@c_ptr)
end
|
#unlink ⇒ Object
Also known as:
remove