decoration_mail 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2d7556c609c70c2e9d612827105bbe8a43314391
4
- data.tar.gz: fc75966a7363b32e78ab925377ec1fb6ee769dad
3
+ metadata.gz: 0b04b9978b9c7fa7cda09b212f30f61c6529c283
4
+ data.tar.gz: 6ff9ac2fd21e687d405845f05fd5f5dd1836400c
5
5
  SHA512:
6
- metadata.gz: 78b3b99dd20fd9020f034ccd3075ba7befa43f002f2a44298c5461b9f42ccc38faa796f36a686d1c00a67a571b4c9d7b25272ccbef156eb96217c856aee3b3fe
7
- data.tar.gz: d1a1d911d951077d8135e63b6ddc069a732819c1c25fd89b14e5a73f9726f6cf6068ed19913279ffca75dabe452ab25642dc6c5cef354c87e3dc1d2e381edc3b
6
+ metadata.gz: 343b7877015138a6114f69694ebd54316ad99941061500a32fb36a8d46e4be8768003bb89cc2a413ae81b3e5fc9592b934b7f73ee77ade7a81f4a527ac3f3273
7
+ data.tar.gz: 5d473d0a78811792ed800754665e6a52260810b7293f5f053d63a4edf67d8f3b0593a0ea0c0f5ed136ab093ed710f6578f170ff6543d6be8e004d144d141e105
@@ -4,6 +4,7 @@ module DecorationMail
4
4
  class HTML
5
5
  def initialize(str)
6
6
  @tree = Nokogiri.HTML(str)
7
+ @unresolved_references = @tree.css('img').map{|img| img['src'] }
7
8
  raise ArgumentError, 'invalid HTML' if @tree.search("body").empty?
8
9
 
9
10
  convert_font_color_to_css
@@ -26,6 +27,7 @@ module DecorationMail
26
27
  end
27
28
 
28
29
  def update_img_src(from, to)
30
+ @unresolved_references.delete(from)
29
31
  @tree.css("img[src=\"#{from}\"]").each do |e|
30
32
  if to
31
33
  e['src'] = to
@@ -36,6 +38,7 @@ module DecorationMail
36
38
  end
37
39
 
38
40
  def to_s
41
+ remove_invalid_references
39
42
  to_div.to_html
40
43
  end
41
44
 
@@ -119,5 +122,19 @@ module DecorationMail
119
122
  end
120
123
  end
121
124
  end
125
+
126
+ def invalid_references
127
+ @unresolved_references.reject do |src|
128
+ %w(http https data).include? URI.parse(src).scheme
129
+ end
130
+ end
131
+
132
+ def remove_invalid_references
133
+ invalid_references.each do |src|
134
+ @tree.css("img[src=\"#{src}\"]").each do |e|
135
+ e.remove
136
+ end
137
+ end
138
+ end
122
139
  end
123
140
  end
@@ -1,3 +1,3 @@
1
1
  module DecorationMail
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.3"
3
3
  end
@@ -283,4 +283,18 @@ describe DecorationMail::Base do
283
283
  end
284
284
  end
285
285
  end
286
+
287
+ context "with imcomplete HTML mail(contains img tag but image part is omitted)" do
288
+ subject do
289
+ Mail.read(File.expand_path('../../resources/incomplete_decoration.eml', __FILE__)).decoration
290
+ end
291
+
292
+ it "has no images" do
293
+ expect(subject.images).to be_empty
294
+ end
295
+
296
+ it "omits img tag in returned HTML" do
297
+ expect(subject.save{}).not_to match '<img'
298
+ end
299
+ end
286
300
  end
@@ -59,14 +59,14 @@ describe DecorationMail::HTML do
59
59
 
60
60
  it "changes img's src to new one" do
61
61
  subject.update_img_src('cid:hoge', 'http://example.com/img.gif')
62
- subject.to_s.should == %(<div>ほげほげ<img src="http://example.com/img.gif"><img src="fuga.gif">\n</div>)
62
+ subject.to_s.should == %(<div>ほげほげ<img src="http://example.com/img.gif">\n</div>)
63
63
  end
64
64
 
65
65
  context "when src is nil" do
66
66
  before{ subject.update_img_src('cid:hoge', nil) }
67
67
 
68
68
  it "deletes the img tag" do
69
- subject.to_s.should == %(<div>ほげほげ<img src="fuga.gif">\n</div>)
69
+ subject.to_s.should == %(<div>ほげほげ</div>)
70
70
  end
71
71
  end
72
72
  end
@@ -89,6 +89,52 @@ describe DecorationMail::HTML do
89
89
  should be_converted_to '<div>ほげほげ</div>'
90
90
  end
91
91
  end
92
+
93
+ context "when the html contains cid reference which is not resloved by #update_img_src" do
94
+ subject{ DecorationMail::HTML.new('<body>ほげほげ<img src="cid:hoge"><img src="cid:fuga"></body>') }
95
+ before do
96
+ subject.update_img_src('cid:hoge', 'hoge.gif')
97
+ end
98
+
99
+ it "removes the img tag" do
100
+ subject.to_s.should == %(<div>ほげほげ<img src="hoge.gif">\n</div>)
101
+ end
102
+ end
103
+
104
+ context "when the html contains img tag whose src is relative path" do
105
+ it "removes the img tag" do
106
+ '<body>ほげほげ<img src="../hoge.gif" /></body>'.
107
+ should be_converted_to '<div>ほげほげ</div>'
108
+ end
109
+ end
110
+
111
+ context "when the html contains img tag whose src is absolute URI with file scheme" do
112
+ it "removes the img tag" do
113
+ '<body>ほげほげ<img src="file:///hoge.gif" /></body>'.
114
+ should be_converted_to '<div>ほげほげ</div>'
115
+ end
116
+ end
117
+
118
+ context "when the html contains img tag whose src is absolute URI with http scheme" do
119
+ it "keeps the img tag" do
120
+ '<body>ほげほげ<img src="http://example.com/hoge.gif" /></body>'.
121
+ should be_converted_to %(<div>ほげほげ<img src="http://example.com/hoge.gif">\n</div>)
122
+ end
123
+ end
124
+
125
+ context "when the html contains img tag whose src is absolute URI with https scheme" do
126
+ it "keeps the img tag" do
127
+ '<body>ほげほげ<img src="https://example.com/hoge.gif" /></body>'.
128
+ should be_converted_to %(<div>ほげほげ<img src="https://example.com/hoge.gif">\n</div>)
129
+ end
130
+ end
131
+
132
+ context "when the html contains img tag whose src is URI with data scheme" do
133
+ it "keeps the img tag" do
134
+ '<body>ほげほげ<img src="data:image/gif;base64,R0lG" /></body>'.
135
+ should be_converted_to %(<div>ほげほげ<img src="data:image/gif;base64,R0lG">\n</div>)
136
+ end
137
+ end
92
138
  end
93
139
 
94
140
  describe "#convert_font_color_to_css" do
@@ -0,0 +1,32 @@
1
+ Return-Path: <example@gmail.com>
2
+ X-Original-To: example@green-bell.jp
3
+ Delivered-To: example@green-bell.jp
4
+ MIME-Version: 1.0
5
+ Date: Wed, 14 Jan 2015 17:33:44 +0900
6
+ Message-ID: <CAAbqp33jRwy6MW_njv6Qd=kzygGcXD_qYiv-qqvRBaZCbtY+pA@mail.gmail.com>
7
+ Subject: =?UTF-8?B?5LiN5a6M5YWo44Gq44OH44Kz44Oh?=
8
+ From: <example@gmail.com>
9
+ To: example@green-bell.jp
10
+ Content-Type: multipart/related; boundary=001a11c23564f9bc19050c98980b
11
+
12
+ --001a11c23564f9bc19050c98980b
13
+ Content-Type: multipart/alternative; boundary=001a11c23564f9bc16050c98980a
14
+
15
+ --001a11c23564f9bc16050c98980a
16
+ Content-Type: text/plain; charset=UTF-8
17
+ Content-Transfer-Encoding: base64
18
+
19
+ Q29udGVudC1JROOBruWPgueFp+OBruOBv+OBp+OAgQ0KDQrigIsNCueUu+WDj+acrOS9k+OBjOa3
20
+ u+S7mOOBleOCjOOBpuOBhOOBquOBhOODoeODvOODq+OBp+OBmeOAgg0K
21
+ --001a11c23564f9bc16050c98980a
22
+ Content-Type: text/html; charset=UTF-8
23
+ Content-Transfer-Encoding: base64
24
+
25
+ PGRpdiBkaXI9Imx0ciI+PHNwYW4gc3R5bGU9ImZvbnQtc2l6ZToxNHB4Ij5Db250ZW50LUlE44Gu
26
+ 5Y+C54Wn44Gu44G/44Gn44CBPC9zcGFuPjxkaXY+PHNwYW4gc3R5bGU9ImZvbnQtc2l6ZToxNHB4
27
+ Ij48aW1nIHNyYz0iY2lkOmlpX2k0d2diaHV1MF8xNGFlNzkzODA1NTU5ZTU4IiB3aWR0aD0iMSIg
28
+ aGVpZ2h0PSIxIj48YnI+4oCLPGJyPjwvc3Bhbj48L2Rpdj48ZGl2PjxzcGFuIHN0eWxlPSJmb250
29
+ LXNpemU6MTRweCI+55S75YOP5pys5L2T44GM5re75LuY44GV44KM44Gm44GE44Gq44GE44Oh44O8
30
+ 44Or44Gn44GZ44CCPC9zcGFuPjxicj48L2Rpdj48L2Rpdj4NCg==
31
+ --001a11c23564f9bc16050c98980a--
32
+ --001a11c23564f9bc19050c98980b--
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decoration_mail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dai Akatsuka
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-12-08 00:00:00.000000000 Z
12
+ date: 2015-01-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mail
@@ -108,6 +108,7 @@ files:
108
108
  - spec/resources/au_decoration_with_attachment.eml
109
109
  - spec/resources/docomo_decoration.eml
110
110
  - spec/resources/docomo_decoration_with_attachment.eml
111
+ - spec/resources/incomplete_decoration.eml
111
112
  - spec/resources/other_decoration.eml
112
113
  - spec/resources/softbank_decoration.eml
113
114
  - spec/resources/softbank_decoration_with_attachment.eml
@@ -144,6 +145,7 @@ test_files:
144
145
  - spec/resources/au_decoration_with_attachment.eml
145
146
  - spec/resources/docomo_decoration.eml
146
147
  - spec/resources/docomo_decoration_with_attachment.eml
148
+ - spec/resources/incomplete_decoration.eml
147
149
  - spec/resources/other_decoration.eml
148
150
  - spec/resources/softbank_decoration.eml
149
151
  - spec/resources/softbank_decoration_with_attachment.eml