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

parse_io should respect @encoding when no encoding is passed to it #757

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/nokogiri/xml/sax/parser.rb
Expand Up @@ -68,8 +68,7 @@ class Attribute < Struct.new(:localname, :prefix, :uri, :value)

# Create a new Parser with +doc+ and +encoding+
def initialize doc = Nokogiri::XML::SAX::Document.new, encoding = 'UTF-8'
check_encoding(encoding)
@encoding = encoding
@encoding = check_encoding(encoding)
@document = doc
@warned = false
end
Expand All @@ -87,9 +86,9 @@ def parse thing, &block

###
# Parse given +io+
def parse_io io, encoding = 'ASCII'
check_encoding(encoding)
@encoding = encoding
def parse_io io, encoding = nil
encoding ||= @encoding
@encoding = check_encoding(encoding)
ctx = ParserContext.io(io, ENCODINGS[encoding])
yield ctx if block_given?
ctx.parse_with self
Expand All @@ -115,6 +114,7 @@ def parse_memory data
private
def check_encoding(encoding)
raise ArgumentError.new("'#{encoding}' is not a valid encoding") unless ENCODINGS[encoding]
encoding
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions test/xml/sax/test_parser.rb
Expand Up @@ -159,6 +159,15 @@ def test_parser_sets_encoding
assert_equal 'UTF-8', parser.encoding
end

def test_parser_respects_encoding_ivar
doc = Doc.new
parser = XML::SAX::Parser.new doc, 'SHIFT-JIS'
File.open(SHIFT_JIS_XML) do |io|
parser.parse(io)
assert_includes(parser.document.data, 'こんにちは')
end
end

def test_errors_set_after_parsing_bad_dom
doc = Nokogiri::XML('<foo><bar></foo>')
assert doc.errors
Expand Down