Class: Nokogiri::XML::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/nokogiri/xml/node.rb

Direct Known Subclasses

Document, Text

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

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

Returns:

  • (Boolean)


155
156
157
# File 'lib/nokogiri/xml/node.rb', line 155

def cdata?
  type == CDATA_SECTION_NODE
end

#childrenObject

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_namespacesObject

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

Returns:

  • (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_pathObject



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

Returns:

  • (Boolean)


130
131
132
# File 'lib/nokogiri/xml/node.rb', line 130

def has_attribute?(property)
  key? property
end

#html?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/nokogiri/xml/node.rb', line 163

def html?
  type == HTML_DOCUMENT_NODE
end

#nextObject



96
97
98
# File 'lib/nokogiri/xml/node.rb', line 96

def next
  next_sibling
end

#removeObject



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

#textObject Also known as: inner_text



139
140
141
# File 'lib/nokogiri/xml/node.rb', line 139

def text
  content
end

#to_htmlObject 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

Returns:

  • (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