Class: Nokogiri::XML::NodeSet

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ NodeSet

Returns a new instance of NodeSet.

Yields:

  • (_self)

Yield Parameters:



8
9
10
# File 'lib/nokogiri/xml/node_set.rb', line 8

def initialize
  yield self if block_given?
end

Instance Attribute Details

#documentObject

Returns the value of attribute document.



6
7
8
# File 'lib/nokogiri/xml/node_set.rb', line 6

def document
  @document
end

Instance Method Details

#<<(node) ⇒ Object

Append node to the NodeSet.



44
45
46
# File 'lib/nokogiri/xml/node_set.rb', line 44

def << node
  push(node)
end

#add_class(name) ⇒ Object

Append the class attribute name to all Node objects in the NodeSet.



85
86
87
88
89
90
91
92
# File 'lib/nokogiri/xml/node_set.rb', line 85

def add_class name
  each do |el|
    next unless el.respond_to? :get_attribute
    classes = el.get_attribute('class').to_s.split(" ")
    el.set_attribute('class', classes.push(name).uniq.join(" "))
  end
  self
end

#after(datum) ⇒ Object

Insert datum after the last Node in this NodeSet



38
39
40
# File 'lib/nokogiri/xml/node_set.rb', line 38

def after datum
  last.after datum
end

#at(path, ns = {}) ⇒ Object

If path is a string, search this document for path returning the first Node. Otherwise, index in to the array with path.



78
79
80
81
# File 'lib/nokogiri/xml/node_set.rb', line 78

def at path, ns = {}
  return self[path] if path.is_a?(Numeric)
  search(path, ns).first
end

#attr(key, value = nil, &blk) ⇒ Object Also known as: set

Set the attribute key to value or the return value of blk on all Node objects in the NodeSet.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/nokogiri/xml/node_set.rb', line 112

def attr key, value = nil, &blk
  if value or blk
    each do |el|
      el.set_attribute(key, value || blk[el])
    end
    return self      
  end    
  if key.is_a? Hash
    key.each { |k,v| self.attr(k,v) }
    return self
  else
    return self[0].get_attribute(key)
  end
end

#before(datum) ⇒ Object

Insert datum before the first Node in this NodeSet



32
33
34
# File 'lib/nokogiri/xml/node_set.rb', line 32

def before datum
  first.before datum
end

#each(&block) ⇒ Object

Iterate over each node, yielding to block



140
141
142
143
144
145
146
# File 'lib/nokogiri/xml/node_set.rb', line 140

def each(&block)
  x = 0
  while x < length
    yield self[x]
    x += 1
  end
end

#empty?Boolean

Is this NodeSet empty?

Returns:

  • (Boolean)


26
27
28
# File 'lib/nokogiri/xml/node_set.rb', line 26

def empty?
  length == 0
end

#firstObject

Get the first element of the NodeSet.



14
15
16
# File 'lib/nokogiri/xml/node_set.rb', line 14

def first
  self[0]
end

#inner_textObject Also known as: text

Get the inner text of all contained Node objects



150
151
152
# File 'lib/nokogiri/xml/node_set.rb', line 150

def inner_text
  collect{|j| j.inner_text}.join('')
end

#lastObject

Get the last element of the NodeSet.



20
21
22
# File 'lib/nokogiri/xml/node_set.rb', line 20

def last
  self[length - 1]
end

#remove_attr(name) ⇒ Object

Remove the attributed named name from all Node objects in the NodeSet



130
131
132
133
134
135
136
# File 'lib/nokogiri/xml/node_set.rb', line 130

def remove_attr name
  each do |el|
    next unless el.respond_to? :remove_attribute
    el.remove_attribute(name)
  end
  self      
end

#remove_class(name = nil) ⇒ Object

Remove the class attribute name from all Node objects in the NodeSet.



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/nokogiri/xml/node_set.rb', line 96

def remove_class name = nil
  each do |el|
    next unless el.respond_to? :get_attribute
    if name
      classes = el.get_attribute('class').to_s.split(" ")
      el.set_attribute('class', (classes - [name]).uniq.join(" "))
    else
      el.remove_attribute("class")
    end
  end
  self
end

#search(*paths) ⇒ Object Also known as: /, xpath, css

Search this document for paths



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/nokogiri/xml/node_set.rb', line 60

def search *paths
  sub_set = NodeSet.new
  document.decorate(sub_set)
  each do |node|
    node.search(*paths).each do |sub_node|
      sub_set << sub_node
    end
  end
  sub_set.document = document
  sub_set
end

#sizeObject



178
179
180
# File 'lib/nokogiri/xml/node_set.rb', line 178

def size
  length
end

#to_htmlObject



174
175
176
# File 'lib/nokogiri/xml/node_set.rb', line 174

def to_html
  map { |x| x.to_html }.join('')
end

#to_sObject



170
171
172
# File 'lib/nokogiri/xml/node_set.rb', line 170

def to_s
  map { |x| x.to_s }.join
end

Unlink this NodeSet and all Node objects it contains from their current context.



51
52
53
54
55
# File 'lib/nokogiri/xml/node_set.rb', line 51

def unlink
  each { |node| node.unlink }
  self.document = nil
  self
end

#wrap(html, &blk) ⇒ Object

Wrap this NodeSet with html or the results of the builder in blk



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/nokogiri/xml/node_set.rb', line 157

def wrap(html, &blk)
  each do |j|
    new_parent = Nokogiri.make(html, &blk)
    j.replace(new_parent)
    nest = new_parent
    if nest.child
      nest = nest.child until nest.child.nil?
    end
    j.parent = nest
  end
  self
end