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

Fix a problem that parse exception message can't be generated for invalid encoding XML #123

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/rexml/parseexception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def to_s
err << "\nLine: #{line}\n"
err << "Position: #{position}\n"
err << "Last 80 unconsumed characters:\n"
err.force_encoding("ASCII-8BIT")
err << @source.buffer[0..80].force_encoding("ASCII-8BIT").gsub(/\n/, ' ')
end

Expand Down
13 changes: 13 additions & 0 deletions test/parse/test_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ def test_empty_namespace_attribute_name
DETAIL
end

def test_empty_namespace_attribute_name_with_utf8_character
exception = assert_raise(REXML::ParseException) do
parse("<x :\xE2\x80\x8B>") # U+200B ZERO WIDTH SPACE
end
assert_equal(<<-DETAIL.chomp.force_encoding("ASCII-8BIT"), exception.to_s)
Invalid attribute name: <:\xE2\x80\x8B>
Line: 1
Position: 8
Last 80 unconsumed characters:
:\xE2\x80\x8B>
DETAIL
end

def test_garbage_less_than_before_root_element_at_line_start
exception = assert_raise(REXML::ParseException) do
parse("<\n<x/>")
Expand Down
Loading