diff --git a/src/ng/templateRequest.js b/src/ng/templateRequest.js index 3b34c146c147..f5bce1b83349 100644 --- a/src/ng/templateRequest.js +++ b/src/ng/templateRequest.js @@ -68,7 +68,7 @@ function $TemplateRequestProvider() { // are included in there. This also makes Angular accept any script // directive, no matter its name. However, we still need to unwrap trusted // types. - if (!isString(tpl) || !$templateCache.get(tpl)) { + if (!isString(tpl) || isUndefined($templateCache.get(tpl))) { tpl = $sce.getTrustedResourceUrl(tpl); } diff --git a/test/ng/templateRequestSpec.js b/test/ng/templateRequestSpec.js index fd84027c4ca9..dc78e21f3bc8 100644 --- a/test/ng/templateRequestSpec.js +++ b/test/ng/templateRequestSpec.js @@ -158,6 +158,39 @@ describe('$templateRequest', function() { }).not.toThrow(); })); + it('should accept empty templates and refuse null or undefined templates in cache', + inject(function($rootScope, $templateRequest, $templateCache, $sce) { + + // Will throw on any template not in cache. + spyOn($sce, 'getTrustedResourceUrl').and.returnValue(false); + + expect(function() { + $templateRequest('tpl.html'); // should go through $sce + $rootScope.$digest(); + }).toThrow(); + + $templateCache.put('tpl.html'); // is a no-op, so $sce check as well. + expect(function() { + $templateRequest('tpl.html'); + $rootScope.$digest(); + }).toThrow(); + $templateCache.removeAll(); + + $templateCache.put('tpl.html', null); // makes no sense, but it's been added, so trust it. + expect(function() { + $templateRequest('tpl.html'); + $rootScope.$digest(); + }).not.toThrow(); + $templateCache.removeAll(); + + $templateCache.put('tpl.html', ''); // should work (empty template) + expect(function() { + $templateRequest('tpl.html'); + $rootScope.$digest(); + }).not.toThrow(); + $templateCache.removeAll(); + })); + it('should keep track of how many requests are going on', inject(function($rootScope, $templateRequest, $httpBackend) {