File: test_misc.rb

package info (click to toggle)
ruby-premailer 1.14.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 440 kB
  • sloc: ruby: 2,344; makefile: 3
file content (426 lines) | stat: -rw-r--r-- 13,057 bytes parent folder | download
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# encoding: UTF-8
require File.expand_path(File.dirname(__FILE__)) + '/helper'

# Random tests for specific issues.
#
# The test suite will be cleaned up at some point soon.
class TestMisc < Premailer::TestCase

  def test_styles_in_the_body
    html = <<END_HTML
    <html>
    <body>
    <style type="text/css"> p { color: red; } </style>
		<p>Test</p>
		</body>
		</html>
END_HTML

    premailer = Premailer.new(html, :adapter => :nokogiri, :with_html_string => true)
    premailer.to_inline_css

    assert_match /color\:\s*red/i,  premailer.processed_doc.at('p')['style']
  end

  def test_commented_out_styles_in_the_body
    html = <<END_HTML
    <html>
    <body>
    <style type="text/css"> <!-- p { color: red; } --> </style>
		<p>Test</p>
		</body>
		</html>
END_HTML

    premailer = Premailer.new(html, :adapter => :nokogiri, :with_html_string => true)
    premailer.to_inline_css

    assert_match /color\:\s*red/i,  premailer.processed_doc.at('p')['style']
  end

  def test_not_applying_styles_to_the_head
    html = <<END_HTML
    <html>
    <head>
    <title>Title</title>
    <style type="text/css"> * { color: red; } </style>
    </head>
    <body>
		<p><a>Test</a></p>
		</body>
		</html>
END_HTML

    [:nokogiri, :nokogiri_fast, :nokogumbo].each do |adapter|
      premailer = Premailer.new(html, :with_html_string => true, :adapter => adapter)
      premailer.to_inline_css

      h = premailer.processed_doc.at('head')
      assert_nil h['style']

      t = premailer.processed_doc.at('title')
      assert_nil t['style']
    end
  end

  def test_multiple_identical_ids
    html = <<-END_HTML
    <html>
    <head>
    <style type="text/css"> #the_id { color: red; } </style>
    </head>
    <body>
		<p id="the_id">Test</p>
		<p id="the_id">Test</p>
		</body>
		</html>
    END_HTML

    premailer = Premailer.new(html, :adapter => :nokogiri, :with_html_string => true)
    premailer.to_inline_css
    premailer.processed_doc.search('p').each do |el|
      assert_match /red/i, el['style']
    end
  end

  def test_preserving_styles
    html = <<END_HTML
    <html>
    <head>
    <link rel="stylesheet" href="#"/>
    <style type="text/css"> a:hover { color: red; } </style>
    </head>
    <body>
		<p><a>Test</a></p>
		</body>
		</html>
END_HTML
    [:nokogiri, :nokogiri_fast, :nokogumbo].each do |adapter|
      premailer = Premailer.new(html, :with_html_string => true, :preserve_styles => true,  :adapter => adapter)
      premailer.to_inline_css
      assert_equal 1, premailer.processed_doc.search('head link').length
      assert_equal 1, premailer.processed_doc.search('head style').length

      premailer = Premailer.new(html, :with_html_string => true, :preserve_styles => false, :adapter => adapter)
      premailer.to_inline_css
      assert_nil premailer.processed_doc.at('body link')

      # should be preserved as unmergeable

      assert_match /color: red/i, premailer.processed_doc.at('head style').inner_html

      assert_match /a:hover/i, premailer.processed_doc.at('style').inner_html

    end
  end

  def test_unmergable_rules
    html = <<END_HTML
    <html> <head> <style type="text/css"> a { color:blue; } a:hover { color: red; } </style> </head>
		<p><a>Test</a></p>
		</body> </html>
END_HTML

    premailer = Premailer.new(html, :adapter => :nokogiri, :with_html_string => true, :verbose => true)
    premailer.to_inline_css

    # blue should be inlined
    refute_match /a\:hover[\s]*\{[\s]*color\:[\s]*blue[\s]*;[\s]*\}/i, premailer.processed_doc.at('head style').inner_html
    # red should remain in <style> block
    assert_match /a\:hover[\s]*\{[\s]*color\:[\s]*red;[\s]*\}/i, premailer.processed_doc.at('head style').inner_html
  end


  def test_drop_unmergable_rules
    html = <<END_HTML
    <html> <head> <style type="text/css"> a { color:blue; } a:hover { color: red; } </style> </head>
    <p><a>Test</a></p>
    </body> </html>
END_HTML

    premailer = Premailer.new(html, :with_html_string => true, :verbose => true, :drop_unmergeable_css_rules => true)
    premailer.to_inline_css

    # no <style> block should exist
    assert_nil premailer.processed_doc.at('head style')
  end  

  def test_unmergable_media_queries
    html = <<END_HTML
    <html> <head>
    <style type="text/css">
    a { color: blue; }
    @media (min-width:500px) {
      a { color: red; }
    }
    @media screen and (orientation: portrait) {
      a { color: green; }
    }
    </style>
    </head>
    <body>
    <p><a>Test</a></p>
    </body> </html>
END_HTML

    [:nokogiri, :nokogiri_fast, :nokogumbo].each do |adapter|
      premailer = Premailer.new(html, :with_html_string => true, :adapter => adapter)
      premailer.to_inline_css

      style_tag = premailer.processed_doc.at('head style')
      assert style_tag, "#{adapter} failed to add a head style tag"

      style_tag_contents = style_tag.inner_html

      assert_equal "color: blue;", premailer.processed_doc.at('a').attributes['style'].to_s,
                   "#{adapter}: Failed to inline the default style"
      assert_match /@media \(min-width:500px\) \{.*?a \{.*?color: red;.*?\}.*?\}/m, style_tag_contents,
                   "#{adapter}: Failed to add media query with no type to style"
      assert_match /@media screen and \(orientation: portrait\) \{.*?a \{.*?color: green;.*?\}.*?\}/m, style_tag_contents,
                   "#{adapter}: Failed to add media query with type to style"
    end

  end

  def test_unmergable_rules_with_no_body
    html = <<END_HTML
    <html>
    <style type="text/css"> a:hover { color: red; } </style>
		<p><a>Test</a></p>
		</html>
END_HTML

    premailer = Premailer.new(html, :adapter => :nokogiri, :with_html_string => true)
    premailer.to_inline_css
    assert_match /a\:hover[\s]*\{[\s]*color\:[\s]*red;[\s]*\}/i, premailer.processed_doc.at('style').inner_html
  end

  # in response to https://github.com/alexdunae/premailer/issues#issue/7
  def test_ignoring_link_pseudo_selectors
    html = <<END_HTML
    <html>
    <style type="text/css"> td a:link.top_links { color: red; } </style>
    <body>
		<td><a class="top_links">Test</a></td>
		</body>
		</html>
END_HTML

    premailer = Premailer.new(html, :adapter => :nokogiri, :with_html_string => true)
    premailer.to_inline_css
    assert_match /color: red/, premailer.processed_doc.at('a').attributes['style'].to_s
  end

  # in response to https://github.com/alexdunae/premailer/issues#issue/7
  #
  # fails sometimes in JRuby, see https://github.com/alexdunae/premailer/issues/79
  def test_parsing_bad_markup_around_tables
    html = <<END_HTML
    <html>
    <style type="text/css">
      .style3 { font-size: xx-large; }
      .style5 { background-color: #000080; }
    </style>
    <tr>
      <td valign="top" class="style3">
      <!-- MSCellType="ContentHead" -->
      <strong>PROMOCION CURSOS PRESENCIALES</strong></td>
      <strong>
      <td valign="top" style="height: 125px" class="style5">
      <!-- MSCellType="DecArea" -->
      <img alt="" src="../../images/CertisegGold.GIF" width="608" height="87" /></td>
    </tr>
END_HTML

    premailer = Premailer.new(html, :adapter => :nokogiri, :with_html_string => true)
    premailer.to_inline_css
    assert_match /font-size: xx-large/, premailer.processed_doc.search('.style3').first.attributes['style'].to_s
    refute_match /background: #000080/, premailer.processed_doc.search('.style5').first.attributes['style'].to_s
    assert_match /#000080/, premailer.processed_doc.search('.style5').first.attributes['bgcolor'].to_s
  end

  def test_preserve_original_style_attribute
    html = <<END_HTML
    <html>
    <style type="text/css">
      .style3 { font-size: xx-large; }
      .style5 { background-color: #000080; }
    </style>
    <tr>
      <td valign="top" class="style3">
      <!-- MSCellType="ContentHead" -->
      <strong>PROMOCION CURSOS PRESENCIALES</strong></td>
      <strong>
      <td valign="top" style="height: 125px; text-align: center" class="style5">
      <!-- MSCellType="DecArea" -->
      <img alt="" src="../../images/CertisegGold.GIF" width="608" height="87" /></td>
    </tr>
END_HTML

    premailer = Premailer.new(
      html,
      adapter: :nokogiri,
      with_html_string: true,
      preserve_style_attribute: true
    )

    premailer.to_inline_css

    style5 = premailer.processed_doc.search('.style5').first
    style5_style = style5.attributes['style'].to_s

    assert_match /font-size: xx-large/, premailer.processed_doc.search('.style3').first.attributes['style'].to_s
    assert_match /background-color: #000080/, style5_style
    assert_match /text-align: center/, style5_style
    assert_match /#000080/, style5.attributes['bgcolor'].to_s
  end

  # in response to https://github.com/alexdunae/premailer/issues/56
  def test_inline_important
    html = <<END_HTML
    <html>
    <style type="text/css">
      p { color: red !important; }
    </style>
    <body>
      <p style='color: green !important;'>test</p></div>
    </body>
    </html>
END_HTML

    premailer = Premailer.new(html, :with_html_string => true, :adapter => :nokogiri)
    premailer.to_inline_css
    assert_equal 'color: green !important;', premailer.processed_doc.search('p').first.attributes['style'].to_s
  end

  # in response to https://github.com/alexdunae/premailer/issues/28
  def test_handling_shorthand_auto_properties
    html = <<END_HTML
    <html>
    <style type="text/css">
      #page { margin: 0; margin-left: auto; margin-right: auto; }
      p { border: 1px solid black; border-right: none; }

    </style>
    <body>
      <div id='page'><p>test</p></div>
    </body>
    </html>
END_HTML

    premailer = Premailer.new(html, :adapter => :nokogiri, :with_html_string => true)
    premailer.to_inline_css

    assert_match /margin: 0 auto/, premailer.processed_doc.search('#page').first.attributes['style'].to_s
    assert_match /border-style: solid none solid solid;/, premailer.processed_doc.search('p').first.attributes['style'].to_s
  end

  def test_removing_scripts
    html = <<END_HTML
    <html>
    <head>
      <script>script to be removed</script>
    </head>
    <body>
      content
    </body>
    </html>
END_HTML

    [:nokogiri, :nokogiri_fast, :nokogumbo].each do |adapter|
      premailer = Premailer.new(html, :with_html_string => true, :remove_scripts => true, :adapter => adapter)
      premailer.to_inline_css
      assert_equal 0, premailer.processed_doc.search('script').length
    end

    [:nokogiri, :nokogiri_fast, :nokogumbo].each do |adapter|
      premailer = Premailer.new(html, :with_html_string => true, :remove_scripts => false, :adapter => adapter)
      premailer.to_inline_css
      assert_equal 1, premailer.processed_doc.search('script').length
    end
  end

  def test_strip_important_from_attributes
    html = <<END_HTML
    <html>
    <head>
      <style>td { background-color: #FF0000 !important; }</style>
    </head>
    <body>
      <table><tr><td>red</td></tr></table>
    </body>
    </html>
END_HTML

    [:nokogiri, :nokogiri_fast, :nokogumbo].each do |adapter|
      premailer = Premailer.new(html, :with_html_string => true, :adapter => adapter)
      assert_match 'bgcolor="#FF0000"', premailer.to_inline_css
    end
  end

  def test_scripts_with_nokogiri
    html = <<END_HTML
    <html>
    <body>
    <script type="application/ld+json">
    {
      "@context": "http://schema.org",
      "@type": "Person",
      "name": "John Doe",
      "jobTitle": "Graduate research assistant",
      "affiliation": "University of Dreams",
      "additionalName": "Johnny",
      "url": "http://www.example.com",
      "address": {
        "@type": "PostalAddress",
        "streetAddress": "1234 Peach Drive",
        "addressLocality": "Wonderland",
        "addressRegion": "Georgia"
      }
    }
    </script
    </body>
    </html>
END_HTML

    premailer = Premailer.new(html, :with_html_string => true, :remove_scripts => false, :adapter => :nokogiri)
    premailer.to_inline_css

    assert !premailer.processed_doc.css('script[type="application/ld+json"]').first.children.first.cdata?
  end

  def test_style_without_data_in_content
    html = <<END_HTML
    <html>
    <head>
      <style>#logo {content:url(good.png)};}</style>
    </head>
    <body>
      <image id="logo"/>
    </body>
    </html>
END_HTML
    [:nokogiri, :nokogiri_fast, :nokogumbo].each do |adapter|
      premailer = Premailer.new(html, :with_html_string => true, :adapter => adapter)
      assert_match 'content: url(good.png)', premailer.to_inline_css
    end
  end

  def test_style_with_data_in_content
    html = <<END_HTML
    <html>
    <head>
      <style>#logo {content: url(data:image/png;base64,LOTSOFSTUFF)};}</style>
    </head>
    <body>
      <image id="logo"/>
    </body>
    </html>
END_HTML
    [:nokogiri, :nokogiri_fast, :nokogumbo].each do |adapter|
      premailer = Premailer.new(html, :with_html_string => true, :adapter => adapter)
      assert_match /content:\s*url\(data:image\/png;base64,LOTSOFSTUFF\)/, premailer.to_inline_css
    end
  end

end