def insert_after( new_child, reference_child=nil )
return new_child if reference_child ? ( reference_child.next_sibling == new_child ) : ( new_child == @child_tags.first )
walker = self
while walker
raise "#{new_child.inspect} cannot be added under #{self.inspect}, because it is an ancestor of it!" if walker==new_child
walker = walker.parent_tag
end
new_child.parent_tag.remove_child( new_child ) if new_child.parent_tag
if reference_child
new_idx = @child_tags.index( reference_child )
raise "#{reference_child.inspect} is not a child of #{self.inspect}" unless new_idx
new_idx += 1
else
new_idx = 0
end
new_child.parent_tag = self
succ = @child_tags[ new_idx ]
@child_tags.insert( new_idx, new_child )
new_child.previous_sibling = reference_child
reference_child.next_sibling = new_child if reference_child
new_child.next_sibling = succ
succ.previous_sibling = new_child if succ
new_child
end