Skip to content

Commit

Permalink
XML::Node#{replace,add_previous_sibling,add_next_sibling} edge cases …
Browse files Browse the repository at this point in the history
…fixed related to libxml's text node merging. closes #308
  • Loading branch information
flavorjones committed Aug 30, 2010
1 parent 79982d0 commit 7540988
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* {XML,HTML}::DocumentFragment.{new,parse} no longer strip leading and trailing whitespace. #319
* XML::Node#{add_child,add_previous_sibling,add_next_sibling,replace} return a NodeSet when passed a string.
* Unclosed tags parsed more robustly in fragments. #315
* XML::Node#{replace,add_previous_sibling,add_next_sibling} edge cases fixed related to libxml's text node merging. #308

=== 1.4.3 / 2010/07/28

Expand Down
29 changes: 23 additions & 6 deletions lib/nokogiri/xml/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,14 @@ def add_child node_or_tags
def add_previous_sibling node_or_tags
node_or_tags = coerce(node_or_tags)
if node_or_tags.is_a?(XML::NodeSet)
node_or_tags.each { |n| add_previous_sibling_node n }
if text?
pivot = Nokogiri::XML::Node.new 'dummy', document
add_previous_sibling_node pivot
else
pivot = self
end
node_or_tags.each { |n| pivot.send :add_previous_sibling_node, n }
pivot.unlink if text?
else
add_previous_sibling_node node_or_tags
end
Expand All @@ -272,11 +279,14 @@ def add_previous_sibling node_or_tags
def add_next_sibling node_or_tags
node_or_tags = coerce(node_or_tags)
if node_or_tags.is_a?(XML::NodeSet)
if '1.8.6' == RUBY_VERSION
node_or_tags.reverse.each { |n| add_next_sibling_node n }
if text?
pivot = Nokogiri::XML::Node.new 'dummy', document
add_next_sibling_node pivot
else
node_or_tags.reverse_each { |n| add_next_sibling_node n }
pivot = self
end
node_or_tags.reverse.each { |n| pivot.send :add_next_sibling_node, n }
pivot.unlink if text?
else
add_next_sibling_node node_or_tags
end
Expand Down Expand Up @@ -347,8 +357,15 @@ def children= node_or_tags
def replace node_or_tags
node_or_tags = coerce(node_or_tags)
if node_or_tags.is_a?(XML::NodeSet)
node_or_tags.each { |n| add_previous_sibling n }
unlink
if text?
replacee = Nokogiri::XML::Node.new 'dummy', document
add_previous_sibling_node replacee
unlink
else
replacee = self
end
node_or_tags.each { |n| replacee.add_previous_sibling n }
replacee.unlink
else
replace_node node_or_tags
end
Expand Down
27 changes: 27 additions & 0 deletions test/xml/test_node_reparenting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,34 @@ class TestNodeReparenting < Nokogiri::TestCase
end
end

describe "#add_previous_sibling" do
it "should not merge text nodes during the operation" do
xml = Nokogiri::XML %Q(<root>text node</root>)
replacee = xml.root.children.first
replacee.add_previous_sibling "foo <p></p> bar"
assert_equal "foo <p></p> bartext node", xml.root.children.to_html
end
end

describe "#add_next_sibling" do
it "should not merge text nodes during the operation" do
xml = Nokogiri::XML %Q(<root>text node</root>)
replacee = xml.root.children.first
replacee.add_next_sibling "foo <p></p> bar"
assert_equal "text nodefoo <p></p> bar", xml.root.children.to_html
end
end

describe "#replace" do
describe "a text node with a text node" do
it "should not merge text nodes during the operation" do
xml = Nokogiri::XML %Q(<root>text node</root>)
replacee = xml.root.children.first
replacee.replace "new text node"
assert_equal "new text node", xml.root.children.first.content
end
end

describe "when a document has a default namespace" do
before do
@fruits = Nokogiri::XML(<<-eoxml)
Expand Down

0 comments on commit 7540988

Please sign in to comment.