Skip to content

Commit

Permalink
Ensure SyntaxError on empty XML in strict mode
Browse files Browse the repository at this point in the history
Previously raised RuntimeError in MRI for zero-length docs.

Fixes #1349. Related to #1005 and 2c9b7f1
  • Loading branch information
flavorjones committed Dec 17, 2015
1 parent db38f64 commit aece1ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/nokogiri/xml/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ def self.parse string_or_io, url = nil, encoding = nil, options = ParseOptions::
# Give the options to the user
yield options if block_given?

return new if !options.strict? && empty_doc?(string_or_io)
if empty_doc?(string_or_io)
if options.strict?
raise Nokogiri::XML::SyntaxError.new("Empty document")
else
return new
end
end

doc = if string_or_io.respond_to?(:read)
url ||= string_or_io.respond_to?(:path) ? string_or_io.path : nil
Expand Down
9 changes: 7 additions & 2 deletions test/xml/test_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ def test_dtd_with_empty_internal_subset

# issue #1005
def test_strict_parsing_empty_doc_should_raise_exception
assert_raises(SyntaxError) do
Nokogiri::XML(StringIO.new('')) { |c| c.strict }
["", " "].each do |empty_string|
assert_raises(SyntaxError, "empty string '#{empty_string}' should raise a SyntaxError") do
Nokogiri::XML(empty_string) { |c| c.strict }
end
assert_raises(SyntaxError, "StringIO of '#{empty_string}' should raise a SyntaxError") do
Nokogiri::XML(StringIO.new(empty_string)) { |c| c.strict }
end
end
end

Expand Down

0 comments on commit aece1ef

Please sign in to comment.