Thanks Pesto for your almost correct solution.
The working solution is:
node.children.first.add_previous_sibling(span_node)
You can use NodeSet#before like this:
node.children.before(span_node)
NodeSet#before
is a short-cut method to insert an item as the first element. A more general-purpose solution is to use Node#before and Node#after. For example, some_node.before foo
will add the node foo
as the sibling directly before some_node
. Node#after
is similar. Note that this:
node.children.first.before(span_node)
is thus equivalent to the solution above.
node.prepend_child span_node
Nokogiri::XML::Node#prepend_child Documentation