From 39265bafc3b500166e1584806cb4cff212d7aafa Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Sat, 29 Nov 2008 01:08:07 -0800 Subject: [PATCH] Added a Sass Function called nest that performs a nesting operation on (possibly) comma delimited selectors and emits the result as a string. For example: nest(".foo", "a, em, b") would render: .foo a, .foo em, .foo b The nest function can take any number of arguments and each argument can have any number of comma-delimited selectors. --- Rakefile | 4 ++-- lib/compass.rb | 3 +++ lib/sass_extensions.rb | 13 +++++++++++++ test/sass_extensions_test.rb | 26 ++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 lib/sass_extensions.rb create mode 100644 test/sass_extensions_test.rb diff --git a/Rakefile b/Rakefile index 6ffa272f..7c12d031 100644 --- a/Rakefile +++ b/Rakefile @@ -4,12 +4,12 @@ require 'lib/compass' # ----- Default: Testing ------ -task :default => :tests +task :default => :run_tests require 'rake/testtask' require 'fileutils' -Rake::TestTask.new :test do |t| +Rake::TestTask.new :run_tests do |t| t.libs << 'lib' test_files = FileList['test/**/*_test.rb'] test_files.exclude('test/rails/*', 'test/haml/*') diff --git a/lib/compass.rb b/lib/compass.rb index 285afdc8..683a9502 100644 --- a/lib/compass.rb +++ b/lib/compass.rb @@ -1,3 +1,6 @@ +require 'sass' +require File.join(File.dirname(__FILE__), 'sass_extensions') + ['core_ext', 'version'].each do |file| require File.join(File.dirname(__FILE__), 'compass', file) end diff --git a/lib/sass_extensions.rb b/lib/sass_extensions.rb new file mode 100644 index 00000000..d432d818 --- /dev/null +++ b/lib/sass_extensions.rb @@ -0,0 +1,13 @@ +require 'sass' + +module Sass::Script::Functions + COMMA_SEPARATOR = /\s*,\s*/ + def nest(*arguments) + nested = arguments.map{|a| a.value}.inject do |memo,arg| + ancestors = memo.split(COMMA_SEPARATOR) + descendants = arg.split(COMMA_SEPARATOR) + ancestors.map{|a| descendants.map{|d| "#{a} #{d}"}.join(", ")}.join(", ") + end + Sass::Script::String.new(nested) + end +end \ No newline at end of file diff --git a/test/sass_extensions_test.rb b/test/sass_extensions_test.rb new file mode 100644 index 00000000..7d59748a --- /dev/null +++ b/test/sass_extensions_test.rb @@ -0,0 +1,26 @@ +require File.dirname(__FILE__)+'/test_helper' +require 'compass' + +class SassExtensionsTest < Test::Unit::TestCase + def test_simple + assert_equal "a b", nest("a", "b") + end + def test_left_side_expansion + assert_equal "a c, b c", nest("a, b", "c") + end + def test_right_side_expansion + assert_equal "a b, a c", nest("a", "b, c") + end + def test_both_sides_expansion + assert_equal "a c, a d, b c, b d", nest("a, b", "c, d") + end + def test_three_selectors_expansion + assert_equal "a b, a c, a d", nest("a", "b, c, d") + end + def test_third_argument_expansion + assert_equal "a b e, a b f, a c e, a c f, a d e, a d f", nest("a", "b, c, d", "e, f") + end + def nest(*arguments) + Sass::Script::Functions.nest(*arguments.map{|a| Sass::Script::String.new(a)}).to_s + end +end \ No newline at end of file