Skip to content

Commit 7ca41d5

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 7ca41d5

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-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

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

182+
// Apply keyframe transformations to the cssText of a given rule. The
183+
// keyframeTransforms object is a map of keyframe names to transformer
184+
// functions which take in cssText and spit out transformed cssText.
185+
applyKeyframeTransforms: function(rule, keyframeTransforms) {
186+
var output = rule.cssText;
187+
if (this.rx.ANIMATION_MATCH.test(output)) {
188+
for (var keyframe in keyframeTransforms) {
189+
var transform = keyframeTransforms[keyframe];
190+
output = transform(output);
191+
}
192+
}
193+
rule.cssText = output;
194+
},
195+
182196
// Test if the rules in these styles matches the given `element` and if so,
183197
// collect any custom properties into `props`.
184198
propertyDataFromStyles: function(styles, element) {
@@ -254,15 +268,39 @@
254268
hostSelector;
255269
var hostRx = new RegExp(this.rx.HOST_PREFIX + rxHostSelector +
256270
this.rx.HOST_SUFFIX);
271+
var keyframeTransforms = {};
272+
257273
return styleTransformer.elementStyles(element, function(rule) {
258274
self.applyProperties(rule, properties);
259-
if (rule.cssText && !nativeShadow) {
260-
self._scopeSelector(rule, hostRx, hostSelector,
261-
element._scopeCssViaAttr, scopeSelector);
275+
self.applyKeyframeTransforms(rule, keyframeTransforms);
276+
if (!nativeShadow) {
277+
// If the rule is a keyframe selector, we put a transformation in
278+
// the keyframeTransforms map so that it can be applied to other
279+
// rules in the future. We use an anonymous function for the
280+
// transformer so that we can avoid creating a new RegExp for every
281+
// call to `applyKeyframeTransforms`.
282+
if (Polymer.StyleUtil.isKeyframesSelector(rule)) {
283+
var keyframesNameRx = new RegExp(rule.parent.keyframesName, 'g');
284+
self._scopeKeyframes(rule.parent, scopeSelector);
285+
keyframeTransforms[rule.parent.keyframesName] = function(cssText) {
286+
return cssText.replace(
287+
keyframesNameRx, rule.parent.transformedKeyframesName);
288+
};
289+
} else if (rule.cssText) {
290+
self._scopeSelector(rule, hostRx, hostSelector,
291+
element._scopeCssViaAttr, scopeSelector);
292+
}
262293
}
263294
});
264295
},
265296

297+
// Transforms `@keyframes` names to be unique for the current host.
298+
// Example: @keyframes foo-anim -> @keyframes foo-anim-x-foo-0
299+
_scopeKeyframes: function(rule, scopeId) {
300+
rule.transformedKeyframesName = rule.keyframesName + '-' + scopeId;
301+
rule.selector = '@keyframes ' + rule.transformedKeyframesName;
302+
},
303+
266304
// Strategy: x scope shim a selector e.g. to scope `.x-foo-42` (via classes):
267305
// non-host selector: .a.x-foo -> .x-foo-42 .a.x-foo
268306
// host selector: x-foo.wide -> x-foo.x-foo-42.wide
@@ -357,6 +395,7 @@
357395
// var(--a, fallback-literal(with-one-nested-parentheses))
358396
VAR_MATCH: /(^|\W+)var\([\s]*([^,)]*)[\s]*,?[\s]*((?:[^,)]*)|(?:[^;]*\([^;)]*\)))[\s]*?\)/gi,
359397
VAR_CAPTURE: /\([\s]*(--[^,\s)]*)(?:,[\s]*(--[^,\s)]*))?(?:\)|,)/gi,
398+
ANIMATION_MATCH: /animation|animation-name/g,
360399
IS_VAR: /^--/,
361400
BRACKETED: /\{[^}]*\}/g,
362401
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)