@@ -12527,109 +12527,111 @@ describe('$compile', function() {
12527
12527
});
12528
12528
});
12529
12529
12530
- describe('img[srcset] sanitization', function() {
12531
- it('should not error if srcset is undefined', inject(function($compile, $rootScope) {
12532
- element = $compile('<img ng-prop-srcset="testUrl"></img>')($rootScope);
12533
- // Set srcset to a value
12534
- $rootScope.testUrl = 'http://example.com/';
12535
- $rootScope.$digest();
12536
- expect(element.prop('srcset')).toBe('http://example.com/');
12530
+ ['img', 'source'].forEach(function(srcsetElement) {
12531
+ describe(srcsetElement + '[srcset] sanitization', function() {
12532
+ it('should not error if srcset is undefined', inject(function($compile, $rootScope) {
12533
+ element = $compile('<' + srcsetElement + ' ng-prop-srcset="testUrl"></' + srcsetElement + '>')($rootScope);
12534
+ // Set srcset to a value
12535
+ $rootScope.testUrl = 'http://example.com/';
12536
+ $rootScope.$digest();
12537
+ expect(element.prop('srcset')).toBe('http://example.com/');
12537
12538
12538
- // Now set it to undefined
12539
- $rootScope.testUrl = '';
12540
- $rootScope.$digest();
12541
- expect(element.prop('srcset')).toBe('');
12542
- }));
12539
+ // Now set it to undefined
12540
+ $rootScope.testUrl = '';
12541
+ $rootScope.$digest();
12542
+ expect(element.prop('srcset')).toBe('');
12543
+ }));
12543
12544
12544
- it('should NOT require trusted values for whitelisted values', inject(function($rootScope, $compile, $sce) {
12545
- element = $compile('<img ng-prop-srcset="testUrl"></img >')($rootScope);
12546
- $rootScope.testUrl = 'http://example.com/image.png'; // `http` is whitelisted
12547
- $rootScope.$digest();
12548
- expect(element.prop('srcset')).toEqual('http://example.com/image.png');
12549
- }));
12545
+ it('should NOT require trusted values for whitelisted values', inject(function($rootScope, $compile, $sce) {
12546
+ element = $compile('<' + srcsetElement + ' ng-prop-srcset="testUrl"></' + srcsetElement + ' >')($rootScope);
12547
+ $rootScope.testUrl = 'http://example.com/image.png'; // `http` is whitelisted
12548
+ $rootScope.$digest();
12549
+ expect(element.prop('srcset')).toEqual('http://example.com/image.png');
12550
+ }));
12550
12551
12551
- it('should accept trusted values, if they are also whitelisted', inject(function($rootScope, $compile, $sce) {
12552
- element = $compile('<img ng-prop-srcset="testUrl"></img >')($rootScope);
12553
- $rootScope.testUrl = $sce.trustAsUrl('http://example.com');
12554
- $rootScope.$digest();
12555
- expect(element.prop('srcset')).toEqual('http://example.com');
12556
- }));
12552
+ it('should accept trusted values, if they are also whitelisted', inject(function($rootScope, $compile, $sce) {
12553
+ element = $compile('<' + srcsetElement + ' ng-prop-srcset="testUrl"></' + srcsetElement + ' >')($rootScope);
12554
+ $rootScope.testUrl = $sce.trustAsUrl('http://example.com');
12555
+ $rootScope.$digest();
12556
+ expect(element.prop('srcset')).toEqual('http://example.com');
12557
+ }));
12557
12558
12558
- it('does not work with trusted values', inject(function($rootScope, $compile, $sce) {
12559
- // A limitation of the approach used for srcset is that you cannot use `trustAsUrl`.
12560
- // Use trustAsHtml and ng-bind-html to work around this.
12561
- element = $compile('<img ng-prop-srcset="testUrl"></img >')($rootScope);
12562
- $rootScope.testUrl = $sce.trustAsUrl('javascript:something');
12563
- $rootScope.$digest();
12564
- expect(element.prop('srcset')).toEqual('unsafe:javascript:something');
12559
+ it('does not work with trusted values', inject(function($rootScope, $compile, $sce) {
12560
+ // A limitation of the approach used for srcset is that you cannot use `trustAsUrl`.
12561
+ // Use trustAsHtml and ng-bind-html to work around this.
12562
+ element = $compile('<' + srcsetElement + ' ng-prop-srcset="testUrl"></' + srcsetElement + ' >')($rootScope);
12563
+ $rootScope.testUrl = $sce.trustAsUrl('javascript:something');
12564
+ $rootScope.$digest();
12565
+ expect(element.prop('srcset')).toEqual('unsafe:javascript:something');
12565
12566
12566
- element = $compile('<img ng-prop-srcset="testUrl + \',\' + testUrl"></img >')($rootScope);
12567
- $rootScope.testUrl = $sce.trustAsUrl('javascript:something');
12568
- $rootScope.$digest();
12569
- expect(element.prop('srcset')).toEqual(
12570
- 'unsafe:javascript:something ,unsafe:javascript:something');
12571
- }));
12567
+ element = $compile('<' + srcsetElement + ' ng-prop-srcset="testUrl + \',\' + testUrl"></' + srcsetElement + ' >')($rootScope);
12568
+ $rootScope.testUrl = $sce.trustAsUrl('javascript:something');
12569
+ $rootScope.$digest();
12570
+ expect(element.prop('srcset')).toEqual(
12571
+ 'unsafe:javascript:something ,unsafe:javascript:something');
12572
+ }));
12572
12573
12573
- it('should use $$sanitizeUri', function() {
12574
- var $$sanitizeUri = jasmine.createSpy('$$sanitizeUri').and.returnValue('someSanitizedUrl');
12575
- module(function($provide) {
12576
- $provide.value('$$sanitizeUri', $$sanitizeUri);
12577
- });
12578
- inject(function($compile, $rootScope) {
12579
- element = $compile('<img ng-prop-srcset="testUrl"></img >')($rootScope);
12580
- $rootScope.testUrl = 'someUrl';
12581
- $rootScope.$apply();
12582
- expect(element.prop('srcset')).toBe('someSanitizedUrl');
12583
- expect($$sanitizeUri).toHaveBeenCalledWith($rootScope.testUrl, true);
12574
+ it('should use $$sanitizeUri', function() {
12575
+ var $$sanitizeUri = jasmine.createSpy('$$sanitizeUri').and.returnValue('someSanitizedUrl');
12576
+ module(function($provide) {
12577
+ $provide.value('$$sanitizeUri', $$sanitizeUri);
12578
+ });
12579
+ inject(function($compile, $rootScope) {
12580
+ element = $compile('<' + srcsetElement + ' ng-prop-srcset="testUrl"></' + srcsetElement + ' >')($rootScope);
12581
+ $rootScope.testUrl = 'someUrl';
12582
+ $rootScope.$apply();
12583
+ expect(element.prop('srcset')).toBe('someSanitizedUrl');
12584
+ expect($$sanitizeUri).toHaveBeenCalledWith($rootScope.testUrl, true);
12584
12585
12585
- element = $compile('<img ng-prop-srcset="testUrl + \',\' + testUrl"></img >')($rootScope);
12586
- $rootScope.testUrl = 'javascript:yay';
12587
- $rootScope.$apply();
12588
- expect(element.prop('srcset')).toEqual('someSanitizedUrl ,someSanitizedUrl');
12586
+ element = $compile('<' + srcsetElement + ' ng-prop-srcset="testUrl + \',\' + testUrl"></' + srcsetElement + ' >')($rootScope);
12587
+ $rootScope.testUrl = 'javascript:yay';
12588
+ $rootScope.$apply();
12589
+ expect(element.prop('srcset')).toEqual('someSanitizedUrl ,someSanitizedUrl');
12589
12590
12590
- element = $compile('<img ng-prop-srcset="\'java\' + testUrl"></img >')($rootScope);
12591
- $rootScope.testUrl = 'script:yay, javascript:nay';
12592
- $rootScope.$apply();
12593
- expect(element.prop('srcset')).toEqual('someSanitizedUrl ,someSanitizedUrl');
12594
- });
12595
- });
12596
-
12597
- it('should sanitize all uris in srcset', inject(function($rootScope, $compile) {
12598
- element = $compile('<img ng-prop-srcset="testUrl"></img >')($rootScope);
12599
- var testSet = {
12600
- 'http://example.com/image.png':'http://example.com/image.png',
12601
- ' http://example.com/image.png':'http://example.com/image.png',
12602
- 'http://example.com/image.png ':'http://example.com/image.png',
12603
- 'http://example.com/image.png 128w':'http://example.com/image.png 128w',
12604
- 'http://example.com/image.png 2x':'http://example.com/image.png 2x',
12605
- 'http://example.com/image.png 1.5x':'http://example.com/image.png 1.5x',
12606
- 'http://example.com/image1.png 1x,http://example.com/image2.png 2x':'http://example.com/image1.png 1x,http://example.com/image2.png 2x',
12607
- 'http://example.com/image1.png 1x ,http://example.com/image2.png 2x':'http://example.com/image1.png 1x ,http://example.com/image2.png 2x',
12608
- 'http://example.com/image1.png 1x, http://example.com/image2.png 2x':'http://example.com/image1.png 1x,http://example.com/image2.png 2x',
12609
- 'http://example.com/image1.png 1x , http://example.com/image2.png 2x':'http://example.com/image1.png 1x ,http://example.com/image2.png 2x',
12610
- 'http://example.com/image1.png 48w,http://example.com/image2.png 64w':'http://example.com/image1.png 48w,http://example.com/image2.png 64w',
12611
- //Test regex to make sure doesn't mistake parts of url for width descriptors
12612
- 'http://example.com/image1.png?w=48w,http://example.com/image2.png 64w':'http://example.com/image1.png?w=48w,http://example.com/image2.png 64w',
12613
- 'http://example.com/image1.png 1x,http://example.com/image2.png 64w':'http://example.com/image1.png 1x,http://example.com/image2.png 64w',
12614
- 'http://example.com/image1.png,http://example.com/image2.png':'http://example.com/image1.png ,http://example.com/image2.png',
12615
- 'http://example.com/image1.png ,http://example.com/image2.png':'http://example.com/image1.png ,http://example.com/image2.png',
12616
- 'http://example.com/image1.png, http://example.com/image2.png':'http://example.com/image1.png ,http://example.com/image2.png',
12617
- 'http://example.com/image1.png , http://example.com/image2.png':'http://example.com/image1.png ,http://example.com/image2.png',
12618
- 'http://example.com/image1.png 1x, http://example.com/image2.png 2x, http://example.com/image3.png 3x':
12619
- 'http://example.com/image1.png 1x,http://example.com/image2.png 2x,http://example.com/image3.png 3x',
12620
- 'javascript:doEvilStuff() 2x': 'unsafe:javascript:doEvilStuff() 2x',
12621
- 'http://example.com/image1.png 1x,javascript:doEvilStuff() 2x':'http://example.com/image1.png 1x,unsafe:javascript:doEvilStuff() 2x',
12622
- 'http://example.com/image1.jpg?x=a,b 1x,http://example.com/ima,ge2.jpg 2x':'http://example.com/image1.jpg?x=a,b 1x,http://example.com/ima,ge2.jpg 2x',
12623
- //Test regex to make sure doesn't mistake parts of url for pixel density descriptors
12624
- 'http://example.com/image1.jpg?x=a2x,b 1x,http://example.com/ima,ge2.jpg 2x':'http://example.com/image1.jpg?x=a2x,b 1x,http://example.com/ima,ge2.jpg 2x'
12625
- };
12591
+ element = $compile('<' + srcsetElement + ' ng-prop-srcset="\'java\' + testUrl"></' + srcsetElement + ' >')($rootScope);
12592
+ $rootScope.testUrl = 'script:yay, javascript:nay';
12593
+ $rootScope.$apply();
12594
+ expect(element.prop('srcset')).toEqual('someSanitizedUrl ,someSanitizedUrl');
12595
+ });
12596
+ });
12597
+
12598
+ it('should sanitize all uris in srcset', inject(function($rootScope, $compile) {
12599
+ element = $compile('<' + srcsetElement + ' ng-prop-srcset="testUrl"></' + srcsetElement + ' >')($rootScope);
12600
+ var testSet = {
12601
+ 'http://example.com/image.png':'http://example.com/image.png',
12602
+ ' http://example.com/image.png':'http://example.com/image.png',
12603
+ 'http://example.com/image.png ':'http://example.com/image.png',
12604
+ 'http://example.com/image.png 128w':'http://example.com/image.png 128w',
12605
+ 'http://example.com/image.png 2x':'http://example.com/image.png 2x',
12606
+ 'http://example.com/image.png 1.5x':'http://example.com/image.png 1.5x',
12607
+ 'http://example.com/image1.png 1x,http://example.com/image2.png 2x':'http://example.com/image1.png 1x,http://example.com/image2.png 2x',
12608
+ 'http://example.com/image1.png 1x ,http://example.com/image2.png 2x':'http://example.com/image1.png 1x ,http://example.com/image2.png 2x',
12609
+ 'http://example.com/image1.png 1x, http://example.com/image2.png 2x':'http://example.com/image1.png 1x,http://example.com/image2.png 2x',
12610
+ 'http://example.com/image1.png 1x , http://example.com/image2.png 2x':'http://example.com/image1.png 1x ,http://example.com/image2.png 2x',
12611
+ 'http://example.com/image1.png 48w,http://example.com/image2.png 64w':'http://example.com/image1.png 48w,http://example.com/image2.png 64w',
12612
+ //Test regex to make sure doesn't mistake parts of url for width descriptors
12613
+ 'http://example.com/image1.png?w=48w,http://example.com/image2.png 64w':'http://example.com/image1.png?w=48w,http://example.com/image2.png 64w',
12614
+ 'http://example.com/image1.png 1x,http://example.com/image2.png 64w':'http://example.com/image1.png 1x,http://example.com/image2.png 64w',
12615
+ 'http://example.com/image1.png,http://example.com/image2.png':'http://example.com/image1.png ,http://example.com/image2.png',
12616
+ 'http://example.com/image1.png ,http://example.com/image2.png':'http://example.com/image1.png ,http://example.com/image2.png',
12617
+ 'http://example.com/image1.png, http://example.com/image2.png':'http://example.com/image1.png ,http://example.com/image2.png',
12618
+ 'http://example.com/image1.png , http://example.com/image2.png':'http://example.com/image1.png ,http://example.com/image2.png',
12619
+ 'http://example.com/image1.png 1x, http://example.com/image2.png 2x, http://example.com/image3.png 3x':
12620
+ 'http://example.com/image1.png 1x,http://example.com/image2.png 2x,http://example.com/image3.png 3x',
12621
+ 'javascript:doEvilStuff() 2x': 'unsafe:javascript:doEvilStuff() 2x',
12622
+ 'http://example.com/image1.png 1x,javascript:doEvilStuff() 2x':'http://example.com/image1.png 1x,unsafe:javascript:doEvilStuff() 2x',
12623
+ 'http://example.com/image1.jpg?x=a,b 1x,http://example.com/ima,ge2.jpg 2x':'http://example.com/image1.jpg?x=a,b 1x,http://example.com/ima,ge2.jpg 2x',
12624
+ //Test regex to make sure doesn't mistake parts of url for pixel density descriptors
12625
+ 'http://example.com/image1.jpg?x=a2x,b 1x,http://example.com/ima,ge2.jpg 2x':'http://example.com/image1.jpg?x=a2x,b 1x,http://example.com/ima,ge2.jpg 2x'
12626
+ };
12626
12627
12627
- forEach(testSet, function(ref, url) {
12628
- $rootScope.testUrl = url;
12629
- $rootScope.$digest();
12630
- expect(element.prop('srcset')).toEqual(ref);
12631
- });
12632
- }));
12628
+ forEach(testSet, function(ref, url) {
12629
+ $rootScope.testUrl = url;
12630
+ $rootScope.$digest();
12631
+ expect(element.prop('srcset')).toEqual(ref);
12632
+ });
12633
+ }));
12634
+ });
12633
12635
});
12634
12636
12635
12637
describe('a[href] sanitization', function() {
0 commit comments