Skip to content

Commit

Permalink
Fix a few list output edge cases
Browse files Browse the repository at this point in the history
Don't add parentheses for nested lists in declarations.
  • Loading branch information
mgreter committed May 9, 2015
1 parent 348fde8 commit 99f60f8
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 10 deletions.
3 changes: 2 additions & 1 deletion emitter.cpp
Expand Up @@ -18,7 +18,8 @@ namespace Sass {
in_wrapped(false),
in_media_block(false),
in_declaration(false),
in_declaration_list(false)
in_space_array(false),
in_comma_array(false)
{ }

// return buffer as string
Expand Down
3 changes: 2 additions & 1 deletion emitter.hpp
Expand Up @@ -41,7 +41,8 @@ namespace Sass {
bool in_wrapped;
bool in_media_block;
bool in_declaration;
bool in_declaration_list;
bool in_space_array;
bool in_comma_array;

public:
// return buffer as string
Expand Down
2 changes: 1 addition & 1 deletion functions.cpp
Expand Up @@ -1554,7 +1554,7 @@ namespace Sass {
Output_Style old_style;
old_style = ctx.output_style;
ctx.output_style = NESTED;
To_String to_string(&ctx);
To_String to_string(&ctx, false);
string inspect = v->perform(&to_string);
ctx.output_style = old_style;
return new (ctx.mem) String_Constant(pstate, inspect);
Expand Down
28 changes: 25 additions & 3 deletions inspect.cpp
Expand Up @@ -114,6 +114,7 @@ namespace Sass {
void Inspect::operator()(Declaration* dec)
{
if (dec->value()->concrete_type() == Expression::NULL_VAL) return;
bool was_decl = in_declaration;
in_declaration = true;
if (output_style() == NESTED)
indentation += dec->tabs();
Expand All @@ -128,7 +129,7 @@ namespace Sass {
append_delimiter();
if (output_style() == NESTED)
indentation -= dec->tabs();
in_declaration = false;
in_declaration = was_decl;
}

void Inspect::operator()(Assignment* assn)
Expand Down Expand Up @@ -346,7 +347,19 @@ namespace Sass {
else if (in_media_block && sep != " ") sep += " "; // verified
if (list->empty()) return;
bool items_output = false;
in_declaration_list = in_declaration;

bool was_space_array = in_space_array;
bool was_comma_array = in_comma_array;
if (!in_declaration && (
(list->separator() == List::SPACE && in_space_array) ||
(list->separator() == List::COMMA && in_comma_array)
)) {
append_string("(");
}

if (list->separator() == List::SPACE) in_space_array = true;
else if (list->separator() == List::COMMA) in_comma_array = true;

for (size_t i = 0, L = list->length(); i < L; ++i) {
Expression* list_item = (*list)[i];
if (list_item->is_invisible()) {
Expand All @@ -360,7 +373,16 @@ namespace Sass {
list_item->perform(this);
items_output = true;
}
in_declaration_list = false;

in_comma_array = was_comma_array;
in_space_array = was_space_array;
if (!in_declaration && (
(list->separator() == List::SPACE && in_space_array) ||
(list->separator() == List::COMMA && in_comma_array)
)) {
append_string(")");
}

}

void Inspect::operator()(Binary_Expression* expr)
Expand Down
6 changes: 3 additions & 3 deletions to_string.cpp
Expand Up @@ -11,15 +11,15 @@
namespace Sass {
using namespace std;

To_String::To_String(Context* ctx)
: ctx(ctx) { }
To_String::To_String(Context* ctx, bool in_declaration)
: ctx(ctx), in_declaration(in_declaration) { }
To_String::~To_String() { }

inline string To_String::fallback_impl(AST_Node* n)
{
Emitter emitter(ctx);
Inspect i(emitter);
i.in_declaration_list = true;
i.in_declaration = in_declaration;
n->perform(&i);
return i.get_buffer();
}
Expand Down
3 changes: 2 additions & 1 deletion to_string.hpp
Expand Up @@ -18,9 +18,10 @@ namespace Sass {
string fallback_impl(AST_Node* n);

Context* ctx;
bool in_declaration;

public:
To_String(Context* ctx = 0);
To_String(Context* ctx = 0, bool in_declaration = true);
virtual ~To_String();

string operator()(Null* n);
Expand Down

0 comments on commit 99f60f8

Please sign in to comment.