From b621d8d56fbcaf63fcebb36733d01a2135442f15 Mon Sep 17 00:00:00 2001 From: Zach Dexter Date: Tue, 9 Oct 2012 14:04:58 -0400 Subject: [PATCH] feat(linky): allow optional 'target' argument Closes #1443 --- src/ngSanitize/filter/linky.js | 25 +++++++++++++++++++++++-- test/ngSanitize/filter/linkySpec.js | 7 +++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/ngSanitize/filter/linky.js b/src/ngSanitize/filter/linky.js index 8cdd3c03f915..cc0f7aa6a0b2 100644 --- a/src/ngSanitize/filter/linky.js +++ b/src/ngSanitize/filter/linky.js @@ -8,6 +8,7 @@ * plain email address links. * * @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. * * @usage @@ -24,6 +25,7 @@ 'mailto:us@somewhere.org,\n'+ 'another@somewhere.org,\n'+ 'and one more: ftp://127.0.0.1/.'; + $scope.snippetWithTarget = 'http://angularjs.org/'; }
@@ -43,6 +45,15 @@
+ + linky target + +
<div ng-bind-html="snippetWithTarget | linky:'_blank'">
</div>
+ + +
+ + no filter
<div ng-bind="snippet">
</div>
@@ -75,6 +86,11 @@ toBe('new http://link.'); expect(using('#escaped-html').binding('snippet')).toBe('new http://link.'); }); + + it('should work with the target property', function() { + expect(using('#linky-target').binding("snippetWithTarget | linky:'_blank'")). + toBe('http://angularjs.org/'); + }); */ @@ -82,7 +98,7 @@ angular.module('ngSanitize').filter('linky', function() { var LINKY_URL_REGEXP = /((ftp|https?):\/\/|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s\.\;\,\(\)\{\}\<\>]/, MAILTO_REGEXP = /^mailto:/; - return function(text) { + return function(text, target) { if (!text) return text; var match; var raw = text; @@ -91,6 +107,10 @@ angular.module('ngSanitize').filter('linky', function() { var writer = htmlSanitizeWriter(html); var url; var i; + var properties = {}; + if (angular.isDefined(target)) { + properties.target = target; + } while ((match = raw.match(LINKY_URL_REGEXP))) { // We can not end in these as they are sometimes found at the end of the sentence url = match[0]; @@ -98,7 +118,8 @@ angular.module('ngSanitize').filter('linky', function() { if (match[2] == match[3]) url = 'mailto:' + url; i = match.index; writer.chars(raw.substr(0, i)); - writer.start('a', {href:url}); + properties.href = url; + writer.start('a', properties); writer.chars(match[0].replace(MAILTO_REGEXP, '')); writer.end('a'); raw = raw.substring(i + match[0].length); diff --git a/test/ngSanitize/filter/linkySpec.js b/test/ngSanitize/filter/linkySpec.js index 0448159a09de..a3ef20708588 100644 --- a/test/ngSanitize/filter/linkySpec.js +++ b/test/ngSanitize/filter/linkySpec.js @@ -24,4 +24,11 @@ describe('linky', function() { expect(linky("send email to me@example.com, but")). toEqual('send email to me@example.com, but'); }); + + it('should handle target:', function() { + expect(linky("http://example.com", "_blank")). + toEqual('http://example.com') + expect(linky("http://example.com", "someNamedIFrame")). + toEqual('http://example.com') + }); });