From 4301dc13b0551d23b91c9a4581d225f6885ec669 Mon Sep 17 00:00:00 2001 From: Stian Jensen Date: Wed, 12 Aug 2015 12:31:22 +0200 Subject: [PATCH] feat(ngSanitize): Add custom attributes to linky Optional extra attributes may be defined either as: - a map of attributes and values - a function that takes the url as a parameter and returns a map --- src/ngSanitize/filter/linky.js | 49 +++++++++++++++++++++++++---- test/ngSanitize/filter/linkySpec.js | 21 +++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/ngSanitize/filter/linky.js b/src/ngSanitize/filter/linky.js index 159958e9876c..c3e0af5a3799 100644 --- a/src/ngSanitize/filter/linky.js +++ b/src/ngSanitize/filter/linky.js @@ -15,6 +15,16 @@ * * @param {string} text Input text. * @param {string} target Window (_blank|_self|_parent|_top) or named frame to open links in. + * @param {object|function(url)} [attributes] Add custom attributes to the link element. + * + * Can be one of: + * + * - `object`: A map of attributes + * - `function`: Takes the url as a parameter and returns a map of attributes + * + * If the map of attributes contains a value for `target`, it overrides the value of + * the target parameter. + * * @returns {string} Html-linkified text. * * @usage @@ -32,7 +42,7 @@ 'mailto:us@somewhere.org,\n'+ 'another@somewhere.org,\n'+ 'and one more: ftp://127.0.0.1/.'; - $scope.snippetWithTarget = 'http://angularjs.org/'; + $scope.snippetWithSingleURL = 'http://angularjs.org/'; }]);
@@ -55,10 +65,19 @@ linky target -
<div ng-bind-html="snippetWithTarget | linky:'_blank'">
</div>
+
<div ng-bind-html="snippetWithSingleURL | linky:'_blank'">
</div>
+ + +
+ + + + linky custom attributes + +
<div ng-bind-html="snippetWithSingleURL | linky:'_self':{rel:'nofollow'}">
</div>
-
+
@@ -95,10 +114,17 @@ it('should work with the target property', function() { expect(element(by.id('linky-target')). - element(by.binding("snippetWithTarget | linky:'_blank'")).getText()). + element(by.binding("snippetWithSingleURL | linky:'_blank'")).getText()). toBe('http://angularjs.org/'); expect(element(by.css('#linky-target a')).getAttribute('target')).toEqual('_blank'); }); + + it('should optionally add custom attributes', function() { + expect(element(by.id('linky-custom-attributes')). + element(by.binding("snippetWithSingleURL | linky:'_self':{rel: 'nofollow'}")).getText()). + toBe('http://angularjs.org/'); + expect(element(by.css('#linky-custom-attributes a')).getAttribute('rel')).toEqual('nofollow'); + }); */ @@ -107,7 +133,7 @@ angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) { /((ftp|https?):\/\/|(www\.)|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"\u201d\u2019]/i, MAILTO_REGEXP = /^mailto:/i; - return function(text, target) { + return function(text, target, attributes) { if (!text) return text; var match; var raw = text; @@ -137,8 +163,19 @@ angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) { } function addLink(url, text) { + var key; html.push('http://example.com', 'http://example.com'); }); + + it('should optionally add custom attributes', function() { + expect(linky("http://example.com", "_self", {rel: "nofollow"})). + toEqual('http://example.com'); + }); + + it('should override target parameter with custom attributes', function() { + expect(linky("http://example.com", "_self", {target: "_blank"})). + toEqual('http://example.com'); + }); + + it('should optionally add custom attributes from function', function() { + expect(linky("http://example.com", "_self", function(url) {return {"class": "blue"};})). + toEqual('http://example.com'); + }); + + it('should pass url as parameter to custom attribute function', function() { + var linkParameters = jasmine.createSpy('linkParameters').andReturn({"class": "blue"}); + linky("http://example.com", "_self", linkParameters); + expect(linkParameters).toHaveBeenCalledWith('http://example.com'); + }); });