Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/yalab/mail into yalab-master
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikel Lindsaar committed Apr 16, 2011
2 parents 3fd1a54 + 08f1ab9 commit e96ef1e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/mail/body.rb
Expand Up @@ -162,7 +162,12 @@ def encoded(transfer_encoding = '8bit')
else
# Decode then encode to normalize and allow transforming
# from base64 to Q-P and vice versa
enc.encode(dec.decode(raw_source))
decoded = dec.decode(raw_source)
if defined?(Encoding) && charset && charset != "US-ASCII"
decoded.encode!(charset)
decoded.force_encoding('BINARY') unless Encoding.find(charset).ascii_compatible?
end
enc.encode(decoded)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/mail/fields/unstructured_field.rb
Expand Up @@ -166,6 +166,7 @@ def fold(prepend = 0) # :nodoc:
end

def encode(value)
value.encode!(charset) if defined?(Encoding) && charset
(value.not_ascii_only? ? [value].pack("M").gsub("=\n", '') : value).gsub("\r", "=0D").gsub("\n", "=0A")
end

Expand Down
9 changes: 9 additions & 0 deletions spec/mail/body_spec.rb
Expand Up @@ -382,4 +382,13 @@
body.should include('The')
end
end

describe "non US-ASCII charset" do
it "should encoded" do
body = Mail::Body.new("あいうえお\n")
body.charset = 'iso-2022-jp'
expect = (RUBY_VERSION < '1.9') ? "あいうえお\r\n" : "\e$B$\"$$$&$($*\e(B\r\n"
body.encoded.should == expect
end
end
end
3 changes: 1 addition & 2 deletions spec/mail/encodings_spec.rb
Expand Up @@ -241,13 +241,12 @@

it "should round trip another complex string (koi-8)" do
original = "Слово 9999 и число"
orginial = original.encode('koi8-r') if RUBY_VERSION >= "1.9"
mail = Mail.new
mail.subject = original
mail[:subject].charset = 'koi8-r'
wrapped = mail[:subject].wrapped_value
unwrapped = Mail::Encodings.value_decode(wrapped)
orginial = original.force_encoding('koi8-r').encode!('utf-8') if RUBY_VERSION >= "1.9"

unwrapped.gsub("Subject: ", "").should == original
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/mail/fields/unstructured_field_spec.rb
Expand Up @@ -167,4 +167,10 @@
end
end

describe "iso-2022-jp Subject" do
@field = Mail::UnstructuredField.new("Subject", "あいうえお")
@field.charset = 'iso-2022-jp'
expect = (RUBY_VERSION < '1.9') ? "Subject: =?ISO-2022-JP?Q?=E3=81=82=E3=81=84=E3=81=86=E3=81=88=E3=81=8A?=\r\n" : "Subject: =?ISO-2022-JP?Q?=1B$B$=22$$$&$=28$*=1B=28B?=\r\n"
@field.encoded.should == expect
end
end

0 comments on commit e96ef1e

Please sign in to comment.