Skip to content

Commit 3b1b9c7

Browse files
Merge pull request #122 from angular/master
Text field style merge
2 parents a64349b + 622d32e commit 3b1b9c7

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

src/ng/directive/attrs.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ forEach(ALIASED_ATTR, function(htmlAttr, ngAttr) {
408408
// ng-src, ng-srcset, ng-href are interpolated
409409
forEach(['src', 'srcset', 'href'], function(attrName) {
410410
var normalized = directiveNormalize('ng-' + attrName);
411-
ngAttributeAliasDirectives[normalized] = function() {
411+
ngAttributeAliasDirectives[normalized] = ['$sce', function($sce) {
412412
return {
413413
priority: 99, // it needs to run after the attributes are interpolated
414414
link: function(scope, element, attr) {
@@ -422,6 +422,10 @@ forEach(['src', 'srcset', 'href'], function(attrName) {
422422
propName = null;
423423
}
424424

425+
// We need to sanitize the url at least once, in case it is a constant
426+
// non-interpolated attribute.
427+
attr.$set(normalized, $sce.getTrustedMediaUrl(attr[normalized]));
428+
425429
attr.$observe(normalized, function(value) {
426430
if (!value) {
427431
if (attrName === 'href') {
@@ -441,5 +445,5 @@ forEach(['src', 'srcset', 'href'], function(attrName) {
441445
});
442446
}
443447
};
444-
};
448+
}];
445449
});

src/ng/interpolate.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ function $InterpolateProvider() {
242242

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

247247
var unescapedText = unescapeText(text);
248248
if (contextAllowsConcatenation) {

test/ng/compileSpec.js

+52
Original file line numberDiff line numberDiff line change
@@ -3577,6 +3577,15 @@ describe('$compile', function() {
35773577
})
35783578
);
35793579

3580+
it('should support non-interpolated `src` and `data-src` on the same element',
3581+
inject(function($rootScope, $compile) {
3582+
var element = $compile('<img src="abc" data-src="123">')($rootScope);
3583+
expect(element.attr('src')).toEqual('abc');
3584+
expect(element.attr('data-src')).toEqual('123');
3585+
$rootScope.$digest();
3586+
expect(element.attr('src')).toEqual('abc');
3587+
expect(element.attr('data-src')).toEqual('123');
3588+
}));
35803589

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

12096+
describe('with media url attributes', function() {
12097+
it('should work with interpolated ng-attr-src', inject(function() {
12098+
$rootScope.name = 'some-image.png';
12099+
element = $compile('<img ng-attr-src="{{name}}">')($rootScope);
12100+
expect(element.attr('src')).toBeUndefined();
12101+
12102+
$rootScope.$digest();
12103+
expect(element.attr('src')).toBe('some-image.png');
12104+
12105+
$rootScope.name = 'other-image.png';
12106+
$rootScope.$digest();
12107+
expect(element.attr('src')).toBe('other-image.png');
12108+
}));
12109+
12110+
it('should work with interpolated ng-attr-data-src', inject(function() {
12111+
$rootScope.name = 'some-image.png';
12112+
element = $compile('<img ng-attr-data-src="{{name}}">')($rootScope);
12113+
expect(element.attr('data-src')).toBeUndefined();
12114+
12115+
$rootScope.$digest();
12116+
expect(element.attr('data-src')).toBe('some-image.png');
12117+
12118+
$rootScope.name = 'other-image.png';
12119+
$rootScope.$digest();
12120+
expect(element.attr('data-src')).toBe('other-image.png');
12121+
}));
12122+
12123+
it('should work alongside constant [src]-attribute and [ng-attr-data-src] attributes', inject(function() {
12124+
$rootScope.name = 'some-image.png';
12125+
element = $compile('<img src="constant.png" ng-attr-data-src="{{name}}">')($rootScope);
12126+
expect(element.attr('data-src')).toBeUndefined();
12127+
12128+
$rootScope.$digest();
12129+
expect(element.attr('src')).toBe('constant.png');
12130+
expect(element.attr('data-src')).toBe('some-image.png');
12131+
12132+
$rootScope.name = 'other-image.png';
12133+
$rootScope.$digest();
12134+
expect(element.attr('src')).toBe('constant.png');
12135+
expect(element.attr('data-src')).toBe('other-image.png');
12136+
}));
12137+
});
12138+
1208712139
describe('when an attribute has a dash-separated name', function() {
1208812140
it('should work with different prefixes', inject(function() {
1208912141
$rootScope.name = 'JamieMason';

test/ng/directive/ngSrcSpec.js

+14
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ describe('ngSrc', function() {
7878
expect(element.prop('src')).toEqual('http://somewhere/abc');
7979
}));
8080
}
81+
82+
it('should work with `src` attribute on the same element', inject(function($rootScope, $compile) {
83+
$rootScope.imageUrl = 'dynamic';
84+
element = $compile('<img ng-src="{{imageUrl}}" src="static">')($rootScope);
85+
expect(element.attr('src')).toBe('static');
86+
$rootScope.$digest();
87+
expect(element.attr('src')).toBe('dynamic');
88+
dealoc(element);
89+
90+
element = $compile('<img src="static" ng-src="{{imageUrl}}">')($rootScope);
91+
expect(element.attr('src')).toBe('static');
92+
$rootScope.$digest();
93+
expect(element.attr('src')).toBe('dynamic');
94+
}));
8195
});
8296

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

0 commit comments

Comments
 (0)