From 4173ffc9019351197e2745a57d4dff0aa6e3f18f Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Fri, 17 Feb 2017 14:17:43 -0800 Subject: [PATCH] Factor out _maybeExtendPseudo from _extendCompound. --- lib/src/extend/extender.dart | 48 +++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/lib/src/extend/extender.dart b/lib/src/extend/extender.dart index 0ab1aa5b..1d3b3208 100644 --- a/lib/src/extend/extender.dart +++ b/lib/src/extend/extender.dart @@ -299,20 +299,10 @@ class Extender { for (var i = 0; i < compound.components.length; i++) { var simple = compound.components[i]; - var extendedPseudo = simple is PseudoSelector && simple.selector != null - ? _extendPseudo(simple, extensions, mediaQueryContext, - replace: replace) - : null; - - if (extendedPseudo != null) { - var simples = new List( - compound.components.length - 1 + extendedPseudo.length); - simples.setRange(0, i, compound.components); - simples.setRange(i, i + extendedPseudo.length, extendedPseudo); - simples.setRange(i + extendedPseudo.length, simples.length, - compound.components, i + 1); - original = new CompoundSelector(simples); - } + original = _maybeExtendPseudo( + compound, i, simple, extensions, mediaQueryContext, + replace: replace) ?? + original; var sources = extensions[simple]; if (sources == null) continue; @@ -371,6 +361,36 @@ class Extender { return extended; } + /// If [simple] is a selector pseudoclass, extends its contents and returns a + /// copy of [compound] with the results injected instead of the simple + /// selector at [i] (assumed to be [simple]). + /// + /// If the extension doesn't happen for any reason, returns `null` instead. + CompoundSelector _maybeExtendPseudo( + CompoundSelector compound, + int i, + SimpleSelector simple, + Map> extensions, + List mediaQueryContext, + {bool replace: false}) { + if (simple is PseudoSelector && simple.selector != null) { + var extendedPseudo = _extendPseudo(simple, extensions, mediaQueryContext, + replace: replace); + if (extendedPseudo == null) return null; + + assert(identical(compound.components[i], simple)); + var simples = new List( + compound.components.length - 1 + extendedPseudo.length); + simples.setRange(0, i, compound.components); + simples.setRange(i, i + extendedPseudo.length, extendedPseudo); + simples.setRange(i + extendedPseudo.length, simples.length, + compound.components, i + 1); + return new CompoundSelector(simples); + } else { + return null; + } + } + /// Extends [pseudo] using [extensions], and returns a list of resulting /// pseudo selectors. ///