Class: Nokolexbor::NodeSet

Inherits:
Node
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nokolexbor/node_set.rb

Constant Summary

Constants inherited from Node

Nokolexbor::Node::ATTRIBUTE_NODE, Nokolexbor::Node::CDATA_SECTION_NODE, Nokolexbor::Node::COMMENT_NODE, Nokolexbor::Node::DOCUMENT_FRAG_NODE, Nokolexbor::Node::DOCUMENT_NODE, Nokolexbor::Node::DOCUMENT_TYPE_NODE, Nokolexbor::Node::ELEMENT_NODE, Nokolexbor::Node::ENTITY_NODE, Nokolexbor::Node::ENTITY_REF_NODE, Nokolexbor::Node::LOOKS_LIKE_XPATH, Nokolexbor::Node::NOTATION_NODE, Nokolexbor::Node::PI_NODE, Nokolexbor::Node::TEXT_NODE

Instance Attribute Summary

Attributes inherited from Node

#document

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#<<, #add_next_sibling, #add_previous_sibling, #ancestors, #at, #at_css, #at_xpath, #attributes, #cdata?, #children=, #classes, #comment?, #css, #css_path, #document?, #element?, #fragment, #fragment?, #kwattr_add, #kwattr_append, #kwattr_remove, #kwattr_values, #matches?, #nokogiri_at_css, #parent=, #prepend_child, #processing_instruction?, #replace, #search, #swap, #text?, #traverse, #value?, #write_to

Class Method Details

.new(document, list = []) {|Document| ... } ⇒ Document

Create a NodeSet with document defaulting to list.

Yields:

Returns:



12
13
14
15
16
17
18
# File 'lib/nokolexbor/node_set.rb', line 12

def self.new(document, list = [])
  obj = allocate
  obj.instance_variable_set(:@document, document)
  list.each { |x| obj << x }
  yield obj if block_given?
  obj
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if two NodeSets contain the same number of elements and each element is equal to the corresponding element in the other NodeSet.

Returns:

  • (Boolean)

    true if two NodeSets contain the same number of elements and each element is equal to the corresponding element in the other NodeSet.



142
143
144
145
146
147
148
149
150
# File 'lib/nokolexbor/node_set.rb', line 142

def ==(other)
  return false unless other.is_a?(NodeSet)
  return false unless length == other.length

  each_with_index do |node, i|
    return false unless node == other[i]
  end
  true
end

#add_class(name) ⇒ Object

Add the class attribute name to all containing nodes.



185
186
187
188
189
190
# File 'lib/nokolexbor/node_set.rb', line 185

def add_class(name)
  each do |el|
    el.add_class(name)
  end
  self
end

#after(node) ⇒ Object

Insert node after the last Node in this NodeSet



63
64
65
# File 'lib/nokolexbor/node_set.rb', line 63

def after(node)
  last.after(node)
end

#append_class(name) ⇒ Object

Append the class attribute name to all containing nodes.



195
196
197
198
199
200
# File 'lib/nokolexbor/node_set.rb', line 195

def append_class(name)
  each do |el|
    el.append_class(name)
  end
  self
end

#attr(key, value = nil, &block) ⇒ NodeSet Also known as: set, attribute

Set attributes on each Node in the NodeSet, or get an attribute from the first Node in the NodeSet.

Examples:

Get an attribute from the first Node in a NodeSet.

node_set.attr("href")

Set attributes on each node.

node_set.attr("href" => "http://example.com", "class" => "a")
node_set.attr("href", "http://example.com")
node_set.attr("href") { |node| "http://example.com" }

Returns:

  • (NodeSet)

    self, to support chaining of calls.



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/nokolexbor/node_set.rb', line 233

def attr(key, value = nil, &block)
  unless key.is_a?(Hash) || (key && (value || block))
    return first&.attribute(key)
  end

  hash = key.is_a?(Hash) ? key : { key => value }

  hash.each do |k, v|
    each do |node|
      node[k] = v || yield(node)
    end
  end

  self
end

#before(node) ⇒ Object

Insert node before the first Node in this NodeSet



58
59
60
# File 'lib/nokolexbor/node_set.rb', line 58

def before(node)
  first.before(node)
end

#childrenNodeSet

Returns A new NodeSet containing all the children of all the nodes in the NodeSet.

Returns:

  • (NodeSet)

    A new NodeSet containing all the children of all the nodes in the NodeSet.



154
155
156
157
158
159
160
# File 'lib/nokolexbor/node_set.rb', line 154

def children
  node_set = NodeSet.new(@document)
  each do |node|
    node.children.each { |n| node_set.push(n) }
  end
  node_set
end

#contentString Also known as: text, inner_text, to_str

Get the content of all contained Nodes.

Returns:

  • (String)


80
81
82
# File 'lib/nokolexbor/node_set.rb', line 80

def content
  self.map(&:content).join
end

#destroyObject

Destroy all nodes in the NodeSet.

See Also:

  • Node#destroy


119
120
121
# File 'lib/nokolexbor/node_set.rb', line 119

def destroy
  self.each(&:destroy)
end

#each {|Node| ... } ⇒ Object

Iterate over each node.

Yields:



23
24
25
26
27
28
29
30
# File 'lib/nokolexbor/node_set.rb', line 23

def each
  return to_enum unless block_given?

  0.upto(length - 1) do |x|
    yield self[x]
  end
  self
end

#empty?Boolean

Returns true if this NodeSet is empty.

Returns:

  • (Boolean)

    true if this NodeSet is empty.



53
54
55
# File 'lib/nokolexbor/node_set.rb', line 53

def empty?
  length == 0
end

#first(n = nil) ⇒ Node+

Get the first n elements of the NodeSet.

Parameters:

  • n (Numeric, nil) (defaults to: nil)

Returns:



37
38
39
40
41
42
43
# File 'lib/nokolexbor/node_set.rb', line 37

def first(n = nil)
  return self[0] unless n

  list = []
  [n, length].min.times { |i| list << self[i] }
  list
end

#index(node = nil) ⇒ Integer

Returns The index of the first node in this NodeSet that is equal to node or meets the given block. Returns nil if no match is found.

Returns:

  • (Integer)

    The index of the first node in this NodeSet that is equal to node or meets the given block. Returns nil if no match is found.



68
69
70
71
72
73
74
75
# File 'lib/nokolexbor/node_set.rb', line 68

def index(node = nil)
  if node
    each_with_index { |member, j| return j if member == node }
  elsif block_given?
    each_with_index { |member, j| return j if yield(member) }
  end
  nil
end

#inner_html(*args) ⇒ String

Get the inner html of all contained Nodes.

Returns:

  • (String)


91
92
93
# File 'lib/nokolexbor/node_set.rb', line 91

def inner_html(*args)
  self.map { |n| n.inner_html(*args) }.join
end

#inspectObject



278
279
280
# File 'lib/nokolexbor/node_set.rb', line 278

def inspect
  "[#{map(&:inspect).join(', ')}]"
end

#lastNode?

Get the last element of the NodeSet.

Returns:



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

def last
  self[-1]
end

#nokogiri_css(*args) ⇒ NodeSet

Search this object for CSS rules. rules must be one or more CSS selectors. It supports a mixed syntax of CSS selectors and XPath.

This method uses libxml2 as the selector engine. It works the same way as Nokogiri::Node#css.

Returns:

  • (NodeSet)

    The matched set of Nodes.

See Also:



265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/nokolexbor/node_set.rb', line 265

def nokogiri_css(*args)
  rules, handler, ns, _ = extract_params(args)
  paths = css_rules_to_xpath(rules, ns)

  NodeSet.new(@document) do |set|
    each do |node|
      node.send(:xpath_internal, node, paths, handler, ns, nil).each do |inner_node|
        set << inner_node
      end
    end
  end
end

#outer_html(*args) ⇒ String Also known as: to_s, to_html, serialize

Convert this NodeSet to HTML.

Returns:

  • (String)


98
99
100
# File 'lib/nokolexbor/node_set.rb', line 98

def outer_html(*args)
  self.map { |n| n.outer_html(*args) }.join
end

#popNode?

Returns The last element of this NodeSet and removes it. Returns nil if the set is empty.

Returns:

  • (Node, nil)

    The last element of this NodeSet and removes it. Returns nil if the set is empty.



125
126
127
128
129
# File 'lib/nokolexbor/node_set.rb', line 125

def pop
  return nil if length == 0

  delete(last)
end

#removeObject Also known as: unlink

Remove all nodes in this NodeSet.

See Also:

  • Node#remove


109
110
111
# File 'lib/nokolexbor/node_set.rb', line 109

def remove
  self.each(&:remove)
end

#remove_attr(name) ⇒ Object Also known as: remove_attribute

Remove the attributed named name from all containing nodes.

See Also:

  • Node#remove_attr


215
216
217
218
# File 'lib/nokolexbor/node_set.rb', line 215

def remove_attr(name)
  each { |el| el.delete(name) }
  self
end

#remove_class(name = nil) ⇒ Object

Remove the class attribute name from all containing nodes.



205
206
207
208
209
210
# File 'lib/nokolexbor/node_set.rb', line 205

def remove_class(name = nil)
  each do |el|
    el.remove_class(name)
  end
  self
end

#reverseNodeSet

Returns A new NodeSet containing all the nodes in the NodeSet in reverse order.

Returns:

  • (NodeSet)

    A new NodeSet containing all the nodes in the NodeSet in reverse order.



164
165
166
167
168
169
170
# File 'lib/nokolexbor/node_set.rb', line 164

def reverse
  node_set = NodeSet.new(@document)
  (length - 1).downto(0) do |x|
    node_set.push(self[x])
  end
  node_set
end

#shiftNode?

Returns The first element of this NodeSet and removes it. Returns nil if the set is empty.

Returns:

  • (Node, nil)

    The first element of this NodeSet and removes it. Returns nil if the set is empty.



133
134
135
136
137
# File 'lib/nokolexbor/node_set.rb', line 133

def shift
  return nil if length == 0

  delete(first)
end

#wrap(node_or_tags) ⇒ NodeSet

Wrap all nodes of this NodeSet with node_or_tags.

Returns:

  • (NodeSet)

    self, to support chaining.

See Also:



177
178
179
180
# File 'lib/nokolexbor/node_set.rb', line 177

def wrap(node_or_tags)
  map { |node| node.wrap(node_or_tags) }
  self
end

#xpath(*args) ⇒ NodeSet

Search this node for XPath paths. paths must be one or more XPath queries.

It works the same way as Nokogiri::Node#xpath.

Examples:

node.xpath('.//title')

Returns:

  • (NodeSet)

    The matched set of Nodes.



252
253
254
255
256
257
258
259
260
261
262
# File 'lib/nokolexbor/node_set.rb', line 252

def xpath(*args)
  paths, handler, ns, binds = extract_params(args)

  NodeSet.new(@document) do |set|
    each do |node|
      node.send(:xpath_internal, node, paths, handler, ns, binds).each do |inner_node|
        set << inner_node
      end
    end
  end
end