Class: Leptris::XML::Node

Inherits:
Object
  • Object
show all
Includes:
Searchable
Defined in:
lib/leptris/xml/node.rb

Direct Known Subclasses

Comment, Element, ProcessingInstruction, Text

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/leptris/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_ptrObject (readonly)

Returns the value of attribute c_ptr.



4
5
6
# File 'lib/leptris/xml/node.rb', line 4

def c_ptr
  @c_ptr
end

#documentObject (readonly)

Returns the value of attribute document.



4
5
6
# File 'lib/leptris/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/leptris/xml/node.rb', line 12

def self.wrap(c_ptr, document, parent: nil)
  # Per-document weak-ref cache. Returns the existing wrapper when the
  # same c_ptr is wrapped twice (common in children/sibling walks,
  # repeated xpath queries, traverse-then-access patterns). The cache
  # dies with the document so no stale entries.
  if document && (cached = document.wrapper_cache[c_ptr.address])
    return cached
  end

  node =
    case Leptris::XML::FFI.leptris_node_get_type(c_ptr)
    when Leptris::XML::FFI::NODE_ELEMENT
      Leptris::XML::Element.new(c_ptr, document, parent: parent)
    when Leptris::XML::FFI::NODE_TEXT
      Leptris::XML::Text.new(c_ptr, document, parent: parent)
    when Leptris::XML::FFI::NODE_COMMENT
      Leptris::XML::Comment.new(c_ptr, document, parent: parent)
    when Leptris::XML::FFI::NODE_CDATA
      Leptris::XML::CDATA.new(c_ptr, document, parent: parent)
    when Leptris::XML::FFI::NODE_PI
      Leptris::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



85
86
87
88
89
# File 'lib/leptris/xml/node.rb', line 85

def <=>(other)
  return nil unless other.is_a?(Leptris::XML::Node)
  return nil unless @document == other.document
  Leptris::XML::FFI.leptris_node_compare(@c_ptr, other.c_ptr)
end

#==(other) ⇒ Object



204
205
206
207
# File 'lib/leptris/xml/node.rb', line 204

def ==(other)
  return false unless other.is_a?(Leptris::XML::Node)
  @c_ptr == other.c_ptr
end

#cdata?Boolean

Returns:

  • (Boolean)


68
# File 'lib/leptris/xml/node.rb', line 68

def cdata?;    type == Leptris::XML::FFI::NODE_CDATA;    end

#childObject



91
92
93
94
95
# File 'lib/leptris/xml/node.rb', line 91

def child
  ptr = Leptris::XML::FFI.leptris_node_first_child(@c_ptr)
  return nil if ptr.null?
  Leptris::XML::Node.wrap(ptr, @document, parent: as_element_or_self)
end

#childrenObject



97
98
99
100
101
102
103
104
105
# File 'lib/leptris/xml/node.rb', line 97

def children
  nodes = []
  ptr = Leptris::XML::FFI.leptris_node_first_child(@c_ptr)
  until ptr.nil? || ptr.null?
    nodes << Leptris::XML::Node.wrap(ptr, @document, parent: as_element_or_self)
    ptr = Leptris::XML::FFI.leptris_node_next_sibling(ptr)
  end
  Leptris::XML::NodeSet.new(@document, nodes)
end

#comment?Boolean

Returns:

  • (Boolean)


67
# File 'lib/leptris/xml/node.rb', line 67

def comment?;  type == Leptris::XML::FFI::NODE_COMMENT;  end

#contentObject

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/leptris/xml/node.rb', line 45

def content
  raise NotImplementedError, "#{self.class}#content not implemented"
end

#css_pathObject



187
188
189
190
191
192
193
# File 'lib/leptris/xml/node.rb', line 187

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

#dupObject Also known as: clone



195
196
197
198
199
200
201
# File 'lib/leptris/xml/node.rb', line 195

def dup
  elem_ptr = Leptris::XML::FFI.leptris_node_as_element(@c_ptr)
  raise Leptris::XML::Error, "dup is only supported for element nodes" if elem_ptr.null?
  copy_ptr = Leptris::XML::FFI.leptris_element_copy(elem_ptr, @document.c_ptr)
  raise Leptris::XML::Error, "leptris_element_copy failed" if copy_ptr.null?
  Leptris::XML::Node.wrap(copy_ptr, @document)
end

#element?Boolean

Returns:

  • (Boolean)


65
# File 'lib/leptris/xml/node.rb', line 65

def element?;  type == Leptris::XML::FFI::NODE_ELEMENT;  end

#element_childrenObject Also known as: elements



135
136
137
# File 'lib/leptris/xml/node.rb', line 135

def element_children
  children.select(&:element?)
end

#first_element_childObject



121
122
123
124
125
126
127
128
129
# File 'lib/leptris/xml/node.rb', line 121

def first_element_child
  ptr = Leptris::XML::FFI.leptris_node_first_child(@c_ptr)
  until ptr.nil? || ptr.null?
    node = Leptris::XML::Node.wrap(ptr, @document, parent: as_element_or_self)
    return node if node.element?
    ptr = Leptris::XML::FFI.leptris_node_next_sibling(ptr)
  end
  nil
end

#inner_textObject



56
57
58
# File 'lib/leptris/xml/node.rb', line 56

def inner_text
  content
end

#inspectObject



209
210
211
# File 'lib/leptris/xml/node.rb', line 209

def inspect
  "#<#{self.class.name} ptr=#{c_ptr}>"
end

#last_element_childObject



131
132
133
# File 'lib/leptris/xml/node.rb', line 131

def last_element_child
  children.reverse_each.find(&:element?)
end

#lineObject



81
82
83
# File 'lib/leptris/xml/node.rb', line 81

def line
  Leptris::XML::FFI.leptris_node_line(@c_ptr)
end

#nameObject

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/leptris/xml/node.rb', line 41

def name
  raise NotImplementedError, "#{self.class}#name not implemented"
end

#next_elementObject



140
141
142
143
144
# File 'lib/leptris/xml/node.rb', line 140

def next_element
  sibling = next_sibling
  sibling = sibling.next_sibling until sibling.nil? || sibling.element?
  sibling
end

#next_siblingObject Also known as: next



107
108
109
110
111
# File 'lib/leptris/xml/node.rb', line 107

def next_sibling
  ptr = Leptris::XML::FFI.leptris_node_next_sibling(@c_ptr)
  return nil if ptr.null?
  Leptris::XML::Node.wrap(ptr, @document, parent: @parent)
end

#parentObject



74
75
76
77
78
79
# File 'lib/leptris/xml/node.rb', line 74

def parent
  return @parent if @parent
  ptr = Leptris::XML::FFI.leptris_node_parent(@c_ptr)
  return nil if ptr.null?
  Leptris::XML::Node.wrap(ptr, @document)
end

#pathObject



181
182
183
184
185
# File 'lib/leptris/xml/node.rb', line 181

def path
  str_ptr = Leptris::XML::FFI.leptris_node_get_xpath(@c_ptr)
  return nil if str_ptr.null?
  Leptris::XML::FFI.read_owned_string(str_ptr)
end

#previous_elementObject



146
147
148
149
150
# File 'lib/leptris/xml/node.rb', line 146

def previous_element
  sibling = previous_sibling
  sibling = sibling.previous_sibling until sibling.nil? || sibling.element?
  sibling
end

#previous_siblingObject Also known as: previous



114
115
116
117
118
# File 'lib/leptris/xml/node.rb', line 114

def previous_sibling
  ptr = Leptris::XML::FFI.leptris_node_previous_sibling(@c_ptr)
  return nil if ptr.null?
  Leptris::XML::Node.wrap(ptr, @document, parent: @parent)
end

#processing_instruction?Boolean Also known as: pi?

Returns:

  • (Boolean)


69
70
71
# File 'lib/leptris/xml/node.rb', line 69

def processing_instruction?
  type == Leptris::XML::FFI::NODE_PI
end

#textObject

Dispatching defs, not alias_method: an alias snapshots this base #content (the raise), so subclass overrides would never be seen through the alias. A plain method resolves #content per-call.



52
53
54
# File 'lib/leptris/xml/node.rb', line 52

def text
  content
end

#text?Boolean

Returns:

  • (Boolean)


66
# File 'lib/leptris/xml/node.rb', line 66

def text?;     type == Leptris::XML::FFI::NODE_TEXT;     end

#traverseObject

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 (libleptris #273); the per-node FFI cost is the floor.



171
172
173
174
175
176
177
178
179
# File 'lib/leptris/xml/node.rb', line 171

def traverse
  return enum_for(:traverse) unless block_given?
  callback = ::FFI::Function.new(:int, [:pointer, :pointer], blocking: true) do |node_ptr, _|
    yield Leptris::XML::Node.wrap(node_ptr, @document)
    0
  end
  Leptris::XML::FFI.leptris_node_traverse(
    @c_ptr, Leptris::XML::FFI::TRAVERSE_POST_ORDER, callback, nil)
end

#typeObject Also known as: node_type



60
61
62
# File 'lib/leptris/xml/node.rb', line 60

def type
  Leptris::XML::FFI.leptris_node_get_type(@c_ptr)
end


152
153
154
155
156
157
# File 'lib/leptris/xml/node.rb', line 152

def unlink
  Leptris::XML::FFI.check_status(
    Leptris::XML::FFI.leptris_node_unlink(@c_ptr))
  @parent = nil
  self
end