Skip to content

Commit

Permalink
Fix an edge case in superselector computation (#1866)
Browse files Browse the repository at this point in the history
Closes #1843
  • Loading branch information
nex3 committed Jan 6, 2023
1 parent 14c1634 commit b98fa4f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
* Remove sourcemap comments from Sass sources. The generated sourcemap comment
for the compiled CSS output remains unaffected.

* Fix a bug in `@extend` logic where certain selectors with three or more
combinators were incorrectly considered superselectors of similar selectors
with fewer combinators, causing them to be incorrectly trimmed from the
output.

* Produce a better error message for a number with a leading `+` or `-`, a
decimal point, but no digits.

Expand Down
26 changes: 26 additions & 0 deletions lib/src/extend/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ bool complexIsSuperselector(List<ComplexSelectorComponent> complex1,

var i1 = 0;
var i2 = 0;
Combinator? previousCombinator;
while (true) {
var remaining1 = complex1.length - i1;
var remaining2 = complex2.length - i2;
Expand Down Expand Up @@ -643,6 +644,11 @@ bool complexIsSuperselector(List<ComplexSelectorComponent> complex1,
parents.add(component2);
}

if (!_compatibleWithPreviousCombinator(
previousCombinator, parents ?? const [])) {
return false;
}

var component2 = complex2[endOfSubselector];
var combinator1 = component1.combinators.firstOrNull;
var combinator2 = component2.combinators.firstOrNull;
Expand All @@ -652,6 +658,7 @@ bool complexIsSuperselector(List<ComplexSelectorComponent> complex1,

i1++;
i2 = endOfSubselector + 1;
previousCombinator = combinator1;

if (complex1.length - i1 == 1) {
if (combinator1 == Combinator.followingSibling) {
Expand All @@ -671,6 +678,25 @@ bool complexIsSuperselector(List<ComplexSelectorComponent> complex1,
}
}

/// Returns whether [parents] are valid intersitial components between one
/// complex superselector and another, given that the earlier complex
/// superselector had the combinator [previous].
bool _compatibleWithPreviousCombinator(
Combinator? previous, List<ComplexSelectorComponent> parents) {
if (parents.isEmpty) return true;
if (previous == null) return true;

// The child and next sibling combinators require that the *immediate*
// following component be a superslector.
if (previous != Combinator.followingSibling) return false;

// The following sibling combinator does allow intermediate components, but
// only if they're all siblings.
return parents.every((component) =>
component.combinators.firstOrNull == Combinator.followingSibling ||
component.combinators.firstOrNull == Combinator.nextSibling);
}

/// Returns whether [combinator1] is a supercombinator of [combinator2].
///
/// That is, whether `X combinator1 Y` is a superselector of `X combinator2 Y`.
Expand Down

0 comments on commit b98fa4f

Please sign in to comment.