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