Skip to content

Commit bd49697

Browse files
committed
Merge remote-tracking branch 'web-animations-next/master' into HEAD
2 parents 0d2ab41 + 483fb2e commit bd49697

14 files changed

+344
-30
lines changed

src/apply-preserving-inline-style.js

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
// Copyright 2014 Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
(function(scope, testing) {
16+
17+
var styleAttributes = {
18+
cssText: 1,
19+
length: 1,
20+
parentRule: 1,
21+
};
22+
23+
var styleMethods = {
24+
getPropertyCSSValue: 1,
25+
getPropertyPriority: 1,
26+
getPropertyValue: 1,
27+
item: 1,
28+
removeProperty: 1,
29+
setProperty: 1,
30+
};
31+
32+
var styleMutatingMethods = {
33+
removeProperty: 1,
34+
setProperty: 1,
35+
};
36+
37+
function configureProperty(object, property, descriptor) {
38+
descriptor.enumerable = true;
39+
descriptor.configurable = true;
40+
Object.defineProperty(object, property, descriptor);
41+
}
42+
43+
function AnimatedCSSStyleDeclaration(element) {
44+
WEB_ANIMATIONS_TESTING && console.assert(!(element.style instanceof AnimatedCSSStyleDeclaration),
45+
'Element must not already have an animated style attached.');
46+
47+
// Stores the inline style of the element on its behalf while the
48+
// polyfill uses the element's inline style to simulate web animations.
49+
// This is needed to fake regular inline style CSSOM access on the element.
50+
this._surrogateElement = document.createElement('div');
51+
this._surrogateStyle = this._surrogateElement.style;
52+
this._style = element.style;
53+
this._length = 0;
54+
this._isAnimatedProperty = {};
55+
56+
// Copy the inline style contents over to the surrogate.
57+
for (var i = 0; i < this._style.length; i++) {
58+
var property = this._style[i];
59+
this._surrogateStyle[property] = this._style[property];
60+
}
61+
this._updateIndices();
62+
}
63+
64+
AnimatedCSSStyleDeclaration.prototype = {
65+
get cssText() {
66+
return this._surrogateStyle.cssText;
67+
},
68+
set cssText(text) {
69+
var isAffectedProperty = {};
70+
for (var i = 0; i < this._surrogateStyle.length; i++) {
71+
isAffectedProperty[this._surrogateStyle[i]] = true;
72+
}
73+
this._surrogateStyle.cssText = text;
74+
this._updateIndices();
75+
for (var i = 0; i < this._surrogateStyle.length; i++) {
76+
isAffectedProperty[this._surrogateStyle[i]] = true;
77+
}
78+
for (var property in isAffectedProperty) {
79+
if (!this._isAnimatedProperty[property]) {
80+
this._style.setProperty(property, this._surrogateStyle.getPropertyValue(property));
81+
}
82+
}
83+
},
84+
get length() {
85+
return this._surrogateStyle.length;
86+
},
87+
get parentRule() {
88+
return this._style.parentRule;
89+
},
90+
// Mirror the indexed getters and setters of the surrogate style.
91+
_updateIndices: function() {
92+
while (this._length < this._surrogateStyle.length) {
93+
Object.defineProperty(this, this._length, {
94+
configurable: true,
95+
enumerable: false,
96+
get: (function(index) {
97+
return function() { return this._surrogateStyle[index]; };
98+
})(this._length)
99+
});
100+
this._length++;
101+
}
102+
while (this._length > this._surrogateStyle.length) {
103+
this._length--;
104+
Object.defineProperty(this, this._length, {
105+
configurable: true,
106+
enumerable: false,
107+
value: undefined
108+
});
109+
}
110+
},
111+
_set: function(property, value) {
112+
this._style[property] = value;
113+
this._isAnimatedProperty[property] = true;
114+
},
115+
_clear: function(property) {
116+
this._style[property] = this._surrogateStyle[property];
117+
delete this._isAnimatedProperty[property];
118+
},
119+
};
120+
121+
// Wrap the style methods.
122+
for (var method in styleMethods) {
123+
AnimatedCSSStyleDeclaration.prototype[method] = (function(method, modifiesStyle) {
124+
return function() {
125+
var result = this._surrogateStyle[method].apply(this._surrogateStyle, arguments);
126+
if (modifiesStyle) {
127+
if (!this._isAnimatedProperty[arguments[0]])
128+
this._style[method].apply(this._style, arguments);
129+
this._updateIndices();
130+
}
131+
return result;
132+
}
133+
})(method, method in styleMutatingMethods);
134+
}
135+
136+
// Wrap the style.cssProperty getters and setters.
137+
for (var property in document.documentElement.style) {
138+
if (property in styleAttributes || property in styleMethods) {
139+
continue;
140+
}
141+
(function(property) {
142+
configureProperty(AnimatedCSSStyleDeclaration.prototype, property, {
143+
get: function() {
144+
return this._surrogateStyle[property];
145+
},
146+
set: function(value) {
147+
this._surrogateStyle[property] = value;
148+
this._updateIndices();
149+
if (!this._isAnimatedProperty[property])
150+
this._style[property] = value;
151+
}
152+
});
153+
})(property);
154+
}
155+
156+
function ensureStyleIsPatched(element) {
157+
if (element._webAnimationsPatchedStyle)
158+
return;
159+
160+
// If this style patch fails (on Safari and iOS) use the apply-preserving-inline-style-methods.js
161+
// module instead and restrict inline style interactions to the methods listed in styleMethods.
162+
var animatedStyle = new AnimatedCSSStyleDeclaration(element);
163+
configureProperty(element, 'style', { get: function() { return animatedStyle; } });
164+
165+
// We must keep a handle on the patched style to prevent it from getting GC'd.
166+
element._webAnimationsPatchedStyle = element.style;
167+
}
168+
169+
scope.apply = function(element, property, value) {
170+
ensureStyleIsPatched(element);
171+
element.style._set(scope.propertyName(property), value);
172+
};
173+
174+
scope.clear = function(element, property) {
175+
if (element._webAnimationsPatchedStyle) {
176+
element.style._clear(scope.propertyName(property));
177+
}
178+
};
179+
180+
if (WEB_ANIMATIONS_TESTING)
181+
testing.ensureStyleIsPatched = ensureStyleIsPatched;
182+
183+
})(webAnimationsMinifill, webAnimationsTesting);

src/apply.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,12 @@
1414

1515
(function(scope, testing) {
1616

17-
var aliased = {};
18-
19-
function alias(name, aliases) {
20-
aliases.concat([name]).forEach(function(candidate) {
21-
if (candidate in document.documentElement.style) {
22-
aliased[name] = candidate;
23-
}
24-
});
25-
}
26-
alias('transform', ['webkitTransform', 'msTransform']);
27-
alias('transformOrigin', ['webkitTransformOrigin']);
28-
alias('perspective', ['webkitPerspective']);
29-
alias('perspectiveOrigin', ['webkitPerspectiveOrigin']);
30-
31-
function propertyName(property) {
32-
return aliased[property] || property;
33-
}
34-
3517
scope.apply = function(element, property, value) {
36-
element.style[propertyName(property)] = value;
18+
element.style[scope.propertyName(property)] = value;
3719
};
3820

3921
scope.clear = function(element, property) {
40-
element.style[propertyName(property)] = '';
22+
element.style[scope.propertyName(property)] = '';
4123
};
4224

4325
})(webAnimationsMinifill, webAnimationsTesting);

src/player.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@
6868
return this._currentTime;
6969
},
7070
set currentTime(newTime) {
71+
newTime = +newTime;
72+
if (isNaN(newTime))
73+
return;
7174
if (scope.restart())
7275
this._startTime = null;
7376
if (!this.paused && this._startTime != null) {
@@ -83,6 +86,9 @@
8386
return this._startTime;
8487
},
8588
set startTime(newTime) {
89+
newTime = +newTime;
90+
if (isNaN(newTime))
91+
return;
8692
if (this.paused || this._idle)
8793
return;
8894
this._startTime = newTime;

src/position-handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
function negateDimension(dimension) {
1818
var result = {};
19-
for (k in dimension) {
19+
for (var k in dimension) {
2020
result[k] = -dimension[k];
2121
}
2222
return result;

src/property-names.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2014 Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
(function(scope, testing) {
16+
17+
var aliased = {};
18+
19+
function alias(name, aliases) {
20+
aliases.concat([name]).forEach(function(candidate) {
21+
if (candidate in document.documentElement.style) {
22+
aliased[name] = candidate;
23+
}
24+
});
25+
}
26+
alias('transform', ['webkitTransform', 'msTransform']);
27+
alias('transformOrigin', ['webkitTransformOrigin']);
28+
alias('perspective', ['webkitPerspective']);
29+
alias('perspectiveOrigin', ['webkitPerspectiveOrigin']);
30+
31+
scope.propertyName = function(property) {
32+
return aliased[property] || property;
33+
};
34+
35+
})(webAnimationsMinifill, webAnimationsTesting);

src/timing-utilities.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@
2929
direction: 'normal',
3030
easing: 'linear',
3131
};
32-
if (typeof timingInput == 'number') {
32+
if (typeof timingInput == 'number' && !isNaN(timingInput)) {
3333
timing.duration = timingInput;
3434
} else if (timingInput !== undefined) {
3535
Object.getOwnPropertyNames(timingInput).forEach(function(property) {
3636
if (timingInput[property] != 'auto') {
37-
if (typeof timing[property] == 'number' && typeof timingInput[property] != 'number' && property != 'duration') {
38-
return;
37+
if (typeof timing[property] == 'number' || property == 'duration') {
38+
if (typeof timingInput[property] != 'number' || isNaN(timingInput[property])) {
39+
return;
40+
}
3941
}
4042
if ((property == 'fill') && (fills.indexOf(timingInput[property]) == -1)) {
4143
return;

target-config.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
'src/effect.js',
99
'src/property-interpolation.js',
1010
'src/animation.js',
11-
'src/apply.js',
11+
'src/apply-preserving-inline-style.js',
1212
'src/element-animatable.js',
1313
'src/interpolation.js',
1414
'src/matrix-interpolation.js',
@@ -25,7 +25,9 @@
2525
'src/transform-handler.js',
2626
'src/font-weight-handler.js',
2727
'src/position-handler.js',
28-
'src/shape-handler.js'];
28+
'src/shape-handler.js',
29+
'src/property-names.js',
30+
];
2931

3032
var liteMinifillSrc = [
3133
'src/animation-node.js',
@@ -44,7 +46,9 @@
4446
'src/color-handler.js',
4547
'src/dimension-handler.js',
4648
'src/box-handler.js',
47-
'src/transform-handler.js'];
49+
'src/transform-handler.js',
50+
'src/property-names.js',
51+
];
4852

4953

5054
var sharedSrc = [
@@ -60,6 +64,7 @@
6064

6165
var minifillTest = [
6266
'test/js/animation-node.js',
67+
'test/js/apply-preserving-inline-style.js',
6368
'test/js/box-handler.js',
6469
'test/js/color-handler.js',
6570
'test/js/dimension-handler.js',

0 commit comments

Comments
 (0)