Skip to content

Commit

Permalink
Ensure correct output order of compound selectors
Browse files Browse the repository at this point in the history
Fixes #3084
  • Loading branch information
mgreter committed May 1, 2020
1 parent 9515008 commit b0f5bc2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/ast_selectors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,16 @@ namespace Sass {

}

bool cmpSimpleSelectors(SimpleSelector* a, SimpleSelector* b)
{
return (a->getSortOrder() < b->getSortOrder());
}

void CompoundSelector::sortChildren()
{
std::sort(begin(), end(), cmpSimpleSelectors);
}

/* better return sass::vector? only - is empty container anyway? */
SelectorList* ComplexSelector::resolve_parent_refs(SelectorStack pstack, Backtraces& traces, bool implicit_parent)
{
Expand Down
10 changes: 10 additions & 0 deletions src/ast_selectors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ namespace Sass {
HASH_PROPERTY(bool, has_ns)
public:
SimpleSelector(SourceSpan pstate, sass::string n = "");
// ordering within parent (peudos go last)
virtual int getSortOrder() const = 0;
virtual sass::string ns_name() const;
size_t hash() const override;
virtual bool empty() const;
Expand Down Expand Up @@ -147,6 +149,7 @@ namespace Sass {
class PlaceholderSelector final : public SimpleSelector {
public:
PlaceholderSelector(SourceSpan pstate, sass::string n);
int getSortOrder() const override final { return 0; }
bool isInvisible() const override { return true; }
virtual unsigned long specificity() const override;
virtual bool has_placeholder() override;
Expand All @@ -162,6 +165,7 @@ namespace Sass {
class TypeSelector final : public SimpleSelector {
public:
TypeSelector(SourceSpan pstate, sass::string n);
int getSortOrder() const override final { return 1; }
virtual unsigned long specificity() const override;
SimpleSelector* unifyWith(const SimpleSelector*);
CompoundSelector* unifyWith(CompoundSelector*) override;
Expand All @@ -178,6 +182,7 @@ namespace Sass {
class ClassSelector final : public SimpleSelector {
public:
ClassSelector(SourceSpan pstate, sass::string n);
int getSortOrder() const override final { return 3; }
virtual unsigned long specificity() const override;
bool operator==(const SimpleSelector& rhs) const final override;
ATTACH_CMP_OPERATIONS(ClassSelector)
Expand All @@ -191,6 +196,7 @@ namespace Sass {
class IDSelector final : public SimpleSelector {
public:
IDSelector(SourceSpan pstate, sass::string n);
int getSortOrder() const override final { return 2; }
virtual unsigned long specificity() const override;
CompoundSelector* unifyWith(CompoundSelector*) override;
IDSelector* getIdSelector() final override { return this; }
Expand All @@ -210,6 +216,7 @@ namespace Sass {
ADD_PROPERTY(char, modifier);
public:
AttributeSelector(SourceSpan pstate, sass::string n, sass::string m, String_Obj v, char o = 0);
int getSortOrder() const override final { return 4; }
size_t hash() const override;
virtual unsigned long specificity() const override;
bool operator==(const SimpleSelector& rhs) const final override;
Expand All @@ -230,6 +237,7 @@ namespace Sass {
ADD_PROPERTY(bool, isClass)
public:
PseudoSelector(SourceSpan pstate, sass::string n, bool element = false);
int getSortOrder() const override final { return 5; }
virtual bool is_pseudo_element() const override;
size_t hash() const override;

Expand Down Expand Up @@ -445,6 +453,8 @@ namespace Sass {
bool operator==(const ComplexSelector& rhs) const;
bool operator==(const SimpleSelector& rhs) const;

void sortChildren();

ATTACH_CMP_OPERATIONS(CompoundSelector)
ATTACH_AST_OPERATIONS(CompoundSelector)
ATTACH_CRTP_PERFORM_METHODS()
Expand Down
1 change: 1 addition & 0 deletions src/inspect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,7 @@ namespace Sass {
if (sel->hasRealParent()) {
append_string("&");
}
sel->sortChildren();
for (auto& item : sel->elements()) {
item->perform(this);
}
Expand Down

0 comments on commit b0f5bc2

Please sign in to comment.