Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behavior of Nokogiri 1.5.0 Java #620

Closed
rubiii opened this issue Feb 17, 2012 · 1 comment
Closed

Inconsistent behavior of Nokogiri 1.5.0 Java #620

rubiii opened this issue Feb 17, 2012 · 1 comment

Comments

@rubiii
Copy link

rubiii commented Feb 17, 2012

Running the following code:

require "rubygems"
require "nokogiri"

class Document < Nokogiri::XML::SAX::Document; end

document = Document.new
parser = Nokogiri::XML::SAX::Parser.new document
parser.parse(" ")

results in the following error:

RuntimeError: couldn't parse document: Premature end of file.
    parse_with at nokogiri/XmlSaxParserContext.java:252
  parse_memory at /Users/me/.rvm/gems/jruby-1.6.5/gems/nokogiri-1.5.0-java/lib/nokogiri/xml/sax/parser.rb:110
         parse at /Users/me/.rvm/gems/jruby-1.6.5/gems/nokogiri-1.5.0-java/lib/nokogiri/xml/sax/parser.rb:83
        (root) at /Users/me/gh/nokogiri_java_test.rb:8

This does not happen with Nokogiri 1.4.x (JRuby) and Nokogiri 1.5.0 (MRI).
Noticed this while running the specs for a project of mine.

@yokolet
Copy link
Member

yokolet commented Feb 19, 2012

I fixed the issue in rev. f325a09

This is not a simple bug though it looks really simple. The reason is Nokogiri accepts various types of inputs, also Java API doesn't have any method to know how many bytes are read during parsing. There's no consistent way to know a given document has whitespace(s) only or empty document. Java parser raises the same exception for both whitespace(s) only and empty documents. Only when given input is a string, Nokogiri counts the size before parsing. If the size is less than 1, Nokogiri raises exception. So, this reported case, won't raise exception.

@yokolet yokolet closed this as completed Feb 19, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants