diff --git a/src/ngSanitize/filter/linky.js b/src/ngSanitize/filter/linky.js index 159958e9876c..2780d74c0b33 100644 --- a/src/ngSanitize/filter/linky.js +++ b/src/ngSanitize/filter/linky.js @@ -8,14 +8,25 @@ * @kind function * * @description - * Finds links in text input and turns them into html links. Supports http/https/ftp/mailto and + * Finds links in text input and turns them into html links. Supports `http/https/ftp/mailto` and * plain email address links. * * Requires the {@link ngSanitize `ngSanitize`} module to be installed. * * @param {string} text Input text. - * @param {string} target Window (_blank|_self|_parent|_top) or named frame to open links in. - * @returns {string} Html-linkified 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 and {@link $sanitize sanitized} text. * * @usage @@ -23,25 +34,13 @@ * @example -
Snippet: - - - + + + @@ -55,10 +54,19 @@ + + + + + @@ -68,6 +76,18 @@
FilterSourceRenderedFilterSourceRendered
linky filter
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>
-
+
+ + angular.module('linkyExample', ['ngSanitize']) + .controller('ExampleController', ['$scope', function($scope) { + $scope.snippet = + 'Pretty text with some links:\n'+ + 'http://angularjs.org/,\n'+ + 'mailto:us@somewhere.org,\n'+ + 'another@somewhere.org,\n'+ + 'and one more: ftp://127.0.0.1/.'; + $scope.snippetWithSingleURL = 'http://angularjs.org/'; + }]); + it('should linkify the snippet with urls', function() { expect(element(by.id('linky-filter')).element(by.binding('snippet | linky')).getText()). @@ -95,10 +115,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 +134,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 +164,19 @@ angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) { } function addLink(url, text) { + var key; html.push('http://example.com', 'http://example.com'); }); + + describe('custom attributes', function() { + + it('should optionally add custom attributes', function() { + expect(linky("http://example.com", "_self", {rel: "nofollow"})). + toBeOneOf('http://example.com', + 'http://example.com'); + }); + + + it('should override target parameter with custom attributes', function() { + expect(linky("http://example.com", "_self", {target: "_blank"})). + toBeOneOf('http://example.com', + 'http://example.com'); + }); + + + it('should optionally add custom attributes from function', function() { + expect(linky("http://example.com", "_self", function(url) {return {"class": "blue"};})). + toBeOneOf('http://example.com', + 'http://example.com', + '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'); + }); + + + it('should strip unsafe attributes', function() { + expect(linky("http://example.com", "_self", {"class": "blue", "onclick": "alert('Hi')"})). + toBeOneOf('http://example.com', + 'http://example.com', + 'http://example.com'); + }); + }); });