Skip to content

Commit

Permalink
Merge pull request sass#624 from mgreter/issue/550
Browse files Browse the repository at this point in the history
Output leading zeros on numbers as seen in input (Fixes sass#550)
  • Loading branch information
mgreter committed Nov 6, 2014
2 parents 5673ff4 + 3d1345d commit ca82f08
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
5 changes: 4 additions & 1 deletion ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,20 +992,23 @@ namespace Sass {
////////////////////////////////////////////////
class Number : public Expression {
ADD_PROPERTY(double, value);
ADD_PROPERTY(bool, zero);
vector<string> numerator_units_;
vector<string> denominator_units_;
size_t hash_;
public:
Number(string path, Position position, double val, string u = "")
Number(string path, Position position, double val, string u = "", bool zero = true)
: Expression(path, position),
value_(val),
zero_(zero),
numerator_units_(vector<string>()),
denominator_units_(vector<string>()),
hash_(0)
{
if (!u.empty()) numerator_units_.push_back(u);
concrete_type(NUMBER);
}
bool zero() { return zero_; }
vector<string>& numerator_units() { return numerator_units_; }
vector<string>& denominator_units() { return denominator_units_; }
string type() { return "number"; }
Expand Down
29 changes: 25 additions & 4 deletions eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,16 @@ namespace Sass {
// cerr << "name: " << v->name() << "; type: " << typeid(*value).name() << "; value: " << value->perform(&to_string) << endl;
if (typeid(*value) == typeid(Argument)) value = static_cast<Argument*>(value)->value();

// behave according to as ruby sass (add leading zero)
if (value->concrete_type() == Expression::NUMBER) {
Number* n = static_cast<Number*>(value);
value = new (ctx.mem) Number(n->path(),
n->position(),
n->value(),
n->unit(),
true);
}

// cerr << "\ttype is now: " << typeid(*value).name() << endl << endl;
return value;
}
Expand All @@ -458,24 +468,30 @@ namespace Sass {
{
using Prelexer::number;
Expression* result = 0;
bool zero = !( t->value().substr(0, 1) == "." ||
t->value().substr(0, 2) == "-." );
switch (t->type())
{
case Textual::NUMBER:
result = new (ctx.mem) Number(t->path(),
t->position(),
atof(t->value().c_str()));
atof(t->value().c_str()),
"",
zero);
break;
case Textual::PERCENTAGE:
result = new (ctx.mem) Number(t->path(),
t->position(),
atof(t->value().c_str()),
"%");
"%",
zero);
break;
case Textual::DIMENSION:
result = new (ctx.mem) Number(t->path(),
t->position(),
atof(t->value().c_str()),
Token(number(t->value().c_str())));
Token(number(t->value().c_str())),
zero);
break;
case Textual::HEX: {
string hext(t->value().substr(1)); // chop off the '#'
Expand Down Expand Up @@ -507,7 +523,12 @@ namespace Sass {

Expression* Eval::operator()(Number* n)
{
return n;
// behave according to as ruby sass (add leading zero)
return new (ctx.mem) Number(n->path(),
n->position(),
n->value(),
n->unit(),
true);
}

Expression* Eval::operator()(Boolean* b)
Expand Down
4 changes: 4 additions & 0 deletions inspect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ namespace Sass {
if (n->numerator_units().size() > 1 || n->denominator_units().size() > 0) {
error(d + n->unit() + " is not a valid CSS value", n->path(), n->position());
}
if (!n->zero()) {
if (d.substr(0, 3) == "-0.") d.erase(1, 1);
if (d.substr(0, 2) == "0.") d.erase(0, 1);
}
append_to_buffer(d == "-0" ? "0" : d);
append_to_buffer(n->unit());
}
Expand Down

0 comments on commit ca82f08

Please sign in to comment.