Skip to content

Commit

Permalink
Implement line-feed preserving (dirty)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreter committed Feb 22, 2015
1 parent 3964cff commit 1210dfa
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 14 deletions.
1 change: 1 addition & 0 deletions ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ namespace Sass {
return 0;
}
}
rhs->has_line_break(has_line_break());
return Simple_Selector::unify_with(rhs, ctx);
}

Expand Down
8 changes: 7 additions & 1 deletion ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1706,11 +1706,17 @@ namespace Sass {
class Selector : public AST_Node {
ADD_PROPERTY(bool, has_reference);
ADD_PROPERTY(bool, has_placeholder);
// line break before list separator
ADD_PROPERTY(bool, has_line_feed);
// line break after list separator
ADD_PROPERTY(bool, has_line_break);
public:
Selector(ParserState pstate, bool r = false, bool h = false)
: AST_Node(pstate),
has_reference_(r),
has_placeholder_(h)
has_placeholder_(h),
has_line_feed_(false),
has_line_break_(false)
{ }
virtual ~Selector() = 0;
virtual Selector_Placeholder* find_placeholder();
Expand Down
2 changes: 2 additions & 0 deletions contextualize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace Sass {
for (size_t j = 0, L = s->length(); j < L; ++j) {
parent = (*p)[i];
Complex_Selector* comb = static_cast<Complex_Selector*>((*s)[j]->perform(this));
if (parent->has_line_feed()) comb->has_line_feed(parent->has_line_feed() ||true);
if (comb) *ss << comb;
}
}
Expand Down Expand Up @@ -94,6 +95,7 @@ namespace Sass {
return extender;
}
Compound_Selector* ss = new (ctx.mem) Compound_Selector(s->pstate(), s->length());
ss->has_line_break(s->has_line_break());
for (size_t i = 0, L = s->length(); i < L; ++i) {
Simple_Selector* simp = static_cast<Simple_Selector*>((*s)[i]->perform(this));
if (simp) *ss << simp;
Expand Down
25 changes: 20 additions & 5 deletions emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@ namespace Sass {
in_wrapped(false),
in_declaration(false),
in_declaration_list(false),
scheduled_linefeed(false)
scheduled_linefeed(false),
scheduled_space(false),
// start
allow_before(false),
in_media(false),
in_raw_list(false),
space_scheduled(false),
linefeed_scheduled(false),
double_lf_scheduled(false),
delimiter_scheduled(false),
wspace_scheduled("")
// end
{ }

void Emitter::schedule_linefeed(void)
Expand All @@ -35,10 +46,14 @@ namespace Sass {
// append some text or token to the buffer
void Emitter::append_string(const string& text)
{
// is a linefeed is scheduled?
// check the schedule
if (scheduled_linefeed) {
scheduled_space = false;
scheduled_linefeed = false;
append_optional_linefeed();
append_string(ctx ? ctx->linefeed : "\n");
} else if (scheduled_space) {
scheduled_space = false;
append_string(" ");
}
// add to buffer
buffer += text;
Expand Down Expand Up @@ -99,13 +114,13 @@ namespace Sass {
{
if (buffer.size()) {
char lst = buffer.at(buffer.length() - 1);
if (!isspace(lst)) append_string(" ");
if (!isspace(lst)) scheduled_space = true;
}
}

void Emitter::append_optional_linefeed()
{
append_string(ctx ? ctx->linefeed : "\n");
scheduled_linefeed = true;
}

void Emitter::append_scope_opener()
Expand Down
11 changes: 11 additions & 0 deletions emitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace Sass {
bool in_declaration;
bool in_declaration_list;
bool scheduled_linefeed;
bool scheduled_space;

public:
// return buffer as string
Expand Down Expand Up @@ -62,6 +63,16 @@ namespace Sass {
public: // try to be smart
void schedule_linefeed(void);

protected:
bool allow_before;
bool in_media;
bool in_raw_list;
bool space_scheduled;
bool linefeed_scheduled;
bool double_lf_scheduled;
bool delimiter_scheduled;
string wspace_scheduled;

};

}
Expand Down
3 changes: 3 additions & 0 deletions extend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,7 @@ namespace Sass {
#endif


if (pSelector && pSelector->has_line_feed()) pNewSelector->has_line_feed(true);

// Set the sources on our new Complex_Selector to the sources of this simple sequence plus the thing we're extending.
DEBUG_PRINTLN(EXTEND_COMPOUND, "SOURCES SETTING ON NEW SEQ: " << complexSelectorToNode(pNewSelector, ctx))
Expand Down Expand Up @@ -1701,6 +1702,8 @@ namespace Sass {
ExtensionSubsetMap& subsetMap,
set<Compound_Selector> seen) {

pComplexSelector->tail()->has_line_feed(pComplexSelector->has_line_feed());

Node complexSelector = complexSelectorToNode(pComplexSelector, ctx);

DEBUG_PRINTLN(EXTEND_COMPLEX, "EXTEND COMPLEX: " << complexSelector)
Expand Down
33 changes: 31 additions & 2 deletions inspect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ namespace Sass {
append_indentation();
append_token("@media", media_block);
append_mandatory_space();
in_media = true;
media_block->media_queries()->perform(this);
in_media = false;
media_block->block()->perform(this);
}

Expand All @@ -95,6 +97,7 @@ namespace Sass {

void Inspect::operator()(At_Rule* at_rule)
{
in_raw_list = true;
append_indentation();
append_token(at_rule->keyword(), at_rule);
if (at_rule->selector()) {
Expand All @@ -107,6 +110,7 @@ namespace Sass {
else {
append_delimiter();
}
in_raw_list = false;
}

void Inspect::operator()(Declaration* dec)
Expand Down Expand Up @@ -342,6 +346,7 @@ namespace Sass {
void Inspect::operator()(List* list)
{
string sep(list->separator() == List::SPACE ? " " : ",");
if (in_media && sep == "," && !in_declaration_list) sep = ", ";
if (list->empty()) return;
bool items_output = false;
in_declaration_list = in_declaration;
Expand All @@ -352,6 +357,8 @@ namespace Sass {
}
if (items_output) {
append_string(sep);
// if (!list_item->pstate().token.ws_before().empty())
// append_wspace(list_item->pstate().token.ws_before());
}
if (items_output && sep != " ")
append_optional_space();
Expand Down Expand Up @@ -393,7 +400,9 @@ namespace Sass {
void Inspect::operator()(Function_Call* call)
{
append_token(call->name(), call);
in_media = true;
call->arguments()->perform(this);
in_media = false;
}

void Inspect::operator()(Function_Call_Schema* call)
Expand Down Expand Up @@ -756,16 +765,22 @@ namespace Sass {
void Inspect::operator()(Selector_Placeholder* s)
{
append_token(s->name(), s);
if (s->has_line_break()) append_optional_linefeed();
if (s->has_line_break()) append_indentation();

}

void Inspect::operator()(Type_Selector* s)
{
allow_before = false;
append_token(s->name(), s);
}

void Inspect::operator()(Selector_Qualifier* s)
{
append_token(s->name(), s);
if (s->has_line_break()) append_optional_linefeed();
if (s->has_line_break()) append_indentation();
}

void Inspect::operator()(Attribute_Selector* s)
Expand Down Expand Up @@ -796,7 +811,9 @@ namespace Sass {
void Inspect::operator()(Wrapped_Selector* s)
{
append_token(s->name(), s);
in_wrapped = true;
s->selector()->perform(this);
in_wrapped = false;
append_string(")");
}

Expand All @@ -805,6 +822,9 @@ namespace Sass {
for (size_t i = 0, L = s->length(); i < L; ++i) {
(*s)[i]->perform(this);
}
if (!in_raw_list) {
if (s->has_line_break()) append_optional_linefeed();
}
}

void Inspect::operator()(Complex_Selector* c)
Expand All @@ -822,21 +842,30 @@ namespace Sass {
case Complex_Selector::ADJACENT_TO: append_string("+"); break;
}
if (tail && comb != Complex_Selector::ANCESTOR_OF) {
append_optional_space();
// render line break after combinator
if (c->has_line_break()) {
append_optional_linefeed();
} else append_optional_space();
}
if (tail) tail->perform(this);
}

void Inspect::operator()(Selector_List* g)
{
allow_before = false;
if (g->empty()) return;
append_indentation();
for (size_t i = 0, L = g->length(); i < L; ++i) {
if (i == 0 && !in_wrapped) append_indentation();
smap.add_open_mapping((*g)[i]);
(*g)[i]->perform(this);
smap.add_close_mapping((*g)[i]);
if (i < L - 1) {
append_comma_separator();
allow_before = true;
if ((*g)[i]->has_line_feed()) {
append_optional_linefeed();
append_indentation();
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ namespace Sass {


Node::Node(const TYPE& type, Complex_Selector::Combinator combinator, Complex_Selector* pSelector, NodeDequePtr& pCollection)
: mType(type), mCombinator(combinator), mpSelector(pSelector), mpCollection(pCollection)
{}
: got_line_feed(false), mType(type), mCombinator(combinator), mpSelector(pSelector), mpCollection(pCollection)
{ /* if (pSelector) has_line_feed = pSelector->has_line_feed(); */ }


Node Node::clone(Context& ctx) const {
Expand Down Expand Up @@ -241,6 +241,7 @@ namespace Sass {
Selector_Reference* selectorRef = new (ctx.mem) Selector_Reference(ParserState("[NODE]"), NULL);
fakeHead->elements().push_back(selectorRef);
pFirst->head(fakeHead);
pFirst->has_line_feed(pFirst->has_line_feed() || pFirst->tail()->has_line_feed() || toConvert.got_line_feed);

return pFirst;
}
Expand Down
1 change: 1 addition & 0 deletions node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace Sass {
bool isSelector() const { return mType == SELECTOR; }
bool isCollection() const { return mType == COLLECTION; }
bool isNil() const { return mType == NIL; }
bool got_line_feed;

Complex_Selector::Combinator combinator() const { return mCombinator; }

Expand Down
7 changes: 6 additions & 1 deletion output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ namespace Sass {
append_indentation();
append_token("@supports", f);
append_mandatory_space();
in_media = true;
q->perform(this);
in_media = false;
append_scope_opener();

bool old_in_directive = in_directive;
Expand Down Expand Up @@ -312,7 +314,9 @@ namespace Sass {
append_indentation();
append_token("@media", m);
append_mandatory_space();
in_media = true;
q->perform(this);
in_media = false;
append_scope_opener();

bool old_in_directive = in_directive;
Expand Down Expand Up @@ -381,6 +385,7 @@ namespace Sass {
in_keyframes = kwd.compare("@keyframes") == 0;
if (output_style() == NESTED) if (!in_keyframes) indentation += a->tabs();

in_raw_list = !in_keyframes;
append_indentation();
append_token(kwd, a);
if (s) {
Expand All @@ -391,7 +396,7 @@ namespace Sass {
append_mandatory_space();
v->perform(this);
}

in_raw_list = false;
if (!b) {
append_delimiter();
return;
Expand Down
Loading

0 comments on commit 1210dfa

Please sign in to comment.