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

Commit 610927d

Browse files
Zach DexterIgorMinar
Zach Dexter
authored andcommitted
feat(linky): allow optional 'target' argument
Closes #1443
1 parent 55d1580 commit 610927d

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/ngSanitize/filter/linky.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* plain email address links.
99
*
1010
* @param {string} text Input text.
11+
* @param {string} target Window (_blank|_self|_parent|_top) or named frame to open links in.
1112
* @returns {string} Html-linkified text.
1213
*
1314
* @usage
@@ -24,6 +25,7 @@
2425
'mailto:us@somewhere.org,\n'+
2526
'another@somewhere.org,\n'+
2627
'and one more: ftp://127.0.0.1/.';
28+
$scope.snippetWithTarget = 'http://angularjs.org/';
2729
}
2830
</script>
2931
<div ng-controller="Ctrl">
@@ -43,6 +45,15 @@
4345
<div ng-bind-html="snippet | linky"></div>
4446
</td>
4547
</tr>
48+
<tr id="linky-target">
49+
<td>linky target</td>
50+
<td>
51+
<pre>&lt;div ng-bind-html="snippetWithTarget | linky:'_blank'"&gt;<br>&lt;/div&gt;</pre>
52+
</td>
53+
<td>
54+
<div ng-bind-html="snippetWithTarget | linky:'_blank'"></div>
55+
</td>
56+
</tr>
4657
<tr id="escaped-html">
4758
<td>no filter</td>
4859
<td><pre>&lt;div ng-bind="snippet"&gt;<br>&lt;/div&gt;</pre></td>
@@ -75,14 +86,19 @@
7586
toBe('new <a href="http://link">http://link</a>.');
7687
expect(using('#escaped-html').binding('snippet')).toBe('new http://link.');
7788
});
89+
90+
it('should work with the target property', function() {
91+
expect(using('#linky-target').binding("snippetWithTarget | linky:'_blank'")).
92+
toBe('<a target="_blank" href="http://angularjs.org/">http://angularjs.org/</a>');
93+
});
7894
</doc:scenario>
7995
</doc:example>
8096
*/
8197
angular.module('ngSanitize').filter('linky', function() {
8298
var LINKY_URL_REGEXP = /((ftp|https?):\/\/|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s\.\;\,\(\)\{\}\<\>]/,
8399
MAILTO_REGEXP = /^mailto:/;
84100

85-
return function(text) {
101+
return function(text, target) {
86102
if (!text) return text;
87103
var match;
88104
var raw = text;
@@ -91,14 +107,19 @@ angular.module('ngSanitize').filter('linky', function() {
91107
var writer = htmlSanitizeWriter(html);
92108
var url;
93109
var i;
110+
var properties = {};
111+
if (angular.isDefined(target)) {
112+
properties.target = target;
113+
}
94114
while ((match = raw.match(LINKY_URL_REGEXP))) {
95115
// We can not end in these as they are sometimes found at the end of the sentence
96116
url = match[0];
97117
// if we did not match ftp/http/mailto then assume mailto
98118
if (match[2] == match[3]) url = 'mailto:' + url;
99119
i = match.index;
100120
writer.chars(raw.substr(0, i));
101-
writer.start('a', {href:url});
121+
properties.href = url;
122+
writer.start('a', properties);
102123
writer.chars(match[0].replace(MAILTO_REGEXP, ''));
103124
writer.end('a');
104125
raw = raw.substring(i + match[0].length);

test/ngSanitize/filter/linkySpec.js

+7
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,11 @@ describe('linky', function() {
2424
expect(linky("send email to me@example.com, but")).
2525
toEqual('send email to <a href="mailto:me@example.com">me@example.com</a>, but');
2626
});
27+
28+
it('should handle target:', function() {
29+
expect(linky("http://example.com", "_blank")).
30+
toEqual('<a target="_blank" href="http://example.com">http://example.com</a>')
31+
expect(linky("http://example.com", "someNamedIFrame")).
32+
toEqual('<a target="someNamedIFrame" href="http://example.com">http://example.com</a>')
33+
});
2734
});

0 commit comments

Comments
 (0)