Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(compile): do not create directives for constant media URL attributes #16737

Merged
merged 3 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/ng/directive/attrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ forEach(ALIASED_ATTR, function(htmlAttr, ngAttr) {
// ng-src, ng-srcset, ng-href are interpolated
forEach(['src', 'srcset', 'href'], function(attrName) {
var normalized = directiveNormalize('ng-' + attrName);
ngAttributeAliasDirectives[normalized] = function() {
ngAttributeAliasDirectives[normalized] = ['$sce', function($sce) {
return {
priority: 99, // it needs to run after the attributes are interpolated
link: function(scope, element, attr) {
Expand All @@ -422,6 +422,10 @@ forEach(['src', 'srcset', 'href'], function(attrName) {
propName = null;
}

// We need to sanitize the url at least once, in case it is a constant
// non-interpolated attribute.
attr.$set(normalized, $sce.getTrustedMediaUrl(attr[normalized]));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could check the current value and only call $set() if getTrustedMediaUrl() return a different value.


attr.$observe(normalized, function(value) {
if (!value) {
if (attrName === 'href') {
Expand All @@ -441,5 +445,5 @@ forEach(['src', 'srcset', 'href'], function(attrName) {
});
}
};
};
}];
});
2 changes: 1 addition & 1 deletion src/ng/interpolate.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ function $InterpolateProvider() {

// Provide a quick exit and simplified result function for text with no interpolation
if (!text.length || text.indexOf(startSymbol) === -1) {
if (mustHaveExpression && !contextAllowsConcatenation) return;
if (mustHaveExpression) return;

var unescapedText = unescapeText(text);
if (contextAllowsConcatenation) {
Expand Down
52 changes: 52 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3577,6 +3577,15 @@ describe('$compile', function() {
})
);

it('should support non-interpolated `src` and `data-src` on the same element',
inject(function($rootScope, $compile) {
var element = $compile('<img src="abc" data-src="123">')($rootScope);
expect(element.attr('src')).toEqual('abc');
expect(element.attr('data-src')).toEqual('123');
$rootScope.$digest();
expect(element.attr('src')).toEqual('abc');
expect(element.attr('data-src')).toEqual('123');
}));

it('should call observer only when the attribute value changes', function() {
module(function() {
Expand Down Expand Up @@ -12084,6 +12093,49 @@ describe('$compile', function() {
expect(element.attr('test6')).toBe('Misko');
}));

describe('with media url attributes', function() {
it('should work with interpolated ng-attr-src', inject(function() {
$rootScope.name = 'some-image.png';
element = $compile('<img ng-attr-src="{{name}}">')($rootScope);
expect(element.attr('src')).toBeUndefined();

$rootScope.$digest();
expect(element.attr('src')).toBe('some-image.png');

$rootScope.name = 'other-image.png';
$rootScope.$digest();
expect(element.attr('src')).toBe('other-image.png');
}));

it('should work with interpolated ng-attr-data-src', inject(function() {
$rootScope.name = 'some-image.png';
element = $compile('<img ng-attr-data-src="{{name}}">')($rootScope);
expect(element.attr('data-src')).toBeUndefined();

$rootScope.$digest();
expect(element.attr('data-src')).toBe('some-image.png');

$rootScope.name = 'other-image.png';
$rootScope.$digest();
expect(element.attr('data-src')).toBe('other-image.png');
}));

it('should work alongside constant [src]-attribute and [ng-attr-data-src] attributes', inject(function() {
$rootScope.name = 'some-image.png';
element = $compile('<img src="constant.png" ng-attr-data-src="{{name}}">')($rootScope);
expect(element.attr('data-src')).toBeUndefined();

$rootScope.$digest();
expect(element.attr('src')).toBe('constant.png');
expect(element.attr('data-src')).toBe('some-image.png');

$rootScope.name = 'other-image.png';
$rootScope.$digest();
expect(element.attr('src')).toBe('constant.png');
expect(element.attr('data-src')).toBe('other-image.png');
}));
});

describe('when an attribute has a dash-separated name', function() {
it('should work with different prefixes', inject(function() {
$rootScope.name = 'JamieMason';
Expand Down
14 changes: 14 additions & 0 deletions test/ng/directive/ngSrcSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ describe('ngSrc', function() {
expect(element.prop('src')).toEqual('http://somewhere/abc');
}));
}

it('should work with `src` attribute on the same element', inject(function($rootScope, $compile) {
$rootScope.imageUrl = 'dynamic';
element = $compile('<img ng-src="{{imageUrl}}" src="static">')($rootScope);
expect(element.attr('src')).toBe('static');
$rootScope.$digest();
expect(element.attr('src')).toBe('dynamic');
dealoc(element);

element = $compile('<img src="static" ng-src="{{imageUrl}}">')($rootScope);
expect(element.attr('src')).toBe('static');
$rootScope.$digest();
expect(element.attr('src')).toBe('dynamic');
}));
});

describe('iframe[ng-src]', function() {
Expand Down