Skip to content

Commit d4327e4

Browse files
committed
[csslib] Address review feedback on parser, analyzer, and polyfill fixes
- Widen ItemTerm guard to `term is LiteralTerm` so CSS Grid `[ident]` line names are preserved. - Add `_maxSupportsDepth = 512` in `processSupportsConditionInParens` to prevent stack overflow on deeply nested `@supports` conditions. - Count actual `@include` and `@extend` expansions rather than container list lengths so large stylesheets expand normally. - Scope `AllExtends._extendsToRemove` per `visitDeclarationGroup` call so nested rulesets inside `@keyframes` or `@font-face` do not lose declarations or throw `RangeError`. - Fix `_VarDefAndUsage.visitExpressions` and `resolveUsageTerminal` so removing undefined `var()` usages does not skip subsequent expressions.
1 parent b2e2424 commit d4327e4

4 files changed

Lines changed: 152 additions & 46 deletions

File tree

‎pkgs/csslib/lib/parser.dart‎

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ const _legacyPseudoElements = <String>{
181181

182182
/// A simple recursive descent parser for CSS.
183183
class _Parser {
184+
static const int _maxSupportsDepth = 512;
185+
184186
final Tokenizer tokenizer;
185187

186188
/// File containing the source being parsed, used to report errors with
@@ -189,6 +191,7 @@ class _Parser {
189191

190192
Token? _previousToken;
191193
late Token _peekToken;
194+
int _supportsDepth = 0;
192195

193196
_Parser(this.file, String text, {int start = 0})
194197
: tokenizer = Tokenizer(file, text, true, start) {
@@ -1090,16 +1093,36 @@ class _Parser {
10901093
SupportsConditionInParens processSupportsConditionInParens() {
10911094
var start = _peekToken.span;
10921095
_eat(TokenKind.LPAREN);
1093-
// Try to parse a condition.
1094-
var condition = processSupportsCondition();
1095-
if (condition != null) {
1096+
if (_supportsDepth >= _maxSupportsDepth) {
1097+
_error('Supports condition nesting exceeded maximum depth',
1098+
_makeSpan(start));
1099+
var parenDepth = 1;
1100+
while (parenDepth > 0 && !_peekKind(TokenKind.END_OF_FILE)) {
1101+
if (_maybeEat(TokenKind.LPAREN)) {
1102+
parenDepth++;
1103+
} else if (_maybeEat(TokenKind.RPAREN)) {
1104+
parenDepth--;
1105+
} else {
1106+
_next();
1107+
}
1108+
}
1109+
return SupportsConditionInParens(null, _makeSpan(start));
1110+
}
1111+
_supportsDepth++;
1112+
try {
1113+
// Try to parse a condition.
1114+
var condition = processSupportsCondition();
1115+
if (condition != null) {
1116+
_eat(TokenKind.RPAREN);
1117+
return SupportsConditionInParens.nested(condition, _makeSpan(start));
1118+
}
1119+
// Otherwise, parse a declaration.
1120+
var declaration = processDeclaration([]);
10961121
_eat(TokenKind.RPAREN);
1097-
return SupportsConditionInParens.nested(condition, _makeSpan(start));
1122+
return SupportsConditionInParens(declaration, _makeSpan(start));
1123+
} finally {
1124+
_supportsDepth--;
10981125
}
1099-
// Otherwise, parse a declaration.
1100-
var declaration = processDeclaration([]);
1101-
_eat(TokenKind.RPAREN);
1102-
return SupportsConditionInParens(declaration, _makeSpan(start));
11031126
}
11041127

11051128
ViewportDirective processViewportDirective() {
@@ -2355,7 +2378,7 @@ class _Parser {
23552378

23562379
_eat(TokenKind.RBRACK);
23572380

2358-
if (term is NumberTerm) {
2381+
if (term is LiteralTerm) {
23592382
return ItemTerm(term.value, term.text, _makeSpan(start));
23602383
}
23612384
return ItemTerm(0, '', _makeSpan(start));

‎pkgs/csslib/lib/src/analyzer.dart‎

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,10 @@ class _MediaRulesReplacer extends Visitor {
452452
/// Expand all @include at the top-level the ruleset(s) associated with the
453453
/// mixin.
454454
class TopLevelIncludes extends Visitor {
455+
static const int _maxExpansions = 256;
455456
StyleSheet? _styleSheet;
456457
final Messages _messages;
458+
int _expansions = 0;
457459

458460
/// Map of variable name key to it's definition.
459461
final map = <String, MixinDefinition>{};
@@ -487,7 +489,12 @@ class TopLevelIncludes extends Visitor {
487489
} else if (map.containsKey(node.name)) {
488490
var mixinDef = map[node.name];
489491
if (mixinDef is MixinRulesetDirective) {
490-
_TopLevelIncludeReplacer.replace(_styleSheet!, node, mixinDef.rulesets);
492+
_TopLevelIncludeReplacer.replace(
493+
_styleSheet!,
494+
node,
495+
_expansions++ < _maxExpansions
496+
? mixinDef.rulesets
497+
: const <TreeNode>[]);
491498
} else if (currDef is MixinRulesetDirective && _anyRulesets(currDef)) {
492499
final mixinRuleset = currDef;
493500
var index = mixinRuleset.rulesets.indexOf(node);
@@ -538,7 +545,6 @@ class TopLevelIncludes extends Visitor {
538545

539546
/// @include as a top-level with ruleset(s).
540547
class _TopLevelIncludeReplacer extends Visitor {
541-
static const int _maxExpansions = 256;
542548
final IncludeDirective _include;
543549
final List<TreeNode> _newRules;
544550

@@ -556,9 +562,7 @@ class _TopLevelIncludeReplacer extends Visitor {
556562
void visitStyleSheet(StyleSheet node) {
557563
var index = node.topLevels.indexOf(_include);
558564
if (index != -1) {
559-
if (node.topLevels.length < _maxExpansions) {
560-
node.topLevels.insertAll(index + 1, _newRules);
561-
}
565+
node.topLevels.insertAll(index + 1, _newRules);
562566
node.topLevels.replaceRange(index, index + 1, [NoOp()]);
563567
}
564568
super.visitStyleSheet(node);
@@ -568,9 +572,7 @@ class _TopLevelIncludeReplacer extends Visitor {
568572
void visitMixinRulesetDirective(MixinRulesetDirective node) {
569573
var index = node.rulesets.indexOf(_include);
570574
if (index != -1) {
571-
if (node.rulesets.length < _maxExpansions) {
572-
node.rulesets.insertAll(index + 1, _newRules);
573-
}
575+
node.rulesets.insertAll(index + 1, _newRules);
574576
// Only the resolve the @include once.
575577
node.rulesets.replaceRange(index, index + 1, [NoOp()]);
576578
}
@@ -734,8 +736,10 @@ class CallMixin extends Visitor {
734736

735737
/// Expand all @include inside of a declaration associated with a mixin.
736738
class DeclarationIncludes extends Visitor {
739+
static const int _maxExpansions = 256;
737740
StyleSheet? _styleSheet;
738741
final Messages _messages;
742+
int _expansions = 0;
739743

740744
/// Map of variable name key to it's definition.
741745
final Map<String, MixinDefinition> map = <String, MixinDefinition>{};
@@ -804,7 +808,12 @@ class DeclarationIncludes extends Visitor {
804808
rulesets.add(IncludeMixinAtDeclaration(
805809
ruleset as IncludeDirective, ruleset.span));
806810
}
807-
_IncludeReplacer.replace(_styleSheet!, node, rulesets);
811+
_IncludeReplacer.replace(
812+
_styleSheet!,
813+
node,
814+
_expansions++ < _maxExpansions
815+
? rulesets
816+
: const <Declaration>[]);
808817
}
809818
}
810819
}
@@ -816,7 +825,11 @@ class DeclarationIncludes extends Visitor {
816825

817826
if (mixinDef is MixinDeclarationDirective) {
818827
_IncludeReplacer.replace(
819-
_styleSheet!, node, mixinDef.declarations.declarations);
828+
_styleSheet!,
829+
node,
830+
_expansions++ < _maxExpansions
831+
? mixinDef.declarations.declarations
832+
: const <TreeNode>[]);
820833
}
821834
} else {
822835
_messages.warning('Undefined mixin ${node.include.name}', node.span);
@@ -832,7 +845,11 @@ class DeclarationIncludes extends Visitor {
832845
if (currDef is MixinDeclarationDirective &&
833846
mixinDef is MixinDeclarationDirective) {
834847
_IncludeReplacer.replace(
835-
_styleSheet!, node, mixinDef.declarations.declarations);
848+
_styleSheet!,
849+
node,
850+
_expansions++ < _maxExpansions
851+
? mixinDef.declarations.declarations
852+
: const <TreeNode>[]);
836853
} else if (currDef is MixinDeclarationDirective) {
837854
var decls =
838855
(currDef as MixinDeclarationDirective).declarations.declarations;
@@ -889,7 +906,6 @@ class DeclarationIncludes extends Visitor {
889906

890907
/// @include as a top-level with ruleset(s).
891908
class _IncludeReplacer extends Visitor {
892-
static const int _maxExpansions = 256;
893909
final TreeNode _include;
894910
final List<TreeNode> _newDeclarations;
895911

@@ -907,9 +923,7 @@ class _IncludeReplacer extends Visitor {
907923
void visitDeclarationGroup(DeclarationGroup node) {
908924
var index = _findInclude(node.declarations, _include);
909925
if (index != -1) {
910-
if (node.declarations.length < _maxExpansions) {
911-
node.declarations.insertAll(index + 1, _newDeclarations);
912-
}
926+
node.declarations.insertAll(index + 1, _newDeclarations);
913927
// Change @include to NoOp so it's processed only once.
914928
node.declarations.replaceRange(index, index + 1, [NoOp()]);
915929
}
@@ -956,7 +970,7 @@ class AllExtends extends Visitor {
956970

957971
SelectorGroup? _currSelectorGroup;
958972
int? _currDeclIndex;
959-
final _extendsToRemove = <int>[];
973+
List<int> _extendsToRemove = <int>[];
960974

961975
@override
962976
void visitRuleSet(RuleSet node) {
@@ -993,6 +1007,8 @@ class AllExtends extends Visitor {
9931007
@override
9941008
void visitDeclarationGroup(DeclarationGroup node) {
9951009
var oldDeclIndex = _currDeclIndex;
1010+
var oldExtendsToRemove = _extendsToRemove;
1011+
_extendsToRemove = <int>[];
9961012

9971013
var decls = node.declarations;
9981014
for (_currDeclIndex = 0;
@@ -1009,6 +1025,7 @@ class AllExtends extends Visitor {
10091025
_extendsToRemove.clear();
10101026
}
10111027

1028+
_extendsToRemove = oldExtendsToRemove;
10121029
_currDeclIndex = oldDeclIndex;
10131030
}
10141031
}
@@ -1018,14 +1035,16 @@ class AllExtends extends Visitor {
10181035
// TODO(terry): Need to handle !optional glag.
10191036
/// Changes any selector that matches @extend.
10201037
class InheritExtends extends Visitor {
1038+
static const int _maxExpansions = 256;
10211039
final AllExtends _allExtends;
10221040

10231041
InheritExtends(Messages messages, this._allExtends);
10241042

10251043
@override
10261044
void visitSelectorGroup(SelectorGroup node) {
1045+
var expansions = 0;
10271046
for (var selectorsIndex = 0;
1028-
selectorsIndex < node.selectors.length && node.selectors.length < 64;
1047+
selectorsIndex < node.selectors.length && expansions < _maxExpansions;
10291048
selectorsIndex++) {
10301049
var selectors = node.selectors[selectorsIndex];
10311050
var isLastNone = false;
@@ -1040,6 +1059,8 @@ class InheritExtends extends Visitor {
10401059
if (matches != null) {
10411060
for (var match in matches) {
10421061
if (match == node || match.selectors.isEmpty) continue;
1062+
if (expansions >= _maxExpansions) break;
1063+
expansions++;
10431064
// Create a new group.
10441065
var newSelectors = selectors.clone();
10451066
var newSeq = match.selectors[0].clone();

‎pkgs/csslib/lib/src/polyfill.dart‎

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,14 @@ class _VarDefinitionsIncludes extends Visitor {
8383
/// Find var- definitions in a style sheet.
8484
/// [found] list of known definitions.
8585
class _VarDefAndUsage extends Visitor {
86+
static const int _maxExpansions = 256;
8687
final Messages _messages;
8788
final Map<String, VarDefinition> _knownVarDefs;
8889
final varDefs = <String, VarDefinition>{};
8990

9091
VarDefinition? currVarDefinition;
9192
List<Expression>? currentExpressions;
93+
int _expansions = 0;
9294

9395
_VarDefAndUsage(this._messages, this._knownVarDefs);
9496

@@ -118,8 +120,19 @@ class _VarDefAndUsage extends Visitor {
118120
@override
119121
void visitExpressions(Expressions node) {
120122
var oldExpressions = currentExpressions;
121-
currentExpressions = node.expressions;
122-
super.visitExpressions(node);
123+
var expressions = node.expressions;
124+
currentExpressions = expressions;
125+
var index = 0;
126+
while (index < expressions.length) {
127+
var current = expressions[index];
128+
current.visit(this);
129+
if (index < expressions.length &&
130+
identical(expressions[index], current)) {
131+
index++;
132+
} else if (_expansions++ >= _maxExpansions) {
133+
break;
134+
}
135+
}
123136
currentExpressions = oldExpressions;
124137
}
125138

@@ -139,12 +152,7 @@ class _VarDefAndUsage extends Visitor {
139152
if (def == currVarDefinition) {
140153
def = null;
141154
}
142-
if (def != null) {
143-
if (def.badUsage) {
144-
// Remove any expressions pointing to a bad var definition.
145-
expressions.removeAt(index);
146-
return;
147-
}
155+
if (def != null && !def.badUsage) {
148156
_resolveVarUsage(currentExpressions!, index,
149157
_findTerminalVarDefinition(_knownVarDefs, def));
150158
} else if (node.defaultValues.any((e) => e is VarUsage)) {
@@ -172,7 +180,9 @@ class _VarDefAndUsage extends Visitor {
172180
}
173181
// Remove var usage that points at an undefined definition.
174182
expressions.removeAt(index);
175-
_messages.warning('Variable is not defined.', node.span);
183+
if (def == null) {
184+
_messages.warning('Variable is not defined.', node.span);
185+
}
176186
}
177187

178188
var oldExpressions = currentExpressions;
@@ -187,7 +197,7 @@ class _VarDefAndUsage extends Visitor {
187197
visited ??= <VarDefinition>{};
188198

189199
var varDef = _knownVarDefs[usage.name];
190-
if (varDef != null && !visited.add(varDef)) {
200+
if (varDef != null && (varDef.badUsage || !visited.add(varDef))) {
191201
varDef = null;
192202
}
193203
List<Expression> expressions;
@@ -205,14 +215,11 @@ class _VarDefAndUsage extends Visitor {
205215
if (expr is VarUsage) {
206216
// Get terminal value.
207217
result.addAll(resolveUsageTerminal(expr, visited));
218+
} else {
219+
result.add(expr);
208220
}
209221
}
210222

211-
// We're at a terminal just return the VarDefinition expression.
212-
if (result.isEmpty && varDef != null && varDef.expression is Expressions) {
213-
result = (varDef.expression as Expressions).expressions;
214-
}
215-
216223
return result;
217224
}
218225

0 commit comments

Comments
 (0)