Skip to content

Commit 474e9a7

Browse files
committed
Add and use a unification_order method
1 parent 880cd26 commit 474e9a7

File tree

4 files changed

+68
-15
lines changed

4 files changed

+68
-15
lines changed

src/ast.cpp

+2-15
Original file line numberDiff line numberDiff line change
@@ -505,27 +505,14 @@ namespace Sass {
505505
return false;
506506
}
507507

508-
namespace {
509-
510-
int SelectorOrder(Simple_Selector_Ptr sel) {
511-
if (Cast<Element_Selector>(sel)) return 1;
512-
if (Cast<Id_Selector>(sel) || Cast<Class_Selector>(sel)) return 2;
513-
if (Cast<Attribute_Selector>(sel)) return 3;
514-
if (Cast<Pseudo_Selector>(sel)) return Cast<Pseudo_Selector>(sel)->is_pseudo_element() ? 6 : 4;
515-
if (Cast<Wrapped_Selector>(sel)) return 5;
516-
return 7;
517-
}
518-
519-
} // namespace
520-
521508
Compound_Selector_Ptr Simple_Selector::unify_with(Compound_Selector_Ptr rhs)
522509
{
523510
const size_t rsize = rhs->length();
524511
for (size_t i = 0; i < rsize; ++i)
525512
{ if (to_string() == rhs->at(i)->to_string()) return rhs; }
526-
const int lhs_order = SelectorOrder(this);
513+
const int lhs_order = this->unification_order();
527514
size_t i = rsize;
528-
while (i > 0 && lhs_order < SelectorOrder(rhs->at(i - 1))) --i;
515+
while (i > 0 && lhs_order < rhs->at(i - 1)->unification_order()) --i;
529516
rhs->elements().insert(rhs->elements().begin() + i, this);
530517
return rhs;
531518
}

src/ast.hpp

+47
Original file line numberDiff line numberDiff line change
@@ -2215,6 +2215,7 @@ namespace Sass {
22152215
virtual ~Selector() = 0;
22162216
virtual size_t hash() = 0;
22172217
virtual unsigned long specificity() const = 0;
2218+
virtual int unification_order() const = 0;
22182219
virtual void set_media_block(Media_Block_Ptr mb) {
22192220
media_block(mb);
22202221
}
@@ -2394,6 +2395,10 @@ namespace Sass {
23942395
{
23952396
return 0;
23962397
}
2398+
int unification_order() const
2399+
{
2400+
throw std::runtime_error("unification_order for Parent_Selector is undefined");
2401+
}
23972402
std::string type() const { return "selector"; }
23982403
static std::string type_name() { return "selector"; }
23992404
ATTACH_AST_OPERATIONS(Parent_Selector)
@@ -2433,6 +2438,10 @@ namespace Sass {
24332438
{
24342439
return Constants::Specificity_Base;
24352440
}
2441+
int unification_order() const
2442+
{
2443+
return Constants::UnificationOrder_Placeholder;
2444+
}
24362445
virtual bool has_placeholder() {
24372446
return true;
24382447
}
@@ -2457,6 +2466,10 @@ namespace Sass {
24572466
if (name() == "*") return 0;
24582467
else return Constants::Specificity_Element;
24592468
}
2469+
int unification_order() const
2470+
{
2471+
return Constants::UnificationOrder_Element;
2472+
}
24602473
virtual Simple_Selector_Ptr unify_with(Simple_Selector_Ptr);
24612474
virtual Compound_Selector_Ptr unify_with(Compound_Selector_Ptr);
24622475
virtual bool operator==(const Simple_Selector& rhs) const;
@@ -2482,6 +2495,10 @@ namespace Sass {
24822495
{
24832496
return Constants::Specificity_Class;
24842497
}
2498+
int unification_order() const
2499+
{
2500+
return Constants::UnificationOrder_Class;
2501+
}
24852502
virtual Compound_Selector_Ptr unify_with(Compound_Selector_Ptr);
24862503
ATTACH_AST_OPERATIONS(Class_Selector)
24872504
ATTACH_CRTP_PERFORM_METHODS()
@@ -2502,6 +2519,10 @@ namespace Sass {
25022519
{
25032520
return Constants::Specificity_ID;
25042521
}
2522+
int unification_order() const
2523+
{
2524+
return Constants::UnificationOrder_Id;
2525+
}
25052526
virtual Compound_Selector_Ptr unify_with(Compound_Selector_Ptr);
25062527
ATTACH_AST_OPERATIONS(Id_Selector)
25072528
ATTACH_CRTP_PERFORM_METHODS()
@@ -2538,6 +2559,10 @@ namespace Sass {
25382559
{
25392560
return Constants::Specificity_Attr;
25402561
}
2562+
int unification_order() const
2563+
{
2564+
return Constants::UnificationOrder_Attribute;
2565+
}
25412566
virtual bool operator==(const Simple_Selector& rhs) const;
25422567
virtual bool operator==(const Attribute_Selector& rhs) const;
25432568
virtual bool operator<(const Simple_Selector& rhs) const;
@@ -2599,6 +2624,12 @@ namespace Sass {
25992624
return Constants::Specificity_Element;
26002625
return Constants::Specificity_Pseudo;
26012626
}
2627+
int unification_order() const
2628+
{
2629+
if (is_pseudo_element())
2630+
return Constants::UnificationOrder_PseudoElement;
2631+
return Constants::UnificationOrder_PseudoClass;
2632+
}
26022633
virtual bool operator==(const Simple_Selector& rhs) const;
26032634
virtual bool operator==(const Pseudo_Selector& rhs) const;
26042635
virtual bool operator<(const Simple_Selector& rhs) const;
@@ -2627,6 +2658,10 @@ namespace Sass {
26272658
virtual bool has_parent_ref() const;
26282659
virtual bool has_real_parent_ref() const;
26292660
virtual unsigned long specificity() const;
2661+
int unification_order() const
2662+
{
2663+
return Constants::UnificationOrder_Wrapped;
2664+
}
26302665
virtual bool find ( bool (*f)(AST_Node_Obj) );
26312666
virtual bool operator==(const Simple_Selector& rhs) const;
26322667
virtual bool operator==(const Wrapped_Selector& rhs) const;
@@ -2709,6 +2744,10 @@ namespace Sass {
27092744
{ sum += (*this)[i]->specificity(); }
27102745
return sum;
27112746
}
2747+
int unification_order() const
2748+
{
2749+
throw std::runtime_error("unification_order for Compound_Selector is undefined");
2750+
}
27122751

27132752
virtual bool has_placeholder()
27142753
{
@@ -2837,6 +2876,10 @@ namespace Sass {
28372876
if (tail()) sum += tail()->specificity();
28382877
return sum;
28392878
}
2879+
int unification_order() const
2880+
{
2881+
throw std::runtime_error("unification_order for Complex_Selector is undefined");
2882+
}
28402883
virtual void set_media_block(Media_Block_Ptr mb) {
28412884
media_block(mb);
28422885
if (tail_) tail_->set_media_block(mb);
@@ -2960,6 +3003,10 @@ namespace Sass {
29603003
}
29613004
return sum;
29623005
}
3006+
int unification_order() const
3007+
{
3008+
throw std::runtime_error("unification_order for Selector_List is undefined");
3009+
}
29633010
virtual void set_media_block(Media_Block_Ptr mb) {
29643011
media_block(mb);
29653012
for (Complex_Selector_Obj cs : elements()) {

src/constants.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ namespace Sass {
1818
extern const unsigned long Specificity_Pseudo = 1000;
1919
extern const unsigned long Specificity_ID = 1000000;
2020

21+
extern const int UnificationOrder_Element = 1;
22+
extern const int UnificationOrder_Id = 2;
23+
extern const int UnificationOrder_Class = 2;
24+
extern const int UnificationOrder_Attribute = 3;
25+
extern const int UnificationOrder_PseudoClass = 4;
26+
extern const int UnificationOrder_Wrapped = 5;
27+
extern const int UnificationOrder_PseudoElement = 6;
28+
extern const int UnificationOrder_Placeholder = 7;
29+
2130
// sass keywords
2231
extern const char at_root_kwd[] = "@at-root";
2332
extern const char import_kwd[] = "@import";

src/constants.hpp

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ namespace Sass {
1818
extern const unsigned long Specificity_Pseudo;
1919
extern const unsigned long Specificity_ID;
2020

21+
// Selector unification order;
22+
extern const int UnificationOrder_Element;
23+
extern const int UnificationOrder_Id;
24+
extern const int UnificationOrder_Class;
25+
extern const int UnificationOrder_Attribute;
26+
extern const int UnificationOrder_PseudoClass;
27+
extern const int UnificationOrder_Wrapped;
28+
extern const int UnificationOrder_PseudoElement;
29+
extern const int UnificationOrder_Placeholder;
30+
2131
// sass keywords
2232
extern const char at_root_kwd[];
2333
extern const char import_kwd[];

0 commit comments

Comments
 (0)