From b6e4a71166c7f00f4140fd7ea8f0cd81b4487a3f Mon Sep 17 00:00:00 2001 From: Xiangru Chen Date: Sat, 14 Jul 2012 17:40:24 +0800 Subject: [PATCH] fix(ngSrc): don't set src if value is empty string Current implementation of ngSrc may lead to empty src attribute when page is loading. For example: can be temporarily rendered as before the image resource is loaded. Some browser emits a request to the current page when seeing (Firefox13 and IE8 will, Chromium20 won't), which leads to performance problems. --- src/ng/directive/booleanAttrs.js | 3 +++ test/ng/directive/ngSrcSpec.js | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 test/ng/directive/ngSrcSpec.js diff --git a/src/ng/directive/booleanAttrs.js b/src/ng/directive/booleanAttrs.js index cce10a3b4e45..2cc26ce71439 100644 --- a/src/ng/directive/booleanAttrs.js +++ b/src/ng/directive/booleanAttrs.js @@ -302,6 +302,9 @@ forEach(['src', 'href'], function(attrName) { priority: 99, // it needs to run after the attributes are interpolated link: function(scope, element, attr) { attr.$observe(normalized, function(value) { + if (!value) + return; + attr.$set(attrName, value); // on IE, if "ng:src" directive declaration is used and "src" attribute doesn't exist diff --git a/test/ng/directive/ngSrcSpec.js b/test/ng/directive/ngSrcSpec.js new file mode 100644 index 000000000000..a917c5116bec --- /dev/null +++ b/test/ng/directive/ngSrcSpec.js @@ -0,0 +1,17 @@ +'use strict'; + +describe('ngSrc', function() { + var element; + + afterEach(function() { + dealoc(element); + }); + + it('should not result empty string in img src', inject(function($rootScope, $compile) { + $rootScope.image = {}; + element = $compile('')($rootScope); + $rootScope.$digest(); + expect(element.attr('src')).not.toBe(''); + expect(element.attr('src')).toBe(undefined); + })); +});