Skip to content

Commit

Permalink
Fix parsing of colors (remove dynamic cast on eval)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreter authored and xzyfer committed Mar 23, 2018
1 parent 046b32a commit 07222d6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
15 changes: 0 additions & 15 deletions src/eval.cpp
Expand Up @@ -599,10 +599,6 @@ namespace Sass {
switch (op_type) {
case Sass_OP::EQ: return *l_n == *r_c ? bool_true : bool_false;
case Sass_OP::NEQ: return *l_n == *r_c ? bool_false : bool_true;
case Sass_OP::LT: return *l_n < *r_c ? bool_true : bool_false;
case Sass_OP::GTE: return *l_n < *r_c ? bool_false : bool_true;
case Sass_OP::LTE: return *l_n < *r_c || *l_n == *r_c ? bool_true : bool_false;
case Sass_OP::GT: return *l_n < *r_c || *l_n == *r_c ? bool_false : bool_true;
case Sass_OP::ADD: case Sass_OP::SUB: case Sass_OP::MUL: case Sass_OP::DIV: case Sass_OP::MOD:
return Operators::op_number_color(op_type, *l_n, *r_c, ctx.c_options, b_in->pstate());
default: break;
Expand Down Expand Up @@ -643,10 +639,6 @@ namespace Sass {
switch (op_type) {
case Sass_OP::EQ: return *l_c == *r_n ? bool_true : bool_false;
case Sass_OP::NEQ: return *l_c == *r_n ? bool_false : bool_true;
case Sass_OP::LT: return *l_c < *r_n ? bool_true : bool_false;
case Sass_OP::GTE: return *l_c < *r_n ? bool_false : bool_true;
case Sass_OP::LTE: return *l_c < *r_n || *l_c == *r_n ? bool_true : bool_false;
case Sass_OP::GT: return *l_c < *r_n || *l_c == *r_n ? bool_false : bool_true;
case Sass_OP::ADD: case Sass_OP::SUB: case Sass_OP::MUL: case Sass_OP::DIV: case Sass_OP::MOD:
return Operators::op_color_number(op_type, *l_c, *r_n, ctx.c_options, b_in->pstate());
default: break;
Expand Down Expand Up @@ -1265,13 +1257,6 @@ namespace Sass {

Expression_Ptr Eval::operator()(String_Constant_Ptr s)
{
if (!s->is_delayed() && name_to_color(s->value())) {
Color_Ptr c = SASS_MEMORY_COPY(name_to_color(s->value())); // copy
c->pstate(s->pstate());
c->disp(s->value());
c->is_delayed(true);
return c;
}
return s;
}

Expand Down
3 changes: 3 additions & 0 deletions src/inspect.cpp
Expand Up @@ -658,6 +658,9 @@ namespace Sass {
}

std::stringstream hexlet;
// dart sass compressed all colors in regular css always
// ruby sass and libsass does it only when not delayed
// since color math is going to be removed, this can go too
bool compressed = opt.output_style == COMPRESSED;
hexlet << '#' << std::setw(1) << std::setfill('0');
// create a short color hexlet if there is any need for it
Expand Down
24 changes: 18 additions & 6 deletions src/parser.cpp
Expand Up @@ -1578,7 +1578,7 @@ namespace Sass {
return nr;
}

Expression_Ptr Parser::lexed_hex_color(const ParserState& pstate, const std::string& parsed)
Value_Ptr Parser::lexed_hex_color(const ParserState& pstate, const std::string& parsed)
{
Color_Ptr color = NULL;
if (parsed[0] != '#') {
Expand Down Expand Up @@ -1628,6 +1628,19 @@ namespace Sass {
return color;
}

Value_Ptr Parser::color_or_string(const std::string& lexed) const
{
if (auto color = name_to_color(lexed)) {
auto c = SASS_MEMORY_NEW(Color, color);
c->is_delayed(true);
c->pstate(pstate);
c->disp(lexed);
return c;
} else {
return SASS_MEMORY_NEW(String_Constant, pstate, lexed);
}
}

// parse one value for a list
Expression_Obj Parser::parse_value()
{
Expand Down Expand Up @@ -1670,7 +1683,7 @@ namespace Sass {
{ return SASS_MEMORY_NEW(Null, pstate); }

if (lex< identifier >()) {
return SASS_MEMORY_NEW(String_Constant, pstate, lexed);
return color_or_string(lexed);
}

if (lex< percentage >())
Expand Down Expand Up @@ -1841,7 +1854,7 @@ namespace Sass {
return schema->length() > 0 ? schema.detach() : NULL;
}

String_Constant_Obj Parser::parse_static_value()
Value_Obj Parser::parse_static_value()
{
lex< static_value >();
Token str(lexed);
Expand All @@ -1852,8 +1865,7 @@ namespace Sass {
--str.end;
--position;

String_Constant_Ptr str_node = SASS_MEMORY_NEW(String_Constant, pstate, str.time_wspace());
return str_node;
return color_or_string(str.time_wspace());;
}

String_Obj Parser::parse_string()
Expand Down Expand Up @@ -1986,7 +1998,7 @@ namespace Sass {
}
if (peek < exactly < '-' > >()) break;
}
else if (lex< sequence < identifier > >()) {
else if (lex< identifier >()) {
schema->append(SASS_MEMORY_NEW(String_Constant, pstate, lexed));
if ((*position == '"' || *position == '\'') || peek < alternatives < alpha > >()) {
// need_space = true;
Expand Down
8 changes: 5 additions & 3 deletions src/parser.hpp
Expand Up @@ -290,7 +290,7 @@ namespace Sass {
String_Obj parse_url_function_argument();
String_Obj parse_interpolated_chunk(Token, bool constant = false, bool css = true);
String_Obj parse_string();
String_Constant_Obj parse_static_value();
Value_Obj parse_static_value();
String_Schema_Obj parse_css_variable_value(bool top_level = true);
String_Schema_Obj parse_css_variable_value_token(bool top_level = true);
String_Obj parse_ie_property();
Expand Down Expand Up @@ -325,6 +325,8 @@ namespace Sass {
Error_Obj parse_error();
Debug_Obj parse_debug();

Value_Ptr color_or_string(const std::string& lexed) const;

// be more like ruby sass
Expression_Obj lex_almost_any_value_token();
Expression_Obj lex_almost_any_value_chars();
Expand Down Expand Up @@ -380,12 +382,12 @@ namespace Sass {
static Number_Ptr lexed_number(const ParserState& pstate, const std::string& parsed);
static Number_Ptr lexed_dimension(const ParserState& pstate, const std::string& parsed);
static Number_Ptr lexed_percentage(const ParserState& pstate, const std::string& parsed);
static Expression_Ptr lexed_hex_color(const ParserState& pstate, const std::string& parsed);
static Value_Ptr lexed_hex_color(const ParserState& pstate, const std::string& parsed);
private:
Number_Ptr lexed_number(const std::string& parsed) { return lexed_number(pstate, parsed); };
Number_Ptr lexed_dimension(const std::string& parsed) { return lexed_dimension(pstate, parsed); };
Number_Ptr lexed_percentage(const std::string& parsed) { return lexed_percentage(pstate, parsed); };
Expression_Ptr lexed_hex_color(const std::string& parsed) { return lexed_hex_color(pstate, parsed); };
Value_Ptr lexed_hex_color(const std::string& parsed) { return lexed_hex_color(pstate, parsed); };

static const char* re_attr_sensitive_close(const char* src);
static const char* re_attr_insensitive_close(const char* src);
Expand Down

0 comments on commit 07222d6

Please sign in to comment.