Skip to content

Commit

Permalink
Merge pull request sass#1179 from mgreter/bugfix/issue_1178
Browse files Browse the repository at this point in the history
Fix a few list output edge cases
  • Loading branch information
mgreter committed May 9, 2015
2 parents 8768c44 + 265ced8 commit b4cd48f
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 17 deletions.
3 changes: 2 additions & 1 deletion emitter.cpp
Original file line number Diff line number Diff line change
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
8 changes: 7 additions & 1 deletion emitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ namespace Sass {
bool scheduled_delimiter;

public:
// output strings different in comments
bool in_comment;
// selector list does not get linefeeds
bool in_wrapped;
// lists always get a space after delimiter
bool in_media_block;
// nested list must not have parentheses
bool in_declaration;
bool in_declaration_list;
// nested lists need parentheses
bool in_space_array;
bool in_comma_array;

public:
// return buffer as string
Expand Down
2 changes: 1 addition & 1 deletion functions.cpp
Original file line number Diff line number Diff line change
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
40 changes: 32 additions & 8 deletions inspect.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "inspect.hpp"
#include "ast.hpp"
#include "context.hpp"
#include "utf8/checked.h"
#include <cmath>
#include <string>
#include <iostream>
#include <iomanip>
#include <stdint.h>
#include <stdint.h>

#include "ast.hpp"
#include "inspect.hpp"
#include "context.hpp"
#include "utf8/checked.h"

namespace Sass {
using namespace std;

Expand Down Expand Up @@ -99,9 +100,10 @@ namespace Sass {
append_token(at_rule->keyword(), at_rule);
if (at_rule->selector()) {
append_mandatory_space();
bool was_wrapped = in_wrapped;
in_wrapped = true;
at_rule->selector()->perform(this);
in_wrapped = false;
in_wrapped = was_wrapped;
}
if (at_rule->block()) {
at_rule->block()->perform(this);
Expand All @@ -114,6 +116,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 +131,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 +349,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 +375,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: 4 additions & 2 deletions output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,19 +349,21 @@ namespace Sass {

append_scope_opener();

bool format = kwd != "@font-face";;

for (size_t i = 0, L = b->length(); i < L; ++i) {
Statement* stm = (*b)[i];
if (!stm->is_hoistable()) {
stm->perform(this);
if (i < L - 1) append_special_linefeed();
if (i < L - 1 && format) append_special_linefeed();
}
}

for (size_t i = 0, L = b->length(); i < L; ++i) {
Statement* stm = (*b)[i];
if (stm->is_hoistable()) {
stm->perform(this);
if (i < L - 1) append_special_linefeed();
if (i < L - 1 && format) append_special_linefeed();
}
}

Expand Down
6 changes: 3 additions & 3 deletions to_string.cpp
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 b4cd48f

Please sign in to comment.