# encoding: UTF-8 require 'spec_helper' describe Roadie::Inliner do let(:provider) { double("asset provider", :all => '') } def use_css(css) provider.stub(:all).with(['global.css']).and_return(css) end def rendering(html, options = {}) url_options = options.fetch(:url_options, {:host => 'example.com'}) Nokogiri::HTML.parse Roadie::Inliner.new(provider, ['global.css'], html, url_options).execute end describe "initialization" do it "warns about asset_path_prefix being non-functional" do expect { Roadie::Inliner.new(provider, [], '', :asset_path_prefix => 'foo') }.to raise_error(ArgumentError, /asset_path_prefix/) end end describe "inlining styles" do before(:each) do # Make sure to have some css even when we don't specify any # We have specific tests for when this is nil use_css '' end it "inlines simple attributes" do use_css 'p { color: green }' rendering('
').should have_styling('color' => 'green') end it "inlines browser-prefixed attributes" do use_css 'p { -vendor-color: green }' rendering('').should have_styling('-vendor-color' => 'green') end it "inlines CSS3 attributes" do use_css 'p { border-radius: 2px; }' rendering('').should have_styling('border-radius' => '2px') end it "keeps the order of the styles that are inlined" do use_css 'h1 { padding: 2px; margin: 5px; }' rendering('').should have_styling([['padding', '2px'], ['margin', '5px']]) end it "combines multiple selectors into one" do use_css 'p { color: green; } .tip { float: right; }' rendering('').should have_styling([['color', 'green'], ['float', 'right']]) end it "uses the attributes with the highest specificity when conflicts arises" do use_css "p { color: red; } .safe { color: green; }" rendering('').should have_styling('color' => 'green') end it "sorts styles by specificity order" do use_css 'p { margin: 2px; } #big { margin: 10px; } .down { margin-bottom: 5px; }' rendering('').should have_styling([ ['margin', '2px'], ['margin-bottom', '5px'] ]) rendering('').should have_styling([ ['margin-bottom', '5px'], ['margin', '10px'] ]) end it "supports multiple selectors for the same rules" do use_css 'p, a { color: green; }' rendering('').tap do |document| document.should have_styling('color' => 'green').at_selector('p') document.should have_styling('color' => 'green').at_selector('a') end end it "respects !important properties" do use_css "a { text-decoration: underline !important; } a.hard-to-spot { text-decoration: none; }" rendering('').should have_styling('text-decoration' => 'underline') end it "combines with already present inline styles" do use_css "p { color: green }" rendering('').should have_styling([['color', 'green'], ['font-size', '1.1em']]) end it "does not touch already present inline styles" do use_css "p { color: red }" rendering('').should have_styling([['color', 'red'], ['color', 'green']]) end it "ignores selectors with :psuedo-classes" do use_css 'p:hover { color: red }' rendering('').should_not have_styling('color' => 'red') end describe "inlineHello World