@@ -123,26 +123,28 @@ class _CssShim {
123
123
List <_Rule > cssToRules (String css) =>
124
124
new _Parser (css).parse ();
125
125
126
- String scopeRules (List <_Rule > rules) {
127
- final scopedRules = [];
126
+ String scopeRules (List <_Rule > rules, {bool emitMode: false }) {
127
+ if (emitMode) {
128
+ return rules.map (ruleToString).join ("\n " );
129
+ }
128
130
131
+ final scopedRules = [];
129
132
var prevRule;
130
133
rules.forEach ((rule) {
131
134
if (prevRule != null && prevRule.selectorText == POLYFILL_NON_STRICT ) {
132
- scopedRules.add (scopeNonStrictMode (rule));
133
-
135
+ scopedRules.add (scopeNonStrictMode (rule, emitMode));
134
136
} else if (prevRule != null && prevRule.selectorText == POLYFILL_UNSCOPED_NEXT_SELECTOR ) {
135
137
final content = extractContent (prevRule);
136
138
scopedRules.add (ruleToString (new _Rule (content, body: rule.body)));
137
139
138
140
} else if (prevRule != null && prevRule.selectorText == POLYFILL_NEXT_SELECTOR ) {
139
141
final content = extractContent (prevRule);
140
- scopedRules.add (scopeStrictMode (new _Rule (content, body: rule.body)));
142
+ scopedRules.add (scopeStrictMode (new _Rule (content, body: rule.body), false ));
141
143
142
144
} else if (rule.selectorText != POLYFILL_NON_STRICT &&
143
145
rule.selectorText != POLYFILL_UNSCOPED_NEXT_SELECTOR &&
144
146
rule.selectorText != POLYFILL_NEXT_SELECTOR ) {
145
- scopedRules.add (scopeStrictMode (rule));
147
+ scopedRules.add (scopeStrictMode (rule, false ));
146
148
}
147
149
148
150
prevRule = rule;
@@ -159,19 +161,22 @@ class _CssShim {
159
161
return "${rule .selectorText } ${rule .body }" ;
160
162
}
161
163
162
- String scopeStrictMode (_Rule rule) {
164
+ String scopeStrictMode (_Rule rule, bool emitMode ) {
163
165
if (rule.hasNestedRules) {
164
- final selector = rule.selectorText;
165
- final rules = scopeRules (rule.rules);
166
- return '$selector {\n $rules \n }' ;
166
+ final rules = scopeRules (rule.rules, emitMode: rule.selectorText.contains ("keyframes" ));
167
+ return "${rule .selectorText } {\n $rules \n }" ;
167
168
} else {
168
169
final scopedSelector = scopeSelector (rule.selectorText, strict: true );
169
170
final scopedBody = cssText (rule);
170
171
return "$scopedSelector $scopedBody " ;
171
172
}
172
173
}
173
174
174
- String scopeNonStrictMode (_Rule rule) {
175
+ String scopeNonStrictMode (_Rule rule, bool emitMode) {
176
+ if (rule.hasNestedRules && rule.selectorText == "keyframes" ) {
177
+ final rules = scopeRules (rule.rules, emitMode: true );
178
+ return '${rule .selectorText } {\n $rules \n }' ;
179
+ }
175
180
final scopedBody = cssText (rule);
176
181
final scopedSelector = scopeSelector (rule.selectorText, strict: false );
177
182
return "${scopedSelector } $scopedBody " ;
@@ -281,7 +286,7 @@ class _Lexer {
281
286
advance ();
282
287
return new _Token ("}" , "rparen" );
283
288
}
284
- if (isMedia (peek)) return scanMedia ();
289
+ if (isDeclaration (peek)) return scanDeclaration ();
285
290
if (isSelector (peek)) return scanSelector ();
286
291
if (isBodyStart (peek)) return scanBody ();
287
292
@@ -291,7 +296,7 @@ class _Lexer {
291
296
bool isSelector (int v) => ! isBodyStart (v) && v != $EOF ;
292
297
bool isBodyStart (int v) => v == $LBRACE ;
293
298
bool isBodyEnd (int v) => v == $RBRACE ;
294
- bool isMedia (int v) => v == 64 ; //@ = 64
299
+ bool isDeclaration (int v) => v == 64 ; //@ = 64
295
300
296
301
void skipWhitespace () {
297
302
while (isWhitespace (peek)) {
@@ -321,7 +326,7 @@ class _Lexer {
321
326
return new _Token (string, "body" );
322
327
}
323
328
324
- _Token scanMedia () {
329
+ _Token scanDeclaration () {
325
330
int start = index;
326
331
advance ();
327
332
@@ -330,7 +335,10 @@ class _Lexer {
330
335
331
336
advance (); //skip {
332
337
333
- return new _Token (string, "media" );
338
+ // we assume that declaration cannot start with media and contain keyframes.
339
+ String type = string.contains ("keyframes" ) ? "keyframes" :
340
+ (string.startsWith ("@media" ) ? "media" : string);
341
+ return new _Token (string, type);
334
342
}
335
343
336
344
void advance () {
@@ -370,8 +378,8 @@ class _Parser {
370
378
371
379
_Rule parseRule () {
372
380
try {
373
- if (next.type == "media" ) {
374
- return parseMedia ();
381
+ if (next.type == "media" || next.type == "keyframes" ) {
382
+ return parseMedia (next.type );
375
383
} else {
376
384
return parseCssRule ();
377
385
}
@@ -380,8 +388,8 @@ class _Parser {
380
388
}
381
389
}
382
390
383
- _Rule parseMedia () {
384
- advance ("media" );
391
+ _Rule parseMedia (type ) {
392
+ advance (type );
385
393
final media = current.string;
386
394
387
395
final rules = [];
0 commit comments