require 'spec_helper' require 'nokogiri' describe Galakei::DocomoCss::Stylesheet do context "simple stylesheet" do before do parser = CssParser::Parser.new parser.add_block!(<<-EOD) span { color: red; } EOD @stylesheet = described_class.new(parser) end it "should apply style to matching element" do doc = Nokogiri::HTML.fragment("foo") @stylesheet.apply(doc) doc.to_s.should == %q{foo} end it "should not apply style to non-matching element" do doc = Nokogiri::HTML.fragment("

foo

") @stylesheet.apply(doc) doc.to_s.should == %q{

foo

} end end context "stylesheet with multiple styles" do before do parser = CssParser::Parser.new parser.add_block!(<<-EOD) div { background-color: red; } .alC { text-align: center } EOD @stylesheet = described_class.new(parser) end it "should apply style to element that matches one style" do doc = Nokogiri::HTML.fragment("
foo") @stylesheet.apply(doc) doc.to_s.should == %q{
foo
} end end context "stylesheet with pseudo style" do before do parser = CssParser::Parser.new parser.add_block!(<<-EOD) a:link { color: red; } a:focus { color: green; } a:visited { color: blue; } EOD @stylesheet = described_class.new(parser) end it "should add to head" do doc = Nokogiri::HTML(<<-EOD) foo EOD @stylesheet.apply(doc) doc.at("//a").to_s.should == %q{foo} expected = <<-EOD EOD doc.at("/html/head/style").to_s.strip.should == expected.strip end end ((1..6).map {|i| "h#{i}"} + %w[p]).each do |tag| context "style applied to #{tag}" do before do parser = CssParser::Parser.new parser.add_block!(<<-EOD) #{tag}.color { color: red; } #{tag}.fontsize { font-size: x-small; } #{tag}.backgroundcolor { background-color: blue; } .classonly { line-height: 1px; } EOD @stylesheet = described_class.new(parser) end it "should wrap children in span for color" do doc = Nokogiri::HTML("<#{tag} class='color'>foo") @stylesheet.apply(doc) doc.at("//#{tag}").to_s.should == %Q{<#{tag} class="color">foo} end it "should wrap children in span for font-size" do doc = Nokogiri::HTML("<#{tag} class='fontsize'>foo") @stylesheet.apply(doc) doc.at("//#{tag}").to_s.should == %Q{<#{tag} class="fontsize">foo} end it "should wrap multiple children in single span" do doc = Nokogiri::HTML("<#{tag} class='fontsize'>foo
bar") @stylesheet.apply(doc) doc.at("//#{tag}").to_s.should == %Q{<#{tag} class="fontsize">foo
bar
} end it "should wrap element in div for background-color" do doc = Nokogiri::HTML("<#{tag} class='backgroundcolor'>foo") @stylesheet.apply(doc) doc.at("//div").to_s.should == %Q{
<#{tag} class="backgroundcolor">foo
} end it "should applied css of tag omitted" do doc = Nokogiri::HTML("<#{tag} class='classonly'>foo") @stylesheet.apply(doc) doc.at("//#{tag}").to_s.should == %Q{<#{tag} class="classonly" style="line-height: 1px;">foo} end end end context "style applied to child of h1" do before do parser = CssParser::Parser.new parser.add_block!(<<-EOD) h1 span { color: red; } EOD @stylesheet = described_class.new(parser) end it "should not apply style to single h1" do doc = Nokogiri::HTML("

foo

") @stylesheet.apply(doc) doc.at("//h1").to_s.should == %q{

foo

} end it "should apply style to neseted element" do doc = Nokogiri::HTML("

foo

") @stylesheet.apply(doc) doc.at("//h1").to_s.should == %q{

foo

} end end shared_examples_for 'border' do it 'applied border' do elm = subject elm.previous_sibling.to_s.should == @img elm.next_sibling.to_s.should == @img end end shared_examples_for 'not border' do it "don't applied border" do elm = subject elm.previous_sibling.to_s.should_not == @img elm.next_sibling.to_s.should_not == @img end end shared_examples_for 'border bottom' do it 'applied border bottom' do elm = subject elm.previous_sibling.to_s.should_not == @img elm.next_sibling.to_s.should == @img end end shared_examples_for 'border top' do it 'applied border top' do elm = subject elm.previous_sibling.to_s.should == @img elm.next_sibling.to_s.should_not == @img end end context 'border css applied to div' do let(:body) { "
test
" } before do parser = CssParser::Parser.new parser.add_block!(css) @stylesheet = described_class.new(parser) @doc = Nokogiri::HTML(body) @stylesheet.apply(@doc) @img = %q[] end subject { @doc.at('//div') } context 'border' do let(:css) { "div { border: 1px solid #000000; } "} it_should_behave_like 'border' end context 'border-top' do let(:css) { "div { border-top: 1px solid #000000; } "} it_should_behave_like 'border top' end context 'border-bottom' do let(:css) { "div { border-bottom: 1px solid #000000; } "} it_should_behave_like 'border bottom' end context 'bordre with class' do let(:css) { ".border { border: 1px solid #000000; }" } let(:body) { "
test
" } it_should_behave_like 'border' end context 'border-bottom with height' do let(:css) { "div { border-bottom: 5px solid #000000; } "} it 'applied border bottom' do subject.next_sibling.to_s.should == %q[] end end end context 'border css applied to h(n)' do before do parser = CssParser::Parser.new parser.add_block!(css) @stylesheet = described_class.new(parser) @doc = Nokogiri::HTML("

test

") @stylesheet.apply(@doc) @img = %q[] end subject { @doc.at("//h1") } context 'border' do let(:css) { "h1 { border: 1px solid #000000; } "} it_should_behave_like 'border' end context 'border-top' do let(:css) { "h1 { border-top: 1px solid #000000; } "} it_should_behave_like 'border top' end context 'border-bottom' do let(:css) { "h1 { border-bottom: 1px solid #000000; } "} it_should_behave_like 'border bottom' end end context 'border css applied to p' do before do parser = CssParser::Parser.new parser.add_block!(css) @stylesheet = described_class.new(parser) @doc = Nokogiri::HTML("

test

") @stylesheet.apply(@doc) @img = %q[] end subject { @doc.at("//p") } context 'border' do let(:css) { "p { border: 1px solid #000000; } "} it_should_behave_like 'not border' end end end