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