Permalink
Browse files

Add an append function.

This is necessary even though we have join,
since join has no way of appending a list.
  • Loading branch information...
1 parent ffb80b1 commit f8487426c9b0de95c040f193a90be06c3ee93466 @nex3 nex3 committed Nov 10, 2010
Showing with 65 additions and 1 deletion.
  1. +4 −0 doc-src/SASS_CHANGELOG.md
  2. +2 −1 doc-src/SASS_REFERENCE.md
  3. +35 −0 lib/sass/script/functions.rb
  4. +24 −0 test/sass/functions_test.rb
@@ -61,6 +61,10 @@ However, you can now do more with them using the new {file:Sass/Script/Functions
joins together two lists into one.
For example, `join(1px 2px, 10px 5px)` returns `1px 2px 10px 5px`.
+* The {Sass::Script::Functions#append `append($list, $val)` function}
+ appends values to the end of a list.
+ For example, `append(1px 2px, 10px)` returns `1px 2px 10px`.
+
* The {Sass::Script::Functions#join `length($list)` function}
returns the length of a list.
For example, `length(1px 2px 10px 5px)` returns `4`.
@@ -654,7 +654,8 @@ On their own, lists don't do much,
but the {file:Sass/Script/Functions.html#list-functions Sass list functions}
make them useful.
The {Sass::Script::Functions#nth nth function} can access items in a list,
-and the {Sass::Script::Functions#join join function} can join multiple lists together.
+the {Sass::Script::Functions#join join function} can join multiple lists together,
+and the {Sass::Script::Functions#append append function} can add items to lists.
The [`@each` rule](#each-directive) can also add styles for each item in a list.
In addition to containing simple values, lists can contain other lists.
@@ -1043,6 +1043,41 @@ def join(list1, list2, separator = Sass::Script::String.new("auto"))
declare :join, [:list1, :list2]
declare :join, [:list1, :list2, :separator]
+ # Appends a single value onto the end of a list.
+ #
+ # Unless the `$separator` argument is passed,
+ # if the list has only one item,
+ # the resulting list will be space-separated.
+ #
+ # @example
+ # append(10px 20px, 30px) => 10px 20px 30px
+ # append((blue, red), green) => blue, red, green
+ # append(10px 20px, 30px 40px) => 10px 20px (30px 40px)
+ # join(10px, 20px, comma) => 10px, 20px
+ # join((blue, red), green, space) => blue red green
+ # @overload join(list, val, separator: auto)
+ # @param list1 [Literal] The first list to join
+ # @param list2 [Literal] The second list to join
+ # @param separator [String] How the list separator (comma or space) should be determined.
+ # If this is `comma` or `space`, that is always the separator;
+ # if this is `auto` (the default), the separator is determined as explained above.
+ def append(list, val, separator = Sass::Script::String.new("auto"))
+ assert_type separator, :String
+ unless %w[auto space comma].include?(separator.value)
+ raise ArgumentError.new("Separator name must be space, comma, or auto")
+ end
+ sep = list.separator if list.is_a?(Sass::Script::List)
+ Sass::Script::List.new(
+ list.to_a + [val],
+ if separator.value == 'auto'
+ sep || :space
+ else
+ separator.value.to_sym
+ end)
+ end
+ declare :append, [:list, :val]
+ declare :append, [:list, :val, :separator]
+
private
# This method implements the pattern of transforming a numeric value into
@@ -644,6 +644,30 @@ def test_join
assert_error_message("Separator name must be space, comma, or auto for `join'", "join(1, 2, baboon)")
end
+ def test_append
+ assert_equal("1 2 3", evaluate("append(1 2, 3)"))
+ assert_equal("1 2 3 4", evaluate("append(1 2, 3 4)"))
+ assert_equal("false", evaluate("(1 2 3 4) == append(1 2, 3 4)"))
+ assert_equal("true", evaluate("(1 2 (3 4)) == append(1 2, 3 4)"))
+ assert_equal("1, 2, 3", evaluate("append((1, 2), 3)"))
+ assert_equal("1, 2, 3, 4", evaluate("append((1, 2), (3, 4))"))
+ assert_equal("false", evaluate("(1, 2, 3, 4) == append((1, 2), (3, 4))"))
+ assert_equal("true", evaluate("(1, 2, (3, 4)) == append((1, 2), (3, 4))"))
+
+ assert_equal("1 2", evaluate("append(1, 2)"))
+ assert_equal("1 2 3, 4", evaluate("append(1 2, (3, 4))"))
+ assert_equal("true", evaluate("(1 2 (3, 4)) == append(1 2, (3, 4))"))
+ assert_equal("1, 2, 3 4", evaluate("append((1, 2), 3 4)"))
+ assert_equal("true", evaluate("(1, 2, 3 4) == append((1, 2), 3 4)"))
+
+ assert_equal("1 2", evaluate("append(1, 2, auto)"))
+ assert_equal("1, 2, 3 4", evaluate("append(1 2, 3 4, comma)"))
+ assert_equal("1 2 3, 4", evaluate("append((1, 2), (3, 4), space)"))
+ assert_equal("1, 2", evaluate("append(1, 2, comma)"))
+
+ assert_error_message("Separator name must be space, comma, or auto for `append'", "append(1, 2, baboon)")
+ end
+
def test_keyword_args_rgb
assert_equal(%Q{white}, evaluate("rgb($red: 255, $green: 255, $blue: 255)"))
end

0 comments on commit f848742

Please sign in to comment.