OLD | NEW |
(Empty) | |
| 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 Animated
CSSStyleDeclaration), |
| 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.getPropertyValu
e(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, modifiesSt
yle) { |
| 124 return function() { |
| 125 var result = this._surrogateStyle[method].apply(this._surrogateStyle, ar
guments); |
| 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-in
line-style-methods.js |
| 161 // module instead and restrict inline style interactions to the methods list
ed 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); |
OLD | NEW |