Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
Re-implement selector unification
Browse files Browse the repository at this point in the history
This new implementation is similar to the one in dart-sass.

Also fixes a few bugs in hash calculation (`==` objects having different hashes) and comparison (`a < b` and `b < a` being true at the same time; this should also fix #2776).

The following tests are fixed by this change:

```
/spec/selector-functions/unify/universal_simple
/spec/extend-tests/237_extend_with_universal_selector_different_namespace
/spec/extend-tests/040_test_universal_unification_with_namespaced_element_target
/spec/extend-tests/053_test_element_unification_with_namespaced_universal_target
/spec/extend-tests/236_extend_with_universal_selector_empty_namespace
/spec/extend-tests/096_test_long_extender_runs_unification
/spec/extend-tests/060_test_element_unification_with_namespaceless_element_target
/spec/extend-tests/051_test_element_unification_with_namespaceless_universal_target
/spec/extend-tests/038_test_universal_unification_with_namespaceless_element_target
/spec/extend-tests/029_test_universal_unification_with_namespaceless_universal_target
/spec/extend-tests/031_test_universal_unification_with_namespaced_universal_target
/spec/extend-tests/062_test_element_unification_with_namespaced_element_target
```

sass-spec output_styles update: https://github.com/sass/sass-spec/pull/1319/files
  • Loading branch information
glebm authored and xzyfer committed Dec 5, 2018
1 parent 598bade commit ace6628
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 193 deletions.
1 change: 1 addition & 0 deletions include/sass/base.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef SASS_BASE_H
#define SASS_BASE_H

// #define DEBUG
// #define DEBUG_SHARED_PTR

#ifdef _MSC_VER
Expand Down
6 changes: 6 additions & 0 deletions src/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@ namespace Sass {
return hash_;
}

template <typename P, typename V>
typename std::vector<T>::iterator insert(P position, const V& val) {
reset_hash();
return elements_.insert(position, val);
}

typename std::vector<T>::iterator end() { return elements_.end(); }
typename std::vector<T>::iterator begin() { return elements_.begin(); }
typename std::vector<T>::const_iterator end() const { return elements_.end(); }
Expand Down
1 change: 0 additions & 1 deletion src/ast_fwd_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,6 @@ namespace Sass {
typedef std::pair<Complex_Selector_Obj, SubSetMapPairs> SubSetMapResult;
typedef std::vector<SubSetMapResult> SubSetMapResults;

#define OrderSelectors OrderFunction<Selector_Obj>
typedef std::set<Selector_Obj, OrderNodes> SelectorSet;

typedef std::deque<Complex_Selector_Obj> ComplexSelectorDeque;
Expand Down
32 changes: 11 additions & 21 deletions src/ast_sel_cmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,7 @@ namespace Sass {

bool Simple_Selector::operator== (const Selector_List& rhs) const
{
size_t len = rhs.length();
if (len > 1) return false;
if (len == 0) return empty();
return *this == *rhs.at(0);
return rhs.length() == 1 && *this == *rhs.at(0);
}

bool Simple_Selector::operator< (const Selector_List& rhs) const
Expand All @@ -483,9 +480,9 @@ namespace Sass {

bool Simple_Selector::operator== (const Complex_Selector& rhs) const
{
if (rhs.tail()) return false;
if (!rhs.head()) return empty();
return *this == *rhs.head();
return !rhs.tail() && rhs.head() &&
rhs.combinator() == Complex_Selector::ANCESTOR_OF &&
*this == *rhs.head();
}

bool Simple_Selector::operator< (const Complex_Selector& rhs) const
Expand All @@ -497,10 +494,7 @@ namespace Sass {

bool Simple_Selector::operator== (const Compound_Selector& rhs) const
{
size_t len = rhs.length();
if (len > 1) return false;
if (len == 0) return empty();
return *this == *rhs.at(0);
return rhs.length() == 1 && *this == *rhs.at(0);
}

bool Simple_Selector::operator< (const Compound_Selector& rhs) const
Expand Down Expand Up @@ -832,15 +826,11 @@ namespace Sass {

bool Type_Selector::operator< (const Type_Selector& rhs) const
{
if (is_ns_eq(rhs))
{
if (rhs.has_ns_ && has_ns_)
return name() < rhs.name();
if (!rhs.has_ns_ && !has_ns_)
return name() < rhs.name();
return true;
}
return ns() < rhs.ns();
return has_ns_ == rhs.has_ns_
? (ns_ == rhs.ns_
? name_ < rhs.name_
: ns_ < rhs.ns_)
: has_ns_ < rhs.has_ns_;
}

bool Class_Selector::operator< (const Class_Selector& rhs) const
Expand Down Expand Up @@ -912,4 +902,4 @@ namespace Sass {
/*#########################################################################*/
/*#########################################################################*/

}
}
Loading

0 comments on commit ace6628

Please sign in to comment.