forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathngBindSpec.js
289 lines (243 loc) · 11.4 KB
/
ngBindSpec.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
'use strict';
describe('ngBind*', function() {
var element;
afterEach(function() {
dealoc(element);
});
describe('ngBind', function() {
it('should set text', inject(function($rootScope, $compile) {
element = $compile('<div ng-bind="a"></div>')($rootScope);
expect(element.text()).toEqual('');
$rootScope.a = 'misko';
$rootScope.$digest();
expect(element.hasClass('ng-binding')).toEqual(true);
expect(element.text()).toEqual('misko');
}));
it('should set text to blank if undefined', inject(function($rootScope, $compile) {
element = $compile('<div ng-bind="a"></div>')($rootScope);
$rootScope.a = 'misko';
$rootScope.$digest();
expect(element.text()).toEqual('misko');
$rootScope.a = undefined;
$rootScope.$digest();
expect(element.text()).toEqual('');
$rootScope.a = null;
$rootScope.$digest();
expect(element.text()).toEqual('');
}));
it('should suppress rendering of falsy values', inject(function($rootScope, $compile) {
element = $compile('<div><span ng-bind="null"></span>' +
'<span ng-bind="undefined"></span>' +
'<span ng-bind="\'\'"></span>-' +
'<span ng-bind="0"></span>' +
'<span ng-bind="false"></span>' +
'</div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('-0false');
}));
they('should jsonify $prop', [[{a: 1}, '{"a":1}'], [true, 'true'], [false, 'false']], function(prop) {
inject(function($rootScope, $compile) {
$rootScope.value = prop[0];
element = $compile('<div ng-bind="value"></div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual(prop[1]);
});
});
it('should use custom toString when present', inject(function($rootScope, $compile) {
$rootScope.value = {
toString: function() {
return 'foo';
}
};
element = $compile('<div ng-bind="value"></div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('foo');
}));
it('should NOT use toString on array objects', inject(function($rootScope, $compile) {
$rootScope.value = [];
element = $compile('<div ng-bind="value"></div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('[]');
}));
it('should NOT use toString on Date objects', inject(function($rootScope, $compile) {
$rootScope.value = new Date(2014, 10, 10, 0, 0, 0);
element = $compile('<div ng-bind="value"></div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe(JSON.stringify($rootScope.value));
expect(element.text()).not.toEqual($rootScope.value.toString());
}));
it('should one-time bind if the expression starts with two colons', inject(function($rootScope, $compile) {
element = $compile('<div ng-bind="::a"></div>')($rootScope);
$rootScope.a = 'lucas';
expect($rootScope.$$watchers.length).toEqual(1);
$rootScope.$digest();
expect(element.text()).toEqual('lucas');
expect($rootScope.$$watchers.length).toEqual(0);
$rootScope.a = undefined;
$rootScope.$digest();
expect(element.text()).toEqual('lucas');
}));
it('should be possible to bind to a new value within the same $digest', inject(function($rootScope, $compile) {
element = $compile('<div ng-bind="::a"></div>')($rootScope);
$rootScope.$watch('a', function(newVal) { if (newVal === 'foo') { $rootScope.a = 'bar'; } });
$rootScope.a = 'foo';
$rootScope.$digest();
expect(element.text()).toEqual('bar');
$rootScope.a = undefined;
$rootScope.$digest();
expect(element.text()).toEqual('bar');
}));
it('should remove the binding if the value is defined at the end of a $digest loop', inject(function($rootScope, $compile) {
element = $compile('<div ng-bind="::a"></div>')($rootScope);
$rootScope.$watch('a', function(newVal) { if (newVal === 'foo') { $rootScope.a = undefined; } });
$rootScope.a = 'foo';
$rootScope.$digest();
expect(element.text()).toEqual('');
$rootScope.a = 'bar';
$rootScope.$digest();
expect(element.text()).toEqual('bar');
$rootScope.a = 'man';
$rootScope.$digest();
expect(element.text()).toEqual('bar');
}));
});
describe('ngBindTemplate', function() {
it('should ngBindTemplate', inject(function($rootScope, $compile) {
element = $compile('<div ng-bind-template="Hello {{name}}!"></div>')($rootScope);
$rootScope.name = 'Misko';
$rootScope.$digest();
expect(element.hasClass('ng-binding')).toEqual(true);
expect(element.text()).toEqual('Hello Misko!');
}));
it('should one-time bind the expressions that start with ::', inject(function($rootScope, $compile) {
element = $compile('<div ng-bind-template="{{::hello}} {{::name}}!"></div>')($rootScope);
$rootScope.name = 'Misko';
expect($rootScope.$$watchers.length).toEqual(2);
$rootScope.$digest();
expect(element.hasClass('ng-binding')).toEqual(true);
expect(element.text()).toEqual(' Misko!');
expect($rootScope.$$watchers.length).toEqual(1);
$rootScope.hello = 'Hello';
$rootScope.name = 'Lucas';
$rootScope.$digest();
expect(element.text()).toEqual('Hello Misko!');
expect($rootScope.$$watchers.length).toEqual(0);
}));
it('should render object as JSON ignore $$', inject(function($rootScope, $compile) {
element = $compile('<pre>{{ {key:"value", $$key:"hide"} }}</pre>')($rootScope);
$rootScope.$digest();
expect(fromJson(element.text())).toEqual({key:'value'});
}));
});
describe('ngBindHtml', function() {
it('should complain about accidental use of interpolation', inject(function($compile) {
expect(function() {
$compile('<div ng-bind-html="{{myHtml}}"></div>');
}).toThrowMinErr('$parse', 'syntax',
'Syntax Error: Token \'{\' invalid key at column 2 of the expression [{{myHtml}}] starting at [{myHtml}}]');
}));
describe('SCE disabled', function() {
beforeEach(function() {
module(function($sceProvider) { $sceProvider.enabled(false); });
});
it('should set html', inject(function($rootScope, $compile) {
element = $compile('<div ng-bind-html="html"></div>')($rootScope);
$rootScope.html = '<div onclick="">hello</div>';
$rootScope.$digest();
expect(lowercase(element.html())).toEqual('<div onclick="">hello</div>');
}));
it('should update html', inject(function($rootScope, $compile, $sce) {
element = $compile('<div ng-bind-html="html"></div>')($rootScope);
$rootScope.html = 'hello';
$rootScope.$digest();
expect(lowercase(element.html())).toEqual('hello');
$rootScope.html = 'goodbye';
$rootScope.$digest();
expect(lowercase(element.html())).toEqual('goodbye');
}));
it('should one-time bind if the expression starts with two colons', inject(function($rootScope, $compile) {
element = $compile('<div ng-bind-html="::html"></div>')($rootScope);
$rootScope.html = '<div onclick="">hello</div>';
expect($rootScope.$$watchers.length).toEqual(1);
$rootScope.$digest();
expect(element.text()).toEqual('hello');
expect($rootScope.$$watchers.length).toEqual(0);
$rootScope.html = '<div onclick="">hello</div>';
$rootScope.$digest();
expect(element.text()).toEqual('hello');
}));
});
describe('SCE enabled', function() {
it('should NOT set html for untrusted values', inject(function($rootScope, $compile) {
element = $compile('<div ng-bind-html="html"></div>')($rootScope);
$rootScope.html = '<div onclick="">hello</div>';
expect(function() { $rootScope.$digest(); }).toThrow();
}));
it('should NOT set html for wrongly typed values', inject(function($rootScope, $compile, $sce) {
element = $compile('<div ng-bind-html="html"></div>')($rootScope);
$rootScope.html = $sce.trustAsCss('<div onclick="">hello</div>');
expect(function() { $rootScope.$digest(); }).toThrow();
}));
it('should set html for trusted values', inject(function($rootScope, $compile, $sce) {
element = $compile('<div ng-bind-html="html"></div>')($rootScope);
$rootScope.html = $sce.trustAsHtml('<div onclick="">hello</div>');
$rootScope.$digest();
expect(lowercase(element.html())).toEqual('<div onclick="">hello</div>');
}));
it('should update html', inject(function($rootScope, $compile, $sce) {
element = $compile('<div ng-bind-html="html"></div>')($rootScope);
$rootScope.html = $sce.trustAsHtml('hello');
$rootScope.$digest();
expect(lowercase(element.html())).toEqual('hello');
$rootScope.html = $sce.trustAsHtml('goodbye');
$rootScope.$digest();
expect(lowercase(element.html())).toEqual('goodbye');
}));
it('should not cause infinite recursion for trustAsHtml object watches',
inject(function($rootScope, $compile, $sce) {
// Ref: https://github.com/angular/angular.js/issues/3932
// If the binding is a function that creates a new value on every call via trustAs, we'll
// trigger an infinite digest if we don't take care of it.
element = $compile('<div ng-bind-html="getHtml()"></div>')($rootScope);
$rootScope.getHtml = function() {
return $sce.trustAsHtml('<div onclick="">hello</div>');
};
$rootScope.$digest();
expect(lowercase(element.html())).toEqual('<div onclick="">hello</div>');
}));
it('should handle custom $sce objects', function() {
function MySafeHtml(val) { this.val = val; }
module(function($provide) {
$provide.decorator('$sce', function($delegate) {
$delegate.trustAsHtml = function(html) { return new MySafeHtml(html); };
$delegate.getTrustedHtml = function(mySafeHtml) { return mySafeHtml.val; };
$delegate.valueOf = function(v) { return v instanceof MySafeHtml ? v.val : v; };
return $delegate;
});
});
inject(function($rootScope, $compile, $sce) {
// Ref: https://github.com/angular/angular.js/issues/14526
// Previous code used toString for change detection, which fails for custom objects
// that don't override toString.
element = $compile('<div ng-bind-html="getHtml()"></div>')($rootScope);
var html = 'hello';
$rootScope.getHtml = function() { return $sce.trustAsHtml(html); };
$rootScope.$digest();
expect(lowercase(element.html())).toEqual('hello');
html = 'goodbye';
$rootScope.$digest();
expect(lowercase(element.html())).toEqual('goodbye');
});
});
describe('when $sanitize is available', function() {
beforeEach(function() { module('ngSanitize'); });
it('should sanitize untrusted html', inject(function($rootScope, $compile) {
element = $compile('<div ng-bind-html="html"></div>')($rootScope);
$rootScope.html = '<div onclick="">hello</div>';
$rootScope.$digest();
expect(lowercase(element.html())).toEqual('<div>hello</div>');
}));
});
});
});
});