Skip to content

Commit

Permalink
Merge pull request sass#706 from mgreter/feature/inspect-function
Browse files Browse the repository at this point in the history
Implement inspect function (Fix sass#701)
  • Loading branch information
xzyfer committed Dec 9, 2014
2 parents 98a6ff3 + 5c8ff6d commit 4f905ad
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ namespace Sass {
// Path Functions
register_function(ctx, image_url_sig, image_url, env);
// Misc Functions
register_function(ctx, inspect_sig, inspect, env);
register_function(ctx, unique_id_sig, unique_id, env);
}

Expand Down
12 changes: 12 additions & 0 deletions functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,18 @@ namespace Sass {
// MISCELLANEOUS FUNCTIONS
//////////////////////////

Signature inspect_sig = "inspect($value)";
BUILT_IN(inspect)
{
Expression* v = ARG("$value", Expression);
if (v->concrete_type() == Expression::NULL_VAL) {
return new (ctx.mem) String_Constant(path, position, "null");
} else if (v->concrete_type() == Expression::BOOLEAN && *v == 0) {
return new (ctx.mem) String_Constant(path, position, "false");
}
return v;
}

Signature unique_id_sig = "unique-id()";
BUILT_IN(unique_id)
{
Expand Down
2 changes: 2 additions & 0 deletions functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ namespace Sass {
extern Signature abs_sig;
extern Signature min_sig;
extern Signature max_sig;
extern Signature inspect_sig;
extern Signature random_sig;
extern Signature length_sig;
extern Signature nth_sig;
Expand Down Expand Up @@ -148,6 +149,7 @@ namespace Sass {
BUILT_IN(abs);
BUILT_IN(min);
BUILT_IN(max);
BUILT_IN(inspect);
BUILT_IN(random);
BUILT_IN(length);
BUILT_IN(nth);
Expand Down
18 changes: 18 additions & 0 deletions inspect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,24 @@ namespace Sass {
append_to_buffer("@content;");
}

void Inspect::operator()(Map* map)
{
if (map->empty()) return;
if (map->is_invisible()) return;
bool items_output = false;
append_to_buffer("(");
for (auto key : map->keys()) {
if (key->is_invisible()) continue;
if (map->at(key)->is_invisible()) continue;
if (items_output) append_to_buffer(", ");
key->perform(this);
append_to_buffer(": ");
map->at(key)->perform(this);
items_output = true;
}
append_to_buffer(")");
}

void Inspect::operator()(List* list)
{
string sep(list->separator() == List::SPACE ? " " : ", ");
Expand Down
1 change: 1 addition & 0 deletions inspect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace Sass {
virtual void operator()(Mixin_Call*);
virtual void operator()(Content*);
// expressions
virtual void operator()(Map*);
virtual void operator()(List*);
virtual void operator()(Binary_Expression*);
virtual void operator()(Unary_Expression*);
Expand Down

0 comments on commit 4f905ad

Please # to comment.