From eebfda03a041f5dce2e73f1c7e09c8af61d686b5 Mon Sep 17 00:00:00 2001 From: John Shahid Date: Wed, 1 May 2013 22:28:26 -0400 Subject: [PATCH] Don't throw an exception on empty string io on JRuby. Fix #883. --- lib/nokogiri/xml/document.rb | 9 ++++++++- test/xml/test_document.rb | 5 +++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/nokogiri/xml/document.rb b/lib/nokogiri/xml/document.rb index 57c6049b722..87445542fd5 100644 --- a/lib/nokogiri/xml/document.rb +++ b/lib/nokogiri/xml/document.rb @@ -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 @@ -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 diff --git a/test/xml/test_document.rb b/test/xml/test_document.rb index 7604daf85e8..29a1ec94add 100644 --- a/test/xml/test_document.rb +++ b/test/xml/test_document.rb @@ -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')