class Nokogiri::XML::Node

{Nokogiri::XML::Node} is your window to the fun filled world of dealing with XML and HTML tags. A {Nokogiri::XML::Node} may be treated similarly to a hash with regard to attributes. For example:

node = Nokogiri::XML::DocumentFragment.parse("<a href='#foo' id='link'>link</a>").at_css("a")
node.to_html # => "<a href=\"#foo\" id=\"link\">link</a>"
node['href'] # => "#foo"
node.keys # => ["href", "id"]
node.values # => ["#foo", "link"]
node['class'] = 'green' # => "green"
node.to_html # => "<a href=\"#foo\" id=\"link\" class=\"green\">link</a>"

See the method group entitled “Working With Node Attributes” for the full set of methods.

{Nokogiri::XML::Node} also has methods that let you move around your tree. For navigating your tree, see:

When printing or otherwise emitting a document or a node (and its subtree), there are a few methods you might want to use:

You may search this node's subtree using {#xpath} and {#css}

Constants

ATTRIBUTE_DECL

Attribute declaration type

ATTRIBUTE_NODE

Attribute node type

CDATA_SECTION_NODE

CDATA node type, see {Nokogiri::XML::Node#cdata?}

COMMENT_NODE

Comment node type, see {Nokogiri::XML::Node#comment?}

DOCB_DOCUMENT_NODE

DOCB document node type

DOCUMENT_FRAG_NODE

Document fragment node type

DOCUMENT_NODE

Document node type, see {Nokogiri::XML::Node#xml?}

DOCUMENT_TYPE_NODE

Document type node type

DTD_NODE

DTD node type

ELEMENT_DECL

Element declaration type

ELEMENT_NODE

Element node type, see {Nokogiri::XML::Node#element?}

ENTITY_DECL

Entity declaration type

ENTITY_NODE

Entity node type

ENTITY_REF_NODE

Entity reference node type

HTML_DOCUMENT_NODE

HTML document node type, see {Nokogiri::XML::Node#html?}

IMPLIED_XPATH_CONTEXTS

@private

NAMESPACE_DECL

Namespace declaration type

NOTATION_NODE

Notation node type

PI_NODE

PI node type

TEXT_NODE

Text node type, see {Nokogiri::XML::Node#text?}

USING_LIBXML_WITH_BROKEN_SERIALIZATION
XINCLUDE_END

XInclude end type

XINCLUDE_START

XInclude start type

Public Class Methods

new(p1, p2, *args) click to toggle source

@overload new(name, document)

Create a new node with +name+ sharing GC lifecycle with +document+.
@param name [String]
@param document [Nokogiri::XML::Document]
@yieldparam node [Nokogiri::XML::Node]
@return [Nokogiri::XML::Node]
@see Nokogiri::XML::Node#initialize
static VALUE
rb_xml_node_new(int argc, VALUE *argv, VALUE klass)
{
  xmlDocPtr doc;
  xmlNodePtr node;
  VALUE name;
  VALUE document;
  VALUE rest;
  VALUE rb_node;

  rb_scan_args(argc, argv, "2*", &name, &document, &rest);

  Data_Get_Struct(document, xmlDoc, doc);

  node = xmlNewNode(NULL, (xmlChar *)StringValueCStr(name));
  node->doc = doc->doc;
  noko_xml_document_pin_node(node);

  rb_node = noko_xml_node_wrap(
              klass == cNokogiriXmlNode ? (VALUE)NULL : klass,
              node
            );
  rb_obj_call_init(rb_node, argc, argv);

  if (rb_block_given_p()) { rb_yield(rb_node); }

  return rb_node;
}
new(name, document) click to toggle source

Create a new node with name sharing GC lifecycle with document. @param name [String] @param document [Nokogiri::XML::Document] @yieldparam node [Nokogiri::XML::Node] @return [Nokogiri::XML::Node] @see Nokogiri::XML::Node.new

# File lib/nokogiri/xml/node.rb, line 99
def initialize(name, document)
  # This is intentionally empty.
end

Public Instance Methods

<<(node_or_tags) click to toggle source

Add node_or_tags as a child of this Node. node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns self, to support chaining of calls (e.g., root << child1 << child2)

Also see related method add_child.

# File lib/nokogiri/xml/node.rb, line 174
def <<(node_or_tags)
  add_child node_or_tags
  self
end
<=>(other) click to toggle source

Compare two Node objects with respect to their Document. Nodes from different documents cannot be compared.

# File lib/nokogiri/xml/node.rb, line 1000
def <=>(other)
  return nil unless other.is_a?(Nokogiri::XML::Node)
  return nil unless document == other.document
  compare other
end
==(other) click to toggle source

Test to see if this Node is equal to other

# File lib/nokogiri/xml/node.rb, line 991
def ==(other)
  return false unless other
  return false unless other.respond_to?(:pointer_id)
  pointer_id == other.pointer_id
end
>(selector) click to toggle source

Search this node's immediate children using CSS selector selector

# File lib/nokogiri/xml/node.rb, line 113
def >(selector)
  ns = document.root.namespaces
  xpath CSS.xpath_for(selector, :prefix => "./", :ns => ns).first
end
[](name) click to toggle source

Get the attribute value for the attribute name

# File lib/nokogiri/xml/node.rb, line 381
def [](name)
  get(name.to_s)
end
Also aliased as: get_attribute, attr
[]=(name, value) click to toggle source

Set the attribute value for the attribute name to value

# File lib/nokogiri/xml/node.rb, line 387
def []=(name, value)
  set name.to_s, value.to_s
end
Also aliased as: set_attribute
accept(visitor) click to toggle source

Accept a visitor. This method calls “visit” on visitor with self.

# File lib/nokogiri/xml/node.rb, line 985
def accept(visitor)
  visitor.visit(self)
end
add_child(node_or_tags) click to toggle source

Add node_or_tags as a child of this Node. node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns the reparented node (if node_or_tags is a Node), or NodeSet (if node_or_tags is a DocumentFragment, NodeSet, or string).

Also see related method +<<+.

# File lib/nokogiri/xml/node.rb, line 129
def add_child(node_or_tags)
  node_or_tags = coerce(node_or_tags)
  if node_or_tags.is_a?(XML::NodeSet)
    node_or_tags.each { |n| add_child_node_and_reparent_attrs n }
  else
    add_child_node_and_reparent_attrs node_or_tags
  end
  node_or_tags
end
add_class(names) click to toggle source

Ensure HTML CSS classes are present on a Node. Any CSS classes in names that already exist in the Node's class attribute are not added. Note that any existing duplicates in the class attribute are not removed. Compare with {#append_class}.

This is a convenience function and is equivalent to:

node.kwattr_add("class", names)

@see kwattr_add @see classes @see append_class @see remove_class

@param names [String, Array<String>]

CSS class names to be added to the Node's +class+
attribute. May be a string containing whitespace-delimited
names, or an Array of String names. Any class names already
present will not be added. Any class names not present will
be added. If no +class+ attribute exists, one is created.

@return [Node] Returns self for ease of chaining method calls.

@example Ensure that a Node has CSS class “section”

node                      # => <div></div>
node.add_class("section") # => <div class="section"></div>
node.add_class("section") # => <div class="section"></div> # duplicate not added

@example Ensure that a Node has CSS classes “section” and “header”, via a String argument.

node                             # => <div class="section section"></div>
node.add_class("section header") # => <div class="section section header"></div>
# Note that the CSS class "section" is not added because it is already present.
# Note also that the pre-existing duplicate CSS class "section" is not removed.

@example Ensure that a Node has CSS classes “section” and “header”, via an Array argument.

node                                  # => <div></div>
node.add_class(["section", "header"]) # => <div class="section header"></div>
# File lib/nokogiri/xml/node.rb, line 500
def add_class(names)
  kwattr_add("class", names)
end

Adds a namespace definition with prefix using href value. The result is as if parsed XML for this node had included an attribute 'xmlns:prefix=value'. A default namespace for this node (“xmlns=”) can be added by passing 'nil' for prefix. Namespaces added this way will not show up in attributes, but they will be included as an xmlns attribute when the node is serialized to XML.

static VALUE
add_namespace_definition(VALUE rb_node, VALUE rb_prefix, VALUE rb_href)
{
  xmlNodePtr c_node, element;
  xmlNsPtr c_namespace;
  const xmlChar *c_prefix = (const xmlChar *)(NIL_P(rb_prefix) ? NULL : StringValueCStr(rb_prefix));

  Data_Get_Struct(rb_node, xmlNode, c_node);
  element = c_node ;

  c_namespace = xmlSearchNs(c_node->doc, c_node, c_prefix);

  if (!c_namespace) {
    if (c_node->type != XML_ELEMENT_NODE) {
      element = c_node->parent;
    }
    c_namespace = xmlNewNs(element, (const xmlChar *)StringValueCStr(rb_href), c_prefix);
  }

  if (!c_namespace) {
    return Qnil ;
  }

  if (NIL_P(rb_prefix) || c_node != element) {
    xmlSetNs(c_node, c_namespace);
  }

  return noko_xml_namespace_wrap(c_namespace, c_node->doc);
}
Also aliased as: add_namespace
add_next_sibling(node_or_tags) click to toggle source

Insert node_or_tags after this Node (as a sibling). node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns the reparented node (if node_or_tags is a Node), or NodeSet (if node_or_tags is a DocumentFragment, NodeSet, or string).

Also see related method after.

# File lib/nokogiri/xml/node.rb, line 199
def add_next_sibling(node_or_tags)
  raise ArgumentError.new("A document may not have multiple root nodes.") if (parent && parent.document?) && !(node_or_tags.comment? || node_or_tags.processing_instruction?)

  add_sibling :next, node_or_tags
end
Also aliased as: next=
add_previous_sibling(node_or_tags) click to toggle source

Insert node_or_tags before this Node (as a sibling). node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns the reparented node (if node_or_tags is a Node), or NodeSet (if node_or_tags is a DocumentFragment, NodeSet, or string).

Also see related method before.

# File lib/nokogiri/xml/node.rb, line 186
def add_previous_sibling(node_or_tags)
  raise ArgumentError.new("A document may not have multiple root nodes.") if (parent && parent.document?) && !(node_or_tags.comment? || node_or_tags.processing_instruction?)

  add_sibling :previous, node_or_tags
end
Also aliased as: previous=
after(node_or_tags) click to toggle source

Insert node_or_tags after this node (as a sibling). node_or_tags can be a Nokogiri::XML::Node, a Nokogiri::XML::DocumentFragment, or a string containing markup.

Returns self, to support chaining of calls.

Also see related method add_next_sibling.

# File lib/nokogiri/xml/node.rb, line 224
def after(node_or_tags)
  add_next_sibling node_or_tags
  self
end
ancestors(selector = nil) click to toggle source

Get a list of ancestor Node for this Node. If selector is given, the ancestors must match selector

# File lib/nokogiri/xml/node.rb, line 955
def ancestors(selector = nil)
  return NodeSet.new(document) unless respond_to?(:parent)
  return NodeSet.new(document) unless parent

  parents = [parent]

  while parents.last.respond_to?(:parent)
    break unless ctx_parent = parents.last.parent
    parents << ctx_parent
  end

  return NodeSet.new(document, parents) unless selector

  root = parents.last
  search_results = root.search(selector)

  NodeSet.new(document, parents.find_all { |parent|
    search_results.include?(parent)
  })
end
append_class(names) click to toggle source

Add HTML CSS classes to a Node, regardless of duplication. Compare with {#add_class}.

This is a convenience function and is equivalent to:

node.kwattr_append("class", names)

@see kwattr_append @see classes @see add_class @see remove_class

@param names [String, Array<String>]

CSS class names to be appended to the Node's +class+
attribute. May be a string containing whitespace-delimited
names, or an Array of String names. All class names passed
in will be appended to the +class+ attribute even if they
are already present in the attribute value. If no +class+
attribute exists, one is created.

@return [Node] Returns self for ease of chaining method calls.

@example Append “section” to a Node's CSS class attriubute

node                         # => <div></div>
node.append_class("section") # => <div class="section"></div>
node.append_class("section") # => <div class="section section"></div> # duplicate added!

@example Append “section” and “header” to a Node's CSS class attribute, via a String argument.

node                                # => <div class="section section"></div>
node.append_class("section header") # => <div class="section section section header"></div>
# Note that the CSS class "section" is appended even though it is already present.

@example Append “section” and “header” to a Node's CSS class attribute, via an Array argument.

node                                     # => <div></div>
node.append_class(["section", "header"]) # => <div class="section header"></div>
node.append_class(["section", "header"]) # => <div class="section header section header"></div>
# File lib/nokogiri/xml/node.rb, line 541
def append_class(names)
  kwattr_append("class", names)
end
attr(name)
Alias for: []
attribute(name) click to toggle source

Get the attribute node with name

static VALUE
attr(VALUE self, VALUE name)
{
  xmlNodePtr node;
  xmlAttrPtr prop;
  Data_Get_Struct(self, xmlNode, node);
  prop = xmlHasProp(node, (xmlChar *)StringValueCStr(name));

  if (! prop) { return Qnil; }
  return noko_xml_node_wrap(Qnil, (xmlNodePtr)prop);
}
attribute_nodes() click to toggle source

@overload attribute_nodes()

Get the attributes for a Node
@return [Array<Nokogiri::XML::Attr>] containing the Node's attributes.
static VALUE
attribute_nodes(VALUE rb_node)
{
  xmlNodePtr c_node;

  Data_Get_Struct(rb_node, xmlNode, c_node);

  return noko_xml_node_attrs(c_node);
}
attribute_with_ns(name, namespace) click to toggle source

Get the attribute node with name and namespace

static VALUE
attribute_with_ns(VALUE self, VALUE name, VALUE namespace)
{
  xmlNodePtr node;
  xmlAttrPtr prop;
  Data_Get_Struct(self, xmlNode, node);
  prop = xmlHasNsProp(node, (xmlChar *)StringValueCStr(name),
                      NIL_P(namespace) ? NULL : (xmlChar *)StringValueCStr(namespace));

  if (! prop) { return Qnil; }
  return noko_xml_node_wrap(Qnil, (xmlNodePtr)prop);
}
attributes() click to toggle source

Returns a hash containing the node's attributes. The key is the attribute name without any namespace, the value is a Nokogiri::XML::Attr representing the attribute. If you need to distinguish attributes with the same name, with different namespaces use attribute_nodes instead.

# File lib/nokogiri/xml/node.rb, line 397
def attributes
  attribute_nodes.each_with_object({}) do |node, hash|
    hash[node.node_name] = node
  end
end
before(node_or_tags) click to toggle source

Insert node_or_tags before this node (as a sibling). node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns self, to support chaining of calls.

Also see related method add_previous_sibling.

# File lib/nokogiri/xml/node.rb, line 212
def before(node_or_tags)
  add_previous_sibling node_or_tags
  self
end
blank? click to toggle source

Is this node blank?

static VALUE
blank_eh(VALUE self)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);
  return (1 == xmlIsBlankNode(node)) ? Qtrue : Qfalse ;
}
canonicalize(mode = XML::XML_C14N_1_0, inclusive_namespaces = nil, with_comments = false) click to toggle source
# File lib/nokogiri/xml/node.rb, line 1137
def canonicalize(mode = XML::XML_C14N_1_0, inclusive_namespaces = nil, with_comments = false)
  c14n_root = self
  document.canonicalize(mode, inclusive_namespaces, with_comments) do |node, parent|
    tn = node.is_a?(XML::Node) ? node : parent
    tn == c14n_root || tn.ancestors.include?(c14n_root)
  end
end
cdata?() click to toggle source

Returns true if this is a CDATA

# File lib/nokogiri/xml/node.rb, line 877
def cdata?
  type == CDATA_SECTION_NODE
end
child click to toggle source

Returns the child node

static VALUE
child(VALUE self)
{
  xmlNodePtr node, child;
  Data_Get_Struct(self, xmlNode, node);

  child = node->children;
  if (!child) { return Qnil; }

  return noko_xml_node_wrap(Qnil, child);
}
children click to toggle source

Get the list of children for this node as a NodeSet

static VALUE
children(VALUE self)
{
  xmlNodePtr node;
  xmlNodePtr child;
  xmlNodeSetPtr set;
  VALUE document;
  VALUE node_set;

  Data_Get_Struct(self, xmlNode, node);

  child = node->children;
  set = xmlXPathNodeSetCreate(child);

  document = DOC_RUBY_OBJECT(node->doc);

  if (!child) { return noko_xml_node_set_wrap(set, document); }

  child = child->next;
  while (NULL != child) {
    xmlXPathNodeSetAddUnique(set, child);
    child = child->next;
  }

  node_set = noko_xml_node_set_wrap(set, document);

  return node_set;
}
children=(node_or_tags) click to toggle source

Set the inner html for this Node node_or_tags node_or_tags can be a Nokogiri::XML::Node, a Nokogiri::XML::DocumentFragment, or a string containing markup.

Returns the reparented node (if node_or_tags is a Node), or NodeSet (if node_or_tags is a DocumentFragment, NodeSet, or string).

Also see related method inner_html=

# File lib/nokogiri/xml/node.rb, line 248
def children=(node_or_tags)
  node_or_tags = coerce(node_or_tags)
  children.unlink
  if node_or_tags.is_a?(XML::NodeSet)
    node_or_tags.each { |n| add_child_node_and_reparent_attrs n }
  else
    add_child_node_and_reparent_attrs node_or_tags
  end
  node_or_tags
end
classes() click to toggle source

Get the CSS class names of a Node.

This is a convenience function and is equivalent to:

node.kwattr_values("class")

@see kwattr_values @see add_class @see append_class @see remove_class

@return [Array<String>]

The CSS classes present in the Node's +class+ attribute. If
the attribute is empty or non-existent, the return value is
an empty array.

@example

node         # => <div class="section title header"></div>
node.classes # => ["section", "title", "header"]
# File lib/nokogiri/xml/node.rb, line 457
def classes
  kwattr_values("class")
end
Alias for: dup
comment?() click to toggle source

Returns true if this is a Comment

# File lib/nokogiri/xml/node.rb, line 872
def comment?
  type == COMMENT_NODE
end
content click to toggle source

Returns the plaintext content for this Node. Note that entities will always be expanded in the returned string.

static VALUE
get_native_content(VALUE self)
{
  xmlNodePtr node;
  xmlChar *content;

  Data_Get_Struct(self, xmlNode, node);

  content = xmlNodeGetContent(node);
  if (content) {
    VALUE rval = NOKOGIRI_STR_NEW2(content);
    xmlFree(content);
    return rval;
  }
  return Qnil;
}
Also aliased as: text, inner_text
content=(string) click to toggle source

Set the Node's content to a Text node containing string. The string gets XML escaped, not interpreted as markup.

# File lib/nokogiri/xml/node.rb, line 305
def content=(string)
  self.native_content = encode_special_chars(string.to_s)
end
create_external_subset(name, external_id, system_id) click to toggle source

Create an external subset

static VALUE
create_external_subset(VALUE self, VALUE name, VALUE external_id, VALUE system_id)
{
  xmlNodePtr node;
  xmlDocPtr doc;
  xmlDtdPtr dtd;

  Data_Get_Struct(self, xmlNode, node);

  doc = node->doc;

  if (doc->extSubset) {
    rb_raise(rb_eRuntimeError, "Document already has an external subset");
  }

  dtd = xmlNewDtd(
          doc,
          NIL_P(name)        ? NULL : (const xmlChar *)StringValueCStr(name),
          NIL_P(external_id) ? NULL : (const xmlChar *)StringValueCStr(external_id),
          NIL_P(system_id)   ? NULL : (const xmlChar *)StringValueCStr(system_id)
        );

  if (!dtd) { return Qnil; }

  return noko_xml_node_wrap(Qnil, (xmlNodePtr)dtd);
}
create_internal_subset(name, external_id, system_id) click to toggle source

Create the internal subset of a document.

doc.create_internal_subset("chapter", "-//OASIS//DTD DocBook XML//EN", "chapter.dtd")
# => <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML//EN" "chapter.dtd">

doc.create_internal_subset("chapter", nil, "chapter.dtd")
# => <!DOCTYPE chapter SYSTEM "chapter.dtd">
static VALUE
create_internal_subset(VALUE self, VALUE name, VALUE external_id, VALUE system_id)
{
  xmlNodePtr node;
  xmlDocPtr doc;
  xmlDtdPtr dtd;

  Data_Get_Struct(self, xmlNode, node);

  doc = node->doc;

  if (xmlGetIntSubset(doc)) {
    rb_raise(rb_eRuntimeError, "Document already has an internal subset");
  }

  dtd = xmlCreateIntSubset(
          doc,
          NIL_P(name)        ? NULL : (const xmlChar *)StringValueCStr(name),
          NIL_P(external_id) ? NULL : (const xmlChar *)StringValueCStr(external_id),
          NIL_P(system_id)   ? NULL : (const xmlChar *)StringValueCStr(system_id)
        );

  if (!dtd) { return Qnil; }

  return noko_xml_node_wrap(Qnil, (xmlNodePtr)dtd);
}
css_path() click to toggle source

Get the path to this node as a CSS expression

# File lib/nokogiri/xml/node.rb, line 946
def css_path
  path.split(/\//).map { |part|
    part.length == 0 ? nil : part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)')
  }.compact.join(" > ")
end
decorate!() click to toggle source

Decorate this node with the decorators set up in this node's Document

# File lib/nokogiri/xml/node.rb, line 105
def decorate!
  document.decorate(self)
end
default_namespace=(url) click to toggle source

Adds a default namespace supplied as a string url href, to self. The consequence is as an xmlns attribute with supplied argument were present in parsed XML. A default namespace set with this method will now show up in attributes, but when this node is serialized to XML an “xmlns” attribute will appear. See also namespace and namespace=

# File lib/nokogiri/xml/node.rb, line 322
def default_namespace=(url)
  add_namespace_definition(nil, url)
end
delete(name)
Alias for: remove_attribute
description() click to toggle source

Fetch the Nokogiri::HTML::ElementDescription for this node. Returns nil on XML documents and on unknown tags.

# File lib/nokogiri/xml/node.rb, line 914
def description
  return nil if document.xml?
  Nokogiri::HTML::ElementDescription[name]
end
do_xinclude(options = XML::ParseOptions::DEFAULT_XML) { |options| ... } click to toggle source

Do xinclude substitution on the subtree below node. If given a block, a Nokogiri::XML::ParseOptions object initialized from options, will be passed to it, allowing more convenient modification of the parser options.

# File lib/nokogiri/xml/node.rb, line 349
def do_xinclude(options = XML::ParseOptions::DEFAULT_XML)
  options = Nokogiri::XML::ParseOptions.new(options) if Integer === options

  # give options to user
  yield options if block_given?

  # call c extension
  process_xincludes(options.to_i)
end
document click to toggle source

Get the document for this Node

static VALUE
document(VALUE self)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);
  return DOC_RUBY_OBJECT(node->doc);
}
document?() click to toggle source

Returns true if this is a Document

# File lib/nokogiri/xml/node.rb, line 892
def document?
  is_a? XML::Document
end
dup click to toggle source
dup(depth)
dup(depth, new_parent_doc)

Copy this node. An optional depth may be passed in. 0 is a shallow copy, 1 (the default) is a deep copy. An optional new_parent_doc may also be passed in, which will be the new node's parent document. Defaults to the current node's document. current document.

static VALUE
duplicate_node(int argc, VALUE *argv, VALUE self)
{
  VALUE r_level, r_new_parent_doc;
  int level;
  int n_args;
  xmlDocPtr new_parent_doc;
  xmlNodePtr node, dup;

  Data_Get_Struct(self, xmlNode, node);

  n_args = rb_scan_args(argc, argv, "02", &r_level, &r_new_parent_doc);

  if (n_args < 1) {
    r_level = INT2NUM((long)1);
  }
  level = (int)NUM2INT(r_level);

  if (n_args < 2) {
    new_parent_doc = node->doc;
  } else {
    Data_Get_Struct(r_new_parent_doc, xmlDoc, new_parent_doc);
  }

  dup = xmlDocCopyNode(node, new_parent_doc, level);
  if (dup == NULL) { return Qnil; }

  noko_xml_document_pin_node(dup);

  return noko_xml_node_wrap(rb_obj_class(self), dup);
}
Also aliased as: clone
each() { |node_name, value| ... } click to toggle source

Iterate over each attribute name and value pair for this Node.

# File lib/nokogiri/xml/node.rb, line 423
def each
  attribute_nodes.each { |node|
    yield [node.node_name, node.value]
  }
end
elem?()
Alias for: element?
element?() click to toggle source

Returns true if this is an Element node

# File lib/nokogiri/xml/node.rb, line 927
def element?
  type == ELEMENT_NODE
end
Also aliased as: elem?
element_children click to toggle source

Get the list of children for this node as a NodeSet. All nodes will be element nodes.

Example:

@doc.root.element_children.all? { |x| x.element? } # => true
static VALUE
element_children(VALUE self)
{
  xmlNodePtr node;
  xmlNodePtr child;
  xmlNodeSetPtr set;
  VALUE document;
  VALUE node_set;

  Data_Get_Struct(self, xmlNode, node);

  child = xmlFirstElementChild(node);
  set = xmlXPathNodeSetCreate(child);

  document = DOC_RUBY_OBJECT(node->doc);

  if (!child) { return noko_xml_node_set_wrap(set, document); }

  child = xmlNextElementSibling(child);
  while (NULL != child) {
    xmlXPathNodeSetAddUnique(set, child);
    child = xmlNextElementSibling(child);
  }

  node_set = noko_xml_node_set_wrap(set, document);

  return node_set;
}
Also aliased as: elements
Alias for: element_children
encode_special_chars(string) click to toggle source

Encode any special characters in string

static VALUE
encode_special_chars(VALUE self, VALUE string)
{
  xmlNodePtr node;
  xmlChar *encoded;
  VALUE encoded_str;

  Data_Get_Struct(self, xmlNode, node);
  encoded = xmlEncodeSpecialChars(
              node->doc,
              (const xmlChar *)StringValueCStr(string)
            );

  encoded_str = NOKOGIRI_STR_NEW2(encoded);
  xmlFree(encoded);

  return encoded_str;
}
external_subset click to toggle source

Get the external subset

static VALUE
external_subset(VALUE self)
{
  xmlNodePtr node;
  xmlDocPtr doc;
  xmlDtdPtr dtd;

  Data_Get_Struct(self, xmlNode, node);

  if (!node->doc) { return Qnil; }

  doc = node->doc;
  dtd = doc->extSubset;

  if (!dtd) { return Qnil; }

  return noko_xml_node_wrap(Qnil, (xmlNodePtr)dtd);
}
first_element_child click to toggle source

Returns the first child node of this node that is an element.

Example:

@doc.root.first_element_child.element? # => true
static VALUE
first_element_child(VALUE self)
{
  xmlNodePtr node, child;
  Data_Get_Struct(self, xmlNode, node);

  child = xmlFirstElementChild(node);
  if (!child) { return Qnil; }

  return noko_xml_node_wrap(Qnil, child);
}
fragment(tags) click to toggle source

Create a DocumentFragment containing tags that is relative to this context node.

# File lib/nokogiri/xml/node.rb, line 789
def fragment(tags)
  type = document.html? ? Nokogiri::HTML : Nokogiri::XML
  type::DocumentFragment.new(document, tags, self)
end
fragment?() click to toggle source

Returns true if this is a DocumentFragment

# File lib/nokogiri/xml/node.rb, line 907
def fragment?
  type == DOCUMENT_FRAG_NODE
end
get_attribute(name)
Alias for: []
Alias for: key?
html?() click to toggle source

Returns true if this is an HTML::Document node

# File lib/nokogiri/xml/node.rb, line 887
def html?
  type == HTML_DOCUMENT_NODE
end
inner_html(*args) click to toggle source

Get the inner_html for this node's Node#children

# File lib/nokogiri/xml/node.rb, line 941
def inner_html(*args)
  children.map { |x| x.to_html(*args) }.join
end
inner_html=(node_or_tags) click to toggle source

Set the inner html for this Node to node_or_tags node_or_tags can be a Nokogiri::XML::Node, a Nokogiri::XML::DocumentFragment, or a string containing markup.

Returns self.

Also see related method children=

# File lib/nokogiri/xml/node.rb, line 236
def inner_html=(node_or_tags)
  self.children = node_or_tags
  self
end
Alias for: content
internal_subset click to toggle source

Get the internal subset

static VALUE
internal_subset(VALUE self)
{
  xmlNodePtr node;
  xmlDocPtr doc;
  xmlDtdPtr dtd;

  Data_Get_Struct(self, xmlNode, node);

  if (!node->doc) { return Qnil; }

  doc = node->doc;
  dtd = xmlGetIntSubset(doc);

  if (!dtd) { return Qnil; }

  return noko_xml_node_wrap(Qnil, (xmlNodePtr)dtd);
}
key?(attribute) click to toggle source

Returns true if attribute is set

static VALUE
key_eh(VALUE self, VALUE attribute)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);
  if (xmlHasProp(node, (xmlChar *)StringValueCStr(attribute))) {
    return Qtrue;
  }
  return Qfalse;
}
Also aliased as: has_attribute?
keys() click to toggle source

Get the attribute names for this Node.

# File lib/nokogiri/xml/node.rb, line 417
def keys
  attribute_nodes.map(&:node_name)
end
kwattr_add(attribute_name, keywords) click to toggle source

Ensure that values are present in a keyword attribute.

Any values in keywords that already exist in the Node's attribute values are not added. Note that any existing duplicates in the attribute values are not removed. Compare with {#kwattr_append}.

A “keyword attribute” is a node attribute that contains a set of space-delimited values. Perhaps the most familiar example of this is the HTML class attribute used to contain CSS classes. But other keyword attributes exist, for instance [`rel`](developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel).

@see add_class @see kwattr_values @see kwattr_append @see kwattr_remove

@param attribute_name [String] The name of the keyword attribute to be modified.

@param keywords [String, Array<String>]

Keywords to be added to the attribute named
+attribute_name+. May be a string containing
whitespace-delimited values, or an Array of String
values. Any values already present will not be added. Any
values not present will be added. If the named attribute
does not exist, it is created.

@return [Node] Returns self for ease of chaining method calls.

@example Ensure that a Node has “nofollow” in its rel attribute.

node                               # => <a></a>
node.kwattr_add("rel", "nofollow") # => <a rel="nofollow"></a>
node.kwattr_add("rel", "nofollow") # => <a rel="nofollow"></a> # duplicate not added

@example Ensure that a Node has “nofollow” and “noreferrer” in its rel attribute, via a String argument.

node                                          # => <a rel="nofollow nofollow"></a>
node.kwattr_add("rel", "nofollow noreferrer") # => <a rel="nofollow nofollow noreferrer"></a>
# Note that "nofollow" is not added because it is already present.
# Note also that the pre-existing duplicate "nofollow" is not removed.

@example Ensure that a Node has “nofollow” and “noreferrer” in its rel attribute, via an Array argument.

node                                               # => <a></a>
node.kwattr_add("rel", ["nofollow", "noreferrer"]) # => <a rel="nofollow noreferrer"></a>

@since v1.11.0

# File lib/nokogiri/xml/node.rb, line 657
def kwattr_add(attribute_name, keywords)
  keywords = keywordify(keywords)
  current_kws = kwattr_values(attribute_name)
  new_kws = (current_kws + (keywords - current_kws)).join(" ")
  set_attribute(attribute_name, new_kws)
  self
end
kwattr_append(attribute_name, keywords) click to toggle source

Add keywords to a Node's keyword attribute, regardless of duplication. Compare with {#kwattr_add}.

A “keyword attribute” is a node attribute that contains a set of space-delimited values. Perhaps the most familiar example of this is the HTML class attribute used to contain CSS classes. But other keyword attributes exist, for instance [`rel`](developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel).

@see append_class @see kwattr_values @see kwattr_add @see kwattr_remove

@param attribute_name [String] The name of the keyword attribute to be modified.

@param keywords [String, Array<String>]

Keywords to be added to the attribute named
+attribute_name+. May be a string containing
whitespace-delimited values, or an Array of String
values. All values passed in will be appended to the named
attribute even if they are already present in the
attribute. If the named attribute does not exist, it is
created.

@return [Node] Returns self for ease of chaining method calls.

@example Append “nofollow” to the rel attribute.

node                                  # => <a></a>
node.kwattr_append("rel", "nofollow") # => <a rel="nofollow"></a>
node.kwattr_append("rel", "nofollow") # => <a rel="nofollow nofollow"></a> # duplicate added!

@example Append “nofollow” and “noreferrer” to the rel attribute, via a String argument.

node                                             # => <a rel="nofollow"></a>
node.kwattr_append("rel", "nofollow noreferrer") # => <a rel="nofollow nofollow noreferrer"></a>
# Note that "nofollow" is appended even though it is already present.

@example Append “nofollow” and “noreferrer” to the rel attribute, via an Array argument.

node                                                  # => <a></a>
node.kwattr_append("rel", ["nofollow", "noreferrer"]) # => <a rel="nofollow noreferrer"></a>

@since v1.11.0

# File lib/nokogiri/xml/node.rb, line 709
def kwattr_append(attribute_name, keywords)
  keywords = keywordify(keywords)
  current_kws = kwattr_values(attribute_name)
  new_kws = (current_kws + keywords).join(" ")
  set_attribute(attribute_name, new_kws)
  self
end
kwattr_remove(attribute_name, keywords) click to toggle source

Remove keywords from a keyword attribute. Any matching keywords that exist in the named attribute are removed, including any multiple entries.

If no keywords remain after this operation, or if keywords is nil, the attribute is deleted from the node.

A “keyword attribute” is a node attribute that contains a set of space-delimited values. Perhaps the most familiar example of this is the HTML class attribute used to contain CSS classes. But other keyword attributes exist, for instance [`rel`](developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel).

@see remove_class @see kwattr_values @see kwattr_add @see kwattr_append

@param attribute_name [String] The name of the keyword attribute to be modified.

@param keywords [String, Array<String>]

Keywords to be removed from the attribute named
+attribute_name+. May be a string containing
whitespace-delimited values, or an Array of String
values. Any keywords present in the named attribute will be
removed. If no keywords remain, or if +keywords+ is nil, the
attribute is deleted.

@return [Node] Returns self for ease of chaining method calls.

@example

node                                    # => <a rel="nofollow noreferrer">link</a>
node.kwattr_remove("rel", "nofollow")   # => <a rel="noreferrer">link</a>
node.kwattr_remove("rel", "noreferrer") # => <a>link</a> # attribute is deleted when empty

@since v1.11.0

# File lib/nokogiri/xml/node.rb, line 755
def kwattr_remove(attribute_name, keywords)
  if keywords.nil?
    remove_attribute(attribute_name)
    return self
  end

  keywords = keywordify(keywords)
  current_kws = kwattr_values(attribute_name)
  new_kws = current_kws - keywords
  if new_kws.empty?
    remove_attribute(attribute_name)
  else
    set_attribute(attribute_name, new_kws.join(" "))
  end
  self
end
kwattr_values(attribute_name) click to toggle source

Retrieve values from a keyword attribute of a Node.

A “keyword attribute” is a node attribute that contains a set of space-delimited values. Perhaps the most familiar example of this is the HTML class attribute used to contain CSS classes. But other keyword attributes exist, for instance [`rel`](developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel).

@see classes @see kwattr_add @see kwattr_append @see kwattr_remove

@param attribute_name [String] The name of the keyword attribute to be inspected.

@return [Array<String>]

The values present in the Node's +attribute_name+
attribute. If the attribute is empty or non-existent, the
return value is an empty array.

@example

node                      # => <a rel="nofollow noopener external">link</a>
node.kwattr_values("rel") # => ["nofollow", "noopener", "external"]

@since v1.11.0

# File lib/nokogiri/xml/node.rb, line 605
def kwattr_values(attribute_name)
  keywordify(get_attribute(attribute_name) || [])
end
lang click to toggle source

Searches the language of a node, i.e. the values of the xml:lang attribute or the one carried by the nearest ancestor.

static VALUE
get_lang(VALUE self_rb)
{
  xmlNodePtr self ;
  xmlChar *lang ;
  VALUE lang_rb ;

  Data_Get_Struct(self_rb, xmlNode, self);

  lang = xmlNodeGetLang(self);
  if (lang) {
    lang_rb = NOKOGIRI_STR_NEW2(lang);
    xmlFree(lang);
    return lang_rb ;
  }

  return Qnil ;
}
lang= click to toggle source

Set the language of a node, i.e. the values of the xml:lang attribute.

static VALUE
set_lang(VALUE self_rb, VALUE lang_rb)
{
  xmlNodePtr self ;
  xmlChar *lang ;

  Data_Get_Struct(self_rb, xmlNode, self);
  lang = (xmlChar *)StringValueCStr(lang_rb);

  xmlNodeSetLang(self, lang);

  return Qnil ;
}
last_element_child click to toggle source

Returns the last child node of this node that is an element.

Example:

@doc.root.last_element_child.element? # => true
static VALUE
last_element_child(VALUE self)
{
  xmlNodePtr node, child;
  Data_Get_Struct(self, xmlNode, node);

  child = xmlLastElementChild(node);
  if (!child) { return Qnil; }

  return noko_xml_node_wrap(Qnil, child);
}
line click to toggle source

Returns the line for this Node

static VALUE
line(VALUE self)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);

  return INT2NUM(xmlGetLineNo(node));
}
line=(num) click to toggle source

Sets the line for this Node. num must be less than 65535.

static VALUE
set_line(VALUE self, VALUE num)
{
  xmlNodePtr node;
  int value = NUM2INT(num);

  Data_Get_Struct(self, xmlNode, node);
  if (value < 65535) {
    node->line = value;
  }

  return num;
}
matches?(selector) click to toggle source

Returns true if this Node matches selector

# File lib/nokogiri/xml/node.rb, line 782
def matches?(selector)
  ancestors.last.search(selector).include?(self)
end
name
Alias for: node_name
name=(new_name)
Alias for: node_name=
namespace() click to toggle source

returns the namespace of the element or attribute node as a Namespace object, or nil if there is no namespace for the element or attribute.

static VALUE
noko_xml_node_namespace(VALUE rb_node)
{
  xmlNodePtr c_node ;
  Data_Get_Struct(rb_node, xmlNode, c_node);

  if (c_node->ns) {
    return noko_xml_namespace_wrap(c_node->ns, c_node->doc);
  }

  return Qnil ;
}
namespace=(ns) click to toggle source

Set the default namespace on this node (as would be defined with an “xmlns=” attribute in XML source), as a Namespace object ns. Note that a Namespace added this way will NOT be serialized as an xmlns attribute for this node. You probably want default_namespace= instead, or perhaps add_namespace_definition with a nil prefix argument.

# File lib/nokogiri/xml/node.rb, line 332
def namespace=(ns)
  return set_namespace(ns) unless ns

  unless Nokogiri::XML::Namespace === ns
    raise TypeError, "#{ns.class} can't be coerced into Nokogiri::XML::Namespace"
  end
  if ns.document != document
    raise ArgumentError, "namespace must be declared on the same document"
  end

  set_namespace ns
end
namespace_definitions() click to toggle source

returns namespaces defined on self element directly, as an array of Namespace objects. Includes both a default namespace (as in“xmlns=”), and prefixed namespaces (as in “xmlns:prefix=”).

static VALUE
namespace_definitions(VALUE rb_node)
{
  /* this code in the mode of xmlHasProp() */
  xmlNodePtr c_node ;
  xmlNsPtr c_namespace;
  VALUE definitions = rb_ary_new();

  Data_Get_Struct(rb_node, xmlNode, c_node);

  c_namespace = c_node->nsDef;
  if (!c_namespace) {
    return definitions;
  }

  while (c_namespace != NULL) {
    rb_ary_push(definitions, noko_xml_namespace_wrap(c_namespace, c_node->doc));
    c_namespace = c_namespace->next;
  }

  return definitions;
}
namespace_scopes() click to toggle source

returns namespaces in scope for self – those defined on self element directly or any ancestor node – as an array of Namespace objects. Default namespaces (“xmlns=” style) for self are included in this array; Default namespaces for ancestors, however, are not. See also namespaces

static VALUE
namespace_scopes(VALUE rb_node)
{
  xmlNodePtr c_node ;
  xmlNsPtr *namespaces;
  VALUE scopes = rb_ary_new();
  int j;

  Data_Get_Struct(rb_node, xmlNode, c_node);

  namespaces = xmlGetNsList(c_node->doc, c_node);
  if (!namespaces) {
    return scopes;
  }

  for (j = 0 ; namespaces[j] != NULL ; ++j) {
    rb_ary_push(scopes, noko_xml_namespace_wrap(namespaces[j], c_node->doc));
  }

  xmlFree(namespaces);
  return scopes;
}
namespaced_key?(attribute, namespace) click to toggle source

Returns true if attribute is set with namespace

static VALUE
namespaced_key_eh(VALUE self, VALUE attribute, VALUE namespace)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);
  if (xmlHasNsProp(node, (xmlChar *)StringValueCStr(attribute),
                   NIL_P(namespace) ? NULL : (xmlChar *)StringValueCStr(namespace))) {
    return Qtrue;
  }
  return Qfalse;
}
namespaces() click to toggle source

Returns a Hash of +{prefix => value}+ for all namespaces on this node and its ancestors.

This method returns the same namespaces as namespace_scopes.

Returns namespaces in scope for self – those defined on self element directly or any ancestor node – as a Hash of attribute-name/value pairs. Note that the keys in this hash XML attributes that would be used to define this namespace, such as “xmlns:prefix”, not just the prefix. Default namespace set on self will be included with key “xmlns”. However, default namespaces set on ancestor will NOT be, even if self has no explicit default namespace.

# File lib/nokogiri/xml/node.rb, line 863
def namespaces
  namespace_scopes.each_with_object({}) do |ns, hash|
    prefix = ns.prefix
    key = prefix ? "xmlns:#{prefix}" : "xmlns"
    hash[key] = ns.href
  end
end
content= click to toggle source

Set the content for this Node

static VALUE
set_native_content(VALUE self, VALUE content)
{
  xmlNodePtr node, child, next ;
  Data_Get_Struct(self, xmlNode, node);

  child = node->children;
  while (NULL != child) {
    next = child->next ;
    xmlUnlinkNode(child) ;
    noko_xml_document_pin_node(child);
    child = next ;
  }

  xmlNodeSetContent(node, (xmlChar *)StringValueCStr(content));
  return content;
}
Alias for: next_sibling
next=(node_or_tags)
Alias for: add_next_sibling
next_element click to toggle source

Returns the next Nokogiri::XML::Element type sibling node.

static VALUE
next_element(VALUE self)
{
  xmlNodePtr node, sibling;
  Data_Get_Struct(self, xmlNode, node);

  sibling = xmlNextElementSibling(node);
  if (!sibling) { return Qnil; }

  return noko_xml_node_wrap(Qnil, sibling);
}

Returns the next sibling node

static VALUE
next_sibling(VALUE self)
{
  xmlNodePtr node, sibling;
  Data_Get_Struct(self, xmlNode, node);

  sibling = node->next;
  if (!sibling) { return Qnil; }

  return noko_xml_node_wrap(Qnil, sibling) ;
}
Also aliased as: next

Returns the name for this Node

static VALUE
get_name(VALUE self)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);
  if (node->name) {
    return NOKOGIRI_STR_NEW2(node->name);
  }
  return Qnil;
}
Also aliased as: name

Set the name for this Node

static VALUE
set_name(VALUE self, VALUE new_name)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);
  xmlNodeSetName(node, (xmlChar *)StringValueCStr(new_name));
  return new_name;
}
Also aliased as: name=

Get the type for this Node

static VALUE
node_type(VALUE self)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);
  return INT2NUM((long)node->type);
}
Also aliased as: type
parent click to toggle source

Get the parent Node for this Node

static VALUE
get_parent(VALUE self)
{
  xmlNodePtr node, parent;
  Data_Get_Struct(self, xmlNode, node);

  parent = node->parent;
  if (!parent) { return Qnil; }

  return noko_xml_node_wrap(Qnil, parent) ;
}
parent=(parent_node) click to toggle source

Set the parent Node for this Node

# File lib/nokogiri/xml/node.rb, line 311
def parent=(parent_node)
  parent_node.add_child(self)
  parent_node
end
parse(string_or_io, options = nil) { |options| ... } click to toggle source

Parse string_or_io as a document fragment within the context of this node. Returns a XML::NodeSet containing the nodes parsed from string_or_io.

# File lib/nokogiri/xml/node.rb, line 798
def parse(string_or_io, options = nil)
  ##
  # When the current node is unparented and not an element node, use the
  # document as the parsing context instead. Otherwise, the in-context
  # parser cannot find an element or a document node.
  # Document Fragments are also not usable by the in-context parser.
  if !element? && !document? && (!parent || parent.fragment?)
    return document.parse(string_or_io, options)
  end

  options ||= (document.html? ? ParseOptions::DEFAULT_HTML : ParseOptions::DEFAULT_XML)
  if Integer === options
    options = Nokogiri::XML::ParseOptions.new(options)
  end
  # Give the options to the user
  yield options if block_given?

  contents = string_or_io.respond_to?(:read) ?
    string_or_io.read :
    string_or_io

  return Nokogiri::XML::NodeSet.new(document) if contents.empty?

  # libxml2 does not obey the `recover` option after encountering errors during `in_context`
  # parsing, and so this horrible hack is here to try to emulate recovery behavior.
  #
  # Unfortunately, this means we're no longer parsing "in context" and so namespaces that
  # would have been inherited from the context node won't be handled correctly. This hack was
  # written in 2010, and I regret it, because it's silently degrading functionality in a way
  # that's not easily prevented (or even detected).
  #
  # I think preferable behavior would be to either:
  #
  # a. add an error noting that we "fell back" and pointing the user to turning off the `recover` option
  # b. don't recover, but raise a sensible exception
  #
  # For context and background: https://github.com/sparklemotion/nokogiri/issues/313
  # FIXME bug report: https://github.com/sparklemotion/nokogiri/issues/2092
  error_count = document.errors.length
  node_set = in_context(contents, options.to_i)
  if (node_set.empty? && (document.errors.length > error_count))
    if options.recover?
      fragment = Nokogiri::HTML::DocumentFragment.parse contents
      node_set = fragment.children
    else
      raise document.errors[error_count]
    end
  end
  node_set
end
path click to toggle source

Returns the path associated with this Node

static VALUE
noko_xml_node_path(VALUE rb_node)
{
  xmlNodePtr c_node;
  xmlChar *c_path ;
  VALUE rval;

  Data_Get_Struct(rb_node, xmlNode, c_node);

  c_path = xmlGetNodePath(c_node);
  if (c_path == NULL) {
    // see https://github.com/sparklemotion/nokogiri/issues/2250
    // this behavior is clearly undesirable, but is what libxml <= 2.9.10 returned, and so we
    // do this for now to preserve the behavior across libxml2 versions.
    rval = NOKOGIRI_STR_NEW2("?");
  } else {
    rval = NOKOGIRI_STR_NEW2(c_path);
    xmlFree(c_path);
  }

  return rval ;
}
pointer_id click to toggle source

Get the internal pointer number

static VALUE
pointer_id(VALUE self)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);

  return INT2NUM((long)(node));
}
prepend_child(node_or_tags) click to toggle source

Add node_or_tags as the first child of this Node. node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns the reparented node (if node_or_tags is a Node), or NodeSet (if node_or_tags is a DocumentFragment, NodeSet, or string).

Also see related method add_child.

# File lib/nokogiri/xml/node.rb, line 146
def prepend_child(node_or_tags)
  if first = children.first
    # Mimic the error add_child would raise.
    raise RuntimeError, "Document already has a root node" if document? && !(node_or_tags.comment? || node_or_tags.processing_instruction?)
    first.__send__(:add_sibling, :previous, node_or_tags)
  else
    add_child(node_or_tags)
  end
end
Alias for: previous_sibling
previous=(node_or_tags)
previous_element click to toggle source

Returns the previous Nokogiri::XML::Element type sibling node.

static VALUE
previous_element(VALUE self)
{
  xmlNodePtr node, sibling;
  Data_Get_Struct(self, xmlNode, node);

  /*
   *  note that we don't use xmlPreviousElementSibling here because it's buggy pre-2.7.7.
   */
  sibling = node->prev;
  if (!sibling) { return Qnil; }

  while (sibling && sibling->type != XML_ELEMENT_NODE) {
    sibling = sibling->prev;
  }

  return sibling ? noko_xml_node_wrap(Qnil, sibling) : Qnil ;
}

Returns the previous sibling node

static VALUE
previous_sibling(VALUE self)
{
  xmlNodePtr node, sibling;
  Data_Get_Struct(self, xmlNode, node);

  sibling = node->prev;
  if (!sibling) { return Qnil; }

  return noko_xml_node_wrap(Qnil, sibling);
}
Also aliased as: previous
processing_instruction?() click to toggle source

Returns true if this is a ProcessingInstruction node

# File lib/nokogiri/xml/node.rb, line 897
def processing_instruction?
  type == PI_NODE
end
read_only?() click to toggle source

Is this a read only node?

# File lib/nokogiri/xml/node.rb, line 921
def read_only?
  # According to gdome2, these are read-only node types
  [NOTATION_NODE, ENTITY_NODE, ENTITY_DECL].include?(type)
end
Alias for: unlink
remove_attribute(name) click to toggle source

Remove the attribute named name

# File lib/nokogiri/xml/node.rb, line 431
def remove_attribute(name)
  attr = attributes[name].remove if key? name
  clear_xpath_context if Nokogiri.jruby?
  attr
end
Also aliased as: delete
remove_class(names = nil) click to toggle source

Remove HTML CSS classes from a Node. Any CSS classes in names that exist in the Node's class attribute are removed, including any multiple entries.

If no CSS classes remain after this operation, or if names is nil, the class attribute is deleted from the node.

This is a convenience function and is equivalent to:

node.kwattr_remove("class", names)

@see kwattr_remove @see classes @see add_class @see append_class

@param names [String, Array<String>]

CSS class names to be removed from the Node's +class+ attribute. May
be a string containing whitespace-delimited names, or an Array of
String names. Any class names already present will be removed. If no
CSS classes remain, the +class+ attribute is deleted.

@return [Node] Returns self for ease of chaining method calls.

@example

node                         # => <div class="section header"></div>
node.remove_class("section") # => <div class="header"></div>
node.remove_class("header")  # => <div></div> # attribute is deleted when empty
# File lib/nokogiri/xml/node.rb, line 574
def remove_class(names = nil)
  kwattr_remove("class", names)
end
replace(node_or_tags) click to toggle source

Replace this Node with node_or_tags. node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns the reparented node (if node_or_tags is a Node), or NodeSet (if node_or_tags is a DocumentFragment, NodeSet, or string).

Also see related method swap.

# File lib/nokogiri/xml/node.rb, line 266
def replace(node_or_tags)
  raise("Cannot replace a node with no parent") unless parent

  # We cannot replace a text node directly, otherwise libxml will return
  # an internal error at parser.c:13031, I don't know exactly why
  # libxml is trying to find a parent node that is an element or document
  # so I can't tell if this is bug in libxml or not. issue #775.
  if text?
    replacee = Nokogiri::XML::Node.new "dummy", document
    add_previous_sibling_node replacee
    unlink
    return replacee.replace node_or_tags
  end

  node_or_tags = parent.coerce(node_or_tags)

  if node_or_tags.is_a?(XML::NodeSet)
    node_or_tags.each { |n| add_previous_sibling n }
    unlink
  else
    replace_node node_or_tags
  end
  node_or_tags
end
serialize(*args, &block) click to toggle source

Serialize Node using options. Save options can also be set using a block. See SaveOptions.

These two statements are equivalent:

node.serialize(:encoding => 'UTF-8', :save_with => FORMAT | AS_XML)

or

node.serialize(:encoding => 'UTF-8') do |config|
  config.format.as_xml
end
# File lib/nokogiri/xml/node.rb, line 1022
def serialize(*args, &block)
  options = args.first.is_a?(Hash) ? args.shift : {
    :encoding => args[0],
    :save_with => args[1],
  }

  encoding = options[:encoding] || document.encoding
  options[:encoding] = encoding

  outstring = String.new
  outstring.force_encoding(Encoding.find(encoding || "utf-8"))
  io = StringIO.new(outstring)
  write_to io, options, &block
  io.string
end
set_attribute(name, value)
Alias for: []=
swap(node_or_tags) click to toggle source

Swap this Node for node_or_tags node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns self, to support chaining of calls.

Also see related method replace.

# File lib/nokogiri/xml/node.rb, line 298
def swap(node_or_tags)
  replace node_or_tags
  self
end

@!endgroup

Also aliased as: to_str
Alias for: content
text?() click to toggle source

Returns true if this is a Text node

# File lib/nokogiri/xml/node.rb, line 902
def text?
  type == TEXT_NODE
end
to_html(options = {}) click to toggle source

Serialize this Node to HTML

doc.to_html

See Node#write_to for a list of options. For formatted output, use Node#to_xhtml instead.

# File lib/nokogiri/xml/node.rb, line 1045
def to_html(options = {})
  to_format SaveOptions::DEFAULT_HTML, options
end
to_s() click to toggle source

Turn this node in to a string. If the document is HTML, this method returns html. If the document is XML, this method returns XML.

# File lib/nokogiri/xml/node.rb, line 936
def to_s
  document.xml? ? to_xml : to_html
end
to_str()
Alias for: text
to_xhtml(options = {}) click to toggle source

Serialize this Node to XHTML using options

doc.to_xhtml(:indent => 5, :encoding => 'UTF-8')

See Node#write_to for a list of options

# File lib/nokogiri/xml/node.rb, line 1066
def to_xhtml(options = {})
  to_format SaveOptions::DEFAULT_XHTML, options
end
to_xml(options = {}) click to toggle source

Serialize this Node to XML using options

doc.to_xml(:indent => 5, :encoding => 'UTF-8')

See Node#write_to for a list of options

# File lib/nokogiri/xml/node.rb, line 1055
def to_xml(options = {})
  options[:save_with] ||= SaveOptions::DEFAULT_XML
  serialize(options)
end
traverse(&block) click to toggle source

Yields self and all children to block recursively.

# File lib/nokogiri/xml/node.rb, line 978
def traverse(&block)
  children.each { |j| j.traverse(&block) }
  block.call(self)
end
Alias for: node_type
value?(value) click to toggle source

Does this Node's attributes include <value>

# File lib/nokogiri/xml/node.rb, line 411
def value?(value)
  values.include? value
end
values() click to toggle source

Get the attribute values for this Node.

# File lib/nokogiri/xml/node.rb, line 405
def values
  attribute_nodes.map(&:value)
end
wrap(html) click to toggle source

Add html around this node

Returns self

# File lib/nokogiri/xml/node.rb, line 160
def wrap(html)
  new_parent = document.parse(html).first
  add_next_sibling(new_parent)
  new_parent.add_child(self)
  self
end
write_html_to(io, options = {}) click to toggle source

Write Node as HTML to io with options

See Node#write_to for a list of options

# File lib/nokogiri/xml/node.rb, line 1114
def write_html_to(io, options = {})
  write_format_to SaveOptions::DEFAULT_HTML, io, options
end
write_to(io, *options) { |config| ... } click to toggle source

Write Node to io with options. options modify the output of this method. Valid options are:

  • :encoding for changing the encoding

  • :indent_text the indentation text, defaults to one space

  • :indent the number of :indent_text to use, defaults to 2

  • :save_with a combination of SaveOptions constants.

To save with UTF-8 indented twice:

node.write_to(io, :encoding => 'UTF-8', :indent => 2)

To save indented with two dashes:

node.write_to(io, :indent_text => '-', :indent => 2)
# File lib/nokogiri/xml/node.rb, line 1087
def write_to(io, *options)
  options = options.first.is_a?(Hash) ? options.shift : {}
  encoding = options[:encoding] || options[0]
  if Nokogiri.jruby?
    save_options = options[:save_with] || options[1]
    indent_times = options[:indent] || 0
  else
    save_options = options[:save_with] || options[1] || SaveOptions::FORMAT
    indent_times = options[:indent] || 2
  end
  indent_text = options[:indent_text] || " "

  # Any string times 0 returns an empty string. Therefore, use the same
  # string instead of generating a new empty string for every node with
  # zero indentation.
  indentation = indent_times.zero? ? "" : (indent_text * indent_times)

  config = SaveOptions.new(save_options.to_i)
  yield config if block_given?

  native_write_to(io, encoding, indentation, config.options)
end
write_xhtml_to(io, options = {}) click to toggle source

Write Node as XHTML to io with options

See Node#write_to for a list of options

# File lib/nokogiri/xml/node.rb, line 1122
def write_xhtml_to(io, options = {})
  write_format_to SaveOptions::DEFAULT_XHTML, io, options
end
write_xml_to(io, options = {}) click to toggle source

Write Node as XML to io with options

doc.write_xml_to io, :encoding => 'UTF-8'

See Node#write_to for a list of options

# File lib/nokogiri/xml/node.rb, line 1132
def write_xml_to(io, options = {})
  options[:save_with] ||= SaveOptions::DEFAULT_XML
  write_to io, options
end
xml?() click to toggle source

Returns true if this is an XML::Document node

# File lib/nokogiri/xml/node.rb, line 882
def xml?
  type == DOCUMENT_NODE
end

Protected Instance Methods

coerce(data) click to toggle source

@!endgroup

# File lib/nokogiri/xml/node.rb, line 1149
      def coerce(data)
        case data
        when XML::NodeSet
          return data
        when XML::DocumentFragment
          return data.children
        when String
          return fragment(data).children
        when Document, XML::Attr
          # unacceptable
        when XML::Node
          return data
        end

        raise ArgumentError, <<-EOERR
Requires a Node, NodeSet or String argument, and cannot accept a #{data.class}.
(You probably want to select a node from the Document with at() or search(), or create a new Node via Node.new().)
        EOERR
      end

Private Instance Methods

add_child_node_and_reparent_attrs(node) click to toggle source
# File lib/nokogiri/xml/node.rb, line 1228
def add_child_node_and_reparent_attrs(node)
  add_child_node node
  node.attribute_nodes.find_all { |a| a.name =~ /:/ }.each do |attr_node|
    attr_node.remove
    node[attr_node.name] = attr_node.value
  end
end
add_sibling(next_or_previous, node_or_tags) click to toggle source
# File lib/nokogiri/xml/node.rb, line 1182
def add_sibling(next_or_previous, node_or_tags)
  raise("Cannot add sibling to a node with no parent") unless parent

  impl = (next_or_previous == :next) ? :add_next_sibling_node : :add_previous_sibling_node
  iter = (next_or_previous == :next) ? :reverse_each : :each

  node_or_tags = parent.coerce(node_or_tags)
  if node_or_tags.is_a?(XML::NodeSet)
    if text?
      pivot = Nokogiri::XML::Node.new "dummy", document
      send impl, pivot
    else
      pivot = self
    end
    node_or_tags.send(iter) { |n| pivot.send impl, n }
    pivot.unlink if text?
  else
    send impl, node_or_tags
  end
  node_or_tags
end
compare(other) click to toggle source

Compare this Node to other with respect to their Document

static VALUE
compare(VALUE self, VALUE _other)
{
  xmlNodePtr node, other;
  Data_Get_Struct(self, xmlNode, node);
  Data_Get_Struct(_other, xmlNode, other);

  return INT2NUM((long)xmlXPathCmpNodes(other, node));
}
dump_html click to toggle source

Returns the Node as html.

static VALUE
dump_html(VALUE self)
{
  xmlBufferPtr buf ;
  xmlNodePtr node ;
  VALUE html;

  Data_Get_Struct(self, xmlNode, node);

  buf = xmlBufferCreate() ;
  htmlNodeDump(buf, node->doc, node);
  html = NOKOGIRI_STR_NEW2(buf->content);
  xmlBufferFree(buf);
  return html ;
}
get(attribute) click to toggle source

Get the value for attribute

static VALUE
get(VALUE self, VALUE rattribute)
{
  xmlNodePtr node;
  xmlChar *value = 0;
  VALUE rvalue;
  xmlChar *colon;
  xmlChar *attribute, *attr_name, *prefix;
  xmlNsPtr ns;

  if (NIL_P(rattribute)) { return Qnil; }

  Data_Get_Struct(self, xmlNode, node);
  attribute = xmlCharStrdup(StringValueCStr(rattribute));

  colon = (xmlChar *)(uintptr_t)xmlStrchr(attribute, (const xmlChar)':');
  if (colon) {
    /* split the attribute string into separate prefix and name by
     * null-terminating the prefix at the colon */
    prefix = attribute;
    attr_name = colon + 1;
    (*colon) = 0;

    ns = xmlSearchNs(node->doc, node, prefix);
    if (ns) {
      value = xmlGetNsProp(node, attr_name, ns->href);
    } else {
      value = xmlGetProp(node, (xmlChar *)StringValueCStr(rattribute));
    }
  } else {
    value = xmlGetNoNsProp(node, attribute);
  }

  xmlFree((void *)attribute);
  if (!value) { return Qnil; }

  rvalue = NOKOGIRI_STR_NEW2(value);
  xmlFree((void *)value);

  return rvalue ;
}
in_context(p1, p2) click to toggle source

TODO: DOCUMENT ME

static VALUE
in_context(VALUE self, VALUE _str, VALUE _options)
{
  xmlNodePtr node, list = 0, tmp, child_iter, node_children, doc_children;
  xmlNodeSetPtr set;
  xmlParserErrors error;
  VALUE doc, err;
  int doc_is_empty;

  Data_Get_Struct(self, xmlNode, node);

  doc = DOC_RUBY_OBJECT(node->doc);
  err = rb_iv_get(doc, "@errors");
  doc_is_empty = (node->doc->children == NULL) ? 1 : 0;
  node_children = node->children;
  doc_children  = node->doc->children;

  xmlSetStructuredErrorFunc((void *)err, Nokogiri_error_array_pusher);

  /* Twiddle global variable because of a bug in libxml2.
   * http://git.gnome.org/browse/libxml2/commit/?id=e20fb5a72c83cbfc8e4a8aa3943c6be8febadab7
   */
#ifndef HTML_PARSE_NOIMPLIED
  htmlHandleOmittedElem(0);
#endif

  /* This function adds a fake node to the child of +node+.  If the parser
   * does not exit cleanly with XML_ERR_OK, the list is freed.  This can
   * leave the child pointers in a bad state if they were originally empty.
   *
   * http://git.gnome.org/browse/libxml2/tree/parser.c#n13177
   * */
  error = xmlParseInNodeContext(node, StringValuePtr(_str),
                                (int)RSTRING_LEN(_str),
                                (int)NUM2INT(_options), &list);

  /* xmlParseInNodeContext should not mutate the original document or node,
   * so reassigning these pointers should be OK.  The reason we're reassigning
   * is because if there were errors, it's possible for the child pointers
   * to be manipulated. */
  if (error != XML_ERR_OK) {
    node->doc->children = doc_children;
    node->children = node_children;
  }

  /* make sure parent/child pointers are coherent so an unlink will work
   * properly (#331)
   */
  child_iter = node->doc->children ;
  while (child_iter) {
    if (child_iter->parent != (xmlNodePtr)node->doc) {
      child_iter->parent = (xmlNodePtr)node->doc;
    }
    child_iter = child_iter->next;
  }

#ifndef HTML_PARSE_NOIMPLIED
  htmlHandleOmittedElem(1);
#endif

  xmlSetStructuredErrorFunc(NULL, NULL);

  /* Workaround for a libxml2 bug where a parsing error may leave a broken
   * node reference in node->doc->children.
   * This workaround is limited to when a parse error occurs, the document
   * went from having no children to having children, and the context node is
   * part of a document fragment.
   * https://bugzilla.gnome.org/show_bug.cgi?id=668155
   */
  if (error != XML_ERR_OK && doc_is_empty && node->doc->children != NULL) {
    child_iter = node;
    while (child_iter->parent) {
      child_iter = child_iter->parent;
    }

    if (child_iter->type == XML_DOCUMENT_FRAG_NODE) {
      node->doc->children = NULL;
    }
  }

  /* FIXME: This probably needs to handle more constants... */
  switch (error) {
  case XML_ERR_INTERNAL_ERROR:
  case XML_ERR_NO_MEMORY:
    rb_raise(rb_eRuntimeError, "error parsing fragment (%d)", error);
    break;
  default:
    break;
  }

  set = xmlXPathNodeSetCreate(NULL);

  while (list) {
    tmp = list->next;
    list->next = NULL;
    xmlXPathNodeSetAddUnique(set, list);
    noko_xml_document_pin_node(list);
    list = tmp;
  }

  return noko_xml_node_set_wrap(set, doc);
}
inspect_attributes() click to toggle source
# File lib/nokogiri/xml/node.rb, line 1221
def inspect_attributes
  [:name, :namespace, :attribute_nodes, :children]
end
keywordify(keywords) click to toggle source
# File lib/nokogiri/xml/node.rb, line 1171
def keywordify(keywords)
  case keywords
  when Enumerable
    return keywords
  when String
    return keywords.scan(/\S+/)
  else
    raise ArgumentError.new("Keyword attributes must be passed as either a String or an Enumerable, but received #{keywords.class}")
  end
end
native_write_to(io, encoding, options) click to toggle source

Write this Node to io with encoding and options

static VALUE
native_write_to(
  VALUE self,
  VALUE io,
  VALUE encoding,
  VALUE indent_string,
  VALUE options
)
{
  xmlNodePtr node;
  const char *before_indent;
  xmlSaveCtxtPtr savectx;

  Data_Get_Struct(self, xmlNode, node);

  xmlIndentTreeOutput = 1;

  before_indent = xmlTreeIndentString;

  xmlTreeIndentString = StringValueCStr(indent_string);

  savectx = xmlSaveToIO(
              (xmlOutputWriteCallback)noko_io_write,
              (xmlOutputCloseCallback)noko_io_close,
              (void *)io,
              RTEST(encoding) ? StringValueCStr(encoding) : NULL,
              (int)NUM2INT(options)
            );

  xmlSaveTree(savectx, node);
  xmlSaveClose(savectx);

  xmlTreeIndentString = before_indent;
  return io;
}
process_xincludes(options) click to toggle source

Loads and substitutes all xinclude elements below the node. The parser context will be initialized with options.

static VALUE
process_xincludes(VALUE self, VALUE options)
{
  int rcode ;
  xmlNodePtr node;
  VALUE error_list = rb_ary_new();

  Data_Get_Struct(self, xmlNode, node);

  xmlSetStructuredErrorFunc((void *)error_list, Nokogiri_error_array_pusher);
  rcode = xmlXIncludeProcessTreeFlags(node, (int)NUM2INT(options));
  xmlSetStructuredErrorFunc(NULL, NULL);

  if (rcode < 0) {
    xmlErrorPtr error;

    error = xmlGetLastError();
    if (error) {
      rb_exc_raise(Nokogiri_wrap_xml_syntax_error(error));
    } else {
      rb_raise(rb_eRuntimeError, "Could not perform xinclude substitution");
    }
  }

  return self;
}
[]=(property, value) click to toggle source

Set the property to value

static VALUE
set(VALUE self, VALUE property, VALUE value)
{
  xmlNodePtr node, cur;
  xmlAttrPtr prop;
  Data_Get_Struct(self, xmlNode, node);

  /* If a matching attribute node already exists, then xmlSetProp will destroy
   * the existing node's children. However, if Nokogiri has a node object
   * pointing to one of those children, we are left with a broken reference.
   *
   * We can avoid this by unlinking these nodes first.
   */
  if (node->type != XML_ELEMENT_NODE) {
    return (Qnil);
  }
  prop = xmlHasProp(node, (xmlChar *)StringValueCStr(property));
  if (prop && prop->children) {
    for (cur = prop->children; cur; cur = cur->next) {
      if (cur->_private) {
        noko_xml_document_pin_node(cur);
        xmlUnlinkNode(cur);
      }
    }
  }

  xmlSetProp(node, (xmlChar *)StringValueCStr(property),
             (xmlChar *)StringValueCStr(value));

  return value;
}
set_namespace(namespace) click to toggle source

Set the namespace to namespace

static VALUE
set_namespace(VALUE self, VALUE namespace)
{
  xmlNodePtr node;
  xmlNsPtr ns = NULL;

  Data_Get_Struct(self, xmlNode, node);

  if (!NIL_P(namespace)) {
    Data_Get_Struct(namespace, xmlNs, ns);
  }

  xmlSetNs(node, ns);

  return self;
}
to_format(save_option, options) click to toggle source
# File lib/nokogiri/xml/node.rb, line 1207
def to_format(save_option, options)
  return dump_html if USING_LIBXML_WITH_BROKEN_SERIALIZATION

  options[:save_with] = save_option unless options[:save_with]
  serialize(options)
end
write_format_to(save_option, io, options) click to toggle source
# File lib/nokogiri/xml/node.rb, line 1214
def write_format_to(save_option, io, options)
  return (io << dump_html) if USING_LIBXML_WITH_BROKEN_SERIALIZATION

  options[:save_with] ||= save_option
  write_to io, options
end