Class: Nokogiri::XML::Node
- Inherits:
-
Object
- Object
- Nokogiri::XML::Node
- Defined in:
- lib/nokogiri/xml/node.rb
Constant Summary collapse
- CDATA_SECTION_NODE =
4- COMMENT_NODE =
8- DOCUMENT_NODE =
9- HTML_DOCUMENT_NODE =
13- DTD_NODE =
14- ELEMENT_DECL =
15- ATTRIBUTE_DECL =
16- ENTITY_DECL =
17- NAMESPACE_DECL =
18- XINCLUDE_START =
19- XINCLUDE_END =
20- DOCB_DOCUMENT_NODE =
21- @@owned =
{}
Instance Method Summary collapse
- #[](property) ⇒ Object (also: #get_attribute)
-
#after(data) ⇒ Object
Create nodes from
dataand insert them after this node (as a sibling). - #at(path, ns = {}) ⇒ Object
-
#before(data) ⇒ Object
Create nodes from
dataand insert them before this node (as a sibling). - #cdata? ⇒ Boolean
-
#children ⇒ Object
Get the list of children for this node as a NodeSet.
-
#collect_namespaces ⇒ Object
recursively get all namespaces from this node and its subtree.
- #comment? ⇒ Boolean
-
#content=(string, encode = true) ⇒ Object
Set the content to
string. - #css(*rules) ⇒ Object
- #css_path ⇒ Object
-
#decorate! ⇒ Object
Decorate this node with the decorators set up in this node’s Document.
- #has_attribute?(property) ⇒ Boolean
- #html? ⇒ Boolean
- #next ⇒ Object
- #remove ⇒ Object
-
#search(*paths) ⇒ Object
(also: #/)
Search this node for
paths. - #set_attribute(name, value) ⇒ Object
- #text ⇒ Object (also: #inner_text)
- #to_html ⇒ Object (also: #to_s, #inner_html)
-
#traverse(&block) ⇒ Object
Yields self and all children to
blockrecursively. - #xml? ⇒ Boolean
- #xpath(*paths) ⇒ Object
Instance Method Details
#[](property) ⇒ Object Also known as: get_attribute
91 92 93 94 |
# File 'lib/nokogiri/xml/node.rb', line 91 def [](property) return nil unless key?(property) get(property) end |
#after(data) ⇒ Object
Create nodes from data and insert them after this node (as a sibling).
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/nokogiri/xml/node.rb', line 118 def after data classes = document.class.name.split('::') classes[-1] = 'SAX::Parser' handler = AfterHandler.new(self, data) parser = eval(classes.join('::')).new(handler) parser.parse(data) handler.after_nodes.reverse.each do |sibling| self.add_next_sibling sibling end end |
#at(path, ns = {}) ⇒ Object
87 88 89 |
# File 'lib/nokogiri/xml/node.rb', line 87 def at path, ns = {} search("#{path}", ns).first end |
#before(data) ⇒ Object
Create nodes from data and insert them before this node (as a sibling).
107 108 109 110 111 112 113 |
# File 'lib/nokogiri/xml/node.rb', line 107 def before data classes = document.class.name.split('::') classes[-1] = 'SAX::Parser' parser = eval(classes.join('::')).new(BeforeHandler.new(self, data)) parser.parse(data) end |
#cdata? ⇒ Boolean
155 156 157 |
# File 'lib/nokogiri/xml/node.rb', line 155 def cdata? type == CDATA_SECTION_NODE end |
#children ⇒ Object
Get the list of children for this node as a NodeSet
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/nokogiri/xml/node.rb', line 27 def children list = NodeSet.new list.document = document document.decorate(list) first = self.child return list unless first # Empty list list << first unless first.blank? while first = first.next list << first unless first.blank? end list end |
#collect_namespaces ⇒ Object
recursively get all namespaces from this node and its subtree
180 181 182 183 184 185 |
# File 'lib/nokogiri/xml/node.rb', line 180 def collect_namespaces # TODO: print warning message if a prefix refers to more than one URI in the document? ns = {} traverse {|j| ns.merge!(j.namespaces)} ns end |
#comment? ⇒ Boolean
151 152 153 |
# File 'lib/nokogiri/xml/node.rb', line 151 def comment? type == COMMENT_NODE end |
#content=(string, encode = true) ⇒ Object
Set the content to string. If encode, encode any special characters first.
147 148 149 |
# File 'lib/nokogiri/xml/node.rb', line 147 def content= string, encode = true self.native_content = encode_special_chars(string) end |
#css(*rules) ⇒ Object
81 82 83 84 85 |
# File 'lib/nokogiri/xml/node.rb', line 81 def css *rules xpath(*(rules.map { |rule| CSS::Parser.parse(rule).map { |ast| "." + ast.to_xpath } }.flatten.uniq)) end |
#css_path ⇒ Object
173 174 175 176 177 |
# File 'lib/nokogiri/xml/node.rb', line 173 def css_path path.split(/\//).map { |part| part.length == 0 ? nil : part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)') }.compact.join(' > ') end |
#decorate! ⇒ Object
Decorate this node with the decorators set up in this node’s Document
21 22 23 |
# File 'lib/nokogiri/xml/node.rb', line 21 def decorate! document.decorate(self) if document end |
#has_attribute?(property) ⇒ Boolean
130 131 132 |
# File 'lib/nokogiri/xml/node.rb', line 130 def has_attribute?(property) key? property end |
#html? ⇒ Boolean
163 164 165 |
# File 'lib/nokogiri/xml/node.rb', line 163 def html? type == HTML_DOCUMENT_NODE end |
#next ⇒ Object
96 97 98 |
# File 'lib/nokogiri/xml/node.rb', line 96 def next next_sibling end |
#remove ⇒ Object
100 101 102 |
# File 'lib/nokogiri/xml/node.rb', line 100 def remove unlink end |
#search(*paths) ⇒ Object Also known as: /
Search this node for paths. paths can be XPath or CSS, and an optional hash of namespaces may be appended. See Node#xpath and Node#css.
46 47 48 49 50 51 52 53 |
# File 'lib/nokogiri/xml/node.rb', line 46 def search *paths ns = paths.last.is_a?(Hash) ? paths.pop : {} xpath(*(paths.map { |path| path =~ /^(\.\/|\/)/ ? path : CSS::Parser.parse(path).map { |ast| ast.to_xpath } }.flatten.uniq) + [ns]) end |
#set_attribute(name, value) ⇒ Object
135 136 137 |
# File 'lib/nokogiri/xml/node.rb', line 135 def set_attribute(name, value) self[name] = value end |
#text ⇒ Object Also known as: inner_text
139 140 141 |
# File 'lib/nokogiri/xml/node.rb', line 139 def text content end |
#to_html ⇒ Object Also known as: to_s, inner_html
167 168 169 |
# File 'lib/nokogiri/xml/node.rb', line 167 def to_html to_xml end |
#traverse(&block) ⇒ Object
Yields self and all children to block recursively.
189 190 191 192 |
# File 'lib/nokogiri/xml/node.rb', line 189 def traverse(&block) children.each{|j| j.traverse(&block) } block.call(self) end |
#xml? ⇒ Boolean
159 160 161 |
# File 'lib/nokogiri/xml/node.rb', line 159 def xml? type == DOCUMENT_NODE end |
#xpath(*paths) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/nokogiri/xml/node.rb', line 56 def xpath *paths ns = paths.last.is_a?(Hash) ? paths.pop : {} return NodeSet.new unless document.root sets = paths.map { |path| ctx = XPathContext.new(self) ctx.register_namespaces(ns) set = ctx.evaluate(path).node_set set.document = document document.decorate(set) set } return sets.first if sets.length == 1 NodeSet.new do |combined| document.decorate(combined) sets.each do |set| set.each do |node| combined << node end end end end |