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

Commit 8576baf

Browse files
rjametgkalpak
authored andcommitted
fix($templateRequest): trust empty templates in $templateCache as well
Implicitly trust empty templates added to `$templateCache` as is the case for all other templates. Fixes #14479 Closes #14496
1 parent 8d1ee0c commit 8576baf

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/ng/templateRequest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function $TemplateRequestProvider() {
6868
// are included in there. This also makes Angular accept any script
6969
// directive, no matter its name. However, we still need to unwrap trusted
7070
// types.
71-
if (!isString(tpl) || !$templateCache.get(tpl)) {
71+
if (!isString(tpl) || isUndefined($templateCache.get(tpl))) {
7272
tpl = $sce.getTrustedResourceUrl(tpl);
7373
}
7474

test/ng/templateRequestSpec.js

+33
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,39 @@ describe('$templateRequest', function() {
158158
}).not.toThrow();
159159
}));
160160

161+
it('should accept empty templates and refuse null or undefined templates in cache',
162+
inject(function($rootScope, $templateRequest, $templateCache, $sce) {
163+
164+
// Will throw on any template not in cache.
165+
spyOn($sce, 'getTrustedResourceUrl').and.returnValue(false);
166+
167+
expect(function() {
168+
$templateRequest('tpl.html'); // should go through $sce
169+
$rootScope.$digest();
170+
}).toThrow();
171+
172+
$templateCache.put('tpl.html'); // is a no-op, so $sce check as well.
173+
expect(function() {
174+
$templateRequest('tpl.html');
175+
$rootScope.$digest();
176+
}).toThrow();
177+
$templateCache.removeAll();
178+
179+
$templateCache.put('tpl.html', null); // makes no sense, but it's been added, so trust it.
180+
expect(function() {
181+
$templateRequest('tpl.html');
182+
$rootScope.$digest();
183+
}).not.toThrow();
184+
$templateCache.removeAll();
185+
186+
$templateCache.put('tpl.html', ''); // should work (empty template)
187+
expect(function() {
188+
$templateRequest('tpl.html');
189+
$rootScope.$digest();
190+
}).not.toThrow();
191+
$templateCache.removeAll();
192+
}));
193+
161194
it('should keep track of how many requests are going on',
162195
inject(function($rootScope, $templateRequest, $httpBackend) {
163196

0 commit comments

Comments
 (0)