Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add try-catch guard to object-form loading check #434

Merged
merged 3 commits into from
Apr 28, 2016
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions src/web-animations-bonus-object-form-keyframes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,38 @@
// limitations under the License.

(function(shared) {
// If an animation with the new syntax applies an effect, there's no need
// to load this part of the polyfill.
// If the polyfill is being loaded in a context where Element.animate is
// supported but object-form syntax is not, then creating an animation
// using the new syntax will either have no effect or will throw an exception.
// In either case, we want to proceed to load this part of the polyfill.
//
// The test animation starts from an opacity other than the one the element
// already has. The finish opacity is arbitrary. After the test, the element's
// opacity will be left how we found it:
// - If the animation is not created, the test will leave the element's
// opacity untouched at originalOpacity.
// - If the animation is created, it will be cancelled, and leave the
// element's opacity at originalOpacity.
// - If the animation is somehow created and runs without being cancelled,
// when it finishes after 1ms, it will cease to have any effect (because
// fill is not specified), and opacity will again be left at originalOpacity.
var element = document.documentElement;
var animation = element.animate({'opacity': ['1', '0']},
{duration: 1, fill: 'forwards'});
animation.finish();
var animated = getComputedStyle(element).getPropertyValue('opacity') == '0';
animation.cancel();
var animation = null;
var animated = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialize to false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

try {
var originalOpacity = getComputedStyle(element).getPropertyValue('opacity');
var startOpacity = originalOpacity == '0' ? '1' : '0';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

start / end should can be the same target value for simplicity

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

var finishOpacity = '1';
animation = element.animate({'opacity': [startOpacity, finishOpacity]},
{duration: 1});
animation.currentTime = 0;
animated = getComputedStyle(element).getPropertyValue('opacity') == startOpacity;
} catch (err) {
animated = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can drop this once it inits to false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

} finally {
if (animation)
animation.cancel();
}
if (animated) {
return;
}
Expand Down