Skip to content

Commit

Permalink
Avoid Encoding::CompatibilityError when handling binary column
Browse files Browse the repository at this point in the history
  • Loading branch information
bcarreno committed Aug 24, 2015
1 parent 2d58624 commit 512ff79
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/strip_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ def self.strip_string(value, options = nil)
# U+200D ZERO WIDTH JOINER
# U+2060 WORD JOINER
# U+FEFF ZERO WIDTH NO-BREAK SPACE
if value.respond_to?(:gsub!)
value.gsub!(/\A[[:space:]\u180E\u200B\u200C\u200D\u2060\uFEFF]+|[[:space:]\u180E\u200B\u200C\u200D\u2060\uFEFF]+\z/, '')
regex = /\A[[:space:]\u180E\u200B\u200C\u200D\u2060\uFEFF]+|[[:space:]\u180E\u200B\u200C\u200D\u2060\uFEFF]+\z/
if value.respond_to?(:gsub!) && Encoding.compatible?(value, regex)
value.gsub!(regex, '')
end
elsif value.respond_to?(:strip!)
value.strip!
Expand Down
3 changes: 3 additions & 0 deletions test/strip_attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,10 @@ def test_should_strip_unicode
return if "\u0020" != " "
# U200A - HAIR SPACE
# U200B - ZERO WIDTH SPACE
# U20AC - EURO SIGN

assert_equal "foo", StripAttributes.strip("\u200A\u200B foo\u200A\u200B ")
assert_equal "foo\u20AC".force_encoding("ASCII-8BIT"), StripAttributes.strip("foo\u20AC ".force_encoding("ASCII-8BIT"))
end
end
end

0 comments on commit 512ff79

Please sign in to comment.