Skip to content

Commit ace6628

Browse files
glebmxzyfer
authored andcommitted
Re-implement selector unification
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
1 parent 598bade commit ace6628

File tree

10 files changed

+210
-193
lines changed

10 files changed

+210
-193
lines changed

include/sass/base.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef SASS_BASE_H
22
#define SASS_BASE_H
33

4+
// #define DEBUG
45
// #define DEBUG_SHARED_PTR
56

67
#ifdef _MSC_VER

src/ast.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ namespace Sass {
319319
return hash_;
320320
}
321321

322+
template <typename P, typename V>
323+
typename std::vector<T>::iterator insert(P position, const V& val) {
324+
reset_hash();
325+
return elements_.insert(position, val);
326+
}
327+
322328
typename std::vector<T>::iterator end() { return elements_.end(); }
323329
typename std::vector<T>::iterator begin() { return elements_.begin(); }
324330
typename std::vector<T>::const_iterator end() const { return elements_.end(); }

src/ast_fwd_decl.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,6 @@ namespace Sass {
431431
typedef std::pair<Complex_Selector_Obj, SubSetMapPairs> SubSetMapResult;
432432
typedef std::vector<SubSetMapResult> SubSetMapResults;
433433

434-
#define OrderSelectors OrderFunction<Selector_Obj>
435434
typedef std::set<Selector_Obj, OrderNodes> SelectorSet;
436435

437436
typedef std::deque<Complex_Selector_Obj> ComplexSelectorDeque;

src/ast_sel_cmp.cpp

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,7 @@ namespace Sass {
467467

468468
bool Simple_Selector::operator== (const Selector_List& rhs) const
469469
{
470-
size_t len = rhs.length();
471-
if (len > 1) return false;
472-
if (len == 0) return empty();
473-
return *this == *rhs.at(0);
470+
return rhs.length() == 1 && *this == *rhs.at(0);
474471
}
475472

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

484481
bool Simple_Selector::operator== (const Complex_Selector& rhs) const
485482
{
486-
if (rhs.tail()) return false;
487-
if (!rhs.head()) return empty();
488-
return *this == *rhs.head();
483+
return !rhs.tail() && rhs.head() &&
484+
rhs.combinator() == Complex_Selector::ANCESTOR_OF &&
485+
*this == *rhs.head();
489486
}
490487

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

498495
bool Simple_Selector::operator== (const Compound_Selector& rhs) const
499496
{
500-
size_t len = rhs.length();
501-
if (len > 1) return false;
502-
if (len == 0) return empty();
503-
return *this == *rhs.at(0);
497+
return rhs.length() == 1 && *this == *rhs.at(0);
504498
}
505499

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

833827
bool Type_Selector::operator< (const Type_Selector& rhs) const
834828
{
835-
if (is_ns_eq(rhs))
836-
{
837-
if (rhs.has_ns_ && has_ns_)
838-
return name() < rhs.name();
839-
if (!rhs.has_ns_ && !has_ns_)
840-
return name() < rhs.name();
841-
return true;
842-
}
843-
return ns() < rhs.ns();
829+
return has_ns_ == rhs.has_ns_
830+
? (ns_ == rhs.ns_
831+
? name_ < rhs.name_
832+
: ns_ < rhs.ns_)
833+
: has_ns_ < rhs.has_ns_;
844834
}
845835

846836
bool Class_Selector::operator< (const Class_Selector& rhs) const
@@ -912,4 +902,4 @@ namespace Sass {
912902
/*#########################################################################*/
913903
/*#########################################################################*/
914904

915-
}
905+
}

0 commit comments

Comments
 (0)