1
0
Fork 0
mirror of https://github.com/rails/execjs synced 2023-03-27 23:21:20 -04:00

Merge pull request #111 from casperisfine/better-encoding-check

Improve `Encoding` handling
This commit is contained in:
Jean Boussier 2022-02-17 15:54:31 +01:00 committed by GitHub
commit e6db400607
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,15 +5,15 @@ module ExecJS
# workaround for jruby bug http://jira.codehaus.org/browse/JRUBY-6588 # workaround for jruby bug http://jira.codehaus.org/browse/JRUBY-6588
# workaround for rbx bug https://github.com/rubinius/rubinius/issues/1729 # workaround for rbx bug https://github.com/rubinius/rubinius/issues/1729
def encode(string) def encode(string)
if string.encoding.name == 'ASCII-8BIT' if string.encoding == ::Encoding::BINARY
data = string.dup data = string.dup
data.force_encoding('UTF-8') data.force_encoding(::Encoding::UTF_8)
unless data.valid_encoding? unless data.valid_encoding?
raise ::Encoding::UndefinedConversionError, "Could not encode ASCII-8BIT data #{string.dump} as UTF-8" raise ::Encoding::UndefinedConversionError, "Could not encode binary data #{string.dump} as UTF-8"
end end
else else
data = string.encode('UTF-8') data = string.encode(::Encoding::UTF_8)
end end
data data
end end