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.
This commit is contained in:
Chris Eppstein 2008-11-29 01:08:07 -08:00
parent 670595484e
commit 39265bafc3
4 changed files with 44 additions and 2 deletions

View File

@ -4,12 +4,12 @@ require 'lib/compass'
# ----- Default: Testing ------ # ----- Default: Testing ------
task :default => :tests task :default => :run_tests
require 'rake/testtask' require 'rake/testtask'
require 'fileutils' require 'fileutils'
Rake::TestTask.new :test do |t| Rake::TestTask.new :run_tests do |t|
t.libs << 'lib' t.libs << 'lib'
test_files = FileList['test/**/*_test.rb'] test_files = FileList['test/**/*_test.rb']
test_files.exclude('test/rails/*', 'test/haml/*') test_files.exclude('test/rails/*', 'test/haml/*')

View File

@ -1,3 +1,6 @@
require 'sass'
require File.join(File.dirname(__FILE__), 'sass_extensions')
['core_ext', 'version'].each do |file| ['core_ext', 'version'].each do |file|
require File.join(File.dirname(__FILE__), 'compass', file) require File.join(File.dirname(__FILE__), 'compass', file)
end end

13
lib/sass_extensions.rb Normal file
View File

@ -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

View File

@ -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