Class: Leptris::XML::NodeSet

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Searchable

#at, #at_css, #at_xpath, #css, #search, wrap_xpath_result

Constructor Details

#initialize(document, source = nil) ⇒ NodeSet

Two construction modes:

  • eager: pass an Array of Nodes (e.g. Element#children builds one)
  • lazy: pass an FFI::Pointer to a LeptrisXPathResult that this NodeSet will keep alive and free on GC. Each [i] / each call goes through leptris_xpath_result_get instead of pre-materializing.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/leptris/xml/node_set.rb', line 16

def initialize(document, source = nil)
  @document = document
  case source
  when ::FFI::Pointer
    if source.null?
      @result_ptr = nil
      @array = []
    else
      # Wrap in AutoPointer for automatic GC-time cleanup. NodeSet has no
      # explicit #free method (Nokogiri doesn't either), so the AutoPointer
      # double-free risk that Document#free hit doesn't apply here.
      @result_ptr = ::FFI::AutoPointer.new(source, Leptris::XML::FFI.method(:leptris_xpath_result_free))
      @array = nil
    end
  when nil
    @result_ptr = nil
    @array = []
  else
    @result_ptr = nil
    @array = source.to_a
  end
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



9
10
11
# File 'lib/leptris/xml/node_set.rb', line 9

def document
  @document
end

Class Method Details

.from_result(document, result_ptr) ⇒ Object



39
40
41
# File 'lib/leptris/xml/node_set.rb', line 39

def self.from_result(document, result_ptr)
  new(document, result_ptr)
end

Instance Method Details

#[](idx) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/leptris/xml/node_set.rb', line 52

def [](idx)
  if @result_ptr
    return nil if idx < 0 || idx >= length
    ptr = Leptris::XML::FFI.leptris_xpath_result_get_node(@result_ptr, idx)
    return nil if ptr.null?
    Leptris::XML::Node.wrap(ptr, @document)
  else
    @array[idx]
  end
end

#eachObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/leptris/xml/node_set.rb', line 63

def each
  return enum_for(:each) unless block_given?
  if @result_ptr
    # Batch-fetch all node pointers in one FFI call
    # (leptris_xpath_result_get_nodes_ex copies every node kind, not
    # just elements) and wrap each. Wrappers are cached per-Document
    # via Node.wrap, so re-iteration of the same NodeSet hits it.
    n = length
    if n > 0
      buf = ::FFI::MemoryPointer.new(:pointer, n)
      begin
        copied = Leptris::XML::FFI.leptris_xpath_result_get_nodes_ex(
          @result_ptr, buf, nil, n)
        copied.times do |i|
          ptr = buf.get_pointer(i * ::FFI.type_size(:pointer))
          next if ptr.null?
          yield Leptris::XML::Node.wrap(ptr, @document)
        end
        # The batch accessor under-copies mixed-kind results (upstream
        # leptris#477); fall back to per-index fetch for the remainder.
        (copied...n).each do |i|
          ptr = Leptris::XML::FFI.leptris_xpath_result_get_node(@result_ptr, i)
          next if ptr.null?
          yield Leptris::XML::Node.wrap(ptr, @document)
        end
      ensure
        buf.free
      end
    end
  else
    @array.each { |n| yield n }
  end
  self
end

#empty?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/leptris/xml/node_set.rb', line 48

def empty?
  length == 0
end

#first(n = nil) ⇒ Object



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

def first(n = nil)
  return self[0] if n.nil?
  n.times.map { |i| self[i] }.take_while { |x| !x.nil? }
end

#inner_textObject Also known as: text



121
122
123
# File 'lib/leptris/xml/node_set.rb', line 121

def inner_text
  map(&:content).join
end

#inspectObject



142
143
144
# File 'lib/leptris/xml/node_set.rb', line 142

def inspect
  to_a.inspect
end

#lastObject



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

def last
  if @result_ptr
    self[length - 1]
  else
    @array.last
  end
end

#lengthObject Also known as: size



43
44
45
# File 'lib/leptris/xml/node_set.rb', line 43

def length
  @result_ptr ? Leptris::XML::FFI.leptris_xpath_result_count(@result_ptr) : @array.length
end

#to_aObject Also known as: to_ary



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

def to_a
  return @array if @array
  return [] if @result_ptr.nil?
  out = []
  each { |n| out << n }
  @array = out  # memoize so subsequent to_a / each avoids re-fetch
  out
end

#xpath(*paths) ⇒ Object

Raises:

  • (ArgumentError)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/leptris/xml/node_set.rb', line 126

def xpath(*paths)
  handler, _ns, _vars = parse_search_args(paths)
  raise ArgumentError, "custom XPath handlers not supported" if handler
  expr = paths.join(" | ")
  accumulated = Leptris::XML::NodeSet.new(@document)
  each do |node|
    next unless node.is_a?(Leptris::XML::Element)
    result_ptr = Leptris::XML::FFI.leptris_xpath_eval(
      @document.c_ptr, node.c_ptr, expr)
    next if result_ptr.null?
    sub = Leptris::XML::NodeSet.send(:from_result, @document, result_ptr)
    accumulated = merge_node_sets(accumulated, sub)
  end
  accumulated
end