Skip to content

Commit

Permalink
Merge branch 'master' into sourcemap-order
Browse files Browse the repository at this point in the history
  • Loading branch information
nschonni authored Oct 4, 2016
2 parents 859fabe + 7e6c36b commit 2eb614f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
44 changes: 41 additions & 3 deletions src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,26 @@ namespace Sass {
return false;
}

bool SimpleSequence_Selector::has_real_parent_ref()
{
for (Simple_Selector* s : *this) {
if (s && s->has_real_parent_ref()) return true;
}
return false;
}

bool Sequence_Selector::has_parent_ref()
{
return (head() && head()->has_parent_ref()) ||
(tail() && tail()->has_parent_ref());
}

bool Sequence_Selector::has_real_parent_ref()
{
return (head() && head()->has_real_parent_ref()) ||
(tail() && tail()->has_real_parent_ref());
}

bool Sequence_Selector::operator< (const Sequence_Selector& rhs) const
{
// const iterators for tails
Expand Down Expand Up @@ -1110,6 +1124,12 @@ namespace Sass {
Sequence_Selector* tail = this->tail();
SimpleSequence_Selector* head = this->head();

if (!this->has_real_parent_ref() && !implicit_parent) {
CommaSequence_Selector* retval = SASS_MEMORY_NEW(ctx.mem, CommaSequence_Selector, pstate());
*retval << this;
return retval;
}

// first resolve_parent_refs the tail (which may return an expanded list)
CommaSequence_Selector* tails = tail ? tail->resolve_parent_refs(ctx, parents, implicit_parent) : 0;

Expand Down Expand Up @@ -1396,6 +1416,14 @@ namespace Sass {
return false;
}

bool CommaSequence_Selector::has_real_parent_ref()
{
for (Sequence_Selector* s : *this) {
if (s && s->has_real_parent_ref()) return true;
}
return false;
}

bool Selector_Schema::has_parent_ref()
{
if (String_Schema* schema = dynamic_cast<String_Schema*>(contents())) {
Expand All @@ -1404,6 +1432,15 @@ namespace Sass {
return false;
}

bool Selector_Schema::has_real_parent_ref()
{
if (String_Schema* schema = dynamic_cast<String_Schema*>(contents())) {
Parent_Selector* p = dynamic_cast<Parent_Selector*>(schema->at(0));
return schema->length() > 0 && p != NULL && p->is_real_parent_ref();
}
return false;
}

void CommaSequence_Selector::adjust_after_pushing(Sequence_Selector* c)
{
// if (c->has_reference()) has_reference(true);
Expand Down Expand Up @@ -1615,9 +1652,10 @@ namespace Sass {
}

bool Ruleset::is_invisible() const {
CommaSequence_Selector* sl = static_cast<CommaSequence_Selector*>(selector());
for (size_t i = 0, L = sl->length(); i < L; ++i)
if (!(*sl)[i]->has_placeholder()) return false;
if (CommaSequence_Selector* sl = dynamic_cast<CommaSequence_Selector*>(selector())) {
for (size_t i = 0, L = sl->length(); i < L; ++i)
if (!(*sl)[i]->has_placeholder()) return false;
}
return true;
}

Expand Down
22 changes: 19 additions & 3 deletions src/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1897,6 +1897,9 @@ namespace Sass {
virtual bool has_parent_ref() {
return false;
}
virtual bool has_real_parent_ref() {
return false;
}
};
inline Selector::~Selector() { }

Expand All @@ -1912,6 +1915,7 @@ namespace Sass {
: Selector(pstate), contents_(c), at_root_(false)
{ }
virtual bool has_parent_ref();
virtual bool has_real_parent_ref();
virtual size_t hash() {
if (hash_ == 0) {
hash_combine(hash_, contents_->hash());
Expand Down Expand Up @@ -1990,6 +1994,7 @@ namespace Sass {
virtual ~Simple_Selector() = 0;
virtual SimpleSequence_Selector* unify_with(SimpleSequence_Selector*, Context&);
virtual bool has_parent_ref() { return false; };
virtual bool has_real_parent_ref() { return false; };
virtual bool is_pseudo_element() { return false; }
virtual bool is_pseudo_class() { return false; }

Expand All @@ -2012,11 +2017,14 @@ namespace Sass {
// inside strings in declarations (SimpleSequence_Selector).
// only one simple parent selector means the first case.
class Parent_Selector : public Simple_Selector {
ADD_PROPERTY(bool, real)
public:
Parent_Selector(ParserState pstate)
: Simple_Selector(pstate, "&")
Parent_Selector(ParserState pstate, bool r = true)
: Simple_Selector(pstate, "&"), real_(r)
{ /* has_reference(true); */ }
bool is_real_parent_ref() { return real(); };
virtual bool has_parent_ref() { return true; };
virtual bool has_real_parent_ref() { return is_real_parent_ref(); };
virtual unsigned long specificity()
{
return 0;
Expand Down Expand Up @@ -2225,6 +2233,11 @@ namespace Sass {
if (!selector()) return false;
return selector()->has_parent_ref();
}
virtual bool has_real_parent_ref() {
// if (has_reference()) return true;
if (!selector()) return false;
return selector()->has_real_parent_ref();
}
virtual bool has_wrapped_selector()
{
return true;
Expand Down Expand Up @@ -2283,6 +2296,7 @@ namespace Sass {
SimpleSequence_Selector* unify_with(SimpleSequence_Selector* rhs, Context& ctx);
// virtual Placeholder_Selector* find_placeholder();
virtual bool has_parent_ref();
virtual bool has_real_parent_ref();
Simple_Selector* base()
{
// Implement non-const in terms of const. Safe to const_cast since this method is non-const
Expand Down Expand Up @@ -2387,6 +2401,7 @@ namespace Sass {
// if ((h && h->has_placeholder()) || (t && t->has_placeholder())) has_placeholder(true);
}
virtual bool has_parent_ref();
virtual bool has_real_parent_ref();

Sequence_Selector* skip_empty_reference()
{
Expand Down Expand Up @@ -2429,7 +2444,7 @@ namespace Sass {
Sequence_Selector* innermost() { return last(); };

size_t length() const;
CommaSequence_Selector* resolve_parent_refs(Context& ctx, CommaSequence_Selector* parents, bool implicit_parent);
CommaSequence_Selector* resolve_parent_refs(Context& ctx, CommaSequence_Selector* parents, bool implicit_parent = true);
virtual bool is_superselector_of(SimpleSequence_Selector* sub, std::string wrapping = "");
virtual bool is_superselector_of(Sequence_Selector* sub, std::string wrapping = "");
virtual bool is_superselector_of(CommaSequence_Selector* sub, std::string wrapping = "");
Expand Down Expand Up @@ -2545,6 +2560,7 @@ namespace Sass {
// remove parent selector references
// basically unwraps parsed selectors
virtual bool has_parent_ref();
virtual bool has_real_parent_ref();
void remove_parent_selectors();
// virtual Placeholder_Selector* find_placeholder();
CommaSequence_Selector* resolve_parent_refs(Context& ctx, CommaSequence_Selector* parents, bool implicit_parent = true);
Expand Down
1 change: 1 addition & 0 deletions src/debugger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ inline void debug_ast(AST_Node* node, std::string ind, Env* env)
// if (selector->not_selector()) cerr << " [in_declaration]";
std::cerr << " (" << pstate_source_position(node) << ")";
std::cerr << " <" << selector->hash() << ">";
std::cerr << " [" << (selector->is_real_parent_ref() ? "REAL" : "FAKE") << "]";
std::cerr << " <" << prettyprint(selector->pstate().token.ws_before()) << ">" << std::endl;
// debug_ast(selector->selector(), ind + "->", env);

Expand Down
2 changes: 1 addition & 1 deletion src/inspect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ namespace Sass {

void Inspect::operator()(Parent_Selector* p)
{
append_string("&");
if (p->is_real_parent_ref()) append_string("&");
}

void Inspect::operator()(Placeholder_Selector* s)
Expand Down
2 changes: 1 addition & 1 deletion src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ namespace Sass {
// also skip adding parent ref if we only have refs
if (!sel->has_parent_ref() && !in_at_root && !in_root) {
// create the objects to wrap parent selector reference
Parent_Selector* parent = SASS_MEMORY_NEW(ctx.mem, Parent_Selector, pstate);
Parent_Selector* parent = SASS_MEMORY_NEW(ctx.mem, Parent_Selector, pstate, false);
parent->media_block(last_media_block);
SimpleSequence_Selector* head = SASS_MEMORY_NEW(ctx.mem, SimpleSequence_Selector, pstate);
head->media_block(last_media_block);
Expand Down

0 comments on commit 2eb614f

Please sign in to comment.