Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(ngAnimate): safe-guard against missing document #14633

Merged
merged 2 commits into from
May 24, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions src/ngAnimate/animateQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
var activeAnimationsLookup = new $$HashMap();
var disabledElementsLookup = new $$HashMap();
var animationsEnabled = null;
// $document might be mocked out in tests and won't include a real document.
// Providing an empty object with hidden = true will prevent animations from running
var rawDocument = $document[0] || {hidden: true};

function postDigestTaskFactory() {
var postDigestCalled = false;
Expand Down Expand Up @@ -331,7 +334,7 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {

var isStructural = ['enter', 'move', 'leave'].indexOf(event) >= 0;

var documentHidden = animationsEnabled && $document[0].hidden;
var documentHidden = rawDocument.hidden;
Copy link

@ThomasBurleson ThomasBurleson May 18, 2016

Choose a reason for hiding this comment

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

Do you also need to include animationsEnabled ?

Copy link
Contributor

Choose a reason for hiding this comment

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

@ThomasBurleson - that was a hack by @IgorMinar to get the tests passing in G3

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean "not need"? animationsEnabled was added by Igor to fix a test in the g3 sync, but as I wrote in the commit message, this doesn't actually prevent the access to the document in all cases. If animations are enabled, $document[0] is still checked, and it might still be undefined


// this is a hard disable of all animations for the application or on
// the element itself, therefore there is no need to continue further
Expand Down Expand Up @@ -583,7 +586,7 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
* d) the element is not a child of the $rootElement
*/
function areAnimationsAllowed(element, parentElement, event) {
var bodyElement = jqLite($document[0].body);
var bodyElement = jqLite(rawDocument.body);
Copy link
Member

Choose a reason for hiding this comment

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

IIRC there is one more access to $document[0].body. Should we change that too ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this file?

Copy link
Member

Choose a reason for hiding this comment

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

var bodyElementDetected = isMatchingElement(element, bodyElement) || element[0].nodeName === 'HTML';
var rootElementDetected = isMatchingElement(element, $rootElement);
var parentAnimationDetected = false;
Expand Down
30 changes: 30 additions & 0 deletions test/ngAnimate/animateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,36 @@ describe("animations", function() {
});
});


it('should not run animations if the document is unavailable', function() {
var capturedAnimation;

module(function($provide) {
$provide.value('$document', {});

$provide.factory('$$animation', function($$AnimateRunner) {
return function(element, method, options) {
capturedAnimation = arguments;
return new $$AnimateRunner();
};
});
});

inject(function($animate, $rootScope, $rootElement, $document) {
$animate.enabled(true);

var spy = jasmine.createSpy();

element = jqLite('<div></div>');
var runner = $animate.enter(element, $rootElement);
$rootScope.$digest();

$animate.flush();

expect(capturedAnimation).toBeUndefined();
});
});

describe('[ng-animate-children]', function() {
var parent, element, child, capturedAnimation, captureLog;
beforeEach(module(function($provide) {
Expand Down