@@ -11,6 +11,31 @@ const int _maxExpansionDepth = 64;
1111/// `@extend` group, or `Expressions` list during `var()` polyfill resolution.
1212const int _maxExpansions = 4096 ;
1313
14+ bool _checkIncludeLimits (
15+ Messages messages,
16+ String name,
17+ SourceSpan ? span,
18+ Set <String > activeMixins,
19+ int totalExpansions,
20+ int stepCost,
21+ ) {
22+ if (activeMixins.contains (name)) {
23+ messages.error ('Circular @include for $name ' , span);
24+ return false ;
25+ }
26+ if (activeMixins.length >= _maxExpansionDepth) {
27+ messages.error (
28+ '@include depth exceeded $_maxExpansionDepth for $name ' , span);
29+ return false ;
30+ }
31+ if (totalExpansions + stepCost > _maxExpansions) {
32+ messages.error (
33+ '@include expansions exceeded $_maxExpansions for $name ' , span);
34+ return false ;
35+ }
36+ return true ;
37+ }
38+
1439// TODO(terry): Add optimizing phase to remove duplicated selectors in the same
1540// selector group (e.g., .btn, .btn { color: red; }). Also, look
1641// at simplifying selectors expressions too (much harder).
@@ -489,10 +514,8 @@ class TopLevelIncludes extends Visitor {
489514 final name = node.name;
490515 final rules = mixinDef.rulesets;
491516 final stepCost = math.max (1 , rules.length);
492- if (_activeMixins.contains (name) ||
493- _activeMixins.length >= _maxExpansionDepth ||
494- _totalExpansions + stepCost > _maxExpansions) {
495- _messages.error ('Circular or too deep @include for $name ' , node.span);
517+ if (! _checkIncludeLimits (_messages, name, node.span, _activeMixins,
518+ _totalExpansions, stepCost)) {
496519 return const < TreeNode > [];
497520 }
498521 _activeMixins.add (name);
@@ -530,9 +553,7 @@ class TopLevelIncludes extends Visitor {
530553 @override
531554 void visitIncludeDirective (IncludeDirective node) {
532555 final currDef = this .currDef;
533- if (currDef is MixinRulesetDirective && node.name == currDef.name) {
534- currDef.rulesets.removeWhere ((entry) => entry == node);
535- } else if (map.containsKey (node.name)) {
556+ if (map.containsKey (node.name)) {
536557 var mixinDef = map[node.name];
537558 if (mixinDef is MixinRulesetDirective ) {
538559 final targetList = currDef is MixinRulesetDirective
@@ -806,11 +827,8 @@ class DeclarationIncludes extends Visitor {
806827 if (_activeMixins.isEmpty) _totalExpansions = 0 ;
807828 final stepCost = math.max (1 , estimatedCount);
808829 final targetRoot = currDeclGroup ?? _styleSheet! ;
809- if (_activeMixins.contains (mixinName) ||
810- _activeMixins.length >= _maxExpansionDepth ||
811- _totalExpansions + stepCost > _maxExpansions) {
812- _messages.error (
813- 'Circular or too deep @include for $mixinName ' , node.span);
830+ if (! _checkIncludeLimits (_messages, mixinName, node.span, _activeMixins,
831+ _totalExpansions, stepCost)) {
814832 _IncludeReplacer .replace (targetRoot, node, const < TreeNode > []);
815833 return ;
816834 }
@@ -879,9 +897,6 @@ class DeclarationIncludes extends Visitor {
879897 var mixinDef = map[node.include.name];
880898 if (mixinDef == null ) {
881899 _messages.warning ('Undefined mixin ${node .include .name }' , node.span);
882- if (currDeclGroup != null ) {
883- _IncludeReplacer .replace (currDeclGroup! , node, const < TreeNode > []);
884- }
885900 } else if (mixinDef is MixinRulesetDirective ) {
886901 if (! _allIncludes (mixinDef.rulesets) && currDeclGroup != null ) {
887902 _IncludeReplacer .replace (currDeclGroup! , node, const < TreeNode > []);
@@ -1133,13 +1148,25 @@ class InheritExtends extends Visitor {
11331148 static String _selectorText (Selector selector) =>
11341149 (CssPrinter ()..visitSelector (selector)).toString ();
11351150
1151+ static bool _hasCycleInChain (
1152+ Selector selector,
1153+ String candidateText,
1154+ Map <Selector , Selector > parents,
1155+ Map <Selector , String > texts,
1156+ ) {
1157+ for (Selector ? curr = selector; curr != null ; curr = parents[curr]) {
1158+ if ((texts[curr] ?? = _selectorText (curr)) == candidateText) return true ;
1159+ }
1160+ return false ;
1161+ }
1162+
11361163 static Selector ? _deriveSelector (
11371164 SelectorGroup group,
11381165 Selector selectors,
11391166 SelectorGroup match,
11401167 int index,
1141- Map <String , int > seenDepth ,
1142- int depth , {
1168+ Map <Selector , Selector > parents ,
1169+ Map < Selector , String > texts , {
11431170 required bool isLastNone,
11441171 }) {
11451172 if (match == group || match.selectors.isEmpty) return null ;
@@ -1164,16 +1191,15 @@ class InheritExtends extends Visitor {
11641191 derived = newSelectors;
11651192 }
11661193 final derivedText = _selectorText (derived);
1167- if ((seenDepth[ derivedText] ?? depth) < depth ) return null ;
1168- seenDepth[derivedText ] = depth ;
1194+ if (_hasCycleInChain (selectors, derivedText, parents, texts) ) return null ;
1195+ texts[derived ] = derivedText ;
11691196 return derived;
11701197 }
11711198
11721199 @override
11731200 void visitSelectorGroup (SelectorGroup node) {
1174- final seenDepth = < String , int > {
1175- for (final s in node.selectors) _selectorText (s): 0 ,
1176- };
1201+ final parents = < Selector , Selector > {};
1202+ final texts = < Selector , String > {};
11771203 final depths = < Selector , int > {};
11781204 var expansions = 0 ;
11791205 for (var selectorsIndex = 0 ;
@@ -1200,18 +1226,26 @@ class InheritExtends extends Visitor {
12001226 selectors,
12011227 match,
12021228 index,
1203- seenDepth ,
1204- depth ,
1229+ parents ,
1230+ texts ,
12051231 isLastNone: isLastNone,
12061232 );
12071233 isLastNone = false ;
12081234 if (derived == null ) continue ;
1209- if (depth > _maxExpansionDepth || expansions >= _maxExpansions) {
1210- _messages.error ('Maximum @extend depth reached' , node.span);
1235+ if (depth > _maxExpansionDepth) {
1236+ _messages.error (
1237+ '@extend depth exceeded $_maxExpansionDepth ' , node.span);
1238+ super .visitSelectorGroup (node);
1239+ return ;
1240+ }
1241+ if (expansions >= _maxExpansions) {
1242+ _messages.error (
1243+ '@extend expansions exceeded $_maxExpansions ' , node.span);
12111244 super .visitSelectorGroup (node);
12121245 return ;
12131246 }
12141247 expansions++ ;
1248+ parents[derived] = selectors;
12151249 depths[derived] = depth;
12161250 node.selectors.add (derived);
12171251 }
0 commit comments