Skip to content

Commit af234df

Browse files
committed
Improve selectors parsing for nth-child binomials
Fixes #593
1 parent fb82f0b commit af234df

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed

ast.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,21 +1359,22 @@ namespace Sass {
13591359
////////////////////////////////////////////////////////
13601360
class String_Constant : public String {
13611361
ADD_PROPERTY(char, quote_mark);
1362+
ADD_PROPERTY(bool, can_compress_whitespace);
13621363
ADD_PROPERTY(string, value);
13631364
protected:
13641365
size_t hash_;
13651366
public:
13661367
String_Constant(ParserState pstate, string val)
1367-
: String(pstate), quote_mark_(0), value_(read_css_string(val)), hash_(0)
1368+
: String(pstate), quote_mark_(0), can_compress_whitespace_(false), value_(read_css_string(val)), hash_(0)
13681369
{ }
13691370
String_Constant(ParserState pstate, const char* beg)
1370-
: String(pstate), quote_mark_(0), value_(read_css_string(string(beg))), hash_(0)
1371+
: String(pstate), quote_mark_(0), can_compress_whitespace_(false), value_(read_css_string(string(beg))), hash_(0)
13711372
{ }
13721373
String_Constant(ParserState pstate, const char* beg, const char* end)
1373-
: String(pstate), quote_mark_(0), value_(read_css_string(string(beg, end-beg))), hash_(0)
1374+
: String(pstate), quote_mark_(0), can_compress_whitespace_(false), value_(read_css_string(string(beg, end-beg))), hash_(0)
13741375
{ }
13751376
String_Constant(ParserState pstate, const Token& tok)
1376-
: String(pstate), quote_mark_(0), value_(read_css_string(string(tok.begin, tok.end))), hash_(0)
1377+
: String(pstate), quote_mark_(0), can_compress_whitespace_(false), value_(read_css_string(string(tok.begin, tok.end))), hash_(0)
13771378
{ }
13781379
string type() { return "string"; }
13791380
static string type_name() { return "string"; }

debugger.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ inline void debug_ast(AST_Node* node, string ind = "", Env* env = 0)
442442
Binary_Expression* expression = dynamic_cast<Binary_Expression*>(node);
443443
cerr << ind << "Binary_Expression " << expression;
444444
cerr << " (" << pstate_source_position(node) << ")";
445-
cerr << " [" << expression->type() << "]" << endl;
445+
cerr << " [" << expression->type_name() << "]" << endl;
446446
debug_ast(expression->left(), ind + " left: ", env);
447447
debug_ast(expression->right(), ind + " right: ", env);
448448
} else if (dynamic_cast<Map*>(node)) {

output.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,16 @@ namespace Sass {
385385
{
386386
if (String_Quoted* quoted = dynamic_cast<String_Quoted*>(s)) {
387387
return Output::operator()(quoted);
388-
} else if (!in_comment) {
389-
append_token(string_to_output(s->value()), s);
390388
} else {
391-
append_token(s->value(), s);
389+
string value(s->value());
390+
if (s->can_compress_whitespace() && output_style() == COMPRESSED) {
391+
value.erase(std::remove_if(value.begin(), value.end(), ::isspace), value.end());
392+
}
393+
if (!in_comment) {
394+
append_token(string_to_output(value), s);
395+
} else {
396+
append_token(value, s);
397+
}
392398
}
393399
}
394400

parser.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -710,18 +710,9 @@ namespace Sass {
710710
if (lex< alternatives< even, odd > >()) {
711711
expr = new (ctx.mem) String_Quoted(p, lexed);
712712
}
713-
else if (peek< binomial >(position)) {
714-
lex< sequence< optional< coefficient >, exactly<'n'> > >();
715-
String_Constant* var_coef = new (ctx.mem) String_Quoted(p, lexed);
716-
lex< sign >();
717-
String_Constant* op = new (ctx.mem) String_Quoted(p, lexed);
718-
// Binary_Expression::Type op = (lexed == "+" ? Binary_Expression::ADD : Binary_Expression::SUB);
719-
lex< one_plus < digit > >();
720-
String_Constant* constant = new (ctx.mem) String_Quoted(p, lexed);
721-
// expr = new (ctx.mem) Binary_Expression(p, op, var_coef, constant);
722-
String_Schema* schema = new (ctx.mem) String_Schema(p, 3);
723-
*schema << var_coef << op << constant;
724-
expr = schema;
713+
else if (lex< binomial >(position)) {
714+
expr = new (ctx.mem) String_Constant(p, lexed);
715+
((String_Constant*)expr)->can_compress_whitespace(true);
725716
}
726717
else if (peek< sequence< optional<sign>,
727718
zero_plus<digit>,

0 commit comments

Comments
 (0)