Skip to content

Commit a160aee

Browse files
committed
Added support for the ngSrc directive
1 parent 70ce159 commit a160aee

File tree

5 files changed

+47
-9
lines changed

5 files changed

+47
-9
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
* Add the img-wit directive
1010
* Enable out of the box support for the [WIT URLs](http://wurfl.io/documentation/wit-getting-started.php) to download more images in parallel.
11+
* Allow use of the [ngSrc directive](https://docs.angularjs.org/api/ng/directive/ngSrc)
1112

1213
## Usage
1314

@@ -29,6 +30,12 @@
2930

3031
`<img-wit src="http://yourserver.com/image.png"></img-wit>`
3132

33+
or if you want to use the ngSrc directive:
34+
35+
`<img-wit ng-src="{{myUrl}}"></img-wit>`
36+
37+
where {{myUrl}} is the url of the [trusted image](https://docs.angularjs.org/api/ng/service/$sce) to load.
38+
3239
## Examples
3340

3441
Check the [WURFL Image Tailor Documentation](http://wurfl.io/documentation/wit-directives.php) for the list of available settings.

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "angular-wurfl-image-tailor",
33
"description": "An AngularJS directive for WURFL Image Tailor (WIT)",
4-
"version": "0.9.0",
4+
"version": "0.9.1",
55
"main": "./src/angular-wurfl-image-tailor.js",
66
"authors": [
77
"Luca Corbo (https://github.com/lucor)"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-wurfl-image-tailor",
3-
"version": "0.9.0",
3+
"version": "0.9.1",
44
"description": "An AngularJS directive for WURFL Image Tailor (WIT)",
55
"author": "Luca Corbo (https://github.com/lucor)",
66
"maintainers": [

src/angular-wurfl-image-tailor.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* angular-wurfl-image-tailor v0.9.0
2+
* angular-wurfl-image-tailor v0.9.1
33
* Authors: Luca Corbo (https://github.com/lucor)
4-
* (c) 2014 ScientiaMobile, Inc.
4+
* (c) 2014 - 2015 ScientiaMobile, Inc.
55
* License: MIT
66
*/
77

@@ -25,13 +25,24 @@ angular.module('angular-wurfl-image-tailor', [])
2525
restrict: 'E',
2626
replace: false,
2727
scope:{},
28-
template: '<div class="wit"><img src="{{wit_link}}"/></div>',
28+
template: function(element, attributes){
29+
if (attributes['ngSrc']) {
30+
return '<div class="wit"><img ng-src="{{wit_link}}"/></div>';
31+
} else {
32+
return '<div class="wit"><img src="{{wit_link}}"/></div>';
33+
}
34+
},
2935
link: function (scope, element, attributes) {
3036
var wit_link_pieces = [witUrls.get()];
31-
var src = attributes['src'];
37+
var src;
38+
if (attributes['ngSrc']) {
39+
src = attributes['ngSrc'];
40+
} else {
41+
src = attributes['src'];
42+
}
3243
if (!src) return;
3344
angular.forEach(attributes['$attr'], function(attr) {
34-
if (attr != 'src') {
45+
if (attr != 'src' && attr != 'ng-src') {
3546
wit_link_pieces.push(attr + '_' + attributes[attr]);
3647
}
3748
});

test/unit/directiveSpec.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ describe("Unit: Testing angular directive for WURFL Image Tailor", function() {
55
beforeEach(module('angular-wurfl-image-tailor'));
66

77
beforeEach(inject(
8-
['$compile','$rootScope', function($c, $r) {
8+
['$compile','$rootScope', '$sce', function($c, $r, $sce) {
99
compile = $c;
1010
scope = $r;
11+
sce = $sce;
1112
}]
1213
));
1314

@@ -38,4 +39,23 @@ describe("Unit: Testing angular directive for WURFL Image Tailor", function() {
3839
expect(elt.html()).to.be.equal('<div class="wit"><img src="//wit.wurfl.io/w_200/http://test.com/image.jpg"></div>');
3940
});
4041

41-
});
42+
it('should work as an element with ngSrc directive', function () {
43+
var elt = angular.element('<img-wit ng-src="http://test.com/image.jpg"></img-wit>');
44+
compile(elt)(scope);
45+
scope.$digest();
46+
47+
expect(elt.html()).to.be.a('string');
48+
expect(elt.html()).to.be.equal('<div class="wit"><img ng-src="//wit.wurfl.io/http://test.com/image.jpg" src="//wit.wurfl.io/http://test.com/image.jpg"></div>');
49+
});
50+
51+
it('should work as an element with ngSrc directive and a trusted resource', function () {
52+
scope.myUrl = sce.trustAsResourceUrl('http://test.com/image.jpg');
53+
var elt = angular.element('<img-wit ng-src="{{myUrl}}" w="200"></img-wit>');
54+
compile(elt)(scope);
55+
scope.$digest();
56+
57+
expect(elt.html()).to.be.a('string');
58+
expect(elt.html()).to.be.equal('<div class="wit"><img ng-src="//wit.wurfl.io/w_200/http://test.com/image.jpg" src="//wit.wurfl.io/w_200/http://test.com/image.jpg"></div>');
59+
});
60+
61+
});

0 commit comments

Comments
 (0)