class Nokogiri::XML::Node

def add_sibling(next_or_previous, node_or_tags)

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