Permalink
Browse files

Allow flags to separately include link and style tags when inlining css

  • Loading branch information...
1 parent 7ba5d05 commit b43b64dc93535c9e3689f38fb20e444acd609c4a @bdotdub bdotdub committed with Nov 2, 2011
Showing with 26 additions and 2 deletions.
  1. +6 −2 lib/premailer/premailer.rb
  2. +20 −0 test/test_premailer.rb
@@ -139,6 +139,8 @@ module Warnings
# [+preserve_reset+] Whether to preserve styles associated with the MailChimp reset code
# [+with_html_string+] Whether the +html+ param should be treated as a raw string.
# [+verbose+] Whether to print errors and warnings to <tt>$stderr</tt>. Default is +false+.
+ # [+include_link_tags+] Whether to include css from <tt>link rel=stylesheet</tt> tags. Default is +true+.
+ # [+include_style_tags+] Whether to include css from <tt>style</tt> tags. Default is +true+.
# [+adapter+] Which HTML parser to use, either <tt>:nokogiri</tt> or <tt>:hpricot</tt>. Default is <tt>:hpricot</tt>.
def initialize(html, options = {})
@options = {:warn_level => Warnings::SAFE,
@@ -157,6 +159,8 @@ def initialize(html, options = {})
:verbose => false,
:debug => false,
:io_exceptions => false,
+ :include_link_tags => true,
+ :include_style_tags => true,
:adapter => Adapter.use}.merge(options)
@html_file = html
@@ -238,7 +242,7 @@ def load_css_from_options! # :nodoc:
def load_css_from_html! # :nodoc:
if tags = @doc.search("link[@rel='stylesheet'], style")
tags.each do |tag|
- if tag.to_s.strip =~ /^\<link/i && tag.attributes['href'] && media_type_ok?(tag.attributes['media'])
+ if tag.to_s.strip =~ /^\<link/i && tag.attributes['href'] && media_type_ok?(tag.attributes['media']) && @options[:include_link_tags]
# A user might want to <link /> to a local css file that is also mirrored on the site
# but the local one is different (e.g. newer) than the live file, premailer will now choose the local file
@@ -259,7 +263,7 @@ def load_css_from_html! # :nodoc:
@css_parser.load_uri!(link_uri, {:only_media_types => [:screen, :handheld]})
end
- elsif tag.to_s.strip =~ /^\<style/i
+ elsif tag.to_s.strip =~ /^\<style/i && @options[:include_style_tags]
@css_parser.add_block!(tag.inner_html, :base_uri => @base_url, :base_dir => @base_dir, :only_media_types => [:screen, :handheld])
end
end
@@ -212,4 +212,24 @@ def test_premailer_related_attributes
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
end

0 comments on commit b43b64d

Please sign in to comment.