class Nokogiri::XML::Node

def self.new_from_str string

def self.new_from_str string
  $stderr.puts("This method is deprecated and will be removed in 1.2.0 or by March 1, 2009. Instead, use Nokogiri::HTML.fragment()")
  Nokogiri::HTML.fragment(string).first
end

def == other

Test to see if this Node is equal to +other+
##
def == other
  return false unless other
  return false unless other.respond_to?(:pointer_id)
  pointer_id == other.pointer_id
end

def [](name)

Get the attribute value for the attribute +name+
##
def [](name)
  return nil unless key?(name)
  get(name)
end

def after data

(as a sibling).
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 ancestors

Get a list of ancestor Node for this Node
##
def ancestors
  parents = []
  this_parent = self.parent
  while this_parent != nil
    parents << this_parent
    this_parent = this_parent.parent
  end
  parents
end

def at path, ns = document.root.namespaces

Returns nil if nothing is found, otherwise a Node.
Search for the first occurrence of +path+.
##
def at path, ns = document.root.namespaces
  search(path, ns).first
end

def attributes

attribute name, the value is the string value of the attribute.
Returns a hash containing the node's attributes. The key is the
###
def attributes
  Hash[*(attribute_nodes.map { |node|
    [node.node_name, node]
  }.flatten)]
end

def before data

(as a sibling).
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

Get the list of children for this node as a NodeSet
##
def children
  list = NodeSet.new(document)
  document.decorate(list)
  first = self.child
  return list unless first # Empty list
  list << first
  while first = first.next
    list << first
  end
  list
end

def collect_namespaces

recursively get all namespaces from this node and its subtree
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

If +encode+, encode any special characters first.
Set the content to +string+.
###
def content= string, encode = true
  self.native_content = encode_special_chars(string)
end

def css *rules


})
end
node_set.find_all { |node| node['some_attribute'] =~ /#{regex}/ }
def regex node_set, regex
node.css('title:regex("\w+")', Class.new {

For example:
matching NodeSet. Any other arguments are ones that you pass in.
want defined. The first argument to the method will be the current
classes, create a class and implement the custom pseudo class you
Custom CSS pseudo classes may also be defined. To define custom pseudo

node.css('div + p.green', 'div#one')
node.css('body h1.bold')
node.css('title')

selectors. For example:
Search this node for CSS +rules+. +rules+ must be one or more CSS
##
def css *rules
  # Pop off our custom function handler if it exists
  handler = ![
    Hash, String, Symbol
  ].include?(rules.last.class) ? rules.pop : nil
  ns = rules.last.is_a?(Hash) ? rules.pop : document.root.namespaces
  rules = rules.map { |rule|
    CSS.xpath_for(rule, :prefix => ".//", :ns => ns)
  }.flatten.uniq + [ns, handler].compact
  xpath(*rules)
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!

Decorate this node with the decorators set up in this node's Document
##
def decorate!
  document.decorate(self) if document
end

def each &block

Iterate over each attribute name and value pair for this Node.
##
def each &block
  attribute_nodes.each { |node|
    block.call(node.node_name, node.value)
  }
end

def element?

def element?
  type == ELEMENT_NODE
end

def html?

def html?
  type == HTML_DOCUMENT_NODE
end

def inner_html

def inner_html
  children.map { |x| x.to_html }.join
end

def keys

Get the attribute names for this Node.
##
def keys
  attribute_nodes.map { |node| node.node_name }
end

def parent= parent_node

Set the parent Node for this Node
##
def parent= parent_node
  parent_node.add_child(self)
  parent_node
end

def read_only?

def read_only?
  # According to gdome2, these are read-only node types
  [NOTATION_NODE, ENTITY_NODE, ENTITY_DECL].include?(type)
end

def remove_attribute name

Remove the attribute named +name+
##
def remove_attribute name
  attributes[name].remove if key? name
end

def replace(new_node)

replace node with the new node in the document.
###
def replace(new_node)
  if new_node.is_a?(Document)
    raise ArgumentError, <<-EOERR
eplace requires a Node argument, and cannot accept a Document.
robably want to select a node from the Document with at() or search(), or create a new Node via Node.new().)
    EOERR
  end
  replace_with_node new_node
end

def search *paths

See Node#xpath and Node#css.
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.to_s
    path =~ /^(\.\/|\/)/ ? path : CSS.xpath_for(path, :prefix => ".//")
  }.flatten.uniq) + [ns])
end

def serialize encoding = nil, save_options = SaveOptions::FORMAT, &block


end
config.format.as_xml
node.serialize('UTF-8') do |config|

or

node.serialize('UTF-8', FORMAT | AS_XML)

These two statements are equivalent:

can also be set using a block. See SaveOptions.
Serialize Node using +encoding+ and +save_options+. Save options
##
def serialize encoding = nil, save_options = SaveOptions::FORMAT, &block
  io = StringIO.new
  write_to io, encoding, save_options, &block
  io.rewind
  io.read
end

def text?

def text?
  type == TEXT_NODE
end

def to_html encoding = nil

Serialize this Node to HTML using +encoding+
##
def to_html encoding = nil
  # FIXME: this is a hack around broken libxml versions
  return dump_html if %w[2 6] === LIBXML_VERSION.split('.')[0..1]
  serialize(encoding, SaveOptions::FORMAT |
                      SaveOptions::NO_DECLARATION |
                      SaveOptions::NO_EMPTY_TAGS |
                      SaveOptions::AS_HTML)
end

def to_s

def to_s
  document.xml? ? to_xml : to_html
end

def to_xhtml encoding = nil

Serialize this Node to XML using +encoding+
##
def to_xhtml encoding = nil
  # FIXME: this is a hack around broken libxml versions
  return dump_html if %w[2 6] === LIBXML_VERSION.split('.')[0..1]
  serialize(encoding, SaveOptions::FORMAT |
                      SaveOptions::NO_DECLARATION |
                      SaveOptions::NO_EMPTY_TAGS |
                      SaveOptions::AS_XHTML)
end

def to_xml encoding = nil

Serialize this Node to XML using +encoding+
##
def to_xml encoding = nil
  serialize(encoding, SaveOptions::FORMAT | SaveOptions::AS_XML)
end

def traverse(&block)

Yields self and all children to +block+ recursively.
###
def traverse(&block)
  children.each{|j| j.traverse(&block) }
  block.call(self)
end

def values

Get the attribute values for this Node.
##
def values
  attribute_nodes.map { |node| node.value }
end

def write_html_to io, encoding = nil

Write Node as HTML to +io+ with +encoding+
##
def write_html_to io, encoding = nil
  write_to io, encoding, SaveOptions::FORMAT |
    SaveOptions::NO_DECLARATION |
    SaveOptions::NO_EMPTY_TAGS |
    SaveOptions::AS_HTML
end

def write_to io, encoding = nil, save_options = SaveOptions::FORMAT

Write Node to +io+ with +encoding+ and +save_options+
##
def write_to io, encoding = nil, save_options = SaveOptions::FORMAT
  config = SaveOptions.new(save_options)
  yield config if block_given?
  native_write_to(io, encoding, config.options)
end

def write_xhtml_to io, encoding = nil

Write Node as XHTML to +io+ with +encoding+
##
def write_xhtml_to io, encoding = nil
  write_to io, encoding, SaveOptions::FORMAT |
    SaveOptions::NO_DECLARATION |
    SaveOptions::NO_EMPTY_TAGS |
    SaveOptions::AS_XHTML
end

def write_xml_to io, encoding = nil

Write Node as XML to +io+ with +encoding+
##
def write_xml_to io, encoding = nil
  write_to io, encoding, SaveOptions::FORMAT | SaveOptions::AS_XML
end

def xml?

def xml?
  type == DOCUMENT_NODE
end

def xpath *paths


}.new)
end
node_set.find_all { |node| node['some_attribute'] =~ /#{regex}/ }
def regex node_set, regex
node.xpath('.//title[regex(., "\w+")]', Class.new {

For example:
create a class and implement the # function you want to define.
Custom XPath functions may also be defined. To define custom functions

node.xpath('.//xmlns:name', node.root.namespaces)
node.xpath('.//foo:name', { 'foo' => 'http://example.org/' })
node.xpath('.//title')

queries. A hash of namespaces may be appended. For example:
Search this node for XPath +paths+. +paths+ must be one or more XPath
##
def xpath *paths
  # Pop off our custom function handler if it exists
  handler = ![
    Hash, String, Symbol
  ].include?(paths.last.class) ? paths.pop : nil
  ns = paths.last.is_a?(Hash) ? paths.pop : document.root.namespaces
  return NodeSet.new(document) unless document.root
  sets = paths.map { |path|
    ctx = XPathContext.new(self)
    ctx.register_namespaces(ns)
    set = ctx.evaluate(path, handler).node_set
    set.document = document
    document.decorate(set)
    set
  }
  return sets.first if sets.length == 1
  NodeSet.new(document) do |combined|
    document.decorate(combined)
    sets.each do |set|
      set.each do |node|
        combined << node
      end
    end
  end
end