Ticket #63 (closed defect: fixed)
insert_after(html) reverse the order of elements if more that one.
| Reported by: | stjernstrom | Owned by: | why |
|---|---|---|---|
| Priority: | minor | Milestone: | |
| Component: | lib/hpricot | Version: | |
| Keywords: | Cc: | mathias@… |
Description
insert_after(html) reverse the order of elements if html contains more that one element.
Change the looping of the Array to do it reverse solves the problem.
nodes.reverse_each instead of nodes.each
def insert_after(nodes, ele)
case nodes when Array
nodes.reverse_each { |n| insert_after(n, ele) }
else
reparent nodes idx = children.index(ele) children[idx ? idx + 1: children.length, 0] = nodes
end
end
Example code that does not work
doc = Hpricot('<html><body><div id="a-div"></div></body></html>'); doc.search('div').each { |element|
element.after('<p>Paragraph 1</p><p>Paragraph 2</p>') }
puts doc.to_html
>> <html><body><div id="a-div"></div><p>Paragraph 2</p><p>Paragraph 1</p></body></html>
