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

Commit 3624a95

Browse files
fix(compile): do not watch constant media attributes
By creating attribute directives that watch the value of media url attributes (e.g. `img[src]`) we caused a conflict when both `src` and `data-src` were appearing on the same element. As each directive was trying to write to the attributes on the element, where AngularJS treats `src` and `data-src` as synonymous. This commit ensures that we do not create watchers when the media url attribute is a constant (no interpolation). Fixes #16734
1 parent b4e409b commit 3624a95

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/ng/compile.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3871,6 +3871,12 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
38713871
compile: function() {
38723872
return {
38733873
pre: function attrInterpolatePreLinkFn(scope, element, attr) {
3874+
if (trustedContext === $sce.MEDIA_URL && isConstantInterpolation(interpolateFn)) {
3875+
// Constant MEDIA_URL attributes should just be sanitized and not watched.
3876+
attr[name] = interpolateFn(scope);
3877+
return;
3878+
}
3879+
38743880
var $$observers = (attr.$$observers || (attr.$$observers = createMap()));
38753881

38763882
// If the attribute has changed since last $interpolate()ed
@@ -4300,3 +4306,8 @@ function removeComments(jqNodes) {
43004306
}
43014307
return jqNodes;
43024308
}
4309+
4310+
4311+
function isConstantInterpolation(interpolateFn) {
4312+
return interpolateFn && interpolateFn.expressions.length === 0;
4313+
}

test/ng/compileSpec.js

Lines changed: 9 additions & 0 deletions
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+
const 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() {

0 commit comments

Comments
 (0)