Method: Nokogiri::XML::Node#prepend_child

Defined in:
lib/nokogiri/xml/node.rb

#prepend_child(node_or_tags) ⇒ Object

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.



168
169
170
171
172
173
174
175
176
177
# File 'lib/nokogiri/xml/node.rb', line 168

def prepend_child(node_or_tags)
  if (first = children.first)
    # Mimic the error add_child would raise.
    raise "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