Class: Nokogiri::XML::NodeSet
- Inherits:
-
Object
- Object
- Nokogiri::XML::NodeSet
- Includes:
- Enumerable
- Defined in:
- lib/nokogiri/xml/node_set.rb
Instance Attribute Summary collapse
-
#document ⇒ Object
Returns the value of attribute document.
Instance Method Summary collapse
- #<<(node) ⇒ Object
- #add_class(name) ⇒ Object
- #after(datum) ⇒ Object
- #at(path) ⇒ Object
- #attr(key, value = nil, &blk) ⇒ Object (also: #set)
- #before(datum) ⇒ Object
-
#each(&block) ⇒ Object
Iterate over each node, yielding to
block
. - #first ⇒ Object
-
#initialize {|_self| ... } ⇒ NodeSet
constructor
A new instance of NodeSet.
- #inner_text ⇒ Object (also: #text)
- #last ⇒ Object
- #remove_attr(name) ⇒ Object
- #remove_class(name = nil) ⇒ Object
- #search(path) ⇒ Object (also: #/)
- #size ⇒ Object
- #to_html ⇒ Object
- #to_s ⇒ Object
- #wrap(html, &blk) ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ NodeSet
Returns a new instance of NodeSet.
8 9 10 |
# File 'lib/nokogiri/xml/node_set.rb', line 8 def initialize yield self if block_given? end |
Instance Attribute Details
#document ⇒ Object
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
28 29 30 |
# File 'lib/nokogiri/xml/node_set.rb', line 28 def << node push(node) end |
#add_class(name) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/nokogiri/xml/node_set.rb', line 49 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
24 25 26 |
# File 'lib/nokogiri/xml/node_set.rb', line 24 def after datum last.after datum end |
#at(path) ⇒ Object
45 46 47 |
# File 'lib/nokogiri/xml/node_set.rb', line 45 def at path search(path).first end |
#attr(key, value = nil, &blk) ⇒ Object Also known as: set
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/nokogiri/xml/node_set.rb', line 71 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
20 21 22 |
# File 'lib/nokogiri/xml/node_set.rb', line 20 def before datum first.before datum end |
#each(&block) ⇒ Object
Iterate over each node, yielding to block
97 98 99 100 101 102 103 |
# File 'lib/nokogiri/xml/node_set.rb', line 97 def each(&block) x = 0 while x < length yield self[x] x += 1 end end |
#first ⇒ Object
12 13 14 |
# File 'lib/nokogiri/xml/node_set.rb', line 12 def first self[0] end |
#inner_text ⇒ Object Also known as: text
105 106 107 |
# File 'lib/nokogiri/xml/node_set.rb', line 105 def inner_text collect{|j| j.inner_text}.join('') end |
#last ⇒ Object
16 17 18 |
# File 'lib/nokogiri/xml/node_set.rb', line 16 def last self[length - 1] end |
#remove_attr(name) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/nokogiri/xml/node_set.rb', line 87 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
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/nokogiri/xml/node_set.rb', line 58 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(path) ⇒ Object Also known as: /
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/nokogiri/xml/node_set.rb', line 32 def search path sub_set = NodeSet.new document.decorate(sub_set) each do |node| node.search(path).each do |sub_node| sub_set << sub_node end end sub_set.document = document sub_set end |
#size ⇒ Object
131 132 133 |
# File 'lib/nokogiri/xml/node_set.rb', line 131 def size length end |
#to_html ⇒ Object
127 128 129 |
# File 'lib/nokogiri/xml/node_set.rb', line 127 def to_html map { |x| x.to_html }.join('') end |
#to_s ⇒ Object
123 124 125 |
# File 'lib/nokogiri/xml/node_set.rb', line 123 def to_s map { |x| x.to_s }.join end |
#wrap(html, &blk) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/nokogiri/xml/node_set.rb', line 110 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 |