premailer 1.8.6 → 1.8.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -4
  3. data/bin/premailer +0 -1
  4. data/lib/premailer/adapter.rb +6 -2
  5. data/lib/premailer/adapter/hpricot.rb +12 -2
  6. data/lib/premailer/adapter/nokogiri.rb +8 -6
  7. data/lib/premailer/adapter/nokogumbo.rb +243 -0
  8. data/lib/premailer/html_to_plain_text.rb +9 -15
  9. data/lib/premailer/premailer.rb +11 -10
  10. data/lib/premailer/version.rb +1 -1
  11. metadata +64 -69
  12. data/.gitignore +0 -12
  13. data/.jrubyrc +0 -1
  14. data/.travis.yml +0 -34
  15. data/.yardopts +0 -9
  16. data/Gemfile +0 -16
  17. data/gemfiles/.ruby187.gemfile +0 -21
  18. data/init.rb +0 -1
  19. data/local-premailer +0 -9
  20. data/premailer.gemspec +0 -30
  21. data/rakefile.rb +0 -62
  22. data/test/files/base.html +0 -140
  23. data/test/files/chars.html +0 -6
  24. data/test/files/contact_bg.png +0 -0
  25. data/test/files/dialect.png +0 -0
  26. data/test/files/dots_end.png +0 -0
  27. data/test/files/dots_h.gif +0 -0
  28. data/test/files/html4.html +0 -12
  29. data/test/files/html_with_uri.html +0 -9
  30. data/test/files/ignore.css +0 -3
  31. data/test/files/ignore.html +0 -15
  32. data/test/files/import.css +0 -13
  33. data/test/files/inc/2009-placeholder.png +0 -0
  34. data/test/files/iso-8859-2.html +0 -1
  35. data/test/files/iso-8859-5.html +0 -8
  36. data/test/files/no_css.html +0 -11
  37. data/test/files/noimport.css +0 -13
  38. data/test/files/styles.css +0 -106
  39. data/test/files/xhtml.html +0 -11
  40. data/test/future_tests.rb +0 -50
  41. data/test/helper.rb +0 -42
  42. data/test/test_adapter.rb +0 -29
  43. data/test/test_html_to_plain_text.rb +0 -184
  44. data/test/test_links.rb +0 -207
  45. data/test/test_misc.rb +0 -373
  46. data/test/test_premailer.rb +0 -347
  47. data/test/test_warnings.rb +0 -95
@@ -1,347 +0,0 @@
1
- # -*- encoding: UTF-8 -*-
2
-
3
- require File.expand_path(File.dirname(__FILE__)) + '/helper'
4
-
5
- class TestPremailer < Premailer::TestCase
6
- def test_special_characters_nokogiri
7
- html = '<p>cédille c&eacute; & garçon gar&#231;on à &agrave; &nbsp; &amp; &copy;</p>'
8
- premailer = Premailer.new(html, :with_html_string => true, :adapter => :nokogiri)
9
- premailer.to_inline_css
10
- assert_equal 'c&eacute;dille c&eacute; &amp; gar&ccedil;on gar&ccedil;on &agrave; &agrave; &nbsp; &amp; &copy;', premailer.processed_doc.at('p').inner_html
11
- end
12
-
13
- def test_special_characters_nokogiri_remote
14
- remote_setup('chars.html', :adapter => :nokogiri)
15
- @premailer.to_inline_css
16
- assert_equal 'c&eacute;dille c&eacute; &amp; gar&ccedil;on gar&ccedil;on &agrave; &agrave; &nbsp; &amp; &copy;', @premailer.processed_doc.at('p').inner_html
17
- end
18
-
19
- #def test_cyrillic_nokogiri_remote
20
- # if RUBY_VERSION =~ /1.9/
21
- # remote_setup('iso-8859-5.html', :adapter => :nokogiri) #, :encoding => 'iso-8859-5')
22
- # @premailer.to_inline_css
23
- # assert_equal Encoding.find('ISO-8859-5'), @premailer.processed_doc.at('p').inner_html.encoding
24
- # end
25
- #end
26
-
27
- # TODO: this passes when run from rake but not when run from:
28
- # ruby -Itest test/test_premailer.rb -n test_special_characters_hpricot
29
- def test_special_characters_hpricot
30
- html = '<p>cédille c&eacute; & garçon gar&#231;on à &agrave; &nbsp; &amp;</p>'
31
- premailer = Premailer.new(html, :with_html_string => true, :adapter => :hpricot)
32
- premailer.to_inline_css
33
- assert_equal 'c&eacute;dille c&eacute; &amp; gar&ccedil;on gar&ccedil;on &agrave; &agrave; &nbsp; &amp;', premailer.processed_doc.at('p').inner_html
34
- end
35
-
36
- def test_detecting_html
37
- [:nokogiri, :hpricot].each do |adapter|
38
- remote_setup('base.html', :adapter => adapter)
39
- assert !@premailer.is_xhtml?
40
- end
41
- end
42
-
43
- def test_detecting_xhtml
44
- [:nokogiri, :hpricot].each do |adapter|
45
- remote_setup('xhtml.html', :adapter => adapter)
46
- assert @premailer.is_xhtml?
47
- end
48
- end
49
-
50
- def test_self_closing_xhtml_tags
51
- [:nokogiri, :hpricot].each do |adapter|
52
- remote_setup('xhtml.html', :adapter => adapter)
53
- assert_match /<br[\s]*\/>/, @premailer.to_s
54
- assert_match /<br[\s]*\/>/, @premailer.to_inline_css
55
- end
56
- end
57
-
58
- def test_non_self_closing_html_tags
59
- [:nokogiri, :hpricot].each do |adapter|
60
- remote_setup('html4.html', :adapter => adapter)
61
- assert_match /<br>/, @premailer.to_s
62
- assert_match /<br>/, @premailer.to_inline_css
63
- end
64
- end
65
-
66
- def test_mailtos_with_query_strings
67
- html = <<END_HTML
68
- <html>
69
- <a href="mailto:info@example.com?subject=Programmübersicht&amp;body=Lorem ipsum dolor sit amet.">Test</a>
70
- </html>
71
- END_HTML
72
-
73
- qs = 'testing=123'
74
-
75
- [:nokogiri, :hpricot].each do |adapter|
76
- premailer = Premailer.new(html, :with_html_string => true, :link_query_string => qs, :adapter => adapter)
77
- premailer.to_inline_css
78
- assert_no_match /testing=123/, premailer.processed_doc.search('a').first.attributes['href'].to_s
79
- end
80
- end
81
-
82
- def test_escaping_strings
83
- local_setup
84
-
85
- str = %q{url("/images/test.png");}
86
- assert_equal("url(\'/images/test.png\');", Premailer.escape_string(str))
87
- end
88
-
89
- def test_preserving_ignored_style_elements
90
- [:nokogiri, :hpricot].each do |adapter|
91
- local_setup('ignore.html', :adapter => adapter)
92
-
93
- assert_nil @doc.at('h1')['style']
94
- end
95
- end
96
-
97
- def test_preserving_ignored_link_elements
98
- [:nokogiri, :hpricot].each do |adapter|
99
- local_setup('ignore.html', :adapter => adapter)
100
-
101
- assert_nil @doc.at('body')['style']
102
- end
103
- end
104
-
105
- def test_importing_local_css
106
- # , :hpricot
107
- [:nokogiri].each do |adapter|
108
- local_setup('base.html', :adapter => adapter)
109
-
110
- # noimport.css (print stylesheet) sets body { background } to red
111
- assert_no_match /red/, @doc.at('body').attributes['style'].to_s
112
-
113
- # import.css sets .hide to { display: none }
114
- assert_match /display: none/, @doc.at('#hide01').attributes['style'].to_s
115
- end
116
- end
117
-
118
- def test_importing_remote_css
119
- [:nokogiri, :hpricot].each do |adapter|
120
- remote_setup('base.html', :adapter => adapter)
121
-
122
- # noimport.css (print stylesheet) sets body { background } to red
123
- assert_no_match /red/, @doc.at('body')['style']
124
-
125
- # import.css sets .hide to { display: none }
126
- assert_match /display: none/, @doc.at('#hide01')['style']
127
- end
128
- end
129
-
130
- def test_importing_css_as_string
131
- files_base = File.expand_path(File.dirname(__FILE__)) + '/files/'
132
-
133
- css_string = IO.read(File.join(files_base, 'import.css'))
134
-
135
- [:nokogiri, :hpricot].each do |adapter|
136
- premailer = Premailer.new(File.join(files_base, 'no_css.html'), {:css_string => css_string, :adapter => adapter})
137
- premailer.to_inline_css
138
- @doc = premailer.processed_doc
139
-
140
- # import.css sets .hide to { display: none }
141
- assert_match /display: none/, @doc.at('#hide01')['style']
142
- end
143
- end
144
-
145
- def test_local_remote_check
146
- assert Premailer.local_data?( StringIO.new('a') )
147
- assert Premailer.local_data?( '/path/' )
148
- assert !Premailer.local_data?( 'http://example.com/path/' )
149
-
150
- # the old way is deprecated but should still work
151
- premailer = Premailer.new( StringIO.new('a') )
152
- assert premailer.local_uri?( '/path/' )
153
- end
154
-
155
- def test_initialize_can_accept_io_object
156
- [:nokogiri, :hpricot].each do |adapter|
157
- io = StringIO.new('hi mom')
158
- premailer = Premailer.new(io, :adapter => adapter)
159
- assert_match /hi mom/, premailer.to_inline_css
160
- end
161
- end
162
-
163
- def test_initialize_can_accept_html_string
164
- [:nokogiri, :hpricot].each do |adapter|
165
- premailer = Premailer.new('<p>test</p>', :with_html_string => true, :adapter => adapter)
166
- assert_match /test/, premailer.to_inline_css
167
- end
168
- end
169
-
170
- def test_initialize_no_escape_attributes_option
171
- html = <<END_HTML
172
- <html> <body>
173
- <a id="google" href="http://google.com">Google</a>
174
- <a id="noescape" href="{{link_url}}">Link</a>
175
- </body> </html>
176
- END_HTML
177
-
178
- [:nokogiri, :hpricot].each do |adapter|
179
- pm = Premailer.new(html, :with_html_string => true, :adapter => adapter, :escape_url_attributes => false)
180
- pm.to_inline_css
181
- doc = pm.processed_doc
182
- assert_equal doc.at('#google')['href'], 'http://google.com'
183
- assert_equal doc.at('#noescape')['href'], '{{link_url}}'
184
- end
185
- end
186
-
187
- def test_remove_ids
188
- html = <<END_HTML
189
- <html> <head> <style type="text/css"> #remove { color:blue; } </style> </head>
190
- <body>
191
- <p id="remove"><a href="#keep">Test</a></p>
192
- <p id="keep">Test</p>
193
- </body> </html>
194
- END_HTML
195
-
196
- [:nokogiri, :hpricot].each do |adapter|
197
- pm = Premailer.new(html, :with_html_string => true, :remove_ids => true, :adapter => adapter)
198
- pm.to_inline_css
199
- doc = pm.processed_doc
200
- assert_nil doc.at('#remove')
201
- assert_nil doc.at('#keep')
202
- hashed_id = doc.at('a')['href'][1..-1]
203
- assert_not_nil doc.at("\##{hashed_id}")
204
- end
205
- end
206
-
207
- def test_reset_contenteditable
208
- html = <<-___
209
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
210
- <html> <head> <style type="text/css"> #remove { color:blue; } </style> </head>
211
- <body>
212
- <div contenteditable="true" id="editable"> Test </div>
213
- </body> </html>
214
- ___
215
- [:nokogiri, :hpricot].each do |adapter|
216
- pm = Premailer.new(html, :with_html_string => true, :reset_contenteditable => true, :adapter => adapter)
217
- pm.to_inline_css
218
- doc = pm.processed_doc
219
- assert_nil doc.at('#editable')['contenteditable'],
220
- "#{adapter}: contenteditable attribute not removed"
221
- end
222
- end
223
-
224
- def test_carriage_returns_as_entities
225
- html = <<-html
226
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
227
- <html>
228
- <body>\n\r<p>test</p>\n\r<p>test</p>
229
- </body></html>
230
- html
231
-
232
- [:nokogiri, :hpricot].each do |adapter|
233
- pm = Premailer.new(html, :with_html_string => true, :adapter => adapter)
234
- assert_match /\r/, pm.to_inline_css
235
- end
236
- end
237
-
238
-
239
- def test_advanced_selectors
240
- remote_setup('base.html', :adapter => :nokogiri)
241
- assert_match /italic/, @doc.at('h2 + h3')['style']
242
- assert_match /italic/, @doc.at('p[attr~=quote]')['style']
243
- assert_match /italic/, @doc.at('ul li:first-of-type')['style']
244
-
245
- remote_setup('base.html', :adapter => :hpricot)
246
- assert_match /italic/, @doc.at('p[@attr~="quote"]')['style']
247
- assert_match /italic/, @doc.at('ul li:first-of-type')['style']
248
- end
249
-
250
- def test_premailer_related_attributes
251
- html = <<END_HTML
252
- <html> <head> <style>table { -premailer-width: 500; } td { -premailer-height: 20}; </style>
253
- <body>
254
- <table> <tr> <td> Test </td> </tr> </table>
255
- </body> </html>
256
- END_HTML
257
-
258
- [:nokogiri, :hpricot].each do |adapter|
259
- pm = Premailer.new(html, :with_html_string => true, :adapter => adapter)
260
- pm.to_inline_css
261
- doc = pm.processed_doc
262
- assert_equal '500', doc.at('table')['width']
263
- assert_equal '20', doc.at('td')['height']
264
- end
265
- end
266
-
267
- def test_include_link_tags_option
268
- local_setup('base.html', :adapter => :nokogiri, :include_link_tags => true)
269
- assert_match /1\.231/, @doc.at('body').attributes['style'].to_s
270
- assert_match /display: none/, @doc.at('.hide').attributes['style'].to_s
271
-
272
- local_setup('base.html', :adapter => :nokogiri, :include_link_tags => false)
273
- assert_no_match /1\.231/, @doc.at('body').attributes['style'].to_s
274
- assert_match /display: none/, @doc.at('.hide').attributes['style'].to_s
275
- end
276
-
277
- def test_include_style_tags_option
278
- local_setup('base.html', :adapter => :nokogiri, :include_style_tags => true)
279
- assert_match /1\.231/, @doc.at('body').attributes['style'].to_s
280
- assert_match /display: block/, @doc.at('#iphone').attributes['style'].to_s
281
-
282
- local_setup('base.html', :adapter => :nokogiri, :include_style_tags => false)
283
- assert_match /1\.231/, @doc.at('body').attributes['style'].to_s
284
- assert_no_match /display: block/, @doc.at('#iphone').attributes['style'].to_s
285
- end
286
-
287
- def test_input_encoding
288
- html_special_characters = "Ää, Öö, Üü"
289
- expected_html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body><p>" + html_special_characters + "</p></body></html>\n"
290
- pm = Premailer.new(html_special_characters, :with_html_string => true, :adapter => :nokogiri, :input_encoding => "UTF-8")
291
- assert_equal expected_html, pm.to_inline_css
292
- end
293
-
294
- # output_encoding option should return HTML Entities when set to US-ASCII
295
- def test_output_encoding
296
- html_special_characters = "©"
297
- html_entities_characters = "&#169;"
298
- expected_html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body><p>" + html_entities_characters + "</p></body></html>\n"
299
- pm = Premailer.new(html_special_characters, :output_encoding => "US-ASCII", :with_html_string => true, :adapter => :nokogiri, :input_encoding => "UTF-8");
300
- assert_equal expected_html, pm.to_inline_css
301
- end
302
-
303
- def test_meta_encoding_downcase
304
- meta_encoding = '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
305
- expected_html = Regexp.new(Regexp.escape('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'), Regexp::IGNORECASE)
306
- pm = Premailer.new(meta_encoding, :with_html_string => true, :adapter => :nokogiri, :input_encoding => "utf-8")
307
- assert_match expected_html, pm.to_inline_css
308
- end
309
-
310
- def test_meta_encoding_upcase
311
- meta_encoding = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'
312
- expected_html = Regexp.new(Regexp.escape('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'), Regexp::IGNORECASE)
313
- pm = Premailer.new(meta_encoding, :with_html_string => true, :adapter => :nokogiri, :input_encoding => "UTF-8")
314
- assert_match expected_html, pm.to_inline_css
315
- end
316
-
317
- def test_htmlentities
318
- html_entities = "&#8217;"
319
- expected_html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body><p>'</p></body></html>\n"
320
- pm = Premailer.new(html_entities, :with_html_string => true, :adapter => :nokogiri, :replace_html_entities => true)
321
- assert_equal expected_html, pm.to_inline_css
322
- end
323
-
324
- # If a line other than the first line in the html string begins with a URI
325
- # Premailer should not identify the html string as a URI. Otherwise the following
326
- # exception would be raised: ActionView::Template::Error: bad URI(is not URI?)
327
- def test_line_starting_with_uri_in_html_with_linked_css
328
- files_base = File.expand_path(File.dirname(__FILE__)) + '/files/'
329
- html_string = IO.read(File.join(files_base, 'html_with_uri.html'))
330
-
331
- assert_nothing_raised do
332
- premailer = Premailer.new(html_string, :with_html_string => true)
333
- premailer.to_inline_css
334
- end
335
- end
336
-
337
- def test_empty_html_nokogiri
338
- html = ""
339
- css = "a:hover {color:red;}"
340
-
341
- assert_nothing_raised do
342
- pm = Premailer.new(html, :with_html_string => true, :css_string => css, :adapter => :nokogiri)
343
- pm.to_inline_css
344
- end
345
- end
346
-
347
- end
@@ -1,95 +0,0 @@
1
- # encoding: UTF-8
2
- require File.expand_path(File.dirname(__FILE__)) + '/helper'
3
-
4
- class TestWarnings < Premailer::TestCase
5
- def test_element_warnings
6
- html = <<END_HTML
7
- <!DOCTYPE html>
8
- <html>
9
- <head><link rel="alternate" href="http://example.com/"></head>
10
- <body>
11
- <form method="post"> Test </form>
12
- </body>
13
- </html>
14
- END_HTML
15
-
16
- [:nokogiri, :hpricot].each do |adapter|
17
- warnings = get_warnings(html, adapter)
18
- assert_equal 2, warnings.length
19
- assert warnings.any? { |w| w[:message] == 'form HTML element'}
20
- assert warnings.any? { |w| w[:message] == 'link HTML element'}
21
- end
22
- end
23
-
24
- def test_css_warnings
25
- html = <<END_HTML
26
- <!DOCTYPE html>
27
- <html><body>
28
- <div style="margin: 5px; height: 100px;">Test</div>
29
- </body></html>
30
- END_HTML
31
-
32
- [:nokogiri, :hpricot].each do |adapter|
33
- warnings = get_warnings(html, adapter)
34
- assert_equal 2, warnings.length
35
- assert warnings.any? { |w| w[:message] == 'height CSS property'}
36
- assert warnings.any? { |w| w[:message] == 'margin CSS property'}
37
- end
38
- end
39
-
40
- def test_css_aliased_warnings
41
- html = <<END_HTML
42
- <!DOCTYPE html>
43
- <html><body>
44
- <div style="margin-top: 5px;">Test</div>
45
- </body></html>
46
- END_HTML
47
-
48
- [:nokogiri, :hpricot].each do |adapter|
49
- warnings = get_warnings(html, adapter)
50
- assert_equal 1, warnings.length
51
- assert warnings.any? { |w| w[:message] == 'margin-top CSS property'}
52
- end
53
- end
54
-
55
- def test_attribute_warnings
56
- html = <<END_HTML
57
- <!DOCTYPE html>
58
- <html><body>
59
- <img src="#" ismap>
60
- </body></html>
61
- END_HTML
62
-
63
- [:nokogiri, :hpricot].each do |adapter|
64
- warnings = get_warnings(html, adapter)
65
- assert_equal 1, warnings.length
66
- assert warnings.any? { |w| w[:message] == 'ismap HTML attribute'}
67
- end
68
- end
69
-
70
- def test_warn_level
71
- html = <<END_HTML
72
- <!DOCTYPE html>
73
- <html><body>
74
- <div style="color: red; font-family: sans-serif;">Test</div>
75
- </body></html>
76
- END_HTML
77
-
78
- [:nokogiri, :hpricot].each do |adapter|
79
- warnings = get_warnings(html, adapter, Premailer::Warnings::SAFE)
80
- assert_equal 2, warnings.length
81
- end
82
-
83
- [:nokogiri, :hpricot].each do |adapter|
84
- warnings = get_warnings(html, adapter, Premailer::Warnings::POOR)
85
- assert_equal 1, warnings.length
86
- end
87
- end
88
-
89
- protected
90
- def get_warnings(html, adapter = :nokogiri, warn_level = Premailer::Warnings::SAFE)
91
- pm = Premailer.new(html, {:adpater => adapter, :with_html_string => true, :warn_level => warn_level})
92
- pm.to_inline_css
93
- pm.check_client_support
94
- end
95
- end