Skip to content

Commit e9ac292

Browse files
committed
Fix XML::DocumentFragment to return an instance of callee's class
Fixes #1846. Bug introduced in f2f3a3b.
1 parent ab40787 commit e9ac292

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# 1.9.1 / 2018-12-17
2+
3+
## Bug fixes
4+
5+
* 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]
6+
7+
18
# 1.9.0 / 2018-12-17
29

310
## Security Notes

lib/nokogiri/xml/document_fragment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def initialize document, tags = nil, ctx = nil
2828
if Nokogiri.uses_libxml?
2929
def dup
3030
new_document = document.dup
31-
new_fragment = XML::DocumentFragment.new(new_document)
31+
new_fragment = self.class.new(new_document)
3232
children.each do |child|
3333
child.dup(1, new_document).parent = new_fragment
3434
end

test/html/test_document_fragment.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,13 @@ def test_capturing_nonparse_errors_during_node_copy_between_fragments
305305
assert_equal original_errors1, frag1.errors
306306
assert_equal original_errors2, frag2.errors
307307
end
308+
309+
def test_dup_should_create_an_html_document_fragment
310+
# https://github.com/sparklemotion/nokogiri/issues/1846
311+
original = Nokogiri::HTML::DocumentFragment.parse("<div><p>hello</p></div>")
312+
duplicate = original.dup
313+
assert_instance_of Nokogiri::HTML::DocumentFragment, duplicate
314+
end
308315
end
309316
end
310317
end

test/xml/test_document_fragment.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,20 @@ def test_issue_1077_parsing_of_frozen_strings
263263
Nokogiri::XML::DocumentFragment.parse(input) # assert_nothing_raised
264264
end
265265

266+
def test_dup_should_exist_in_a_new_document
267+
# https://github.com/sparklemotion/nokogiri/issues/1063
268+
original = Nokogiri::XML::DocumentFragment.parse("<div><p>hello</p></div>")
269+
duplicate = original.dup
270+
assert_not_equal original.document, duplicate.document
271+
end
272+
273+
def test_dup_should_create_an_xml_document_fragment
274+
# https://github.com/sparklemotion/nokogiri/issues/1846
275+
original = Nokogiri::XML::DocumentFragment.parse("<div><p>hello</p></div>")
276+
duplicate = original.dup
277+
assert_instance_of Nokogiri::XML::DocumentFragment, duplicate
278+
end
279+
266280
def test_dup_creates_tree_with_identical_structure
267281
original = Nokogiri::XML::DocumentFragment.parse("<div><p>hello</p></div>")
268282
duplicate = original.dup

0 commit comments

Comments
 (0)