annotate .svn/pristine/22/229053adf4d0f0cdc30b283c6478a18c47c6fe1d.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents 261b3d9a4903
children
rev   line source
Chris@1464 1 if RUBY_VERSION < '1.9'
Chris@1464 2 require 'iconv'
Chris@1464 3 end
Chris@1464 4
Chris@1464 5 module Redmine
Chris@1464 6 module CodesetUtil
Chris@1464 7
Chris@1464 8 def self.replace_invalid_utf8(str)
Chris@1464 9 return str if str.nil?
Chris@1464 10 if str.respond_to?(:force_encoding)
Chris@1464 11 str.force_encoding('UTF-8')
Chris@1464 12 if ! str.valid_encoding?
Chris@1464 13 str = str.encode("US-ASCII", :invalid => :replace,
Chris@1464 14 :undef => :replace, :replace => '?').encode("UTF-8")
Chris@1464 15 end
Chris@1464 16 elsif RUBY_PLATFORM == 'java'
Chris@1464 17 begin
Chris@1464 18 ic = Iconv.new('UTF-8', 'UTF-8')
Chris@1464 19 str = ic.iconv(str)
Chris@1464 20 rescue
Chris@1464 21 str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
Chris@1464 22 end
Chris@1464 23 else
Chris@1464 24 ic = Iconv.new('UTF-8', 'UTF-8')
Chris@1464 25 txtar = ""
Chris@1464 26 begin
Chris@1464 27 txtar += ic.iconv(str)
Chris@1464 28 rescue Iconv::IllegalSequence
Chris@1464 29 txtar += $!.success
Chris@1464 30 str = '?' + $!.failed[1,$!.failed.length]
Chris@1464 31 retry
Chris@1464 32 rescue
Chris@1464 33 txtar += $!.success
Chris@1464 34 end
Chris@1464 35 str = txtar
Chris@1464 36 end
Chris@1464 37 str
Chris@1464 38 end
Chris@1464 39
Chris@1464 40 def self.to_utf8(str, encoding)
Chris@1464 41 return str if str.nil?
Chris@1464 42 str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding)
Chris@1464 43 if str.empty?
Chris@1464 44 str.force_encoding("UTF-8") if str.respond_to?(:force_encoding)
Chris@1464 45 return str
Chris@1464 46 end
Chris@1464 47 enc = encoding.blank? ? "UTF-8" : encoding
Chris@1464 48 if str.respond_to?(:force_encoding)
Chris@1464 49 if enc.upcase != "UTF-8"
Chris@1464 50 str.force_encoding(enc)
Chris@1464 51 str = str.encode("UTF-8", :invalid => :replace,
Chris@1464 52 :undef => :replace, :replace => '?')
Chris@1464 53 else
Chris@1464 54 str.force_encoding("UTF-8")
Chris@1464 55 if ! str.valid_encoding?
Chris@1464 56 str = str.encode("US-ASCII", :invalid => :replace,
Chris@1464 57 :undef => :replace, :replace => '?').encode("UTF-8")
Chris@1464 58 end
Chris@1464 59 end
Chris@1464 60 elsif RUBY_PLATFORM == 'java'
Chris@1464 61 begin
Chris@1464 62 ic = Iconv.new('UTF-8', enc)
Chris@1464 63 str = ic.iconv(str)
Chris@1464 64 rescue
Chris@1464 65 str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
Chris@1464 66 end
Chris@1464 67 else
Chris@1464 68 ic = Iconv.new('UTF-8', enc)
Chris@1464 69 txtar = ""
Chris@1464 70 begin
Chris@1464 71 txtar += ic.iconv(str)
Chris@1464 72 rescue Iconv::IllegalSequence
Chris@1464 73 txtar += $!.success
Chris@1464 74 str = '?' + $!.failed[1,$!.failed.length]
Chris@1464 75 retry
Chris@1464 76 rescue
Chris@1464 77 txtar += $!.success
Chris@1464 78 end
Chris@1464 79 str = txtar
Chris@1464 80 end
Chris@1464 81 str
Chris@1464 82 end
Chris@1464 83
Chris@1464 84 def self.to_utf8_by_setting(str)
Chris@1464 85 return str if str.nil?
Chris@1464 86 str = self.to_utf8_by_setting_internal(str)
Chris@1464 87 if str.respond_to?(:force_encoding)
Chris@1464 88 str.force_encoding('UTF-8')
Chris@1464 89 end
Chris@1464 90 str
Chris@1464 91 end
Chris@1464 92
Chris@1464 93 def self.to_utf8_by_setting_internal(str)
Chris@1464 94 return str if str.nil?
Chris@1464 95 if str.respond_to?(:force_encoding)
Chris@1464 96 str.force_encoding('ASCII-8BIT')
Chris@1464 97 end
Chris@1464 98 return str if str.empty?
Chris@1464 99 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
Chris@1464 100 if str.respond_to?(:force_encoding)
Chris@1464 101 str.force_encoding('UTF-8')
Chris@1464 102 end
Chris@1464 103 encodings = Setting.repositories_encodings.split(',').collect(&:strip)
Chris@1464 104 encodings.each do |encoding|
Chris@1464 105 if str.respond_to?(:force_encoding)
Chris@1464 106 begin
Chris@1464 107 str.force_encoding(encoding)
Chris@1464 108 utf8 = str.encode('UTF-8')
Chris@1464 109 return utf8 if utf8.valid_encoding?
Chris@1464 110 rescue
Chris@1464 111 # do nothing here and try the next encoding
Chris@1464 112 end
Chris@1464 113 else
Chris@1464 114 begin
Chris@1464 115 return Iconv.conv('UTF-8', encoding, str)
Chris@1464 116 rescue Iconv::Failure
Chris@1464 117 # do nothing here and try the next encoding
Chris@1464 118 end
Chris@1464 119 end
Chris@1464 120 end
Chris@1464 121 str = self.replace_invalid_utf8(str)
Chris@1464 122 if str.respond_to?(:force_encoding)
Chris@1464 123 str.force_encoding('UTF-8')
Chris@1464 124 end
Chris@1464 125 str
Chris@1464 126 end
Chris@1464 127
Chris@1464 128 def self.from_utf8(str, encoding)
Chris@1464 129 str ||= ''
Chris@1464 130 if str.respond_to?(:force_encoding)
Chris@1464 131 str.force_encoding('UTF-8')
Chris@1464 132 if encoding.upcase != 'UTF-8'
Chris@1464 133 str = str.encode(encoding, :invalid => :replace,
Chris@1464 134 :undef => :replace, :replace => '?')
Chris@1464 135 else
Chris@1464 136 str = self.replace_invalid_utf8(str)
Chris@1464 137 end
Chris@1464 138 elsif RUBY_PLATFORM == 'java'
Chris@1464 139 begin
Chris@1464 140 ic = Iconv.new(encoding, 'UTF-8')
Chris@1464 141 str = ic.iconv(str)
Chris@1464 142 rescue
Chris@1464 143 str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
Chris@1464 144 end
Chris@1464 145 else
Chris@1464 146 ic = Iconv.new(encoding, 'UTF-8')
Chris@1464 147 txtar = ""
Chris@1464 148 begin
Chris@1464 149 txtar += ic.iconv(str)
Chris@1464 150 rescue Iconv::IllegalSequence
Chris@1464 151 txtar += $!.success
Chris@1464 152 str = '?' + $!.failed[1, $!.failed.length]
Chris@1464 153 retry
Chris@1464 154 rescue
Chris@1464 155 txtar += $!.success
Chris@1464 156 end
Chris@1464 157 str = txtar
Chris@1464 158 end
Chris@1464 159 end
Chris@1464 160 end
Chris@1464 161 end