Skip to content

Commit

Permalink
Don't throw an exception on empty string io on JRuby. Fix #883.
Browse files Browse the repository at this point in the history
  • Loading branch information
jvshahid committed May 2, 2013
1 parent 8683752 commit eebfda0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/nokogiri/xml/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +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 empty_doc?(string_or_io)

doc = if string_or_io.respond_to?(:read)
url ||= string_or_io.respond_to?(:path) ? string_or_io.path : nil
read_io(string_or_io, url, encoding, options.to_i)
else
# read_memory pukes on empty docs
return new if string_or_io.nil? or string_or_io.empty?
read_memory(string_or_io, url, encoding, options.to_i)
end

Expand Down Expand Up @@ -260,6 +261,12 @@ def to_java
end

private
def self.empty_doc? string_or_io
string_or_io.nil? ||
(string_or_io.respond_to?(:empty?) && string_or_io.empty?) ||
(string_or_io.respond_to?(:eof?) && string_or_io.eof?)
end

def implied_xpath_context
"/"
end
Expand Down
5 changes: 5 additions & 0 deletions test/xml/test_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,11 @@ def test_parse_can_take_io
assert set.length > 0
end

def test_parsing_empty_io
doc = Nokogiri::XML.parse(StringIO.new(''))
refute_nil doc
end

def test_search_on_empty_documents
doc = Nokogiri::XML::Document.new
ns = doc.search('//foo')
Expand Down

1 comment on commit eebfda0

@jcoyne
Copy link

@jcoyne jcoyne commented on eebfda0 May 2, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Please sign in to comment.