Skip to content

Commit

Permalink
Bubble rule nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
xzyfer committed Jan 5, 2015
1 parent ca88350 commit 5de59c0
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 11 deletions.
5 changes: 5 additions & 0 deletions ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ namespace Sass {
for (size_t i = 0, L = v->length(); i < L; ++i) *this << (*v)[i];
return *this;
}
Vectorized& unshift(T element)
{
elements_.insert(elements_.begin(), element);
return *this;
}
vector<T>& elements() { return elements_; }
const vector<T>& elements() const { return elements_; }
vector<T>& elements(vector<T>& e) { elements_ = e; return elements_; }
Expand Down
86 changes: 76 additions & 10 deletions cssize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,39 @@ namespace Sass {
r->block()->perform(this)->block());
p_stack.pop_back();

return rr;
Block* props = new Block(rr->block()->path(), rr->block()->position());
for (size_t i = 0, L = rr->block()->length(); i < L; i++)
{
Statement* s = (*rr->block())[i];
if (!bubblable(s)) *props << s;
}

Block* rules = new Block(rr->block()->path(), rr->block()->position());
for (size_t i = 0, L = rr->block()->length(); i < L; i++)
{
Statement* s = (*rr->block())[i];
if (bubblable(s)) *rules << s;
}

if (props->length())
{
Block* bb = new Block(rr->block()->path(), rr->block()->position());
*bb += props;
// rules.each {|r| r.tabs += 1} if node.style == :nested
rr->block(bb);
rules->unshift(rr);
}

rules = debubble(rules)->block();

return rules;
}

Statement* Cssize::operator()(Media_Block* m)
{
if (parent()->statement_type() == Statement::RULESET)
{ return bubble(m); }

if (parent()->statement_type() == Statement::MEDIA)
{ return new (ctx.mem) Bubble(m->path(), m->position(), m); }

Expand All @@ -63,6 +91,33 @@ namespace Sass {
return debubble(mm->block(), mm)->block();
}

Statement* Cssize::bubble(Media_Block* m)
{
Ruleset* parent = static_cast<Ruleset*>(this->parent());

Block* bb = new (ctx.mem) Block(parent->block()->path(), parent->block()->position());
Ruleset* new_rule = new (ctx.mem) Ruleset(parent->path(),
parent->position(),
parent->selector(),
bb);

for (size_t i = 0, L = m->block()->length(); i < L; ++i) {
*new_rule->block() << (*m->block())[i];
}

Block* wrapper_block = new (ctx.mem) Block(m->block()->path(), m->block()->position());
*wrapper_block << new_rule;
Media_Block* mm = new (ctx.mem) Media_Block(m->path(),
m->position(),
m->media_queries(),
wrapper_block,
m->selector());

Bubble* bubble = new (ctx.mem) Bubble(mm->path(), mm->position(), mm);

return bubble;
}

bool Cssize::bubblable(Statement* s)
{
return s->statement_type() == Statement::RULESET || s->bubbles();
Expand Down Expand Up @@ -113,7 +168,7 @@ namespace Sass {
{
Has_Block* previous_parent = 0;
vector<pair<bool, Block*>> baz = slice_by_bubble(children);
Block* result = new (ctx.mem) Block(parent->path(), parent->position());
Block* result = new (ctx.mem) Block(children->path(), children->position());

for (size_t i = 0, L = baz.size(); i < L; ++i) {
bool is_bubble = baz[i].first;
Expand All @@ -123,7 +178,10 @@ namespace Sass {
if (!parent) {
*result << slice;
}
else if (!previous_parent) {
else if (previous_parent) {
*previous_parent->block() += slice;
}
else {
previous_parent = static_cast<Has_Block*>(parent);

Has_Block* new_parent = static_cast<Has_Block*>(parent);
Expand All @@ -134,17 +192,18 @@ namespace Sass {
continue;
}

Block* wrapper_block = new (ctx.mem) Block(parent->block()->path(),
parent->block()->position(),
parent->block()->length(),
parent->block()->is_root());
Block* wrapper_block = new (ctx.mem) Block(children->block()->path(),
children->block()->position(),
children->block()->length(),
children->block()->is_root());

for (size_t j = 0, K = slice->length(); j < K; ++j)
{
Statement* ss = 0;
Bubble* b = static_cast<Bubble*>((*slice)[j]);

if (!parent ||
parent->statement_type() != Statement::MEDIA ||
b->node()->statement_type() != Statement::MEDIA ||
static_cast<Media_Block*>(b->node())->media_queries() == static_cast<Media_Block*>(parent)->media_queries())
{
Expand All @@ -153,16 +212,23 @@ namespace Sass {
else
{
List* mq = merge_media_queries(static_cast<Media_Block*>(b->node()), static_cast<Media_Block*>(parent));
if (!mq->length()) continue;
static_cast<Media_Block*>(b->node())->media_queries(mq);
ss = b->node();
}

if (!ss) continue;

Statement* ssss = ss->perform(this);
Statement* wrapper = flatten(ssss);
Block* bb = new (ctx.mem) Block(children->block()->path(),
children->block()->position(),
children->block()->length(),
children->block()->is_root());
*bb << ss->perform(this);
Statement* wrapper = flatten(bb);
*wrapper_block << wrapper;

if (wrapper->block()->length()) {
previous_parent = 0;
}
}

if (wrapper_block) {
Expand Down
1 change: 1 addition & 0 deletions cssize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ namespace Sass {

Statement* parent();
vector<pair<bool, Block*>> slice_by_bubble(Statement*);
Statement* bubble(Media_Block*);
Statement* debubble(Block* children, Statement* parent = 0);
Statement* flatten(Statement*);
bool bubblable(Statement*);
Expand Down
4 changes: 3 additions & 1 deletion eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "context.hpp"
#include "backtrace.hpp"
#include "prelexer.hpp"
#include "parser.hpp"

#include <cstdlib>
#include <cmath>
Expand Down Expand Up @@ -767,6 +768,7 @@ namespace Sass {

Expression* Eval::operator()(Media_Query* q)
{
To_String to_string;
String* t = q->media_type();
t = static_cast<String*>(t ? t->perform(this) : 0);
Media_Query* qq = new (ctx.mem) Media_Query(q->path(),
Expand All @@ -778,7 +780,7 @@ namespace Sass {
for (size_t i = 0, L = q->length(); i < L; ++i) {
*qq << static_cast<Media_Query_Expression*>((*q)[i]->perform(this));
}
return qq;
return Parser::from_c_str(qq->perform(&to_string).c_str(), ctx, qq->path(), qq->position()).parse_media_query();;
}

Expression* Eval::operator()(Media_Query_Expression* e)
Expand Down

0 comments on commit 5de59c0

Please sign in to comment.