Skip to content

Commit cfacaad

Browse files
use $templateRequest from AngularJS
- modified `$templateFactory` to use `$templateRequest` provider of AngularJs in case it is avalable - removed the test case which enforces the setting of request header `Accept: text/html` (cherry picked from commit 9a1af98)
1 parent af95206 commit cfacaad

File tree

4 files changed

+81
-27
lines changed

4 files changed

+81
-27
lines changed

src/services.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,9 @@ function $uiRouter($locationProvider: ILocationProvider) {
7171
router['$get'] = $get;
7272
$get.$inject = ['$location', '$browser', '$sniffer', '$rootScope', '$http', '$templateCache'];
7373
function $get($location: ILocationService, $browser: any, $sniffer: any, $rootScope: ng.IScope, $http: IHttpService, $templateCache: ITemplateCacheService) {
74+
ng1LocationService._runtimeServices($rootScope, $location, $sniffer, $browser);
7475
delete router['router'];
7576
delete router['$get'];
76-
77-
services.template.get = (url: string) =>
78-
$http.get(url, { cache: $templateCache, headers: { Accept: 'text/html' }}).then(prop("data")) as any;
79-
80-
ng1LocationService._runtimeServices($rootScope, $location, $sniffer, $browser);
81-
8277
return router;
8378
}
8479
return router;

src/templateFactory.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,23 @@ import { ng as angular } from "./angular";
44
import { IAugmentedJQuery } from "angular";
55
import {
66
isArray, isDefined, isFunction, isObject, services, Obj, IInjectable, tail, kebobString, unnestR, ResolveContext,
7-
Resolvable, RawParams, identity
7+
Resolvable, RawParams, prop
88
} from "ui-router-core";
99
import { Ng1ViewDeclaration } from "./interface";
1010

11+
const service = (token) => {
12+
const $injector = services.$injector;
13+
return $injector.has ? ($injector.has(token) && $injector.get(token)) : $injector.get(token);
14+
};
15+
1116
/**
1217
* Service which manages loading of templates from a ViewConfig.
1318
*/
1419
export class TemplateFactory {
20+
private $templateRequest = service('$templateRequest');
21+
private $templateCache = service('$templateCache');
22+
private $http = service('$http');
23+
1524
/**
1625
* Creates a template from a configuration object.
1726
*
@@ -66,7 +75,13 @@ export class TemplateFactory {
6675
fromUrl(url: (string|Function), params: any) {
6776
if (isFunction(url)) url = (<any> url)(params);
6877
if (url == null) return null;
69-
return services.template.get(<string> url);
78+
79+
if(this.$templateRequest) {
80+
return this.$templateRequest(url);
81+
}
82+
83+
return this.$http.get(url, { cache: this.$templateCache, headers: { Accept: 'text/html' }})
84+
.then(function(response) { return response.data; });
7085
};
7186

7287
/**

test/templateFactorySpec.js

-19
This file was deleted.

test/templateFactorySpec.ts

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import * as angular from "angular";
2+
3+
declare let inject;
4+
5+
let module = angular['mock'].module;
6+
7+
describe('templateFactory', function () {
8+
9+
beforeEach(module('ui.router'));
10+
11+
it('exists', inject(function ($templateFactory) {
12+
expect($templateFactory).toBeDefined();
13+
}));
14+
15+
if (angular.version.major >= 1 && angular.version.minor >= 3) {
16+
// Post 1.2, there is a $templateRequest and a $sce service
17+
describe('should follow $sce policy and', function() {
18+
it('accepts relative URLs', inject(function($templateFactory, $httpBackend, $sce) {
19+
$httpBackend.expectGET('views/view.html').respond(200, 'template!');
20+
$templateFactory.fromUrl('views/view.html');
21+
$httpBackend.flush();
22+
}));
23+
24+
it('rejects untrusted URLs',
25+
inject(function($templateFactory, $httpBackend, $sce) {
26+
let error = 'No error thrown';
27+
try {
28+
$templateFactory.fromUrl('http://evil.com/views/view.html');
29+
} catch (e) {
30+
error = e.message;
31+
}
32+
expect(error).toMatch(/sce:insecurl/);
33+
}));
34+
35+
it('accepts explicitly trusted URLs',
36+
inject(function($templateFactory, $httpBackend, $sce) {
37+
$httpBackend.expectGET('http://evil.com/views/view.html').respond(200, 'template!');
38+
$templateFactory.fromUrl(
39+
$sce.trustAsResourceUrl('http://evil.com/views/view.html'));
40+
$httpBackend.flush();
41+
}));
42+
});
43+
} else { // 1.2 and before will use directly $http
44+
it('does not restrict URL loading', inject(function($templateFactory, $httpBackend) {
45+
$httpBackend.expectGET('http://evil.com/views/view.html').respond(200, 'template!');
46+
$templateFactory.fromUrl('http://evil.com/views/view.html');
47+
$httpBackend.flush();
48+
49+
$httpBackend.expectGET('data:text/html,foo').respond(200, 'template!');
50+
$templateFactory.fromUrl('data:text/html,foo');
51+
$httpBackend.flush();
52+
}));
53+
54+
// Behavior not kept in >1.2 with $templateRequest
55+
it('should request templates as text/html', inject(function($templateFactory, $httpBackend) {
56+
$httpBackend.expectGET('views/view.html', function(headers) {
57+
return headers.Accept === 'text/html';
58+
}).respond(200);
59+
$templateFactory.fromUrl('views/view.html');
60+
$httpBackend.flush();
61+
}));
62+
}
63+
});

0 commit comments

Comments
 (0)