Use a better regex to identify URLs

This commit is contained in:
Konstantinos Sideris 2017-12-24 13:13:07 +02:00
parent 74ffef9826
commit 6835a97b15
4 changed files with 14 additions and 12 deletions

View file

@ -1,5 +1,8 @@
#pragma once #pragma once
#include <QRegExp>
#include <QString>
// Non-theme app configuration. Layouts, fonts spacing etc. // Non-theme app configuration. Layouts, fonts spacing etc.
// //
// Font sizes are in pixels. // Font sizes are in pixels.
@ -16,6 +19,12 @@ namespace dialogs {
static constexpr int labelSize = 15; static constexpr int labelSize = 15;
} }
namespace strings {
static const QString url_html = "<a href=\"\\1\">\\1</a>";
static const QRegExp url_regex(
"((www\\.(?!\\.)|[a-z][a-z0-9+.-]*://)[^\\s<>'\"]+[^!,\\.\\s<>'\"\\]\\)\\:])");
}
// Window geometry. // Window geometry.
namespace window { namespace window {
static constexpr int height = 600; static constexpr int height = 600;

View file

@ -36,9 +36,6 @@ class Menu;
class OverlayModal; class OverlayModal;
class RoomSettings; class RoomSettings;
static const QString URL_HTML = "<a href=\"\\1\">\\1</a>";
static const QRegExp URL_REGEX("((?:https?|ftp)://\\S+)");
class TopRoomBar : public QWidget class TopRoomBar : public QWidget
{ {
Q_OBJECT Q_OBJECT

View file

@ -216,7 +216,7 @@ TopRoomBar::paintEvent(QPaintEvent *event)
elidedText = elidedText =
QFontMetrics(topicLabel_->font()) QFontMetrics(topicLabel_->font())
.elidedText(roomTopic_, Qt::ElideRight, topicLabel_->width() - perFrameResize); .elidedText(roomTopic_, Qt::ElideRight, topicLabel_->width() - perFrameResize);
elidedText.replace(URL_REGEX, URL_HTML); elidedText.replace(conf::strings::url_regex, conf::strings::url_html);
topicLabel_->setText(elidedText); topicLabel_->setText(elidedText);
} }

View file

@ -16,7 +16,6 @@
*/ */
#include <QFontDatabase> #include <QFontDatabase>
#include <QRegExp>
#include <QTextEdit> #include <QTextEdit>
#include "Avatar.h" #include "Avatar.h"
@ -28,9 +27,6 @@
#include "timeline/widgets/ImageItem.h" #include "timeline/widgets/ImageItem.h"
#include "timeline/widgets/VideoItem.h" #include "timeline/widgets/VideoItem.h"
static const QRegExp URL_REGEX("((?:https?|ftp)://\\S+)");
static const QString URL_HTML = "<a href=\"\\1\">\\1</a>";
void void
TimelineItem::init() TimelineItem::init()
{ {
@ -84,7 +80,7 @@ TimelineItem::TimelineItem(mtx::events::MessageType ty,
} }
body = body.toHtmlEscaped(); body = body.toHtmlEscaped();
body.replace(URL_REGEX, URL_HTML); body.replace(conf::strings::url_regex, conf::strings::url_html);
body.replace("\n", "<br/>"); body.replace("\n", "<br/>");
generateTimestamp(timestamp); generateTimestamp(timestamp);
@ -206,7 +202,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Notice
generateTimestamp(timestamp); generateTimestamp(timestamp);
body.replace(URL_REGEX, URL_HTML); body.replace(conf::strings::url_regex, conf::strings::url_html);
body.replace("\n", "<br/>"); body.replace("\n", "<br/>");
body = "<i>" + body + "</i>"; body = "<i>" + body + "</i>";
@ -252,7 +248,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Emote>
generateTimestamp(timestamp); generateTimestamp(timestamp);
emoteMsg = emoteMsg.toHtmlEscaped(); emoteMsg = emoteMsg.toHtmlEscaped();
emoteMsg.replace(URL_REGEX, URL_HTML); emoteMsg.replace(conf::strings::url_regex, conf::strings::url_html);
emoteMsg.replace("\n", "<br/>"); emoteMsg.replace("\n", "<br/>");
if (with_sender) { if (with_sender) {
@ -295,7 +291,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Text>
generateTimestamp(timestamp); generateTimestamp(timestamp);
body = body.toHtmlEscaped(); body = body.toHtmlEscaped();
body.replace(URL_REGEX, URL_HTML); body.replace(conf::strings::url_regex, conf::strings::url_html);
body.replace("\n", "<br/>"); body.replace("\n", "<br/>");
if (with_sender) { if (with_sender) {