Skip to content

Commit

Permalink
support colliding CSS filter effects functions
Browse files Browse the repository at this point in the history
CSS filter effects [0] define several functions (invert, grayscale,
opacity, saturation) that collide with the names of built-in Sass
functions. Overload these functions with a no-op when the filter effects
function signature is discovered. This is compatible with Ruby Sass.

For example, calling `invert` with a color will return an inverted color
as before:

    color: invert(sass#333) => color: #cccccc

But calling `invert` with a percentage will pass through the function
call unharmed.

    filter: invert(30%) => filter: invert(30%)
    filter: invert(.03) => filter: invert(.03)

Fixes sass#151.

[0]: http://www.w3.org/TR/filter-effects/
  • Loading branch information
benesch committed Oct 6, 2014
1 parent 3a404a8 commit 8a866f3
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions functions.cpp
Expand Up @@ -359,9 +359,15 @@ namespace Sass {
position);
}

Signature saturate_sig = "saturate($color, $amount)";
Signature saturate_sig = "saturate($color, $amount: nil)";
BUILT_IN(saturate)
{
Number* filter_amount = dynamic_cast<Number*>(env["$color"]);
if (filter_amount) {
To_String to_string;
return new (ctx.mem) String_Constant(path, position, "saturate(" + filter_amount->perform(&to_string) + ")");
}

Color* rgb_color = ARG("$color", Color);
Number* amount = ARGR("$amount", Number, 0, 100);
HSL hsl_color = rgb_to_hsl(rgb_color->r(),
Expand Down Expand Up @@ -409,6 +415,12 @@ namespace Sass {
Signature grayscale_sig = "grayscale($color)";
BUILT_IN(grayscale)
{
Number* amount = dynamic_cast<Number*>(env["$color"]);
if (amount) {
To_String to_string;
return new (ctx.mem) String_Constant(path, position, "grayscale(" + amount->perform(&to_string) + ")");
}

Color* rgb_color = ARG("$color", Color);
HSL hsl_color = rgb_to_hsl(rgb_color->r(),
rgb_color->g(),
Expand Down Expand Up @@ -438,26 +450,22 @@ namespace Sass {
position);
}

Signature invert_sig = "invert($value)";
Signature invert_sig = "invert($color)";
BUILT_IN(invert)
{
Expression* v = ARG("$value", Expression);
if (v->concrete_type() == Expression::NUMBER) {
Number* amount = dynamic_cast<Number*>(env["$color"]);
if (amount) {
To_String to_string;
String_Constant* str = new String_Constant(path,
position,
v->perform(&to_string));
return new (ctx.mem) String_Constant(path, position, "invert(" + str->value() + ")");
}
else {
Color* rgb_color = static_cast<Color*>(v);
return new (ctx.mem) Color(path,
position,
255 - rgb_color->r(),
255 - rgb_color->g(),
255 - rgb_color->b(),
rgb_color->a());
return new (ctx.mem) String_Constant(path, position, "invert(" + amount->perform(&to_string) + ")");
}

Color* rgb_color = ARG("$color", Color);
return new (ctx.mem) Color(path,
position,
255 - rgb_color->r(),
255 - rgb_color->g(),
255 - rgb_color->b(),
rgb_color->a());
}

////////////////////
Expand All @@ -471,9 +479,14 @@ namespace Sass {
if (ie_kwd) {
return new (ctx.mem) String_Constant(path, position, "alpha(" + ie_kwd->value() + ")");
}
else {
return new (ctx.mem) Number(path, position, ARG("$color", Color)->a());

Number* amount = dynamic_cast<Number*>(env["$color"]);
if (amount) {
To_String to_string;
return new (ctx.mem) String_Constant(path, position, "opacity(" + amount->perform(&to_string) + ")");
}

return new (ctx.mem) Number(path, position, ARG("$color", Color)->a());
}

Signature opacify_sig = "opacify($color, $amount)";
Expand Down

0 comments on commit 8a866f3

Please sign in to comment.