Skip to content
Permalink
Browse files

fix(linky): make urls starting with www. links, like markdown

It's super cool!

Closes #10290
  • Loading branch information
chasefleming authored and caitp committed Dec 2, 2014
1 parent 8df47db commit 915a891ad4cdcaa5e47e976db8f4d402d230be77
Showing with 9 additions and 3 deletions.
  1. +5 −3 src/ngSanitize/filter/linky.js
  2. +4 −0 test/ngSanitize/filter/linkySpec.js
@@ -104,7 +104,7 @@
*/
angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
var LINKY_URL_REGEXP =
/((ftp|https?):\/\/|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"”’]/,
/((ftp|https?):\/\/|(www\.)|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"”’]/,
MAILTO_REGEXP = /^mailto:/;

return function(text, target) {
@@ -117,8 +117,10 @@ 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/mailto then assume mailto
if (match[2] == match[3]) url = 'mailto:' + url;
// if we did not match ftp/http/www/mailto then assume mailto
if (!match[2] && !match[4]) {
url = (match[3] ? 'http://' : 'mailto:') + url;
}
i = match.index;
addText(raw.substr(0, i));
addLink(url, match[0].replace(MAILTO_REGEXP, ''));
@@ -20,6 +20,10 @@ describe('linky', function() {
expect(linky(undefined)).not.toBeDefined();
});

it('should handle www.', function() {
expect(linky('www.example.com')).toEqual('<a href="http://www.example.com">www.example.com</a>');
});

it('should handle mailto:', function() {
expect(linky("mailto:me@example.com")).
toEqual('<a href="mailto:me@example.com">me@example.com</a>');

0 comments on commit 915a891

Please sign in to comment.
You can’t perform that action at this time.