Repository navigation
Expand file tree
/
Copy pathngHrefSpec.js
More file actions
141 lines (110 loc) · 4.73 KB
/
Copy pathngHrefSpec.js
File metadata and controls
141 lines (110 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use strict';
describe('ngHref', function() {
var element;
afterEach(function() {
dealoc(element);
});
it('should interpolate the expression and bind to href', inject(function($compile, $rootScope) {
element = $compile('<a ng-href="some/{{id}}"></div>')($rootScope);
$rootScope.$digest();
expect(element.attr('href')).toEqual('some/');
$rootScope.$apply(function() {
$rootScope.id = 1;
});
expect(element.attr('href')).toEqual('some/1');
}));
it('should bind href and merge with other attrs', inject(function($rootScope, $compile) {
element = $compile('<a ng-href="{{url}}" rel="{{rel}}"></a>')($rootScope);
$rootScope.url = 'http://server';
$rootScope.rel = 'REL';
$rootScope.$digest();
expect(element.attr('href')).toEqual('http://server');
expect(element.attr('rel')).toEqual('REL');
}));
it('should bind href even if no interpolation', inject(function($rootScope, $compile) {
element = $compile('<a ng-href="http://server"></a>')($rootScope);
$rootScope.$digest();
expect(element.attr('href')).toEqual('http://server');
}));
it('should not set the href if ng-href is empty', inject(function($rootScope, $compile) {
$rootScope.url = null;
element = $compile('<a ng-href="{{url}}">')($rootScope);
$rootScope.$digest();
expect(element.attr('href')).toEqual(undefined);
}));
it('should remove the href if ng-href changes to empty', inject(function($rootScope, $compile) {
$rootScope.url = 'http://www.google.com/';
element = $compile('<a ng-href="{{url}}">')($rootScope);
$rootScope.$digest();
$rootScope.url = null;
$rootScope.$digest();
expect(element.attr('href')).toEqual(undefined);
}));
it('should sanitize interpolated url', inject(function($rootScope, $compile) {
/* eslint no-script-url: "off" */
$rootScope.imageUrl = 'javascript:alert(1);';
element = $compile('<a ng-href="{{imageUrl}}">')($rootScope);
$rootScope.$digest();
expect(element.attr('href')).toBe('unsafe:javascript:alert(1);');
}));
it('should sanitize non-interpolated url', inject(function($rootScope, $compile) {
element = $compile('<a ng-href="javascript:alert(1);">')($rootScope);
$rootScope.$digest();
expect(element.attr('href')).toBe('unsafe:javascript:alert(1);');
}));
// Support: IE 9-11 only, Edge 12-17
if (msie || /\bEdge\/1[2-7]\.[\d.]+\b/.test(window.navigator.userAgent)) {
// IE/Edge fail when setting a href to a URL containing a % that isn't a valid escape sequence
// See https://github.com/angular/angular.js/issues/13388
it('should throw error if ng-href contains a non-escaped percent symbol', inject(function($rootScope, $compile) {
expect(function() {
element = $compile('<a ng-href="http://www.google.com/{{\'a%link\'}}">')($rootScope);
}).toThrow();
}));
}
it('should bind numbers', inject(function($rootScope, $compile) {
element = $compile('<a ng-href="{{1234}}"></a>')($rootScope);
$rootScope.$digest();
expect(element.attr('href')).toEqual('1234');
}));
it('should bind and sanitize the result of a (custom) toString() function', inject(function($rootScope, $compile) {
$rootScope.value = {};
element = $compile('<a ng-href="{{value}}"></a>')($rootScope);
$rootScope.$digest();
expect(element.attr('href')).toEqual('[object Object]');
function SafeClass() {}
SafeClass.prototype.toString = function() {
return 'custom value';
};
$rootScope.value = new SafeClass();
$rootScope.$digest();
expect(element.attr('href')).toEqual('custom value');
function UnsafeClass() {}
UnsafeClass.prototype.toString = function() {
return 'javascript:alert(1);';
};
$rootScope.value = new UnsafeClass();
$rootScope.$digest();
expect(element.attr('href')).toEqual('unsafe:javascript:alert(1);');
}));
if (isDefined(window.SVGElement)) {
describe('SVGAElement', function() {
it('should interpolate the expression and bind to xlink:href', inject(function($compile, $rootScope) {
element = $compile('<svg><a ng-href="some/{{id}}"></a></svg>')($rootScope);
var child = element.children('a');
$rootScope.$digest();
expect(child.attr('xlink:href')).toEqual('some/');
$rootScope.$apply(function() {
$rootScope.id = 1;
});
expect(child.attr('xlink:href')).toEqual('some/1');
}));
it('should bind xlink:href even if no interpolation', inject(function($rootScope, $compile) {
element = $compile('<svg><a ng-href="http://server"></a></svg>')($rootScope);
var child = element.children('a');
$rootScope.$digest();
expect(child.attr('xlink:href')).toEqual('http://server');
}));
});
}
});