diff options
author | Cliff Pruitt <cliff.pruitt@cliffpruitt.com> | 2019-07-17 14:12:41 -0400 |
---|---|---|
committer | Cliff Pruitt <cliff.pruitt@cliffpruitt.com> | 2019-07-26 12:18:01 -0400 |
commit | 5b0a805c6a9f22818c17ddd5641b9480ea1d668d (patch) | |
tree | 7df4e584683b61246e35e93292a263f460c10e7f /activesupport | |
parent | 41bc4c6207147a5eeafa323d763e66c06e61cacf (diff) | |
download | rails-5b0a805c6a9f22818c17ddd5641b9480ea1d668d.tar.gz rails-5b0a805c6a9f22818c17ddd5641b9480ea1d668d.tar.bz2 rails-5b0a805c6a9f22818c17ddd5641b9480ea1d668d.zip |
Raise errors for ASCII-8BIT encoding in ActiveSupport::Inflector::transliterate
Adds ArgumentErrors to `ActiveSupport::Inflector::transliterate` if a string is with ASCII-8BIT which will raise an error in `unicode_normalize`.
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/inflector/transliterate.rb | 4 | ||||
-rw-r--r-- | activesupport/test/transliterate_test.rb | 8 |
2 files changed, 12 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/inflector/transliterate.rb b/activesupport/lib/active_support/inflector/transliterate.rb index ec6e9ccb59..61651ba101 100644 --- a/activesupport/lib/active_support/inflector/transliterate.rb +++ b/activesupport/lib/active_support/inflector/transliterate.rb @@ -56,8 +56,12 @@ module ActiveSupport # # transliterate('Jürgen', locale: :de) # # => "Juergen" + # + # Transliteration of ASCII-8BIT / BINARY strings is not + # supported and will raise an ArgumentError. def transliterate(string, replacement = "?", locale: nil) raise ArgumentError, "Can only transliterate strings. Received #{string.class.name}" unless string.is_a?(String) + raise ArgumentError, "Can not transliterate strings with ASCII-8BIT encoding" if string.encoding == ::Encoding::ASCII_8BIT I18n.transliterate( ActiveSupport::Multibyte::Unicode.tidy_bytes(string).unicode_normalize(:nfc), diff --git a/activesupport/test/transliterate_test.rb b/activesupport/test/transliterate_test.rb index 9e29a93ea0..620bb20305 100644 --- a/activesupport/test/transliterate_test.rb +++ b/activesupport/test/transliterate_test.rb @@ -57,4 +57,12 @@ class TransliterateTest < ActiveSupport::TestCase end assert_equal "Can only transliterate strings. Received Object", exception.message end + + def test_transliterate_handles_ascci_8bit_strings + ascii_8bit_string = "A".b + exception = assert_raises ArgumentError do + ActiveSupport::Inflector.transliterate(ascii_8bit_string) + end + assert_equal "Can not transliterate strings with ASCII-8BIT encoding", exception.message + end end |