8
8
* plain email address links.
9
9
*
10
10
* @param {string } text Input text.
11
+ * @param {string } target Window (_blank|_self|_parent|_top) or named frame to open links in.
11
12
* @returns {string } Html-linkified text.
12
13
*
13
14
* @usage
24
25
'mailto:us@somewhere .org,\n'+
25
26
'another@somewhere.org,\n'+
26
27
'and one more: ftp://127.0.0.1/.';
28
+ $scope.snippetWithTarget = 'http://angularjs.org/';
27
29
}
28
30
</script>
29
31
<div ng-controller="Ctrl">
43
45
<div ng-bind-html="snippet | linky"></div>
44
46
</td>
45
47
</tr>
48
+ <tr id="linky-target">
49
+ <td>linky target</td>
50
+ <td>
51
+ <pre><div ng-bind-html="snippetWithTarget | linky:'_blank'"><br></div></pre>
52
+ </td>
53
+ <td>
54
+ <div ng-bind-html="snippetWithTarget | linky:'_blank'"></div>
55
+ </td>
56
+ </tr>
46
57
<tr id="escaped-html">
47
58
<td>no filter</td>
48
59
<td><pre><div ng-bind="snippet"><br></div></pre></td>
75
86
toBe('new <a href="http://link">http://link</a>.');
76
87
expect(using('#escaped-html').binding('snippet')).toBe('new http://link.');
77
88
});
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
+ });
78
94
</doc:scenario>
79
95
</doc:example>
80
96
*/
81
97
angular . module ( 'ngSanitize' ) . filter ( 'linky' , function ( ) {
82
98
var LINKY_URL_REGEXP = / ( ( f t p | h t t p s ? ) : \/ \/ | ( m a i l t o : ) ? [ A - Z a - z 0 - 9 . _ % + - ] + @ ) \S * [ ^ \s \. \; \, \( \) \{ \} \< \> ] / ,
83
99
MAILTO_REGEXP = / ^ m a i l t o : / ;
84
100
85
- return function ( text ) {
101
+ return function ( text , target ) {
86
102
if ( ! text ) return text ;
87
103
var match ;
88
104
var raw = text ;
@@ -91,14 +107,19 @@ angular.module('ngSanitize').filter('linky', function() {
91
107
var writer = htmlSanitizeWriter ( html ) ;
92
108
var url ;
93
109
var i ;
110
+ var properties = { } ;
111
+ if ( angular . isDefined ( target ) ) {
112
+ properties . target = target ;
113
+ }
94
114
while ( ( match = raw . match ( LINKY_URL_REGEXP ) ) ) {
95
115
// We can not end in these as they are sometimes found at the end of the sentence
96
116
url = match [ 0 ] ;
97
117
// if we did not match ftp/http/mailto then assume mailto
98
118
if ( match [ 2 ] == match [ 3 ] ) url = 'mailto:' + url ;
99
119
i = match . index ;
100
120
writer . chars ( raw . substr ( 0 , i ) ) ;
101
- writer . start ( 'a' , { href :url } ) ;
121
+ properties . href = url ;
122
+ writer . start ( 'a' , properties ) ;
102
123
writer . chars ( match [ 0 ] . replace ( MAILTO_REGEXP , '' ) ) ;
103
124
writer . end ( 'a' ) ;
104
125
raw = raw . substring ( i + match [ 0 ] . length ) ;
0 commit comments