From 0ef189abb2962c19c89df4e048a87eac78570ddf Mon Sep 17 00:00:00 2001 From: Artem Fitiskin Date: Sat, 7 Feb 2015 23:19:39 +0300 Subject: [PATCH 1/2] fix #457 change URL-regexp --- app/js/services.js | 61 ++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 21 deletions(-) mode change 100644 => 100755 app/js/services.js diff --git a/app/js/services.js b/app/js/services.js old mode 100644 new mode 100755 index 68b12817..b7fe8954 --- a/app/js/services.js +++ b/app/js/services.js @@ -3480,8 +3480,29 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) "\\uffa1-\\uffdc"; // half width Hangul (Korean) var regexAlphaNumericChars = "0-9\.\_" + regexAlphaChars; - var regExp = new RegExp('(^|\\s)((?:https?://)?telegram\\.me/|@)([a-zA-Z\\d_]{5,32})|((?:(ftp|https?)://|(?:mailto:)?([A-Za-z0-9._%+-]+@))(\\S*\\.\\S*[^\\s.;,(){}<>"\']))|(\\n)|(' + emojiUtf.join('|') + ')|(^|\\s)(#[' + regexAlphaNumericChars + ']{2,20})', 'i'); + // Regular Expression for URL validation by Diego Perini + var urlRegex = "(?:(?:https?|ftp)://)?" + + // user:pass authentication + "(?:\\S+(?::\\S*)?@)?" + + "(?:" + + "(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])(?:\\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])){3}" + + "|" + + // host name + "(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)" + + // domain name + "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*" + + // TLD identifier + "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,24}))" + + ")" + + // port number + "(?::\\d{2,5})?" + + // resource path + "(?:/[^\\s\\.\"\']*)?"; + + var regExp = new RegExp('(^|\\s)((?:https?://)?telegram\\.me/|@)([a-zA-Z\\d_]{5,32})|(' + urlRegex + ')|(\\n)|(' + emojiUtf.join('|') + ')|(^|\\s)(#[' + regexAlphaNumericChars + ']{2,20})', 'i'); + + var emailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; var youtubeRegex = /(?:https?:\/\/)?(?:www\.)?youtu(?:|\.be|be\.com|\.b)(?:\/v\/|\/watch\\?v=|e\/|(?:\/\??#)?\/watch(?:.+)v=)(.{11})(?:\&[^\s]*)?/; var vimeoRegex = /(?:https?:\/\/)?(?:www\.)?vimeo\.com\/(\d+)/; var instagramRegex = /https?:\/\/(?:instagr\.am\/p\/|instagram\.com\/p\/)([a-zA-Z0-9\-\_]+)/i; @@ -3530,7 +3551,6 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) emojiCoords; while ((match = raw.match(regExp))) { - // console.log(2, match); html.push(encodeEntities(raw.substr(0, match.index))); if (match[3]) { // telegram.me links @@ -3550,22 +3570,22 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) ); } } - else if (match[4]) { // URL + else if (match[4]) { // URL & e-mail if (!options.noLinks) { - if (match[6]) { + if (emailRegex.test(match[4])) { html.push( '', - encodeEntities(match[6] + match[7]), + encodeEntities(match[4]), '' ); } else { html.push( '', - encodeEntities(match[5] + '://' + match[7]), + encodeEntities(match[4]), '' ); } @@ -3573,16 +3593,15 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) html.push(encodeEntities(match[0])); } } - else if (match[8]) { // New line + else if (match[5]) { // New line if (!options.noLinebreaks) { html.push('
'); } else { html.push(' '); } } - else if (match[9]) { - - if ((emojiCode = emojiMap[match[9]]) && + else if (match[6]) { + if ((emojiCode = emojiMap[match[6]]) && (emojiCoords = getEmojiSpritesheetCoords(emojiCode))) { emojiTitle = encodeEntities(emojiData[emojiCode][1][0]); @@ -3599,23 +3618,23 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) ':', emojiTitle, ':' ); } else { - html.push(encodeEntities(match[9])); + html.push(encodeEntities(match[6])); } } - else if (match[11]) { + else if (match[8]) { if (!options.noLinks) { html.push( - match[10], + match[7], '', - encodeEntities(match[11]), + encodeEntities(match[8]), '' ); } else { html.push( - match[10], - encodeEntities(match[11]) + match[7], + encodeEntities(match[8]) ); } } @@ -3695,8 +3714,8 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) while ((match = raw.match(regExp))) { text.push(raw.substr(0, match.index)); - if (match[9]) { - if ((emojiCode = emojiMap[match[9]]) && + if (match[6]) { + if ((emojiCode = emojiMap[match[6]]) && (emojiTitle = emojiData[emojiCode][1][0])) { text.push(':' + emojiTitle + ':'); } else { From 1acb5308814dd4b5ec6dfac674b79dccb77d0ba5 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Thu, 12 Feb 2015 14:33:53 +0300 Subject: [PATCH 2/2] Added protocol for correct absolute href Allow mailto: links --- app/js/services.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/js/services.js b/app/js/services.js index b7fe8954..82fa0f3b 100755 --- a/app/js/services.js +++ b/app/js/services.js @@ -3482,7 +3482,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) var regexAlphaNumericChars = "0-9\.\_" + regexAlphaChars; // Regular Expression for URL validation by Diego Perini - var urlRegex = "(?:(?:https?|ftp)://)?" + + var urlRegex = "((?:https?|ftp)://|mailto:)?" + // user:pass authentication "(?:\\S+(?::\\S*)?@)?" + "(?:" + @@ -3583,7 +3583,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) } else { html.push( '', encodeEntities(match[4]), '' @@ -3593,15 +3593,15 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) html.push(encodeEntities(match[0])); } } - else if (match[5]) { // New line + else if (match[6]) { // New line if (!options.noLinebreaks) { html.push('
'); } else { html.push(' '); } } - else if (match[6]) { - if ((emojiCode = emojiMap[match[6]]) && + else if (match[7]) { + if ((emojiCode = emojiMap[match[7]]) && (emojiCoords = getEmojiSpritesheetCoords(emojiCode))) { emojiTitle = encodeEntities(emojiData[emojiCode][1][0]); @@ -3618,13 +3618,13 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) ':', emojiTitle, ':' ); } else { - html.push(encodeEntities(match[6])); + html.push(encodeEntities(match[7])); } } else if (match[8]) { if (!options.noLinks) { html.push( - match[7], + match[8], '', @@ -3633,7 +3633,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) ); } else { html.push( - match[7], + match[8], encodeEntities(match[8]) ); }