Skip to content

Commit 72e9a04

Browse files
committed
Fix parsing of colors (remove dynamic cast on eval)
1 parent bbfcf49 commit 72e9a04

File tree

4 files changed

+26
-24
lines changed

4 files changed

+26
-24
lines changed

src/eval.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,6 @@ namespace Sass {
600600
switch (op_type) {
601601
case Sass_OP::EQ: return *l_n == *r_c ? bool_true : bool_false;
602602
case Sass_OP::NEQ: return *l_n == *r_c ? bool_false : bool_true;
603-
case Sass_OP::LT: return *l_n < *r_c ? bool_true : bool_false;
604-
case Sass_OP::GTE: return *l_n < *r_c ? bool_false : bool_true;
605-
case Sass_OP::LTE: return *l_n < *r_c || *l_n == *r_c ? bool_true : bool_false;
606-
case Sass_OP::GT: return *l_n < *r_c || *l_n == *r_c ? bool_false : bool_true;
607603
case Sass_OP::ADD: case Sass_OP::SUB: case Sass_OP::MUL: case Sass_OP::DIV: case Sass_OP::MOD:
608604
return Operators::op_number_color(op_type, *l_n, *r_c, ctx.c_options, b_in->pstate());
609605
default: break;
@@ -644,10 +640,6 @@ namespace Sass {
644640
switch (op_type) {
645641
case Sass_OP::EQ: return *l_c == *r_n ? bool_true : bool_false;
646642
case Sass_OP::NEQ: return *l_c == *r_n ? bool_false : bool_true;
647-
case Sass_OP::LT: return *l_c < *r_n ? bool_true : bool_false;
648-
case Sass_OP::GTE: return *l_c < *r_n ? bool_false : bool_true;
649-
case Sass_OP::LTE: return *l_c < *r_n || *l_c == *r_n ? bool_true : bool_false;
650-
case Sass_OP::GT: return *l_c < *r_n || *l_c == *r_n ? bool_false : bool_true;
651643
case Sass_OP::ADD: case Sass_OP::SUB: case Sass_OP::MUL: case Sass_OP::DIV: case Sass_OP::MOD:
652644
return Operators::op_color_number(op_type, *l_c, *r_n, ctx.c_options, b_in->pstate());
653645
default: break;
@@ -1271,13 +1263,6 @@ namespace Sass {
12711263

12721264
Expression_Ptr Eval::operator()(String_Constant_Ptr s)
12731265
{
1274-
if (!s->is_delayed() && name_to_color(s->value())) {
1275-
Color_Ptr c = SASS_MEMORY_COPY(name_to_color(s->value())); // copy
1276-
c->pstate(s->pstate());
1277-
c->disp(s->value());
1278-
c->is_delayed(true);
1279-
return c;
1280-
}
12811266
return s;
12821267
}
12831268

src/inspect.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,9 @@ namespace Sass {
645645
}
646646

647647
std::stringstream hexlet;
648+
// dart sass compressed all colors in regular css always
649+
// ruby sass and libsass does it only when not delayed
650+
// since color math is going to be removed, this can go too
648651
bool compressed = opt.output_style == COMPRESSED;
649652
hexlet << '#' << std::setw(1) << std::setfill('0');
650653
// create a short color hexlet if there is any need for it

src/parser.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,7 +1578,7 @@ namespace Sass {
15781578
return nr;
15791579
}
15801580

1581-
Expression_Ptr Parser::lexed_hex_color(const ParserState& pstate, const std::string& parsed)
1581+
Value_Ptr Parser::lexed_hex_color(const ParserState& pstate, const std::string& parsed)
15821582
{
15831583
Color_Ptr color = NULL;
15841584
if (parsed[0] != '#') {
@@ -1628,6 +1628,19 @@ namespace Sass {
16281628
return color;
16291629
}
16301630

1631+
Value_Ptr Parser::color_or_string(const std::string& lexed) const
1632+
{
1633+
if (auto color = name_to_color(lexed)) {
1634+
auto c = SASS_MEMORY_NEW(Color, color);
1635+
c->is_delayed(true);
1636+
c->pstate(pstate);
1637+
c->disp(lexed);
1638+
return c;
1639+
} else {
1640+
return SASS_MEMORY_NEW(String_Constant, pstate, lexed);
1641+
}
1642+
}
1643+
16311644
// parse one value for a list
16321645
Expression_Obj Parser::parse_value()
16331646
{
@@ -1670,7 +1683,7 @@ namespace Sass {
16701683
{ return SASS_MEMORY_NEW(Null, pstate); }
16711684

16721685
if (lex< identifier >()) {
1673-
return SASS_MEMORY_NEW(String_Constant, pstate, lexed);
1686+
return color_or_string(lexed);
16741687
}
16751688

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

1844-
String_Constant_Obj Parser::parse_static_value()
1857+
Value_Obj Parser::parse_static_value()
18451858
{
18461859
lex< static_value >();
18471860
Token str(lexed);
@@ -1852,8 +1865,7 @@ namespace Sass {
18521865
--str.end;
18531866
--position;
18541867

1855-
String_Constant_Ptr str_node = SASS_MEMORY_NEW(String_Constant, pstate, str.time_wspace());
1856-
return str_node;
1868+
return color_or_string(str.time_wspace());;
18571869
}
18581870

18591871
String_Obj Parser::parse_string()
@@ -1986,7 +1998,7 @@ namespace Sass {
19861998
}
19871999
if (peek < exactly < '-' > >()) break;
19882000
}
1989-
else if (lex< sequence < identifier > >()) {
2001+
else if (lex< identifier >()) {
19902002
schema->append(SASS_MEMORY_NEW(String_Constant, pstate, lexed));
19912003
if ((*position == '"' || *position == '\'') || peek < alternatives < alpha > >()) {
19922004
// need_space = true;

src/parser.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ namespace Sass {
290290
String_Obj parse_url_function_argument();
291291
String_Obj parse_interpolated_chunk(Token, bool constant = false, bool css = true);
292292
String_Obj parse_string();
293-
String_Constant_Obj parse_static_value();
293+
Value_Obj parse_static_value();
294294
String_Schema_Obj parse_css_variable_value(bool top_level = true);
295295
String_Schema_Obj parse_css_variable_value_token(bool top_level = true);
296296
String_Obj parse_ie_property();
@@ -325,6 +325,8 @@ namespace Sass {
325325
Error_Obj parse_error();
326326
Debug_Obj parse_debug();
327327

328+
Value_Ptr color_or_string(const std::string& lexed) const;
329+
328330
// be more like ruby sass
329331
Expression_Obj lex_almost_any_value_token();
330332
Expression_Obj lex_almost_any_value_chars();
@@ -380,12 +382,12 @@ namespace Sass {
380382
static Number_Ptr lexed_number(const ParserState& pstate, const std::string& parsed);
381383
static Number_Ptr lexed_dimension(const ParserState& pstate, const std::string& parsed);
382384
static Number_Ptr lexed_percentage(const ParserState& pstate, const std::string& parsed);
383-
static Expression_Ptr lexed_hex_color(const ParserState& pstate, const std::string& parsed);
385+
static Value_Ptr lexed_hex_color(const ParserState& pstate, const std::string& parsed);
384386
private:
385387
Number_Ptr lexed_number(const std::string& parsed) { return lexed_number(pstate, parsed); };
386388
Number_Ptr lexed_dimension(const std::string& parsed) { return lexed_dimension(pstate, parsed); };
387389
Number_Ptr lexed_percentage(const std::string& parsed) { return lexed_percentage(pstate, parsed); };
388-
Expression_Ptr lexed_hex_color(const std::string& parsed) { return lexed_hex_color(pstate, parsed); };
390+
Value_Ptr lexed_hex_color(const std::string& parsed) { return lexed_hex_color(pstate, parsed); };
389391

390392
static const char* re_attr_sensitive_close(const char* src);
391393
static const char* re_attr_insensitive_close(const char* src);

0 commit comments

Comments
 (0)