@@ -245,21 +245,28 class Changeset < ActiveRecord::Base | |||||
245 | private |
|
245 | private | |
246 |
|
246 | |||
247 | def self.to_utf8(str, encoding) |
|
247 | def self.to_utf8(str, encoding) | |
248 |
return str if str. |
|
248 | return str if str.nil? | |
249 | unless encoding.blank? || encoding == 'UTF-8' |
|
249 | str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding) | |
250 | begin |
|
250 | return str if str.empty? | |
251 | str = Iconv.conv('UTF-8', encoding, str) |
|
251 | str.force_encoding("UTF-8") if str.respond_to?(:force_encoding) | |
252 | rescue Iconv::Failure |
|
|||
253 | # do nothing here |
|
|||
254 | end |
|
|||
255 | end |
|
|||
256 | if str.respond_to?(:force_encoding) |
|
252 | if str.respond_to?(:force_encoding) | |
257 | str.force_encoding('UTF-8') |
|
253 | enc = encoding.blank? ? "UTF-8" : encoding | |
|
254 | if enc != "UTF-8" | |||
|
255 | str.force_encoding(enc) | |||
|
256 | str = str.encode("UTF-8") | |||
|
257 | end | |||
258 | if ! str.valid_encoding? |
|
258 | if ! str.valid_encoding? | |
259 | str = str.encode("US-ASCII", :invalid => :replace, |
|
259 | str = str.encode("US-ASCII", :invalid => :replace, | |
260 | :undef => :replace, :replace => '?').encode("UTF-8") |
|
260 | :undef => :replace, :replace => '?').encode("UTF-8") | |
261 | end |
|
261 | end | |
262 | else |
|
262 | else | |
|
263 | unless encoding.blank? || encoding == 'UTF-8' | |||
|
264 | begin | |||
|
265 | str = Iconv.conv('UTF-8', encoding, str) | |||
|
266 | rescue Iconv::Failure | |||
|
267 | # do nothing here | |||
|
268 | end | |||
|
269 | end | |||
263 | # removes invalid UTF8 sequences |
|
270 | # removes invalid UTF8 sequences | |
264 | begin |
|
271 | begin | |
265 | str = Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3] |
|
272 | str = Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3] |
@@ -269,9 +269,9 class ChangesetTest < ActiveSupport::TestCase | |||||
269 | def test_comments_should_be_converted_all_latin1_to_utf8 |
|
269 | def test_comments_should_be_converted_all_latin1_to_utf8 | |
270 | s1 = "\xC2\x80" |
|
270 | s1 = "\xC2\x80" | |
271 | s2 = "\xc3\x82\xc2\x80" |
|
271 | s2 = "\xc3\x82\xc2\x80" | |
|
272 | s4 = s2.dup | |||
272 | if s1.respond_to?(:force_encoding) |
|
273 | if s1.respond_to?(:force_encoding) | |
273 | s3 = s1.dup |
|
274 | s3 = s1.dup | |
274 | s4 = s2.dup |
|
|||
275 | s1.force_encoding('ASCII-8BIT') |
|
275 | s1.force_encoding('ASCII-8BIT') | |
276 | s2.force_encoding('ASCII-8BIT') |
|
276 | s2.force_encoding('ASCII-8BIT') | |
277 | s3.force_encoding('ISO-8859-1') |
|
277 | s3.force_encoding('ISO-8859-1') | |
@@ -289,7 +289,7 class ChangesetTest < ActiveSupport::TestCase | |||||
289 | :scmid => '12345', |
|
289 | :scmid => '12345', | |
290 | :comments => s1) |
|
290 | :comments => s1) | |
291 | assert( c.save ) |
|
291 | assert( c.save ) | |
292 |
assert_equal s |
|
292 | assert_equal s4, c.comments | |
293 | end |
|
293 | end | |
294 |
|
294 | |||
295 | def test_identifier |
|
295 | def test_identifier |
General Comments 0
You need to be logged in to leave comments.
Login now