Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad21f8f

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 angular#14479 Closes angular#14496
1 parent 7f2df14 commit ad21f8f

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/ng/templateRequest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function $TemplateRequestProvider() {
3131
// are included in there. This also makes Angular accept any script
3232
// directive, no matter its name. However, we still need to unwrap trusted
3333
// types.
34-
if (!isString(tpl) || !$templateCache.get(tpl)) {
34+
if (!isString(tpl) || isUndefined($templateCache.get(tpl))) {
3535
tpl = $sce.getTrustedResourceUrl(tpl);
3636
}
3737

test/ng/templateRequestSpec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,39 @@ describe('$templateRequest', function() {
8181
}).not.toThrow();
8282
}));
8383

84+
it('should accept empty templates and refuse null or undefined templates in cache',
85+
inject(function($rootScope, $templateRequest, $templateCache, $sce) {
86+
87+
// Will throw on any template not in cache.
88+
spyOn($sce, 'getTrustedResourceUrl').and.returnValue(false);
89+
90+
expect(function() {
91+
$templateRequest('tpl.html'); // should go through $sce
92+
$rootScope.$digest();
93+
}).toThrow();
94+
95+
$templateCache.put('tpl.html'); // is a no-op, so $sce check as well.
96+
expect(function() {
97+
$templateRequest('tpl.html');
98+
$rootScope.$digest();
99+
}).toThrow();
100+
$templateCache.removeAll();
101+
102+
$templateCache.put('tpl.html', null); // makes no sense, but it's been added, so trust it.
103+
expect(function() {
104+
$templateRequest('tpl.html');
105+
$rootScope.$digest();
106+
}).not.toThrow();
107+
$templateCache.removeAll();
108+
109+
$templateCache.put('tpl.html', ''); // should work (empty template)
110+
expect(function() {
111+
$templateRequest('tpl.html');
112+
$rootScope.$digest();
113+
}).not.toThrow();
114+
$templateCache.removeAll();
115+
}));
116+
84117
it('should keep track of how many requests are going on',
85118
inject(function($rootScope, $templateRequest, $httpBackend) {
86119

0 commit comments

Comments
 (0)