Skip to content
Browse files

Encode multibyte characters correctly #1894

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2088 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 2fed808 commit ca410998abb8ed14bdc5edf0734eb8f83bf12317 @jamis jamis committed
Showing with 60 additions and 1 deletion.
  1. +2 −0 actionmailer/CHANGELOG
  2. +10 −1 actionmailer/lib/action_mailer/quoting.rb
  3. +48 −0 actionmailer/test/quoting_test.rb
View
2 actionmailer/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Encode multibyte characters correctly #1894
+
* Multipart messages specify a MIME-Version header automatically #2003 [John Long]
* Add a unified render method to ActionMailer (delegates to ActionView::Base#render)
View
11 actionmailer/lib/action_mailer/quoting.rb
@@ -3,10 +3,19 @@ module Quoting #:nodoc:
# Convert the given text into quoted printable format, with an instruction
# that the text be eventually interpreted in the given charset.
def quoted_printable(text, charset)
- text = text.gsub( /[^a-z ]/i ) { "=%02x" % $&[0] }.gsub( / /, "_" )
+ text = text.gsub( /[^a-z ]/i ) { quoted_printable_encode($&) }.
+ gsub( / /, "_" )
"=?#{charset}?Q?#{text}?="
end
+ # Convert the given character to quoted printable format, taking into
+ # account multi-byte characters (if executing with $KCODE="u", for instance)
+ def quoted_printable_encode(character)
+ result = ""
+ character.each_byte { |b| result << "=%02x" % b }
+ result
+ end
+
# A quick-and-dirty regexp for determining whether a string contains any
# characters that need escaping.
if !defined?(CHARS_NEEDING_QUOTING)
View
48 actionmailer/test/quoting_test.rb
@@ -0,0 +1,48 @@
+$:.unshift(File.dirname(__FILE__) + "/../lib/")
+$:.unshift(File.dirname(__FILE__) + "/../lib/action_mailer/vendor")
+
+require 'test/unit'
+require 'tmail'
+require 'tempfile'
+
+class QuotingTest < Test::Unit::TestCase
+ def test_quote_multibyte_chars
+ original = "\303\246 \303\270 and \303\245"
+
+ result = execute_in_sandbox(<<-CODE)
+ $:.unshift(File.dirname(__FILE__) + "/../lib/")
+ $KCODE = 'u'
+ require 'jcode'
+ require 'action_mailer/quoting'
+ include ActionMailer::Quoting
+ quoted_printable(#{original.inspect}, "UTF-8")
+ CODE
+
+ unquoted = TMail::Unquoter.unquote_and_convert_to(result, nil)
+ assert_equal unquoted, original
+ end
+
+ private
+
+ # This whole thing *could* be much simpler, but I don't think Tempfile,
+ # popen and others exist on all platforms (like Windows).
+ def execute_in_sandbox(code)
+ test_name = "#{File.dirname(__FILE__)}/am-quoting-test.#{$$}.rb"
+ res_name = "#{File.dirname(__FILE__)}/am-quoting-test.#{$$}.out"
+
+ File.open(test_name, "w+") do |file|
+ file.write(<<-CODE)
+ block = Proc.new do
+ #{code}
+ end
+ puts block.call
+ CODE
+ end
+
+ system("ruby #{test_name} > #{res_name}") or raise "could not run test in sandbox"
+ File.read(res_name)
+ ensure
+ File.delete(test_name) rescue nil
+ File.delete(res_name) rescue nil
+ end
+end

0 comments on commit ca41099

Please sign in to comment.
Something went wrong with that request. Please try again.