class Nokogiri::XML::Node
def [](property)
def [](property) return nil unless key?(property) get(property) end
def after data
Create nodes from +data+ and insert them after this node
###
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
def at path, ns = {}
def at path, ns = {} search("#{path}", ns).first end
def before data
Create nodes from +data+ and insert them before this node
###
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
def cdata?
def cdata? type == CDATA_SECTION_NODE end
def children
##
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
def collect_namespaces
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
def comment?
def comment? type == COMMENT_NODE end
def content= string, encode = true
Set the content to +string+.
###
def content= string, encode = true self.native_content = encode_special_chars(string) end
def css *rules
def css *rules xpath(*(rules.map { |rule| CSS::Parser.parse(rule).map { |ast| "." + ast.to_xpath } }.flatten.uniq)) end
def css_path
def css_path path.split(/\//).map { |part| part.length == 0 ? nil : part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)') }.compact.join(' > ') end
def decorate!
##
def decorate! document.decorate(self) if document end
def has_attribute?(property)
def has_attribute?(property) key? property end
def html?
def html? type == HTML_DOCUMENT_NODE end
def next
def next next_sibling end
def remove
def remove unlink end
def search *paths
optional hash of namespaces may be appended.
Search this node for +paths+. +paths+ can be XPath or CSS, and an
##
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
def set_attribute(name, value)
def set_attribute(name, value) self[name] = value end
def text
def text content end
def to_html
def to_html to_xml end
def traverse(&block)
###
def traverse(&block) children.each{|j| j.traverse(&block) } block.call(self) end
def xml?
def xml? type == DOCUMENT_NODE end
def xpath *paths
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