Skip to content

Commit

Permalink
Implement handling of nested :not pseudo selectors
Browse files Browse the repository at this point in the history
They should not appear in the output. Ruby sass seems to
have a minor bug, as it inserts additional spaces before
the comma when the pseudo selector gets eliminated.
  • Loading branch information
mgreter committed Apr 6, 2015
1 parent f82a41b commit b5dfefa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
31 changes: 31 additions & 0 deletions ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,8 @@ namespace Sass {
virtual unsigned long specificity() {
return Constants::Specificity_Universal;
};
virtual bool is_child(const string& name) { return false; }
virtual bool has_child(const string& name) { return false; }
};
inline Selector::~Selector() { }

Expand Down Expand Up @@ -1931,6 +1933,13 @@ namespace Sass {
Wrapped_Selector(ParserState pstate, string n, Selector* sel)
: Simple_Selector(pstate), name_(n), selector_(sel)
{ }
virtual bool is_child(const string& name) {
return name_ == name;
}
virtual bool has_child(const string& name) {
if (selector_ == 0) return false;
return selector_->has_child(name);
}
// Selectors inside the negation pseudo-class are counted like any
// other, but the negation itself does not count as a pseudo-class.
virtual unsigned long specificity()
Expand Down Expand Up @@ -1984,6 +1993,13 @@ namespace Sass {
{ sum += (*this)[i]->specificity(); }
return sum;
}
virtual bool has_child(const string& name) {
for (auto el : elements()) {
if (el->is_child(name)) return true;
if (el->has_child(name)) return true;
}
return false;
}
bool is_empty_reference()
{
return length() == 1 &&
Expand Down Expand Up @@ -2045,6 +2061,12 @@ namespace Sass {
if (tail()) sum += tail()->specificity();
return sum;
}
virtual bool has_child(const string& name) {
return (head_ && head_->is_child(name)) ||
(head_ && head_->has_child(name)) ||
(tail_ && tail_->is_child(name)) ||
(tail_ && tail_->has_child(name));
}
bool operator<(const Complex_Selector& rhs) const;
bool operator==(const Complex_Selector& rhs) const;
inline bool operator!=(const Complex_Selector& rhs) const { return !(*this == rhs); }
Expand Down Expand Up @@ -2126,6 +2148,15 @@ namespace Sass {
{ sum += (*this)[i]->specificity(); }
return sum;
}
virtual bool has_child(const string& name)
{
for (size_t i = 0, L = length(); i < L; ++i)
{
if ((*this)[i]->is_child(name)) return true;
if ((*this)[i]->has_child(name)) return true;
}
return false;
}
// vector<Complex_Selector*> members() { return elements_; }
ATTACH_OPERATIONS();
};
Expand Down
10 changes: 10 additions & 0 deletions output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ namespace Sass {

}

void Output::operator()(Wrapped_Selector* s)
{
if (s->name() == ":not(") {
if (s->has_child(":has(") || s->has_child(":not(")) {
return; // abort
}
}
return Inspect::operator()(s);
}

void Output::operator()(Comment* c)
{
To_String to_string(ctx);
Expand Down
1 change: 1 addition & 0 deletions output.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace Sass {
virtual void operator()(Media_Block*);
virtual void operator()(At_Rule*);
virtual void operator()(Keyframe_Rule*);
virtual void operator()(Wrapped_Selector*);
virtual void operator()(Import*);
virtual void operator()(Comment*);
virtual void operator()(String_Quoted*);
Expand Down

0 comments on commit b5dfefa

Please sign in to comment.