forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngSrcsetSpec.js
51 lines (40 loc) · 1.77 KB
/
ngSrcsetSpec.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
'use strict';
/* eslint-disable no-script-url */
describe('ngSrcset', function() {
var element;
afterEach(function() {
dealoc(element);
});
it('should not result empty string in img srcset', inject(function($rootScope, $compile) {
$rootScope.image = {};
element = $compile('<img ng-srcset="{{image.url}} 2x">')($rootScope);
$rootScope.$digest();
expect(element.attr('srcset')).toBeUndefined();
}));
it('should sanitize good urls', inject(function($rootScope, $compile) {
$rootScope.imageUrl = 'http://example.com/image1.png 1x, http://example.com/image2.png 2x';
element = $compile('<img ng-srcset="{{imageUrl}}">')($rootScope);
$rootScope.$digest();
expect(element.attr('srcset')).toBe('http://example.com/image1.png 1x,http://example.com/image2.png 2x');
}));
it('should sanitize evil url', inject(function($rootScope, $compile) {
$rootScope.imageUrl = 'http://example.com/image1.png 1x, javascript:doEvilStuff() 2x';
element = $compile('<img ng-srcset="{{imageUrl}}">')($rootScope);
$rootScope.$digest();
expect(element.attr('srcset')).toBe('http://example.com/image1.png 1x,unsafe:javascript:doEvilStuff() 2x');
}));
it('should not throw an error if undefined', inject(function($rootScope, $compile) {
element = $compile('<img ng-attr-srcset="{{undefined}}">')($rootScope);
$rootScope.$digest();
}));
it('should interpolate the expression and bind to srcset', inject(function($compile, $rootScope) {
var element = $compile('<img 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);
}));
});