From 3d1345deccac164b8487b796d4ab2a9767c0c2b2 Mon Sep 17 00:00:00 2001 From: Marcel Greter Date: Wed, 5 Nov 2014 20:32:34 +0100 Subject: [PATCH] Handles leading zeros (numbers) as seen in input --- ast.hpp | 5 ++++- eval.cpp | 29 +++++++++++++++++++++++++---- inspect.cpp | 4 ++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/ast.hpp b/ast.hpp index 5e9b2b415..f6200eef9 100644 --- a/ast.hpp +++ b/ast.hpp @@ -992,13 +992,15 @@ namespace Sass { //////////////////////////////////////////////// class Number : public Expression { ADD_PROPERTY(double, value); + ADD_PROPERTY(bool, zero); vector numerator_units_; vector 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()), denominator_units_(vector()), hash_(0) @@ -1006,6 +1008,7 @@ namespace Sass { if (!u.empty()) numerator_units_.push_back(u); concrete_type(NUMBER); } + bool zero() { return zero_; } vector& numerator_units() { return numerator_units_; } vector& denominator_units() { return denominator_units_; } string type() { return "number"; } diff --git a/eval.cpp b/eval.cpp index 9d6218ac8..0a763db63 100644 --- a/eval.cpp +++ b/eval.cpp @@ -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(value)->value(); + // behave according to as ruby sass (add leading zero) + if (value->concrete_type() == Expression::NUMBER) { + Number* n = static_cast(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; } @@ -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 '#' @@ -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) diff --git a/inspect.cpp b/inspect.cpp index ec79b86aa..c412922d7 100644 --- a/inspect.cpp +++ b/inspect.cpp @@ -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()); }