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

Commit 69ecf4c

Browse files
committed
feat(linky): support tel scheme
1 parent 0c1fbdd commit 69ecf4c

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

docs/app/src/errors.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
angular.module('errors', ['ngSanitize'])
22

33
.filter('errorLink', ['$sanitize', function ($sanitize) {
4-
var LINKY_URL_REGEXP = /((ftp|https?):\/\/|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s\.\;\,\(\)\{\}<>]/g,
5-
MAILTO_REGEXP = /^mailto:/,
4+
var LINKY_URL_REGEXP =
5+
/((ftp|https?):\/\/|(www\.)|(tel:)[0-9]+|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"]/ig,
6+
MAILTO_TEL_REGEXP = /^(mailto|tel):/i,
67
STACK_TRACE_REGEXP = /:\d+:\d+$/;
78

89
var truncate = function (text, nchars) {
@@ -22,11 +23,11 @@ angular.module('errors', ['ngSanitize'])
2223
return url;
2324
}
2425

25-
// if we did not match ftp/http/mailto then assume mailto
26-
if (!/^((ftp|https?):\/\/|mailto:)/.test(url)) url = 'mailto:' + url;
26+
// if we did not match ftp/http/www/tel/mailto then assume mailto
27+
if (!/^((ftp|https?):\/\/|tel:|mailto:)/i.test(url)) url = 'mailto:' + url;
2728

2829
return '<a' + targetHtml + ' href="' + url +'">' +
29-
truncate(url.replace(MAILTO_REGEXP, ''), 60) +
30+
truncate(url.replace(MAILTO_TEL_REGEXP, ''), 60) +
3031
'</a>';
3132
}));
3233
};

src/ngSanitize/filter/linky.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @kind function
99
*
1010
* @description
11-
* Finds links in text input and turns them into html links. Supports http/https/ftp/mailto and
11+
* Finds links in text input and turns them into html links. Supports http/https/ftp/tel/mailto and
1212
* plain email address links.
1313
*
1414
* Requires the {@link ngSanitize `ngSanitize`} module to be installed.
@@ -29,6 +29,7 @@
2929
$scope.snippet =
3030
'Pretty text with some links:\n'+
3131
'http://angularjs.org/,\n'+
32+
'call tel:28091891 now,\n'+
3233
'mailto:us@somewhere.org,\n'+
3334
3435
'and one more: ftp://127.0.0.1/.';
@@ -71,15 +72,15 @@
7172
<file name="protractor.js" type="protractor">
7273
it('should linkify the snippet with urls', function() {
7374
expect(element(by.id('linky-filter')).element(by.binding('snippet | linky')).getText()).
74-
toBe('Pretty text with some links: http://angularjs.org/, us@somewhere.org, ' +
75-
'[email protected], and one more: ftp://127.0.0.1/.');
76-
expect(element.all(by.css('#linky-filter a')).count()).toEqual(4);
75+
toBe('Pretty text with some links: http://angularjs.org/, call 28091891 now, ' +
76+
'us@somewhere.org, [email protected], and one more: ftp://127.0.0.1/.');
77+
expect(element.all(by.css('#linky-filter a')).count()).toEqual(5);
7778
});
7879
7980
it('should not linkify snippet without the linky filter', function() {
8081
expect(element(by.id('escaped-html')).element(by.binding('snippet')).getText()).
81-
toBe('Pretty text with some links: http://angularjs.org/, mailto:us@somewhere.org, ' +
82-
'[email protected], and one more: ftp://127.0.0.1/.');
82+
toBe('Pretty text with some links: http://angularjs.org/, call tel:28091891 now, ' +
83+
'mailto:us@somewhere.org, [email protected], and one more: ftp://127.0.0.1/.');
8384
expect(element.all(by.css('#escaped-html a')).count()).toEqual(0);
8485
});
8586
@@ -104,8 +105,8 @@
104105
*/
105106
angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
106107
var LINKY_URL_REGEXP =
107-
/((ftp|https?):\/\/|(www\.)|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"]/i,
108-
MAILTO_REGEXP = /^mailto:/i;
108+
/((ftp|https?):\/\/|(www\.)|(tel:)[0-9]+|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"]/i,
109+
MAILTO_TEL_REGEXP = /^(mailto|tel):/i;
109110

110111
return function(text, target) {
111112
if (!text) return text;
@@ -117,13 +118,13 @@ angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
117118
while ((match = raw.match(LINKY_URL_REGEXP))) {
118119
// We can not end in these as they are sometimes found at the end of the sentence
119120
url = match[0];
120-
// if we did not match ftp/http/www/mailto then assume mailto
121-
if (!match[2] && !match[4]) {
121+
// if we did not match ftp/http/www/tel/mailto then assume mailto
122+
if (!match[2] && !match[4] && !match[5]) {
122123
url = (match[3] ? 'http://' : 'mailto:') + url;
123124
}
124125
i = match.index;
125126
addText(raw.substr(0, i));
126-
addLink(url, match[0].replace(MAILTO_REGEXP, ''));
127+
addLink(url, match[0].replace(MAILTO_TEL_REGEXP, ''));
127128
raw = raw.substring(i + match[0].length);
128129
}
129130
addText(raw);

test/ngSanitize/filter/linkySpec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ describe('linky', function() {
4444
toEqual('my email is &#34;<a href="mailto:[email protected]">[email protected]</a>&#34;');
4545
});
4646

47+
it('should handle tel:', function() {
48+
expect(linky("tel:28091891")).
49+
toEqual('<a href="tel:28091891">28091891</a>');
50+
expect(linky("call tel:28091891 now")).
51+
toEqual('call <a href="tel:28091891">28091891</a> now');
52+
});
53+
4754
it('should handle quotes in the email', function() {
4855
expect(linky('foo@"bar".com')).toEqual('<a href="mailto:foo@&#34;bar&#34;.com">foo@&#34;bar&#34;.com</a>');
4956
});

0 commit comments

Comments
 (0)