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
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">
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><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>
4657 <tr id="escaped-html">
4758 <td>no filter</td>
4859 <td><pre><div ng-bind="snippet"><br></div></pre></td>
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 */
8197angular . module ( 'ngSanitize' ) . filter ( 'linky' , function ( ) {
8298 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 \. \; \, \( \) \{ \} \< \> ] / ,
8399 MAILTO_REGEXP = / ^ m a i l t o : / ;
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 ) ;
0 commit comments