Skip to content

Commit 5698e63

Browse files
author
Chris Joel
committed
Non-destructive @keyframes rule transformation.
Previously, the transformer did not disambiguate selectors in `@media` blocks and keyframes in `@keyframes` blocks. Now, the transformer can safely transform `@keyframes` blocks. Before a selector is transformed, if the selector has a parent, it is checked. If the checked parent is a `@keyframes` rule, the selector transformation is skipped. Element-specific `@keyframes` are suffixed with the scoped element name. For example, `@keyframes foo` in an element scoped with `x-el-0` will by transformed to `@keyframes foo-x-el-0`. References to that animation in the element's local styles will be updated as well.
1 parent 92edf4a commit 5698e63

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

src/lib/css-parse.html

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
node.type = this.types.MEDIA_RULE;
7474
} else if (s.match(this._rx.keyframesRule)) {
7575
node.type = this.types.KEYFRAMES_RULE;
76+
node.keyframesName =
77+
node.selector.split(this._rx.multipleSpaces).pop();
7678
}
7779
} else {
7880
if (s.indexOf(this.VAR_START) === 0) {

src/lib/style-properties.html

+34-3
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@
179179
rule.cssText = output;
180180
},
181181

182+
applyKeyframeTransforms: function(rule, keyframeTransforms) {
183+
var output = rule.cssText;
184+
if (this.rx.ANIMATION_MATCH.test(output)) {
185+
for (var keyframe in keyframeTransforms) {
186+
var transform = keyframeTransforms[keyframe];
187+
output = transform(output);
188+
}
189+
}
190+
rule.cssText = output;
191+
},
192+
182193
// Test if the rules in these styles matches the given `element` and if so,
183194
// collect any custom properties into `props`.
184195
propertyDataFromStyles: function(styles, element) {
@@ -254,15 +265,34 @@
254265
hostSelector;
255266
var hostRx = new RegExp(this.rx.HOST_PREFIX + rxHostSelector +
256267
this.rx.HOST_SUFFIX);
268+
var keyframeTransforms = {};
269+
257270
return styleTransformer.elementStyles(element, function(rule) {
258271
self.applyProperties(rule, properties);
259-
if (rule.cssText && !nativeShadow) {
260-
self._scopeSelector(rule, hostRx, hostSelector,
261-
element._scopeCssViaAttr, scopeSelector);
272+
self.applyKeyframeTransforms(rule, keyframeTransforms);
273+
if (!nativeShadow) {
274+
if (Polymer.StyleUtil.isKeyframesSelector(rule)) {
275+
var keyframesNameRx = new RegExp(rule.parent.keyframesName, 'g');
276+
self._scopeKeyframes(rule.parent, scopeSelector);
277+
keyframeTransforms[rule.parent.keyframesName] = function(cssText) {
278+
return cssText.replace(
279+
keyframesNameRx, rule.parent.transformedKeyframesName);
280+
};
281+
} else if (rule.cssText) {
282+
self._scopeSelector(rule, hostRx, hostSelector,
283+
element._scopeCssViaAttr, scopeSelector);
284+
}
262285
}
263286
});
264287
},
265288

289+
// Transforms `@keyframes` names to be unique for the current host.
290+
// Example: @keyframes foo-anim -> @keyframes foo-anim-x-foo-0
291+
_scopeKeyframes: function(rule, scopeId) {
292+
rule.transformedKeyframesName = rule.keyframesName + '-' + scopeId;
293+
rule.selector = '@keyframes ' + rule.transformedKeyframesName;
294+
},
295+
266296
// Strategy: x scope shim a selector e.g. to scope `.x-foo-42` (via classes):
267297
// non-host selector: .a.x-foo -> .x-foo-42 .a.x-foo
268298
// host selector: x-foo.wide -> x-foo.x-foo-42.wide
@@ -357,6 +387,7 @@
357387
// var(--a, fallback-literal(with-one-nested-parentheses))
358388
VAR_MATCH: /(^|\W+)var\([\s]*([^,)]*)[\s]*,?[\s]*((?:[^,)]*)|(?:[^;]*\([^;)]*\)))[\s]*?\)/gi,
359389
VAR_CAPTURE: /\([\s]*(--[^,\s)]*)(?:,[\s]*(--[^,\s)]*))?(?:\)|,)/gi,
390+
ANIMATION_MATCH: /animation|animation-name/g,
360391
IS_VAR: /^--/,
361392
BRACKETED: /\{[^}]*\}/g,
362393
HOST_PREFIX: '(?:^|[^.#[:])',

src/lib/style-transformer.html

+6-2
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,12 @@
148148
// transforms a css rule to a scoped rule.
149149
_transformRule: function(rule, transformer, scope, hostScope) {
150150
var p$ = rule.selector.split(COMPLEX_SELECTOR_SEP);
151-
for (var i=0, l=p$.length, p; (i<l) && (p=p$[i]); i++) {
152-
p$[i] = transformer.call(this, p, scope, hostScope);
151+
// we want to skip transformation of rules that appear in keyframes,
152+
// because they are keyframe selectors, not element selectors.
153+
if (!Polymer.StyleUtil.isKeyframesSelector(rule)) {
154+
for (var i=0, l=p$.length, p; (i<l) && (p=p$[i]); i++) {
155+
p$[i] = transformer.call(this, p, scope, hostScope);
156+
}
153157
}
154158
// NOTE: save transformedSelector for subsequent matching of elements
155159
// against selectors (e.g. when calculating style properties)

src/lib/style-util.html

+9-2
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,22 @@
4444
return style.__cssRules;
4545
},
4646

47+
// Tests if a rule is a keyframes selector, which looks almost exactly
48+
// like a normal selector but is not (it has nothing to do with scoping
49+
// for example).
50+
isKeyframesSelector: function(rule) {
51+
return rule.parent &&
52+
rule.parent.type === Polymer.StyleUtil.ruleTypes.KEYFRAMES_RULE;
53+
},
54+
4755
forEachStyleRule: function(node, callback) {
4856
if (!node) {
4957
return;
5058
}
5159
var skipRules = false;
5260
if (node.type === this.ruleTypes.STYLE_RULE) {
5361
callback(node);
54-
} else if (node.type === this.ruleTypes.KEYFRAMES_RULE ||
55-
node.type === this.ruleTypes.MIXIN_RULE) {
62+
} else if (node.type === this.ruleTypes.MIXIN_RULE) {
5663
skipRules = true;
5764
}
5865
var r$ = node.rules;

0 commit comments

Comments
 (0)