Skip to content

Commit b4cd48f

Browse files
committed
Merge pull request #1179 from mgreter/bugfix/issue_1178
Fix a few list output edge cases
2 parents 8768c44 + 265ced8 commit b4cd48f

File tree

7 files changed

+51
-17
lines changed

7 files changed

+51
-17
lines changed

emitter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ namespace Sass {
1818
in_wrapped(false),
1919
in_media_block(false),
2020
in_declaration(false),
21-
in_declaration_list(false)
21+
in_space_array(false),
22+
in_comma_array(false)
2223
{ }
2324

2425
// return buffer as string

emitter.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,17 @@ namespace Sass {
3737
bool scheduled_delimiter;
3838

3939
public:
40+
// output strings different in comments
4041
bool in_comment;
42+
// selector list does not get linefeeds
4143
bool in_wrapped;
44+
// lists always get a space after delimiter
4245
bool in_media_block;
46+
// nested list must not have parentheses
4347
bool in_declaration;
44-
bool in_declaration_list;
48+
// nested lists need parentheses
49+
bool in_space_array;
50+
bool in_comma_array;
4551

4652
public:
4753
// return buffer as string

functions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ namespace Sass {
15541554
Output_Style old_style;
15551555
old_style = ctx.output_style;
15561556
ctx.output_style = NESTED;
1557-
To_String to_string(&ctx);
1557+
To_String to_string(&ctx, false);
15581558
string inspect = v->perform(&to_string);
15591559
ctx.output_style = old_style;
15601560
return new (ctx.mem) String_Constant(pstate, inspect);

inspect.cpp

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
#include "inspect.hpp"
2-
#include "ast.hpp"
3-
#include "context.hpp"
4-
#include "utf8/checked.h"
51
#include <cmath>
62
#include <string>
73
#include <iostream>
84
#include <iomanip>
95
#include <stdint.h>
106
#include <stdint.h>
117

8+
#include "ast.hpp"
9+
#include "inspect.hpp"
10+
#include "context.hpp"
11+
#include "utf8/checked.h"
12+
1213
namespace Sass {
1314
using namespace std;
1415

@@ -99,9 +100,10 @@ namespace Sass {
99100
append_token(at_rule->keyword(), at_rule);
100101
if (at_rule->selector()) {
101102
append_mandatory_space();
103+
bool was_wrapped = in_wrapped;
102104
in_wrapped = true;
103105
at_rule->selector()->perform(this);
104-
in_wrapped = false;
106+
in_wrapped = was_wrapped;
105107
}
106108
if (at_rule->block()) {
107109
at_rule->block()->perform(this);
@@ -114,6 +116,7 @@ namespace Sass {
114116
void Inspect::operator()(Declaration* dec)
115117
{
116118
if (dec->value()->concrete_type() == Expression::NULL_VAL) return;
119+
bool was_decl = in_declaration;
117120
in_declaration = true;
118121
if (output_style() == NESTED)
119122
indentation += dec->tabs();
@@ -128,7 +131,7 @@ namespace Sass {
128131
append_delimiter();
129132
if (output_style() == NESTED)
130133
indentation -= dec->tabs();
131-
in_declaration = false;
134+
in_declaration = was_decl;
132135
}
133136

134137
void Inspect::operator()(Assignment* assn)
@@ -346,7 +349,19 @@ namespace Sass {
346349
else if (in_media_block && sep != " ") sep += " "; // verified
347350
if (list->empty()) return;
348351
bool items_output = false;
349-
in_declaration_list = in_declaration;
352+
353+
bool was_space_array = in_space_array;
354+
bool was_comma_array = in_comma_array;
355+
if (!in_declaration && (
356+
(list->separator() == List::SPACE && in_space_array) ||
357+
(list->separator() == List::COMMA && in_comma_array)
358+
)) {
359+
append_string("(");
360+
}
361+
362+
if (list->separator() == List::SPACE) in_space_array = true;
363+
else if (list->separator() == List::COMMA) in_comma_array = true;
364+
350365
for (size_t i = 0, L = list->length(); i < L; ++i) {
351366
Expression* list_item = (*list)[i];
352367
if (list_item->is_invisible()) {
@@ -360,7 +375,16 @@ namespace Sass {
360375
list_item->perform(this);
361376
items_output = true;
362377
}
363-
in_declaration_list = false;
378+
379+
in_comma_array = was_comma_array;
380+
in_space_array = was_space_array;
381+
if (!in_declaration && (
382+
(list->separator() == List::SPACE && in_space_array) ||
383+
(list->separator() == List::COMMA && in_comma_array)
384+
)) {
385+
append_string(")");
386+
}
387+
364388
}
365389

366390
void Inspect::operator()(Binary_Expression* expr)

output.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,19 +349,21 @@ namespace Sass {
349349

350350
append_scope_opener();
351351

352+
bool format = kwd != "@font-face";;
353+
352354
for (size_t i = 0, L = b->length(); i < L; ++i) {
353355
Statement* stm = (*b)[i];
354356
if (!stm->is_hoistable()) {
355357
stm->perform(this);
356-
if (i < L - 1) append_special_linefeed();
358+
if (i < L - 1 && format) append_special_linefeed();
357359
}
358360
}
359361

360362
for (size_t i = 0, L = b->length(); i < L; ++i) {
361363
Statement* stm = (*b)[i];
362364
if (stm->is_hoistable()) {
363365
stm->perform(this);
364-
if (i < L - 1) append_special_linefeed();
366+
if (i < L - 1 && format) append_special_linefeed();
365367
}
366368
}
367369

to_string.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
namespace Sass {
1212
using namespace std;
1313

14-
To_String::To_String(Context* ctx)
15-
: ctx(ctx) { }
14+
To_String::To_String(Context* ctx, bool in_declaration)
15+
: ctx(ctx), in_declaration(in_declaration) { }
1616
To_String::~To_String() { }
1717

1818
inline string To_String::fallback_impl(AST_Node* n)
1919
{
2020
Emitter emitter(ctx);
2121
Inspect i(emitter);
22-
i.in_declaration_list = true;
22+
i.in_declaration = in_declaration;
2323
n->perform(&i);
2424
return i.get_buffer();
2525
}

to_string.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ namespace Sass {
1818
string fallback_impl(AST_Node* n);
1919

2020
Context* ctx;
21+
bool in_declaration;
2122

2223
public:
23-
To_String(Context* ctx = 0);
24+
To_String(Context* ctx = 0, bool in_declaration = true);
2425
virtual ~To_String();
2526

2627
string operator()(Null* n);

0 commit comments

Comments
 (0)