From 5159eb76350a4da587bab3d73e212ed134cde789 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Tue, 2 Nov 2010 18:39:30 -0700 Subject: fix the linky filter --- src/filters.js | 32 ++++++++++++++++++-------------- src/scenario/dsl.js | 1 - 2 files changed, 18 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/filters.js b/src/filters.js index 64d8c8ba..0cff6d56 100644 --- a/src/filters.js +++ b/src/filters.js @@ -360,7 +360,8 @@ angularFilter.html = function(html, option){ * @function * * @description - * Finds links in text input and turns them into html links. Supports http/https/ftp/mailto links. + * Finds links in text input and turns them into html links. Supports http/https/ftp/mailto and + * plane email address links. * * @param {string} text Input text. * @returns {string} Html-linkified text. @@ -369,7 +370,8 @@ angularFilter.html = function(html, option){ Snippet: @@ -392,13 +394,14 @@ and one more: ftp://127.0.0.1/.
- + * * @scenario it('should linkify the snippet with urls', function(){ expect(using('#linky-filter').binding('snippet | linky')). toBe('Pretty text with some links:\n' + - 'http://angularjs.org/,' + - 'mailto:us@somewhere.org\n' + + 'http://angularjs.org/,\n' + + 'us@somewhere.org,\n' + + 'another@somewhere.org,\n' + 'and one more: ftp://127.0.0.1/.'); }); @@ -406,7 +409,8 @@ and one more: ftp://127.0.0.1/. expect(using('#escaped-html').binding('snippet')). toBe("Pretty text with some links:\n" + "http://angularjs.org/,\n" + - "mailto:us@somewhere.org\n" + + "mailto:us@somewhere.org,\n" + + "another@somewhere.org,\n" + "and one more: ftp://127.0.0.1/."); }); @@ -420,10 +424,7 @@ and one more: ftp://127.0.0.1/. //TODO: externalize all regexps angularFilter.linky = function(text){ if (!text) return text; - function regExpEscape(text) { - return text.replace(/([\/\.\*\+\?\|\(\)\[\]\{\}\\])/g, '\\$1'); - } - var URL = /(ftp|http|https|mailto):\/\/([^\(\)|\s]+)/; + var URL = /((ftp|https?):\/\/|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s\.\;\,\(\)\{\}\<\>]/; var match; var raw = text; var html = []; @@ -431,13 +432,16 @@ angularFilter.linky = function(text){ var url; var i; while (match=raw.match(URL)) { - url = match[0].replace(/[\.\;\,\(\)\{\}\<\>]$/,''); - i = raw.indexOf(url); + // 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; + i = match.index; writer.chars(raw.substr(0, i)); writer.start('a', {href:url}); - writer.chars(url); + writer.chars(match[0].replace(/^mailto:/, '')); writer.end('a'); - raw = raw.substring(i + url.length); + raw = raw.substring(i + match[0].length); } writer.chars(raw); return new HTML(html.join('')); diff --git a/src/scenario/dsl.js b/src/scenario/dsl.js index 45156369..f37ab71d 100644 --- a/src/scenario/dsl.js +++ b/src/scenario/dsl.js @@ -170,7 +170,6 @@ angular.scenario.dsl('binding', function() { if (element.is('input, textarea')) { done(null, element.val()); } else { - console.log('element.html(): ', element.html()); done(null, element.html()); } return; -- cgit v1.2.3