From 35b206a8971d1eed176ca68bb5257bb63b3b651c Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Sat, 27 Nov 2010 15:34:29 -0800 Subject: [PATCH] Support simple background fallback in the background mixin. --- .../stylesheets/compass/css3/_images.scss | 14 ++++++----- .../functions/gradient_support.rb | 25 ++++++++++++++++--- .../stylesheets/compass/css/gradients.css | 2 ++ test/sass_extensions_test.rb | 8 ++++++ 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/frameworks/compass/stylesheets/compass/css3/_images.scss b/frameworks/compass/stylesheets/compass/css3/_images.scss index 54d1cf86..8d52c56a 100644 --- a/frameworks/compass/stylesheets/compass/css3/_images.scss +++ b/frameworks/compass/stylesheets/compass/css3/_images.scss @@ -15,12 +15,14 @@ ) { $backgrounds: compact($background-1, $background-2, $background-3, $background-4, $background-5, $background-6, $background-7, $background-8, $background-9, $background-10); - - @if $experimental-support-for-svg and prefixed(-svg, $backgrounds) { background: -svg($backgrounds); } - @if $experimental-support-for-webkit and prefixed(-webkit, $backgrounds) { background: -webkit($backgrounds); } - @if $experimental-support-for-mozilla and prefixed(-moz, $backgrounds) { background: -moz($backgrounds); } - @if $experimental-support-for-pie and (prefixed(-pie, $backgrounds) or -compass-list-size($backgrounds) > 1) { -pie-background: -pie($backgrounds); } - background: $backgrounds; + $mult-bgs: -compass-list-size($backgrounds) > 1; + $simple-background: if($mult-bgs or prefixed(-css2, $backgrounds), -css2(-compass-nth($backgrounds, last)), false); + @if not blank($simple-background) { background: $simple-background; } + @if $experimental-support-for-svg and prefixed(-svg, $backgrounds) { background: -svg($backgrounds); } + @if $experimental-support-for-webkit and prefixed(-webkit, $backgrounds) { background: -webkit($backgrounds); } + @if $experimental-support-for-mozilla and prefixed(-moz, $backgrounds) { background: -moz($backgrounds); } + @if $experimental-support-for-pie and (prefixed(-pie, $backgrounds) or $mult-bgs) { -pie-background: -pie($backgrounds); } + background: $backgrounds; } // Background image property support for vendor prefixing within values. diff --git a/lib/compass/sass_extensions/functions/gradient_support.rb b/lib/compass/sass_extensions/functions/gradient_support.rb index 2bf163c0..ee3d27d3 100644 --- a/lib/compass/sass_extensions/functions/gradient_support.rb +++ b/lib/compass/sass_extensions/functions/gradient_support.rb @@ -138,10 +138,10 @@ module Compass::SassExtensions::Functions::GradientSupport Sass::Script::Bool.new(args.any?{|a| a.respond_to?(method)}) end - %w(webkit moz o ms svg pie).each do |prefix| + %w(webkit moz o ms svg pie css2).each do |prefix| class_eval <<-RUBY, __FILE__, __LINE__ + 1 def _#{prefix}(*args) - List.new(*args.map! {|a| add_prefix(:to_#{prefix}, a)}, :comma) + List.new(*args.map! {|a| add_prefix(:to_#{prefix}, a)}) end RUBY end @@ -278,7 +278,7 @@ module Compass::SassExtensions::Functions::GradientSupport Sass::Script::Bool.new(args.any?{|a| a.respond_to?(method)}) end - %w(webkit moz o ms svg pie).each do |prefix| + %w(webkit moz o ms svg pie css2).each do |prefix| class_eval <<-RUBY, __FILE__, __LINE__ + 1 def _#{prefix}(*args) Sass::Script::List.new(args.map! {|a| add_prefix(:to_#{prefix}, a)}, :comma) @@ -395,6 +395,9 @@ module Compass::SassExtensions::Functions::GradientSupport Compass::Logger.new.record(:warning, "PIE does not support radial-gradient.") Sass::Script::String.new("-pie-radial-gradient(unsupported)") end + def to_css2(options = self.options) + Sass::Script::String.new("") + end end class LinearGradient < Sass::Script::Literal @@ -437,6 +440,9 @@ module Compass::SassExtensions::Functions::GradientSupport # the presence of this attribute helps flag when to render a special rule. Sass::Script::String.new to_s(options) end + def to_css2(options = self.options) + Sass::Script::String.new("") + end end module Functions @@ -582,6 +588,19 @@ module Compass::SassExtensions::Functions::GradientSupport end end + def blank(obj) + case obj + when Sass::Script::Bool + Sass::Script::Bool.new !obj.to_bool + when Sass::Script::String + Sass::Script::Bool.new obj.value.strip.size == 0 + when Sass::Script::List + Sass::Script::Bool.new obj.value.size == 0 || obj.value.all?{|el| blank(el).to_bool} + else + Sass::Script::Bool.new false + end + end + private # After using the sass script parser to parse a string, this reconstructs diff --git a/test/fixtures/stylesheets/compass/css/gradients.css b/test/fixtures/stylesheets/compass/css/gradients.css index 721003bc..c495bee8 100644 --- a/test/fixtures/stylesheets/compass/css/gradients.css +++ b/test/fixtures/stylesheets/compass/css/gradients.css @@ -2,12 +2,14 @@ background: white url("foo.png"); } .bg-shortcut-linear-gradient { + background: white ; background: white url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4gPHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PGRlZnM+PGxpbmVhckdyYWRpZW50IGlkPSJncmFkIiB4MT0iMCUiIHkxPSIwJSIgeDI9IjEwMCUiIHkyPSIxMDAlIj48c3RvcCBvZmZzZXQ9IjAlIiBzdG9wLWNvbG9yPSIjZGRkZGRkIi8+PHN0b3Agb2Zmc2V0PSIxMDAlIiBzdG9wLWNvbG9yPSIjYWFhYWFhIi8+PC9saW5lYXJHcmFkaWVudD48L2RlZnM+PHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgZmlsbD0idXJsKCNncmFkKSIgLz48L3N2Zz4g'); background: white -webkit-gradient(linear, 0% 0%, 100% 100%, color-stop(0%, #dddddd), color-stop(100%, #aaaaaa)); background: white -moz-linear-gradient(top left, #dddddd, #aaaaaa); background: white linear-gradient(top left, #dddddd, #aaaaaa); } .bg-shortcut-radial-gradient { + background: white ; background: white url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4gPHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PGRlZnM+PHJhZGlhbEdyYWRpZW50IGlkPSJncmFkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgY3g9IjUwJSIgY3k9IjUwJSIgcj0iMTAwIj48c3RvcCBvZmZzZXQ9IjAlIiBzdG9wLWNvbG9yPSIjZGRkZGRkIi8+PHN0b3Agb2Zmc2V0PSIxMDAlIiBzdG9wLWNvbG9yPSIjYWFhYWFhIi8+PC9yYWRpYWxHcmFkaWVudD48L2RlZnM+PHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgZmlsbD0idXJsKCNncmFkKSIgLz48L3N2Zz4g'); background: white -webkit-gradient(radial, 50% 50%, 0, 50% 50%, 100, color-stop(0%, #dddddd), color-stop(100%, #aaaaaa)); background: white -moz-radial-gradient(center center, #dddddd, #aaaaaa 100px); diff --git a/test/sass_extensions_test.rb b/test/sass_extensions_test.rb index bee9a8be..cbd75f8a 100644 --- a/test/sass_extensions_test.rb +++ b/test/sass_extensions_test.rb @@ -81,6 +81,14 @@ class SassExtensionsTest < Test::Unit::TestCase assert evaluate("tan(pi()/2 + 0.0001)").to_f < -1000, evaluate("tan(pi()/2 - 0.0001)") end + def test_blank + assert_equal "false", evaluate("blank(true)") + assert_equal "true", evaluate("blank(false)") + assert_equal "true", evaluate("blank('')") + assert_equal "true", evaluate("blank(' ')") + assert_equal "true", evaluate("blank(-compass-space-list(' '))") + end + protected def evaluate(value) Sass::Script::Parser.parse(value, 0, 0).perform(Sass::Environment.new).to_s