From ae6bb46f12d5f5ca4db97d94c46cdac83d768f3e Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Tue, 13 Apr 2010 10:02:55 -0700 Subject: [PATCH] [Compass Core] New function called headers() that generates header selectors. --- .../sass_extensions/functions/selectors.rb | 21 +++++++++ test/sass_extensions_test.rb | 45 ++++++++----------- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/lib/compass/sass_extensions/functions/selectors.rb b/lib/compass/sass_extensions/functions/selectors.rb index 34de7508..05838302 100644 --- a/lib/compass/sass_extensions/functions/selectors.rb +++ b/lib/compass/sass_extensions/functions/selectors.rb @@ -39,4 +39,25 @@ module Compass::SassExtensions::Functions::Selectors Sass::Script::String.new(nested) end + # Return the header selectors for the levels indicated + # Defaults to all headers h1 through h6 + # For example: + # headers(all) => h1, h2, h3, h4, h5, h6 + # headers(4) => h1, h2, h3, h4 + # headers(2,4) => h2, h3, h4 + def headers(from = nil, to = nil) + if from && !to + if from.is_a?(Sass::Script::String) && from.value == "all" + from = Sass::Script::Number.new(1) + to = Sass::Script::Number.new(6) + else + to = from + from = Sass::Script::Number.new(1) + end + else + from ||= Sass::Script::Number.new(1) + to ||= Sass::Script::Number.new(6) + end + Sass::Script::String.new((from.value..to.value).map{|n| "h#{n}"}.join(", ")) + end end \ No newline at end of file diff --git a/test/sass_extensions_test.rb b/test/sass_extensions_test.rb index 20773cb4..52833f70 100644 --- a/test/sass_extensions_test.rb +++ b/test/sass_extensions_test.rb @@ -2,50 +2,43 @@ require 'test_helper' class SassExtensionsTest < Test::Unit::TestCase def test_simple - assert_equal "a b", nest("a", "b") + assert_equal "a b", evaluate(%Q{nest("a", "b")}) end def test_left_side_expansion - assert_equal "a c, b c", nest("a, b", "c") + assert_equal "a c, b c", evaluate(%Q{nest("a, b", "c")}) end def test_right_side_expansion - assert_equal "a b, a c", nest("a", "b, c") + assert_equal "a b, a c", evaluate(%Q{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") + assert_equal "a c, a d, b c, b d", evaluate(%Q{nest("a, b", "c, d")}) end def test_three_selectors_expansion - assert_equal "a b, a c, a d", nest("a", "b, c, d") + assert_equal "a b, a c, a d", evaluate(%Q{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") + assert_equal "a b e, a b f, a c e, a c f, a d e, a d f", evaluate(%Q{nest("a", "b, c, d", "e, f")}) end def test_enumerate - assert_equal ".grid-1, .grid-2, .grid-3", enumerate(".grid", 1, 3, "-") + assert_equal ".grid-1, .grid-2, .grid-3", evaluate(%Q{enumerate(".grid", 1, 3, "-")}) end def test_append_selector - assert_equal "div.bar", append_selector("div", ".bar") - assert_equal ".foo1.bar1, .foo1.bar2, .foo2.bar1, .foo2.bar2", append_selector(".foo1, .foo2", ".bar1, .bar2") + assert_equal "div.bar", evaluate(%Q{append_selector("div", ".bar")}) + assert_equal ".foo1.bar1, .foo1.bar2, .foo2.bar1, .foo2.bar2", evaluate(%Q{append_selector(".foo1, .foo2", ".bar1, .bar2")}) + end + + def test_headers + assert_equal "h1, h2, h3, h4, h5, h6", evaluate("headers()") + assert_equal "h1, h2, h3, h4, h5, h6", evaluate("headers(all)") + assert_equal "h1, h2, h3, h4", evaluate("headers(4)") + assert_equal "h2, h3", evaluate("headers(2,3)") + assert_equal "h4, h5, h6", evaluate("headers(4,6)") end protected - def evaluation_content(options) - Sass::Script::Functions::EvaluationContext.new(options) - end - def nest(*arguments) - options = arguments.last.is_a?(Hash) ? arguments.pop : Hash.new - evaluation_content(options).nest(*arguments.map{|a| Sass::Script::String.new(a, :string)}).to_s - end - def enumerate(prefix, from, through, separator = "-", options = {}) - prefix = Sass::Script::String.new(prefix, :string) - from = Sass::Script::Number.new(from) - through = Sass::Script::Number.new(through) - separator = Sass::Script::String.new(separator, :string) - evaluation_content(options).enumerate(prefix, from, through, separator).to_s - end - def append_selector(*arguments) - options = arguments.last.is_a?(Hash) ? arguments.pop : Hash.new - evaluation_content(options).append_selector(*arguments.map{|a| Sass::Script::String.new(a, :string)}).to_s + def evaluate(value) + Sass::Script::Parser.parse(value, 0, 0).perform(Sass::Environment.new).to_s end end