This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
/
Copy pathbooleanAttrsSpec.js
342 lines (271 loc) · 12 KB
/
booleanAttrsSpec.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
'use strict';
describe('boolean attr directives', function() {
var element;
afterEach(function() {
dealoc(element);
});
it('should properly evaluate 0 as false', inject(function($rootScope, $compile) {
// jQuery does not treat 0 as false, when setting attr()
element = $compile('<button ng-disabled="isDisabled">Button</button>')($rootScope);
$rootScope.isDisabled = 0;
$rootScope.$digest();
expect(element.attr('disabled')).toBeFalsy();
$rootScope.isDisabled = 1;
$rootScope.$digest();
expect(element.attr('disabled')).toBeTruthy();
}));
it('should bind disabled', inject(function($rootScope, $compile) {
element = $compile('<button ng-disabled="isDisabled">Button</button>')($rootScope);
$rootScope.isDisabled = false;
$rootScope.$digest();
expect(element.attr('disabled')).toBeFalsy();
$rootScope.isDisabled = true;
$rootScope.$digest();
expect(element.attr('disabled')).toBeTruthy();
}));
it('should bind checked', inject(function($rootScope, $compile) {
element = $compile('<input type="checkbox" ng-checked="isChecked" />')($rootScope);
$rootScope.isChecked = false;
$rootScope.$digest();
expect(element.attr('checked')).toBeFalsy();
$rootScope.isChecked = true;
$rootScope.$digest();
expect(element.attr('checked')).toBeTruthy();
}));
it('should not bind checked when ngModel is present', inject(function($rootScope, $compile) {
// test for https://github.com/angular/angular.js/issues/10662
element = $compile('<input type="checkbox" ng-model="value" ng-false-value="\'false\'" ' +
'ng-true-value="\'true\'" ng-checked="value" />')($rootScope);
$rootScope.value = 'true';
$rootScope.$digest();
expect(element[0].checked).toBe(true);
browserTrigger(element, 'click');
expect(element[0].checked).toBe(false);
expect($rootScope.value).toBe('false');
browserTrigger(element, 'click');
expect(element[0].checked).toBe(true);
expect($rootScope.value).toBe('true');
}));
it('should bind selected', inject(function($rootScope, $compile) {
element = $compile('<select><option value=""></option><option ng-selected="isSelected">Greetings!</option></select>')($rootScope);
jqLite(window.document.body).append(element);
$rootScope.isSelected = false;
$rootScope.$digest();
expect(element.children()[1].selected).toBeFalsy();
$rootScope.isSelected = true;
$rootScope.$digest();
expect(element.children()[1].selected).toBeTruthy();
}));
it('should bind readonly', inject(function($rootScope, $compile) {
element = $compile('<input type="text" ng-readonly="isReadonly" />')($rootScope);
$rootScope.isReadonly = false;
$rootScope.$digest();
expect(element.attr('readOnly')).toBeFalsy();
$rootScope.isReadonly = true;
$rootScope.$digest();
expect(element.attr('readOnly')).toBeTruthy();
}));
it('should bind open', inject(function($rootScope, $compile) {
element = $compile('<details ng-open="isOpen"></details>')($rootScope);
$rootScope.isOpen = false;
$rootScope.$digest();
expect(element.attr('open')).toBeFalsy();
$rootScope.isOpen = true;
$rootScope.$digest();
expect(element.attr('open')).toBeTruthy();
}));
describe('multiple', function() {
it('should NOT bind to multiple via ngMultiple', inject(function($rootScope, $compile) {
element = $compile('<select ng-multiple="isMultiple"></select>')($rootScope);
$rootScope.isMultiple = false;
$rootScope.$digest();
expect(element.attr('multiple')).toBeFalsy();
$rootScope.isMultiple = 'multiple';
$rootScope.$digest();
expect(element.attr('multiple')).toBeFalsy(); // ignore
}));
it('should throw an exception if binding to multiple attribute', inject(function($rootScope, $compile) {
expect(function() {
$compile('<select multiple="{{isMultiple}}"></select>');
}).toThrowMinErr('$compile', 'selmulti', 'Binding to the \'multiple\' attribute is not supported. ' +
'Element: <select multiple="{{isMultiple}}">');
}));
});
});
describe('ngSrc', function() {
it('should interpolate the expression and bind to src with raw same-domain value',
inject(function($compile, $rootScope) {
var element = $compile('<div ng-src="{{id}}"></div>')($rootScope);
$rootScope.$digest();
expect(element.attr('src')).toBeUndefined();
$rootScope.$apply(function() {
$rootScope.id = '/somewhere/here';
});
expect(element.attr('src')).toEqual('/somewhere/here');
dealoc(element);
}));
it('should interpolate the expression and bind to src with a trusted value', inject(function($compile, $rootScope, $sce) {
var element = $compile('<div ng-src="{{id}}"></div>')($rootScope);
$rootScope.$digest();
expect(element.attr('src')).toBeUndefined();
$rootScope.$apply(function() {
$rootScope.id = $sce.trustAsResourceUrl('http://somewhere');
});
expect(element.attr('src')).toEqual('http://somewhere');
dealoc(element);
}));
it('should NOT interpolate a multi-part expression for non-img src attribute', inject(function($compile, $rootScope) {
expect(function() {
var element = $compile('<div ng-src="some/{{id}}"></div>')($rootScope);
dealoc(element);
}).toThrowMinErr(
'$interpolate', 'noconcat', 'Error while interpolating: some/{{id}}\nStrict ' +
'Contextual Escaping disallows interpolations that concatenate multiple expressions ' +
'when a trusted value is required. See http://docs.angularjs.org/api/ng.$sce');
}));
it('should interpolate a multi-part expression for regular attributes', inject(function($compile, $rootScope) {
var element = $compile('<div foo="some/{{id}}"></div>')($rootScope);
$rootScope.$digest();
expect(element.attr('foo')).toBe('some/');
$rootScope.$apply(function() {
$rootScope.id = 1;
});
expect(element.attr('foo')).toEqual('some/1');
}));
it('should NOT interpolate a wrongly typed expression', inject(function($compile, $rootScope, $sce) {
expect(function() {
var element = $compile('<div ng-src="{{id}}"></div>')($rootScope);
$rootScope.$apply(function() {
$rootScope.id = $sce.trustAsUrl('http://somewhere');
});
element.attr('src');
}).toThrowMinErr(
'$interpolate', 'interr', 'Can\'t interpolate: {{id}}\nError: [$sce:insecurl] Blocked ' +
'loading resource from url not allowed by $sceDelegate policy. URL: http://somewhere');
}));
// Support: IE 9-11 only
if (msie) {
it('should update the element property as well as the attribute', inject(
function($compile, $rootScope, $sce) {
// on IE, if "ng:src" directive declaration is used and "src" attribute doesn't exist
// then calling element.setAttribute('src', 'foo') doesn't do anything, so we need
// to set the property as well to achieve the desired effect
var element = $compile('<div ng-src="{{id}}"></div>')($rootScope);
$rootScope.$digest();
expect(element.prop('src')).toBeUndefined();
dealoc(element);
element = $compile('<div ng-src="some/"></div>')($rootScope);
$rootScope.$digest();
expect(element.prop('src')).toEqual('some/');
dealoc(element);
element = $compile('<div ng-src="{{id}}"></div>')($rootScope);
$rootScope.$apply(function() {
$rootScope.id = $sce.trustAsResourceUrl('http://somewhere');
});
expect(element.prop('src')).toEqual('http://somewhere');
dealoc(element);
}));
}
});
describe('ngSrcset', function() {
it('should interpolate the expression and bind to srcset', inject(function($compile, $rootScope) {
var element = $compile('<div ng-srcset="some/{{id}} 2x"></div>')($rootScope);
$rootScope.$digest();
expect(element.attr('srcset')).toBeUndefined();
$rootScope.$apply(function() {
$rootScope.id = 1;
});
expect(element.attr('srcset')).toEqual('some/1 2x');
dealoc(element);
}));
});
describe('ngHref', function() {
var element;
afterEach(function() {
dealoc(element);
});
it('should interpolate the expression and bind to href', inject(function($compile, $rootScope) {
element = $compile('<div 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);
}));
// Support: IE 9-11 only, Edge 12-14+
if (msie || /\bEdge\/[\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) {
element = $compile('<a ng-href="http://www.google.com/{{\'a%link\'}}">')($rootScope);
expect(function() {
$rootScope.$digest();
}).toThrow();
}));
}
if (isDefined(window.SVGElement)) {
describe('SVGAElement', function() {
it('should interpolate the expression and bind to xlink:href and 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/');
expect(child.attr('href')).toEqual('some/');
$rootScope.$apply(function() {
$rootScope.id = 1;
});
expect(child.attr('xlink:href')).toEqual('some/1');
expect(child.attr('href')).toEqual('some/1');
}));
it('should bind xlink:href and 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');
expect(child.attr('href')).toEqual('http://server');
}));
they('should set xlink:href and href to undefined when ng-href value is $prop', ['', 0, null, false, undefined],
function(value) {
var $compile, $rootScope;
inject(function(_$compile_, _$rootScope_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
});
$rootScope.url = value;
element = $compile('<svg><a ng-href="{{url}}"></a></svg>')($rootScope);
var child = element.children('a');
expect(child.attr('xlink:href')).toBeUndefined();
expect(child.attr('href')).toBeUndefined();
}
);
});
}
});