Simplify regex to not hang the browser

This commit is contained in:
raimund-schluessler 2014-11-04 18:49:20 +01:00
parent 5e619d9347
commit 697344cd11
2 changed files with 25 additions and 24 deletions

View file

@ -30,22 +30,23 @@ angular.module('Tasks').directive 'clickableurl', [ '$compile',
scope.$watch('clickableurl', (clickableurl) ->
if !angular.isUndefined(clickableurl)
url_regex = ///
(\s|^)+ # start with a white-space or start of line (to exclude email)
((https?)://)? # protocol: http or https
(([\da-z\.\-]+\.) # domain-name
([a-z\.]{2,}\.?) # top level domain with optional root label
([/\w\.\-]*)*/? # url-path
(\?[\da-z\-=]*)?) # query string
(\s|$)+ # end with white-space
(?:\s|^)+ # start with a white-space or start of line (exclude mail)
(https?://)? # protocol: http or https
(([\da-z\-]+\.{1})+ # domain-name
[a-z]{2,}\.? # top level domain with optional root label
[\.\d/\w\-\%=&+\?~#]*) # url-path
(?:\s|$)+ # end with white-space
///gi
mail_regex = ///
(\s|^)+ # start with a white-space or start of line
(?:\s|^)+ # start with a white-space or start of line
(([\w.!$%&'\*\+-/=\?^`\{\|\}~#])+ # local part
([@]){1} # @
([\da-z\.\-]+\.) # domain-name
([a-z\.]{2,}\.?)) # top level domain with optional root label
(\s|$)+ # end with white-space
([\da-z\-]+\.{1})+ # domain-name
[a-z]{2,}\.?) # top level domain with optional root label
(?:\s|$)+ # end with white-space
///gi
matchs = new Array()
# find URLs
@ -77,19 +78,19 @@ angular.module('Tasks').directive 'clickableurl', [ '$compile',
text = if link.index then link[0].substring(1) else link[0]
# check if email address
if link[4] == '@'
a = $compile('<a href="mailto:' + link[2] + '"
if link[3] == '@'
a = $compile('<a href="mailto:' + link[1] + '"
stop-event="click"></a>')(scope)
a.text(text)
element.append(a)
continue
# check for defined protocol
if angular.isUndefined(link[3])
link[3] = 'http'
if angular.isUndefined(link[1])
link[1] = 'http://'
a = $compile('<a href="' + link[3] + '://' +
link[4] + '"
a = $compile('<a href="' + link[1] +
link[2] + '"
target="_blank" stop-event="click"></a>')(scope)
a.text(text)
element.append(a)

View file

@ -189,8 +189,8 @@
return scope.$watch('clickableurl', function(clickableurl) {
var a, index, link, mail_regex, match, matchs, text, url_regex, _i, _len;
if (!angular.isUndefined(clickableurl)) {
url_regex = /(\s|^)+((https?):\/\/)?(([\da-z\.\-]+\.)([a-z\.]{2,}\.?)([\/\w\.\-]*)*\/?(\?[\da-z\-=]*)?)(\s|$)+/gi;
mail_regex = /(\s|^)+(([\w.!$%&'\*\+-\/=\?^`\{\|\}~#])+([@]){1}([\da-z\.\-]+\.)([a-z\.]{2,}\.?))(\s|$)+/gi;
url_regex = /(?:\s|^)+(https?:\/\/)?(([\da-z\-]+\.{1})+[a-z]{2,}\.?[\.\d\/\w\-\%=&+\?~#]*)(?:\s|$)+/gi;
mail_regex = /(?:\s|^)+(([\w.!$%&'\*\+-\/=\?^`\{\|\}~#])+([@]){1}([\da-z\-]+\.{1})+[a-z]{2,}\.?)(?:\s|$)+/gi;
matchs = new Array();
while ((match = url_regex.exec(clickableurl))) {
matchs.push(match);
@ -218,17 +218,17 @@
}
index = link.index + link[0].length;
text = link.index ? link[0].substring(1) : link[0];
if (link[4] === '@') {
a = $compile('<a href="mailto:' + link[2] + '"\
if (link[3] === '@') {
a = $compile('<a href="mailto:' + link[1] + '"\
stop-event="click"></a>')(scope);
a.text(text);
element.append(a);
continue;
}
if (angular.isUndefined(link[3])) {
link[3] = 'http';
if (angular.isUndefined(link[1])) {
link[1] = 'http://';
}
a = $compile('<a href="' + link[3] + '://' + link[4] + '"\
a = $compile('<a href="' + link[1] + link[2] + '"\
target="_blank" stop-event="click"></a>')(scope);
a.text(text);
element.append(a);