Skip to content

Commit

Permalink
Support nested message/rfc822 case
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Mar 1, 2017
1 parent 09be492 commit d654286
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/chupa-text/decomposers/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ def decompose(data)
body.force_encoding(part.charset) if part.charset

part_data = TextData.new(body, :source_data => data)
part_data.uri = "#{data.uri}\##{i}"
uri = data.uri.dup
if uri.fragment
uri.fragment += "-#{i}"
else
uri.fragment = i.to_s
end
part_data.uri = uri
part_data.mime_type = part.mime_type if part.mime_type
part_data[:encoding] = body.encoding.to_s
yield(part_data)
Expand Down
34 changes: 34 additions & 0 deletions test/fixture/nested-rfc822.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Mime-Version: 1.0
Subject: Hello
From: Sender <[email protected]>
To: Recipient <[email protected]>
Date: Sun, 19 Feb 2017 00:27:55 +0900 (JST)
Content-Type: multipart/alternative;
boundary="++++";
charset=UTF-8
Content-Transfer-Encoding: 8bit

--++++
Content-Type: message/rfc822;
charset=UTF-8
Content-Transfer-Encoding: 8bit
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="====";
charset=UTF-8
Content-Transfer-Encoding: 8bit
--====
Content-Type: message/rfc822;
charset=UTF-8
Content-Transfer-Encoding: 8bit
Mime-Version: 1.0
Content-Type: text/plain
Subject: Sub Hello
Sub World
--====

--++++
29 changes: 28 additions & 1 deletion test/test-mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,22 @@ def decompose(path)
data.mime_type = "message/rfc822"

decomposed = []
decomposer.decompose(data) do |decomposed_data|
decompose_data(data) do |decomposed_data|
decomposed << decomposed_data
end
decomposed
end

def decompose_data(data, &block)
decomposer.decompose(data) do |decomposed_data|
if decomposer.target?(decomposed_data)
decompose_data(decomposed_data, &block)
else
yield(decomposed_data)
end
end
end

sub_test_case("attributes") do
def test_subject
assert_equal(["Hello"], decompose("subject"))
Expand Down Expand Up @@ -163,5 +173,22 @@ def decompose
super(fixture_path("no-mime.eml"))
end
end

sub_test_case("nested message/rfc822") do
def test_body
assert_equal([
[
fixture_path("nested-rfc822.eml") + "#0-0-0",
"Sub World",
],
],
decompose.collect {|data| [data.uri.to_s, data.body]})
end

private
def decompose
super(fixture_path("nested-rfc822.eml"))
end
end
end
end

0 comments on commit d654286

Please sign in to comment.