Skip to content

Commit

Permalink
Fix XML::DocumentFragment to return an instance of callee's class
Browse files Browse the repository at this point in the history
Fixes #1846. Bug introduced in f2f3a3b.
  • Loading branch information
flavorjones committed Dec 18, 2018
1 parent ab40787 commit e9ac292
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 1.9.1 / 2018-12-17

## Bug fixes

* Fix a bug introduced in v1.9.0 where `XML::DocumentFragment#dup` no longer returned an instance of the callee's class, instead always returning an `XML::DocumentFragment`. This notably broke any subclass of `XML::DocumentFragment` including `HTML::DocumentFragment` as well as the Loofah gem's `Loofah::HTML::DocumentFragment`. [#1846]


# 1.9.0 / 2018-12-17

## Security Notes
Expand Down
2 changes: 1 addition & 1 deletion lib/nokogiri/xml/document_fragment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def initialize document, tags = nil, ctx = nil
if Nokogiri.uses_libxml?
def dup
new_document = document.dup
new_fragment = XML::DocumentFragment.new(new_document)
new_fragment = self.class.new(new_document)
children.each do |child|
child.dup(1, new_document).parent = new_fragment
end
Expand Down
7 changes: 7 additions & 0 deletions test/html/test_document_fragment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,13 @@ def test_capturing_nonparse_errors_during_node_copy_between_fragments
assert_equal original_errors1, frag1.errors
assert_equal original_errors2, frag2.errors
end

def test_dup_should_create_an_html_document_fragment
# https://github.com/sparklemotion/nokogiri/issues/1846
original = Nokogiri::HTML::DocumentFragment.parse("<div><p>hello</p></div>")
duplicate = original.dup
assert_instance_of Nokogiri::HTML::DocumentFragment, duplicate
end
end
end
end
14 changes: 14 additions & 0 deletions test/xml/test_document_fragment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,20 @@ def test_issue_1077_parsing_of_frozen_strings
Nokogiri::XML::DocumentFragment.parse(input) # assert_nothing_raised
end

def test_dup_should_exist_in_a_new_document
# https://github.com/sparklemotion/nokogiri/issues/1063
original = Nokogiri::XML::DocumentFragment.parse("<div><p>hello</p></div>")
duplicate = original.dup
assert_not_equal original.document, duplicate.document
end

def test_dup_should_create_an_xml_document_fragment
# https://github.com/sparklemotion/nokogiri/issues/1846
original = Nokogiri::XML::DocumentFragment.parse("<div><p>hello</p></div>")
duplicate = original.dup
assert_instance_of Nokogiri::XML::DocumentFragment, duplicate
end

def test_dup_creates_tree_with_identical_structure
original = Nokogiri::XML::DocumentFragment.parse("<div><p>hello</p></div>")
duplicate = original.dup
Expand Down

0 comments on commit e9ac292

Please sign in to comment.