# do nothing here
end
end
- # removes invalid UTF8 sequences
- begin
- Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3]
- rescue Iconv::InvalidEncoding
- # "UTF-8//IGNORE" is not supported on some OS
- str
+ if str.respond_to?(:force_encoding)
+ str.force_encoding('UTF-8')
+ if ! str.valid_encoding?
+ str = str.encode("US-ASCII", :invalid => :replace,
+ :undef => :replace, :replace => '?').encode("UTF-8")
+ end
+ else
+ # removes invalid UTF8 sequences
+ begin
+ str = Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3]
+ rescue Iconv::InvalidEncoding
+ # "UTF-8//IGNORE" is not supported on some OS
+ end
end
+ str
end
end
def test_invalid_utf8_sequences_in_comments_should_be_stripped
with_settings :commit_logs_encoding => 'UTF-8' do
c = Changeset.new
- c.comments = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
- assert_equal "Texte encod en ISO-8859-1.", c.comments
+ str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
+ c.comments = str
+ if str.respond_to?(:force_encoding)
+ assert_equal "Texte encod? en ISO-8859-1.", c.comments
+ else
+ assert_equal "Texte encod en ISO-8859-1.", c.comments
+ end
end
end