##// END OF EJS Templates
scm: for log in Ruby 1.9, replace invalid UTF-8 to '?' instead of removing....
Toshi MARUYAMA -
r4806:87a6d79b5be7
parent child
Show More
@@ -253,12 +253,20 class Changeset < ActiveRecord::Base
253 # do nothing here
253 # do nothing here
254 end
254 end
255 end
255 end
256 # removes invalid UTF8 sequences
256 if str.respond_to?(:force_encoding)
257 begin
257 str.force_encoding('UTF-8')
258 Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3]
258 if ! str.valid_encoding?
259 rescue Iconv::InvalidEncoding
259 str = str.encode("US-ASCII", :invalid => :replace,
260 # "UTF-8//IGNORE" is not supported on some OS
260 :undef => :replace, :replace => '?').encode("UTF-8")
261 str
261 end
262 else
263 # removes invalid UTF8 sequences
264 begin
265 str = Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3]
266 rescue Iconv::InvalidEncoding
267 # "UTF-8//IGNORE" is not supported on some OS
268 end
262 end
269 end
270 str
263 end
271 end
264 end
272 end
@@ -216,8 +216,13 class ChangesetTest < ActiveSupport::TestCase
216 def test_invalid_utf8_sequences_in_comments_should_be_stripped
216 def test_invalid_utf8_sequences_in_comments_should_be_stripped
217 with_settings :commit_logs_encoding => 'UTF-8' do
217 with_settings :commit_logs_encoding => 'UTF-8' do
218 c = Changeset.new
218 c = Changeset.new
219 c.comments = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
219 str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
220 assert_equal "Texte encod en ISO-8859-1.", c.comments
220 c.comments = str
221 if str.respond_to?(:force_encoding)
222 assert_equal "Texte encod? en ISO-8859-1.", c.comments
223 else
224 assert_equal "Texte encod en ISO-8859-1.", c.comments
225 end
221 end
226 end
222 end
227 end
223
228
General Comments 0
You need to be logged in to leave comments. Login now