Simplify specificity propagation.

A simple selector's source specificity should only come from its
original complex selector, and shouldn't be modified by extensions.
This commit is contained in:
Natalie Weizenbaum 2017-05-25 14:02:21 -07:00
parent 9cfbf79f47
commit 4193b6346b

View File

@ -159,7 +159,10 @@ class Extender {
for (var component in complex.components) {
if (component is CompoundSelector) {
for (var simple in component.components) {
_sourceSpecificity[simple] = complex.maxSpecificity;
// Only source specificity for the original selector is relevant.
// Selectors generated by `@extend` don't get new specificity.
_sourceSpecificity.putIfAbsent(
simple, () => complex.maxSpecificity);
}
}
}
@ -419,11 +422,10 @@ class Extender {
specificity = math.max(specificity, state.specificity);
}
return complexes.map((components) {
var complex = new ComplexSelector(components, lineBreak: lineBreak);
_addSourceSpecificity(complex, specificity);
return complex;
}).toList();
return complexes
.map((components) =>
new ComplexSelector(components, lineBreak: lineBreak))
.toList();
});
return _trim(unifiedPaths.where((complexes) => complexes != null).toList());
@ -598,10 +600,8 @@ class Extender {
var maxSpecificity = 0;
for (var component in complex1.components) {
if (component is CompoundSelector) {
for (var simple in component.components) {
maxSpecificity =
math.max(maxSpecificity, _sourceSpecificity[simple] ?? 0);
}
maxSpecificity =
math.max(maxSpecificity, _sourceSpecificityFor(component));
}
}
@ -630,19 +630,6 @@ class Extender {
return result;
}
/// Adds [specificity] to the [_sourceSpecificity] for all simple selectors in [complex].
void _addSourceSpecificity(ComplexSelector complex, int specificity) {
if (specificity == 0) return;
for (var component in complex.components) {
if (component is CompoundSelector) {
for (var simple in component.components) {
_sourceSpecificity[simple] =
math.max(_sourceSpecificity[simple] ?? 0, specificity);
}
}
}
}
/// Returns the maximum specificity for sources that went into producing
/// [compound].
int _sourceSpecificityFor(CompoundSelector compound) {