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 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; }
EOD
@stylesheet = described_class.new(parser)
end
it "should wrap children in span for color" do
doc = Nokogiri::HTML("<#{tag} class='color'>foo#{tag}>")
@stylesheet.apply(doc)
doc.at("//#{tag}").to_s.should == %Q{<#{tag} class="color">
foo#{tag}>}
end
it "should wrap children in span for font-size" do
doc = Nokogiri::HTML("<#{tag} class='fontsize'>foo#{tag}>")
@stylesheet.apply(doc)
doc.at("//#{tag}").to_s.should == %Q{<#{tag} class="fontsize">
foo#{tag}>}
end
it "should wrap multiple children in single span" do
doc = Nokogiri::HTML("<#{tag} class='fontsize'>foo
bar#{tag}>")
@stylesheet.apply(doc)
doc.at("//#{tag}").to_s.should == %Q{<#{tag} class="fontsize">
foo
bar#{tag}>}
end
it "should wrap element in div for background-color" do
doc = Nokogiri::HTML("<#{tag} class='backgroundcolor'>foo#{tag}>")
@stylesheet.apply(doc)
doc.at("//div").to_s.should == %Q{
<#{tag} class="backgroundcolor">foo#{tag}>
}
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
end