Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion fixtures/similarSelectors.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[
{ "rule1": "ul#nav, .a .b, ul.a.b {}", "rule2": "div > a, .c .d, li.active {}", "expected": true },
{ "rule1": "ul#nav, .a .b, ul.a.b {}", "rule2": "div > a, .c .d .e, li.active {}", "expected": false }
{ "rule1": "ul#nav, .a .b, ul.a.b {}", "rule2": "div > a, .c .d .e, li.active {}", "expected": false },
{ "rule1": "div .x {}", "rule2": ".y span {}", "expected": true },
{ "rule1": ".y span {}", "rule2": "div .x {}", "expected": true }
]
5 changes: 5 additions & 0 deletions lib/restructure/prepare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export default function prepare(ast, options) {
node.block.children.forEach(function(rule) {
rule.prelude.children.forEach(function(simpleselector) {
simpleselector.compareMarker = simpleselector.id;
// keyframe selectors are compared by value (id); keep the
// universal-subject fast-path inert so they only match by
// exact equality, as before
simpleselector.compareMarkerSpecificity = simpleselector.id;
simpleselector.compareMarkerUniversal = false;
});
});
}
Expand Down
10 changes: 10 additions & 0 deletions lib/restructure/prepare/processSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ export default function processSelector(node, usageData) {
simpleSelector.compareMarker += ':' + scope;
}

// The compareMarker without the subject type (specificity, plus scope).
// A selector with a universal subject (no type selector on its subject,
// i.e. tagName === '*') can match an element of any type, so it may
// collide with a same-specificity selector that has a concrete subject
// type. This key lets hasSimilarSelectors detect that case, which a plain
// compareMarker equality misses (see fixtures/similarSelectors.json).
simpleSelector.compareMarkerSpecificity = null; // pre-init to avoid multiple hidden class
simpleSelector.compareMarkerSpecificity = simpleSelector.compareMarker;
simpleSelector.compareMarkerUniversal = tagName === '*';

if (tagName !== '*') {
simpleSelector.compareMarker += ',' + tagName;
}
Expand Down
13 changes: 13 additions & 0 deletions lib/restructure/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ export function hasSimilarSelectors(selectors1, selectors2) {
return true;
}

// The subject-type suffix of compareMarker is a fast-path that
// assumes two equal-specificity selectors with different subject
// types can't match the same element. That doesn't hold when either
// subject is universal (no type selector): such a selector matches an
// element of any type, so it may collide with a same-specificity
// typed-subject selector (e.g. `div .x` vs `.y span`). Treat those as
// similar so the restructurer won't reorder across them and flip the
// cascade winner.
if ((cursor1.data.compareMarkerUniversal || cursor2.data.compareMarkerUniversal) &&
cursor1.data.compareMarkerSpecificity === cursor2.data.compareMarkerSpecificity) {
return true;
}

cursor2 = cursor2.next;
}

Expand Down