Skip to content

Commit

Permalink
Merge pull request #644 from sparklemotion/flavorjones-fix-bad-encodi…
Browse files Browse the repository at this point in the history
…ng-detection

fix: correctly recognize encoding errors in libxml >= 2.12.0
  • Loading branch information
flavorjones authored Jun 12, 2024
2 parents 7834210 + 11d668a commit c38962c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/mechanize/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def encoding_error?(parser=nil)
return false if parser.errors.empty?
parser.errors.any? do |error|
error.message.scrub =~ /(indicate\ encoding)|
(Invalid\ bytes)|
(Invalid\ char)|
(input\ conversion\ failed)/x
end
Expand Down
6 changes: 2 additions & 4 deletions test/test_mechanize_page_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,12 @@ def test_encoding_charset_after_title_bad

# https://gitlab.gnome.org/GNOME/libxml2/-/issues/543
skip if Nokogiri.uses_libxml?([">= 2.11.0", "< 2.12.0"])
expected_encoding = Nokogiri.uses_libxml?("< 2.11.0") ? 'UTF-8' : 'Shift_JIS'

page = util_page UTF8.dup

assert_equal false, page.encoding_error?

assert_equal expected_encoding, page.encoding
assert_equal "UTF-8", page.encoding
end

def test_encoding_charset_after_title_double_bad
Expand All @@ -138,7 +137,6 @@ def test_encoding_charset_bad

# https://gitlab.gnome.org/GNOME/libxml2/-/issues/543
skip if Nokogiri.uses_libxml?([">= 2.11.0", "< 2.12.0"])
expected_encoding = Nokogiri.uses_libxml?("< 2.11.0") ? 'UTF-8' : 'Shift_JIS'

page = util_page(+"<title>#{UTF8_TITLE}</title>")
page.encodings.replace %w[
Expand All @@ -148,7 +146,7 @@ def test_encoding_charset_bad

assert_equal false, page.encoding_error?

assert_equal expected_encoding, page.encoding
assert_equal 'UTF-8', page.encoding
end

def test_encoding_meta_charset
Expand Down

0 comments on commit c38962c

Please sign in to comment.