summaryrefslogtreecommitdiffstats
path: root/core/js/public
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2018-07-11 16:23:56 +0200
committerJoas Schilling <coding@schilljs.com>2018-07-11 16:23:56 +0200
commit72d17ff02c24bc21c6923c5483e072d5359aa595 (patch)
tree716dfb3836edeb914b84fc9db2d459bcc8ae54cd /core/js/public
parenta22bc0e78758a86000b1023c93e554ebca696493 (diff)
downloadnextcloud-server-72d17ff02c24bc21c6923c5483e072d5359aa595.tar.gz
nextcloud-server-72d17ff02c24bc21c6923c5483e072d5359aa595.zip
Improve url detection in comments
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'core/js/public')
-rw-r--r--core/js/public/comments.js17
1 files changed, 8 insertions, 9 deletions
diff --git a/core/js/public/comments.js b/core/js/public/comments.js
index 318d527b13d..9811528e4c1 100644
--- a/core/js/public/comments.js
+++ b/core/js/public/comments.js
@@ -21,8 +21,7 @@
* The downside: anything not ascii is excluded. Not sure how common it is in areas using different
* alphabets… the upside: fake domains with similar looking characters won't be formatted as links
*/
- urlRegex: /((\s|^)(https?:\/\/|([-A-Z0-9+_])*\.([-A-Z])+)[-A-Z0-9+&@#\/%?=~_|!:,.;()]*[-A-Z0-9+&@#\/%=~_|()])/ig,
- protocolRegex: /^https:\/\//,
+ urlRegex: /(\s|^)(https?:\/\/)?((?:[-A-Z0-9+_]*\.)+[-A-Z]+(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|$)/ig,
plainToRich: function(content) {
content = this.formatLinksRich(content);
@@ -35,15 +34,15 @@
},
formatLinksRich: function(content) {
- var self = this;
- return content.replace(this.urlRegex, function(url) {
- var hasProtocol = (url.indexOf('https://') !== -1) || (url.indexOf('http://') !== -1);
- if(!hasProtocol) {
- url = 'https://' + url.trim();
+ return content.replace(this.urlRegex, function(_, leadingSpace, protocol, url, trailingSpace) {
+ var linkText = url;
+ if(!protocol) {
+ protocol = 'https://';
+ } else if (protocol === 'http://'){
+ linkText = protocol + url;
}
- var linkText = url.replace(self.protocolRegex, '');
- return '<a class="external" target="_blank" rel="noopener noreferrer" href="' + url + '">' + linkText + '</a>';
+ return leadingSpace + '<a class="external" target="_blank" rel="noopener noreferrer" href="' + protocol + url + '">' + linkText + '</a>' + trailingSpace;
});
},