Class: Nokogiri::XML::Node

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

Direct Known Subclasses

Document, Text

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#[](property) ⇒ Object Also known as: get_attribute



74
75
76
77
# File 'lib/nokogiri/xml/node.rb', line 74

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).



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

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) ⇒ Object



70
71
72
# File 'lib/nokogiri/xml/node.rb', line 70

def at path
  search("#{path}").first
end

#before(data) ⇒ Object

Create nodes from data and insert them before this node (as a sibling).



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

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

#childrenObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/nokogiri/xml/node.rb', line 20

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

#comment?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/nokogiri/xml/node.rb', line 133

def comment?
  type == COMMENT_NODE
end

#content=(string, encode = true) ⇒ Object

Set the content to string. If encode, encode any special characters first.



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

def content= string, encode = true
  self.native_content = encode_special_chars(string)
end

#css_pathObject



151
152
153
154
155
# File 'lib/nokogiri/xml/node.rb', line 151

def css_path
  path.split(/\//).map { |part|
    part.length == 0 ? nil : part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)')
  }.compact.join(' > ')
end

#decorate!Object



16
17
18
# File 'lib/nokogiri/xml/node.rb', line 16

def decorate!
  document.decorate(self) if document
end

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



35
36
37
38
39
40
41
# File 'lib/nokogiri/xml/node.rb', line 35

def find(*paths)
  find_by_xpath(*(paths.map { |path|
    path =~ /^(\.\/|\/)/ ? path : CSS::Parser.parse(path).map { |ast|
      ast.to_xpath
    }
  }.flatten.uniq))
end

#find_by_css(*rules) ⇒ Object



64
65
66
67
68
# File 'lib/nokogiri/xml/node.rb', line 64

def find_by_css *rules
  find_by_xpath(*(rules.map { |rule|
    CSS::Parser.parse(rule).map { |ast| ast.to_xpath }
  }.flatten.uniq))
end

#find_by_xpath(*paths) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/nokogiri/xml/node.rb', line 45

def find_by_xpath *paths
  sets = paths.map { |path|
    set = XPathContext.new(self).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

#has_attribute?(property) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/nokogiri/xml/node.rb', line 109

def has_attribute?(property)
  key? property
end

#html?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/nokogiri/xml/node.rb', line 141

def html?
  type == HTML_DOCUMENT_NODE
end

#inner_textObject



122
123
124
# File 'lib/nokogiri/xml/node.rb', line 122

def inner_text
  content
end

#nextObject



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

def next
  next_sibling
end

#remove_attribute(name) ⇒ Object



118
119
120
# File 'lib/nokogiri/xml/node.rb', line 118

def remove_attribute name
  remove(name)
end

#set_attribute(name, value) ⇒ Object



114
115
116
# File 'lib/nokogiri/xml/node.rb', line 114

def set_attribute(name, value)
  self[name] = value
end

#to_htmlObject Also known as: to_s, inner_html



145
146
147
# File 'lib/nokogiri/xml/node.rb', line 145

def to_html
  to_xml
end

#xml?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/nokogiri/xml/node.rb', line 137

def xml?
  type == DOCUMENT_NODE
end

#xpathObject



157
158
159
# File 'lib/nokogiri/xml/node.rb', line 157

def xpath
  path
end