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

Commit

Permalink
feat(ngSrcset): add new ngSrcset directive
Browse files Browse the repository at this point in the history
In line with ngSrc and ngHref, this new directive ensures that the
`srcset` HTML5 attribute does not include a pre-interpolated string.
Without it the browser will fetch from the URL with the literal text
`{{hash}}` until AngularJS replaces the expression inside `{{hash}}`.

Closes #2601
  • Loading branch information
samaxes authored and petebacondarwin committed May 14, 2013
1 parent 629fb37 commit d551d72
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
29 changes: 27 additions & 2 deletions src/ng/directive/booleanAttrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,31 @@
* @param {template} ngSrc any string which can contain `{{}}` markup.
*/

/**
* @ngdoc directive
* @name ng.directive:ngSrcset
* @restrict A
*
* @description
* Using Angular markup like `{{hash}}` in a `srcset` attribute doesn't
* work right: The browser will fetch from the URL with the literal
* text `{{hash}}` until Angular replaces the expression inside
* `{{hash}}`. The `ngSrcset` directive solves this problem.
*
* The buggy way to write it:
* <pre>
* <img srcset="http://www.gravatar.com/avatar/{{hash}} 2x"/>
* </pre>
*
* The correct way to write it:
* <pre>
* <img ng-srcset="http://www.gravatar.com/avatar/{{hash}} 2x"/>
* </pre>
*
* @element IMG
* @param {template} ngSrcset any string which can contain `{{}}` markup.
*/

/**
* @ngdoc directive
* @name ng.directive:ngDisabled
Expand Down Expand Up @@ -325,8 +350,8 @@ forEach(BOOLEAN_ATTR, function(propName, attrName) {
});


// ng-src, ng-href are interpolated
forEach(['src', 'href'], function(attrName) {
// ng-src, ng-srcset, ng-href are interpolated
forEach(['src', 'srcset', 'href'], function(attrName) {
var normalized = directiveNormalize('ng-' + attrName);
ngAttributeAliasDirectives[normalized] = function() {
return {
Expand Down
18 changes: 17 additions & 1 deletion test/ng/directive/booleanAttrsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ describe('boolean attr directives', function() {


describe('ngSrc', function() {

it('should interpolate the expression and bind to src', inject(function($compile, $rootScope) {
var element = $compile('<div ng-src="some/{{id}}"></div>')($rootScope);

Expand Down Expand Up @@ -126,6 +125,23 @@ describe('ngSrc', function() {
});


describe('ngSrcset', function() {
it('should interpolate the expression and bind to srcset', inject(function($compile, $rootScope) {
var element = $compile('<div ng-srcset="some/{{id}} 2x"></div>')($rootScope);

$rootScope.$digest();
expect(element.attr('srcset')).toEqual('some/ 2x');

$rootScope.$apply(function() {
$rootScope.id = 1;
});
expect(element.attr('srcset')).toEqual('some/1 2x');

dealoc(element);
}));
});


describe('ngHref', function() {
var element;

Expand Down
16 changes: 16 additions & 0 deletions test/ng/directive/ngSrcsetSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

describe('ngSrcset', function() {
var element;

afterEach(function() {
dealoc(element);
});

it('should not result empty string in img srcset', inject(function($rootScope, $compile) {
$rootScope.image = {};
element = $compile('<img ng-srcset="{{image.url}} 2x">')($rootScope);
$rootScope.$digest();
expect(element.attr('srcset')).toEqual(' 2x');
}));
});

0 comments on commit d551d72

Please sign in to comment.