Skip to content

Commit

Permalink
Adds functionality for set-nth.
Browse files Browse the repository at this point in the history
Fixes #578 and #581.
  • Loading branch information
avoliva authored and xzyfer committed Oct 29, 2014
1 parent 086ab12 commit 5e86443
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ namespace Sass {
// List Functions
register_function(ctx, length_sig, length, env);
register_function(ctx, nth_sig, nth, env);
register_function(ctx, set_nth_sig, set_nth, env);
register_function(ctx, index_sig, index, env);
register_function(ctx, join_sig, join, env);
register_function(ctx, append_sig, append, env);
Expand Down
20 changes: 20 additions & 0 deletions functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,26 @@ namespace Sass {
return l->value_at_index(index);
}

Signature set_nth_sig = "set-nth($list, $n, $value)";
BUILT_IN(set_nth)
{
List* l = dynamic_cast<List*>(env["$list"]);
Number* n = ARG("$n", Number);
Expression* v = ARG("$value", Expression);
if (!l) {
l = new (ctx.mem) List(path, position, 1);
*l << ARG("$list", Expression);
}
if (l->empty()) error("argument `$list` of `" + string(sig) + "` must not be empty", path, position);
double index = std::floor(n->value() < 0 ? l->length() + n->value() : n->value() - 1);
if (index < 0 || index > l->length() - 1) error("index out of bounds for `" + string(sig) + "`", path, position);
List* result = new (ctx.mem) List(path, position, l->length(), l->separator());
for (size_t i = 0, L = l->length(); i < L; ++i) {
*result << ((i == index) ? v : (*l)[i]);
}
return result;
}

Signature index_sig = "index($list, $value)";
BUILT_IN(index)
{
Expand Down
2 changes: 2 additions & 0 deletions functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ namespace Sass {
extern Signature map_values_sig;
extern Signature map_has_key_sig;
extern Signature keywords_sig;
extern Signature set_nth_sig;

BUILT_IN(rgb);
BUILT_IN(rgba_4);
Expand Down Expand Up @@ -174,6 +175,7 @@ namespace Sass {
BUILT_IN(map_values);
BUILT_IN(map_has_key);
BUILT_IN(keywords);
BUILT_IN(set_nth);

}
}

0 comments on commit 5e86443

Please sign in to comment.