Skip to content
This repository was archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
Add tests for ignore elements
Browse files Browse the repository at this point in the history
  • Loading branch information
johanbrook committed Jun 19, 2014
1 parent ad7faa6 commit 04d2cbc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 23 deletions.
3 changes: 3 additions & 0 deletions test/files/ignore.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
color: orange;
}
15 changes: 15 additions & 0 deletions test/files/ignore.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html>
<head>
<title>Should ignore link and style elements with data attribute</title>
<link rel="stylesheet" type="text/css" href="ignore.css" data-premailer="ignore" />
<style type="text/css" data-premailer="ignore">
h1 {
color: red;
}
</style>
</head>
<body>
<h1>Content</h1>
</body>
</html>
62 changes: 39 additions & 23 deletions test/test_premailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_special_characters_nokogiri_remote
end

#def test_cyrillic_nokogiri_remote
# if RUBY_VERSION =~ /1.9/
# 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
Expand All @@ -32,7 +32,7 @@ def test_special_characters_hpricot
premailer.to_inline_css
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
end

def test_detecting_html
[:nokogiri, :hpricot].each do |adapter|
remote_setup('base.html', :adapter => adapter)
Expand Down Expand Up @@ -62,7 +62,7 @@ def test_non_self_closing_html_tags
assert_match /<br>/, @premailer.to_inline_css
end
end

def test_mailtos_with_query_strings
html = <<END_HTML
<html>
Expand All @@ -75,38 +75,54 @@ def test_mailtos_with_query_strings
[:nokogiri, :hpricot].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
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
[:nokogiri, :hpricot].each do |adapter|
local_setup('ignore.html', :adapter => adapter)

assert_nil @doc.at('h1')['style']
end
end

def test_preserving_ignored_link_elements
[:nokogiri, :hpricot].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 }

# 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
[:nokogiri, :hpricot].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 }

# import.css sets .hide to { display: none }
assert_match /display: none/, @doc.at('#hide01')['style']
end
end
Expand All @@ -130,27 +146,27 @@ 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
[:nokogiri, :hpricot].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
[:nokogiri, :hpricot].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>
Expand All @@ -167,12 +183,12 @@ def test_initialize_no_escape_attributes_option
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="remove"><a href="#keep">Test</a></p>
<p id="keep">Test</p>
</body> </html>
END_HTML
Expand All @@ -187,7 +203,7 @@ def test_remove_ids
assert_not_nil doc.at("\##{hashed_id}")
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">
Expand All @@ -201,14 +217,14 @@ def test_carriage_returns_as_entities
assert_match /\r/, 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']

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']
Expand Down Expand Up @@ -294,7 +310,7 @@ def test_htmlentities
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
Expand Down

0 comments on commit 04d2cbc

Please sign in to comment.