Skip to content
Permalink
Browse files

Fix parsing of colors (remove dynamic cast on eval)

  • Loading branch information...
mgreter committed Mar 17, 2018
1 parent bbfcf49 commit 72e9a04e9321f916a036af72bf5fdd2bda2bfc59
Showing with 26 additions and 24 deletions.
  1. +0 −15 src/eval.cpp
  2. +3 −0 src/inspect.cpp
  3. +18 −6 src/parser.cpp
  4. +5 −3 src/parser.hpp
@@ -600,10 +600,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;
@@ -644,10 +640,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;
@@ -1271,13 +1263,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;
}

@@ -645,6 +645,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
@@ -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] != '#') {
@@ -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()
{
@@ -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 >())
@@ -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);
@@ -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()
@@ -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;
@@ -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();
@@ -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();
@@ -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);

1 comment on commit 72e9a04

@xzyfer

This comment has been minimized.

Copy link
Contributor

commented on 72e9a04 Mar 23, 2018

Back ported to 3.5-stable

Please sign in to comment.
You can’t perform that action at this time.