Skip to content

Commit e90a9d2

Browse files
committed
Revert compound re-ordering for non extended selectors
1 parent c3c1c0e commit e90a9d2

File tree

4 files changed

+71
-19
lines changed

4 files changed

+71
-19
lines changed

src/ast_selectors.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,16 @@ namespace Sass {
433433
return false;
434434
}
435435

436+
bool ComplexSelector::isInvalidCss() const
437+
{
438+
for (size_t i = 0; i < length(); i += 1) {
439+
if (CompoundSelectorObj compound = get(i)->getCompound()) {
440+
if (compound->isInvalidCss()) return true;
441+
}
442+
}
443+
return false;
444+
}
445+
436446
SelectorListObj ComplexSelector::wrapInList()
437447
{
438448
SelectorListObj selector =
@@ -525,15 +535,13 @@ namespace Sass {
525535
CompoundSelector::CompoundSelector(SourceSpan pstate, bool postLineBreak)
526536
: SelectorComponent(pstate, postLineBreak),
527537
Vectorized<SimpleSelectorObj>(),
528-
hasRealParent_(false),
529-
extended_(false)
538+
hasRealParent_(false)
530539
{
531540
}
532541
CompoundSelector::CompoundSelector(const CompoundSelector* ptr)
533542
: SelectorComponent(ptr),
534543
Vectorized<SimpleSelectorObj>(*ptr),
535-
hasRealParent_(ptr->hasRealParent()),
536-
extended_(ptr->extended())
544+
hasRealParent_(ptr->hasRealParent())
537545
{ }
538546

539547
size_t CompoundSelector::hash() const
@@ -950,6 +958,23 @@ namespace Sass {
950958
std::sort(begin(), end(), cmpSimpleSelectors);
951959
}
952960

961+
bool CompoundSelector::isInvalidCss() const
962+
{
963+
size_t current = 0, next = 0;
964+
for (const SimpleSelector* sel : elements()) {
965+
next = sel->getSortOrder();
966+
// Must only have one type selector
967+
if (current == 1 && next == 1) {
968+
return true;
969+
}
970+
if (next < current) {
971+
return true;
972+
}
973+
current = next;
974+
}
975+
return false;
976+
}
977+
953978
/* better return sass::vector? only - is empty container anyway? */
954979
SelectorList* ComplexSelector::resolve_parent_refs(SelectorStack pstack, Backtraces& traces, bool implicit_parent)
955980
{

src/ast_selectors.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ namespace Sass {
182182
class ClassSelector final : public SimpleSelector {
183183
public:
184184
ClassSelector(SourceSpan pstate, sass::string n);
185-
int getSortOrder() const override final { return 3; }
185+
int getSortOrder() const override final { return 2; }
186186
virtual unsigned long specificity() const override;
187187
bool operator==(const SimpleSelector& rhs) const final override;
188188
ATTACH_CMP_OPERATIONS(ClassSelector)
@@ -216,7 +216,7 @@ namespace Sass {
216216
ADD_PROPERTY(char, modifier);
217217
public:
218218
AttributeSelector(SourceSpan pstate, sass::string n, sass::string m, String_Obj v, char o = 0);
219-
int getSortOrder() const override final { return 4; }
219+
int getSortOrder() const override final { return 2; }
220220
size_t hash() const override;
221221
virtual unsigned long specificity() const override;
222222
bool operator==(const SimpleSelector& rhs) const final override;
@@ -237,7 +237,7 @@ namespace Sass {
237237
ADD_PROPERTY(bool, isClass)
238238
public:
239239
PseudoSelector(SourceSpan pstate, sass::string n, bool element = false);
240-
int getSortOrder() const override final { return 5; }
240+
int getSortOrder() const override final { return 3; }
241241
virtual bool is_pseudo_element() const override;
242242
size_t hash() const override;
243243

@@ -273,16 +273,17 @@ namespace Sass {
273273
// Between each item there is an implicit ancestor of combinator
274274
////////////////////////////////////////////////////////////////////////////
275275
class ComplexSelector final : public Selector, public Vectorized<SelectorComponentObj> {
276-
ADD_PROPERTY(bool, chroots)
276+
ADD_PROPERTY(bool, chroots);
277277
// line break before list separator
278-
ADD_PROPERTY(bool, hasPreLineFeed)
278+
ADD_PROPERTY(bool, hasPreLineFeed);
279279
public:
280280
ComplexSelector(SourceSpan pstate);
281281

282282
// Returns true if the first components
283-
// is a compound selector and fullfills
283+
// is a compound selector and fulfills
284284
// a few other criteria.
285285
bool isInvisible() const;
286+
bool isInvalidCss() const;
286287

287288
size_t hash() const override;
288289
void cloneChildren() override;
@@ -413,12 +414,11 @@ namespace Sass {
413414
////////////////////////////////////////////////////////////////////////////
414415
class CompoundSelector final : public SelectorComponent, public Vectorized<SimpleSelectorObj> {
415416
ADD_PROPERTY(bool, hasRealParent)
416-
ADD_PROPERTY(bool, extended)
417417
public:
418418
CompoundSelector(SourceSpan pstate, bool postLineBreak = false);
419419

420420
// Returns true if this compound selector
421-
// fullfills various criteria.
421+
// fulfills various criteria.
422422
bool isInvisible() const;
423423

424424
bool empty() const override {
@@ -454,6 +454,7 @@ namespace Sass {
454454
bool operator==(const SimpleSelector& rhs) const;
455455

456456
void sortChildren();
457+
bool isInvalidCss() const;
457458

458459
ATTACH_CMP_OPERATIONS(CompoundSelector)
459460
ATTACH_AST_OPERATIONS(CompoundSelector)

src/extender.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,20 @@ namespace Sass {
755755
sass::vector<Extension> exts = options[0];
756756
for (size_t n = 0; n < exts.size(); n += 1) {
757757
exts[n].assertCompatibleMediaContext(mediaQueryContext, traces);
758+
// To fix invalid css we need to re-order some
759+
// Therefore we need to make copies for them
760+
if (exts[n].extender->isInvalidCss()) {
761+
exts[n].extender = SASS_MEMORY_COPY(exts[n].extender);
762+
for (SelectorComponentObj& component : exts[n].extender->elements()) {
763+
if (CompoundSelector* compound = component->getCompound()) {
764+
if (compound->isInvalidCss()) {
765+
CompoundSelector* copy = SASS_MEMORY_COPY(compound);
766+
copy->sortChildren();
767+
component = copy;
768+
}
769+
}
770+
}
771+
}
758772
result.push_back(exts[n].extender);
759773
}
760774
return result;
@@ -844,10 +858,23 @@ namespace Sass {
844858
}
845859

846860
for (sass::vector<SelectorComponentObj>& components : complexes) {
847-
auto sel = SASS_MEMORY_NEW(ComplexSelector, "[ext]");
861+
auto sel = SASS_MEMORY_NEW(ComplexSelector, "[unified]");
848862
sel->hasPreLineFeed(lineBreak);
849863
sel->elements(components);
864+
865+
/* This seems to do too much in regard of previous behavior
866+
for (SelectorComponentObj& component : sel->elements()) {
867+
if (CompoundSelector* compound = component->getCompound()) {
868+
if (compound->isInvalidCss()) {
869+
CompoundSelector* copy = SASS_MEMORY_COPY(compound);
870+
copy->sortChildren();
871+
component = copy;
872+
}
873+
}
874+
}*/
875+
850876
unifiedPaths.push_back(sel);
877+
851878
}
852879

853880
}

src/inspect.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ namespace Sass {
439439
if (list->is_bracketed()) {
440440
append_string(lbracket(list));
441441
}
442-
// probably ruby sass eqivalent of element_needs_parens
442+
// probably ruby sass equivalent of element_needs_parens
443443
else if (output_style() == TO_SASS &&
444444
list->length() == 1 &&
445445
!list->from_selector() &&
@@ -489,7 +489,7 @@ namespace Sass {
489489
}
490490
append_string(rbracket(list));
491491
}
492-
// probably ruby sass eqivalent of element_needs_parens
492+
// probably ruby sass equivalent of element_needs_parens
493493
else if (output_style() == TO_SASS &&
494494
list->length() == 1 &&
495495
!list->from_selector() &&
@@ -1017,7 +1017,7 @@ namespace Sass {
10171017

10181018

10191019
bool was_comma_array = in_comma_array;
1020-
// probably ruby sass eqivalent of element_needs_parens
1020+
// probably ruby sass equivalent of element_needs_parens
10211021
if (output_style() == TO_SASS && g->length() == 1 &&
10221022
(!Cast<List>((*g)[0]) &&
10231023
!Cast<SelectorList>((*g)[0]))) {
@@ -1045,7 +1045,7 @@ namespace Sass {
10451045
}
10461046

10471047
in_comma_array = was_comma_array;
1048-
// probably ruby sass eqivalent of element_needs_parens
1048+
// probably ruby sass equivalent of element_needs_parens
10491049
if (output_style() == TO_SASS && g->length() == 1 &&
10501050
(!Cast<List>((*g)[0]) &&
10511051
!Cast<SelectorList>((*g)[0]))) {
@@ -1081,7 +1081,7 @@ namespace Sass {
10811081
void Inspect::operator()(SelectorComponent* sel)
10821082
{
10831083
// You should probably never call this method directly
1084-
// But in case anyone does, we will do the upcasting
1084+
// But in case anyone does, we will do the up-casting
10851085
if (auto comp = Cast<CompoundSelector>(sel)) operator()(comp);
10861086
if (auto comb = Cast<SelectorCombinator>(sel)) operator()(comb);
10871087
}
@@ -1091,7 +1091,6 @@ namespace Sass {
10911091
if (sel->hasRealParent()) {
10921092
append_string("&");
10931093
}
1094-
sel->sortChildren();
10951094
for (auto& item : sel->elements()) {
10961095
item->perform(this);
10971096
}

0 commit comments

Comments
 (0)