Skip to content

Commit

Permalink
Merge pull request sass#172 from rowanbeentje/master
Browse files Browse the repository at this point in the history
Fix some division, color and null issues already covered by failing tests
  • Loading branch information
HamptonMakes committed Oct 23, 2013
2 parents 0fb15d9 + 030d078 commit 03f9d07
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 23 deletions.
5 changes: 3 additions & 2 deletions ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,9 +734,10 @@ namespace Sass {
ADD_PROPERTY(double, g);
ADD_PROPERTY(double, b);
ADD_PROPERTY(double, a);
ADD_PROPERTY(string, disp);
public:
Color(string p, size_t l, double r, double g, double b, double a = 1)
: Expression(p, l), r_(r), g_(g), b_(b), a_(a)
Color(string p, size_t l, double r, double g, double b, double a = 1, const string disp = "")
: Expression(p, l), r_(r), g_(g), b_(b), a_(a), disp_(disp)
{ concrete_type(COLOR); }
string type() { return "color"; }
static string type_name() { return "color"; }
Expand Down
2 changes: 1 addition & 1 deletion ast_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace Sass {
Number* new_Number(string p, size_t l, double val);
Percentage* new_Percentage(string p, size_t l, double val);
Dimension* new_Dimension(string p, size_t l, double val, string unit);
Color* new_Color(string p, size_t l, double r, double g, double b, double a = 1);
Color* new_Color(string p, size_t l, double r, double g, double b, double a = 1, string disp = "");
Boolean* new_Boolean(string p, size_t l, bool val);
String_Schema* new_String_Schema(string p, size_t l, size_t size = 0);
String_Constant* new_String_Constant(string p, size_t l, string val);
Expand Down
24 changes: 22 additions & 2 deletions eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ namespace Sass {
}
else {
To_String to_string;
// Special cases: +/- variables which evaluate to null ouput just +/-,
// but +/- null itself outputs the string
if (operand->concrete_type() == Expression::NULL_VAL && typeid(*(u->operand())) == typeid(Variable)) {
u->operand(new (ctx.mem) String_Constant(u->path(), u->line(), ""));
}
String_Constant* result = new (ctx.mem) String_Constant(u->path(),
u->line(),
u->perform(&to_string));
Expand Down Expand Up @@ -429,14 +434,18 @@ namespace Sass {
t->line(),
static_cast<double>(strtol(r.c_str(), NULL, 16)),
static_cast<double>(strtol(g.c_str(), NULL, 16)),
static_cast<double>(strtol(b.c_str(), NULL, 16)));
static_cast<double>(strtol(b.c_str(), NULL, 16)),
1,
t->value());
}
else {
result = new (ctx.mem) Color(t->path(),
t->line(),
static_cast<double>(strtol(string(2,hext[0]).c_str(), NULL, 16)),
static_cast<double>(strtol(string(2,hext[1]).c_str(), NULL, 16)),
static_cast<double>(strtol(string(2,hext[2]).c_str(), NULL, 16)));
static_cast<double>(strtol(string(2,hext[2]).c_str(), NULL, 16)),
1,
t->value());
}
} break;
}
Expand Down Expand Up @@ -691,6 +700,10 @@ namespace Sass {
{
Number* l = static_cast<Number*>(lhs);
Color* r = static_cast<Color*>(rhs);
// TODO: currently SASS converts colors to standard form when adding to strings;
// when https://github.com/nex3/sass/issues/363 is added this can be removed to
// preserve the original value
r->disp("");
double lv = l->value();
switch (op) {
case Binary_Expression::ADD:
Expand Down Expand Up @@ -759,6 +772,13 @@ namespace Sass {
To_String to_string;
Expression::Concrete_Type ltype = lhs->concrete_type();
Expression::Concrete_Type rtype = rhs->concrete_type();

// TODO: currently SASS converts colors to standard form when adding to strings;
// when https://github.com/nex3/sass/issues/363 is added this can be removed to
// preserve the original value
if (ltype == Expression::COLOR) ((Sass::Color*)lhs)->disp("");
if (rtype == Expression::COLOR) ((Sass::Color*)rhs)->disp("");

string lstr(lhs->perform(&to_string));
string rstr(rhs->perform(&to_string));
bool unquoted = false;
Expand Down
1 change: 1 addition & 0 deletions functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ namespace Sass {
Color* c_arg = ARG("$color", Color);
Color* new_c = new (ctx.mem) Color(*c_arg);
new_c->a(ARGR("$alpha", Number, 0, 1)->value());
new_c->disp("");
return new_c;
}

Expand Down
37 changes: 23 additions & 14 deletions inspect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,15 @@ namespace Sass {
{
string sep(list->separator() == List::SPACE ? " " : ", ");
if (list->empty()) return;
Expression* first = (*list)[0];
bool first_invisible = first->is_invisible();
if (!first_invisible) first->perform(this);
for (size_t i = 1, L = list->length(); i < L; ++i) {
Expression* next = (*list)[i];
bool next_invisible = next->is_invisible();
if (i == 1 && !first_invisible && !next_invisible) buffer += sep;
else if (!next_invisible) buffer += sep;
next->perform(this);
bool items_output = false;
for (size_t i = 0, L = list->length(); i < L; ++i) {
Expression* list_item = (*list)[i];
if (list_item->is_invisible()) {
continue;
}
if (items_output) buffer += sep;
list_item->perform(this);
items_output = true;
}
}

Expand Down Expand Up @@ -332,7 +332,12 @@ namespace Sass {
// ss << ctx.colors_to_names[numval];
// }
// else
if (a >= 1) {

// retain the originally specified color definition if unchanged
if (!c->disp().empty()) {
ss << c->disp();
}
else if (a >= 1) {
// see if it's a named color
int numval = r * 0x10000;
numval += g * 0x100;
Expand Down Expand Up @@ -412,10 +417,10 @@ namespace Sass {
}
}

// void Inspect::operator()(Null* n)
// {
// buffer += "null";
// }
void Inspect::operator()(Null* n)
{
buffer += "null";
}

// parameters and arguments
void Inspect::operator()(Parameter* p)
Expand Down Expand Up @@ -449,6 +454,10 @@ namespace Sass {
buffer += a->name();
buffer += ": ";
}
// Special case: argument nulls can be ignored
if (a->value()->concrete_type() == Expression::NULL_VAL) {
return;
}
a->value()->perform(this);
if (a->is_rest_argument()) {
buffer += "...";
Expand Down
2 changes: 1 addition & 1 deletion inspect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace Sass {
virtual void operator()(String_Constant*);
virtual void operator()(Media_Query*);
virtual void operator()(Media_Query_Expression*);
// virtual void operator()(Null*);
virtual void operator()(Null*);
// parameters and arguments
virtual void operator()(Parameter*);
virtual void operator()(Parameters*);
Expand Down
12 changes: 9 additions & 3 deletions output_nested.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,22 @@ namespace Sass {
// Check print conditions
if (typeid(*stm) == typeid(Declaration)) {
Declaration* dec = static_cast<Declaration*>(stm);
if (dec->value()->concrete_type() == Expression::NULL_VAL) {
bPrintExpression = false;
}
if (dec->value()->concrete_type() == Expression::STRING) {
String_Constant* valConst = static_cast<String_Constant*>(dec->value());
string val(valConst->value());
if (val.empty()) {
bPrintExpression = false;
}
}
else if (dec->value()->concrete_type() == Expression::LIST) {
List* list = static_cast<List*>(dec->value());
bool all_invisible = true;
for (size_t list_i = 0, list_L = list->length(); list_i < list_L; ++list_i) {
Expression* item = (*list)[list_i];
if (!item->is_invisible()) all_invisible = false;
}
if (all_invisible) bPrintExpression = false;
}
}
// Print if OK
if (!stm->is_hoistable() && bPrintExpression) {
Expand Down
5 changes: 5 additions & 0 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,9 +856,14 @@ namespace Sass {
Expression* value = parse_comma_list();
if (!lex< exactly<')'> >()) error("unclosed parenthesis");
value->is_delayed(false);
// make sure wrapped lists and division expressions are non-delayed within parentheses
if (value->concrete_type() == Expression::LIST) {
List* l = static_cast<List*>(value);
if (!l->empty()) (*l)[0]->is_delayed(false);
} else if (typeid(*value) == typeid(Binary_Expression)) {
Binary_Expression* b = static_cast<Binary_Expression*>(value);
Binary_Expression* lhs = static_cast<Binary_Expression*>(b->left());
if (lhs && lhs->type() == Binary_Expression::DIV) lhs->is_delayed(false);
}
return value;
}
Expand Down
2 changes: 2 additions & 0 deletions to_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ namespace Sass {
return i.get_buffer();
}

inline string To_String::operator()(Null* n)
{ return ""; }
}
3 changes: 3 additions & 0 deletions to_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Sass {
using namespace std;

class Context;
class Null;

class To_String : public Operation_CRTP<string, To_String> {
// import all the class-specific methods and override as desired
Expand All @@ -23,6 +24,8 @@ namespace Sass {
To_String(Context* ctx = 0);
virtual ~To_String();

string operator()(Null* n);

template <typename U>
string fallback(U n) { return fallback_impl(n); }
};
Expand Down

0 comments on commit 03f9d07

Please sign in to comment.