Skip to content

Commit ef84e69

Browse files
Kristopher Muratajeremy
authored andcommitted
Parameterize should accept malformed utf8 characters [#4323 state:resolved]
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
1 parent 98b4424 commit ef84e69

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

activesupport/lib/active_support/inflector/transliterate.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# encoding: utf-8
22
require 'iconv'
3+
require 'kconv'
34
require 'active_support/core_ext/string/multibyte'
45

56
module ActiveSupport
@@ -44,6 +45,8 @@ def transliterate(string)
4445
# <%= link_to(@person.name, person_path(@person)) %>
4546
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
4647
def parameterize(string, sep = '-')
48+
# remove malformed utf8 characters
49+
string = string.toutf8 unless string.is_utf8?
4750
# replace accented chars with their ascii equivalents
4851
parameterized_string = transliterate(string)
4952
# Turn unwanted chars into the separator
@@ -58,4 +61,4 @@ def parameterize(string, sep = '-')
5861
parameterized_string.downcase
5962
end
6063
end
61-
end
64+
end

activesupport/test/inflector_test_cases.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ module InflectorTestCases
159159
"Trailing bad characters!@#" => "trailing-bad-characters",
160160
"!@#Leading bad characters" => "leading-bad-characters",
161161
"Squeeze separators" => "squeeze-separators",
162-
"Test with + sign" => "test-with-sign"
162+
"Test with + sign" => "test-with-sign",
163+
"Test with malformed utf8 \251" => "test-with-malformed-utf8"
163164
}
164165

165166
StringToParameterizeWithNoSeparator = {
@@ -168,7 +169,8 @@ module InflectorTestCases
168169
"Trailing bad characters!@#" => "trailingbadcharacters",
169170
"!@#Leading bad characters" => "leadingbadcharacters",
170171
"Squeeze separators" => "squeezeseparators",
171-
"Test with + sign" => "testwithsign"
172+
"Test with + sign" => "testwithsign",
173+
"Test with malformed utf8 \251" => "testwithmalformedutf8"
172174
}
173175

174176
StringToParameterizeWithUnderscore = {
@@ -177,12 +179,14 @@ module InflectorTestCases
177179
"Trailing bad characters!@#" => "trailing_bad_characters",
178180
"!@#Leading bad characters" => "leading_bad_characters",
179181
"Squeeze separators" => "squeeze_separators",
180-
"Test with + sign" => "test_with_sign"
182+
"Test with + sign" => "test_with_sign",
183+
"Test with malformed utf8 \251" => "test_with_malformed_utf8"
181184
}
182185

183186
StringToParameterizedAndNormalized = {
184187
"Malmö" => "malmo",
185-
"Garçons" => "garcons"
188+
"Garçons" => "garcons",
189+
"Ops\331" => "ops"
186190
}
187191

188192
UnderscoreToHuman = {

0 commit comments

Comments
 (0)