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

feat(linky): support tel scheme #12196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions docs/app/src/errors.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
angular.module('errors', ['ngSanitize'])

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

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

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

return '<a' + targetHtml + ' href="' + url +'">' +
truncate(url.replace(MAILTO_REGEXP, ''), 60) +
truncate(url.replace(MAILTO_TEL_REGEXP, ''), 60) +
'</a>';
}));
};
Expand Down
23 changes: 12 additions & 11 deletions src/ngSanitize/filter/linky.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @kind function
*
* @description
* Finds links in text input and turns them into html links. Supports http/https/ftp/mailto and
* Finds links in text input and turns them into html links. Supports http/https/ftp/tel/mailto and
* plain email address links.
*
* Requires the {@link ngSanitize `ngSanitize`} module to be installed.
Expand All @@ -29,6 +29,7 @@
$scope.snippet =
'Pretty text with some links:\n'+
'http://angularjs.org/,\n'+
'call tel:28091891 now,\n'+
'mailto:[email protected],\n'+
'[email protected],\n'+
'and one more: ftp://127.0.0.1/.';
Expand Down Expand Up @@ -71,15 +72,15 @@
<file name="protractor.js" type="protractor">
it('should linkify the snippet with urls', function() {
expect(element(by.id('linky-filter')).element(by.binding('snippet | linky')).getText()).
toBe('Pretty text with some links: http://angularjs.org/, [email protected], ' +
'[email protected], and one more: ftp://127.0.0.1/.');
expect(element.all(by.css('#linky-filter a')).count()).toEqual(4);
toBe('Pretty text with some links: http://angularjs.org/, call 28091891 now, ' +
'[email protected], [email protected], and one more: ftp://127.0.0.1/.');
expect(element.all(by.css('#linky-filter a')).count()).toEqual(5);
});

it('should not linkify snippet without the linky filter', function() {
expect(element(by.id('escaped-html')).element(by.binding('snippet')).getText()).
toBe('Pretty text with some links: http://angularjs.org/, mailto:[email protected], ' +
'[email protected], and one more: ftp://127.0.0.1/.');
toBe('Pretty text with some links: http://angularjs.org/, call tel:28091891 now, ' +
'mailto:[email protected], [email protected], and one more: ftp://127.0.0.1/.');
expect(element.all(by.css('#escaped-html a')).count()).toEqual(0);
});

Expand All @@ -104,8 +105,8 @@
*/
angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
var LINKY_URL_REGEXP =
/((ftp|https?):\/\/|(www\.)|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"”’]/i,
MAILTO_REGEXP = /^mailto:/i;
/((ftp|https?):\/\/|(www\.)|(tel:)[0-9]+|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"”’]/i,
MAILTO_TEL_REGEXP = /^(mailto|tel):/i;

return function(text, target) {
if (!text) return text;
Expand All @@ -117,13 +118,13 @@ angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
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];
// if we did not match ftp/http/www/mailto then assume mailto
if (!match[2] && !match[4]) {
// if we did not match ftp/http/www/tel/mailto then assume mailto
if (!match[2] && !match[4] && !match[5]) {
url = (match[3] ? 'http://' : 'mailto:') + url;
}
i = match.index;
addText(raw.substr(0, i));
addLink(url, match[0].replace(MAILTO_REGEXP, ''));
addLink(url, match[0].replace(MAILTO_TEL_REGEXP, ''));
raw = raw.substring(i + match[0].length);
}
addText(raw);
Expand Down
7 changes: 7 additions & 0 deletions test/ngSanitize/filter/linkySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ describe('linky', function() {
toEqual('my email is &#34;<a href="mailto:[email protected]">[email protected]</a>&#34;');
});

it('should handle tel:', function() {
expect(linky("tel:28091891")).
toEqual('<a href="tel:28091891">28091891</a>');
expect(linky("call tel:28091891 now")).
toEqual('call <a href="tel:28091891">28091891</a> now');
});

it('should handle quotes in the email', function() {
expect(linky('foo@"bar".com')).toEqual('<a href="mailto:foo@&#34;bar&#34;.com">foo@&#34;bar&#34;.com</a>');
});
Expand Down