Skip to content

Commit 34614da

Browse files
author
Chris Joel
committed
Update PR per feedback.
- Cache some parsing artifacts. - Replace `.bind` with factory pattern. - Fix property access where appropriate for the current scope.
1 parent 86fee9b commit 34614da

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

src/lib/style-properties.html

+47-7
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,43 @@
182182
// keyframeTransforms object is a map of keyframe names to transformer
183183
// functions which take in cssText and spit out transformed cssText.
184184
applyKeyframeTransforms: function(rule, keyframeTransforms) {
185+
var input = rule.cssText;
185186
var output = rule.cssText;
186-
if (this.rx.ANIMATION_MATCH.test(output)) {
187-
for (var keyframe in keyframeTransforms) {
188-
var transform = keyframeTransforms[keyframe];
189-
output = transform(output);
187+
188+
if (rule.hasAnimations == null) {
189+
// Cache whether or not the rule has any animations to begin with:
190+
rule.hasAnimations = this.rx.ANIMATION_MATCH.test(input);
191+
}
192+
193+
// If there are no animations referenced, we can skip transforms:
194+
if (rule.hasAnimations) {
195+
var transform;
196+
// If we haven't transformed this rule before, we iterate over all
197+
// transforms:
198+
if (rule.keyframeNamesToTransform == null) {
199+
rule.keyframeNamesToTransform = [];
200+
201+
for (var keyframe in keyframeTransforms) {
202+
transform = keyframeTransforms[keyframe];
203+
output = transform(input);
204+
// If the transform actually changed the CSS text, we cache the
205+
// transform name for future use:
206+
if (input !== output) {
207+
input = output;
208+
rule.keyframeNamesToTransform.push(keyframe);
209+
}
210+
}
211+
} else {
212+
// If we already have a list of keyframe names that apply to this
213+
// rule, we apply only those keyframe name transforms:
214+
for (var i = 0; i < rule.keyframeNamesToTransform.length; ++i) {
215+
transform = keyframeTransforms[rule.keyframeNamesToTransform[i]];
216+
input = transform(input);
217+
}
218+
output = input;
190219
}
191220
}
221+
192222
rule.cssText = output;
193223
},
194224

@@ -258,6 +288,7 @@
258288
return props;
259289
},
260290

291+
261292
transformStyles: function(element, properties, scopeSelector) {
262293
var self = this;
263294
var hostSelector = styleTransformer
@@ -278,9 +309,8 @@
278309
i < keyframesRules.length;
279310
keyframesRule = keyframesRules[++i]) {
280311
this._scopeKeyframes(keyframesRule, scopeSelector);
281-
keyframeTransforms[keyframesRule.keyframesName] = function(rule, cssText) {
282-
return cssText.replace(rule.keyframesNameRx, rule.transformedKeyframesName);
283-
}.bind(this, keyframesRule);
312+
keyframeTransforms[keyframesRule.keyframesName] =
313+
this._keyframesRuleTransformer(keyframesRule);
284314
}
285315
}
286316

@@ -297,6 +327,16 @@
297327
});
298328
},
299329

330+
// Generate a factory for transforming a chunk of CSS text to handle a
331+
// particular scoped keyframes rule.
332+
_keyframesRuleTransformer: function(keyframesRule) {
333+
return function(cssText) {
334+
return cssText.replace(
335+
keyframesRule.keyframesNameRx,
336+
keyframesRule.transformedKeyframesName);
337+
};
338+
},
339+
300340
// Transforms `@keyframes` names to be unique for the current host.
301341
// Example: @keyframes foo-anim -> @keyframes foo-anim-x-foo-0
302342
_scopeKeyframes: function(rule, scopeId) {

src/lib/style-transformer.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
var p$ = rule.selector.split(COMPLEX_SELECTOR_SEP);
151151
// we want to skip transformation of rules that appear in keyframes,
152152
// because they are keyframe selectors, not element selectors.
153-
if (!Polymer.StyleUtil.isKeyframesSelector(rule)) {
153+
if (!styleUtil.isKeyframesSelector(rule)) {
154154
for (var i=0, l=p$.length, p; (i<l) && (p=p$[i]); i++) {
155155
p$[i] = transformer.call(this, p, scope, hostScope);
156156
}

src/lib/style-util.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
// for example).
5353
isKeyframesSelector: function(rule) {
5454
return rule.parent &&
55-
rule.parent.type === Polymer.StyleUtil.ruleTypes.KEYFRAMES_RULE;
55+
rule.parent.type === this.ruleTypes.KEYFRAMES_RULE;
5656
},
5757

5858
forEachRule: function(node, styleRuleCallback, keyframesRuleCallback) {

0 commit comments

Comments
 (0)