From bf4d559523e956523a617213fa1cc3c36fab24e4 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Thu, 13 Sep 2018 11:02:54 +0300 Subject: [PATCH] Strip paragraph tags fixes #438 --- src/Utils.cpp | 12 ++++++------ src/Utils.h | 8 ++------ src/timeline/TimelineItem.cpp | 11 +++++------ src/timeline/TimelineView.cpp | 12 ++++++++---- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/Utils.cpp b/src/Utils.cpp index 5a0558f4..308c0af9 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -350,11 +350,11 @@ utils::markdownToHtml(const QString &text) // The buffer is no longer needed. free((char *)tmp_buf); - return QString::fromStdString(html).trimmed(); -} + auto result = QString::fromStdString(html).trimmed(); -std::string -utils::stripHtml(const QString &text) -{ - return text.trimmed().remove(QRegExp("<[^>]*>")).toStdString(); + // Strip paragraph tags. + result.replace("

", ""); + result.replace("

", ""); + + return result; } diff --git a/src/Utils.h b/src/Utils.h index 93484ca3..dc688845 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -204,10 +204,10 @@ QString getMessageBody(const RoomMessageT &event) { if (event.content.format.empty()) - return QString::fromStdString(event.content.body); + return QString::fromStdString(event.content.body).toHtmlEscaped(); if (event.content.format != common::FORMAT_MSG_TYPE) - return QString::fromStdString(event.content.body); + return QString::fromStdString(event.content.body).toHtmlEscaped(); return QString::fromStdString(event.content.formatted_body); } @@ -219,8 +219,4 @@ linkifyMessage(const QString &body); //! Convert the input markdown text to html. QString markdownToHtml(const QString &text); - -//! Return the plain text version of an html document. -std::string -stripHtml(const QString &text); } diff --git a/src/timeline/TimelineItem.cpp b/src/timeline/TimelineItem.cpp index c4abe7cf..cd12207c 100644 --- a/src/timeline/TimelineItem.cpp +++ b/src/timeline/TimelineItem.cpp @@ -310,11 +310,12 @@ TimelineItem::TimelineItem(mtx::events::MessageType ty, auto displayName = Cache::displayName(room_id_, userid); auto timestamp = QDateTime::currentDateTime(); - // Generate the html body to rendered. + // Generate the html body to be rendered. auto formatted_body = utils::markdownToHtml(body); - // Extract the plain text version for the sidebar. - body = QString::fromStdString(utils::stripHtml(formatted_body)); + // Escape html if the input is not formatted. + if (formatted_body == body.trimmed().toHtmlEscaped()) + formatted_body = body.toHtmlEscaped(); if (ty == mtx::events::MessageType::Emote) { formatted_body = QString("%1").arg(formatted_body); @@ -651,9 +652,7 @@ TimelineItem::markReceived(bool isEncrypted) void TimelineItem::generateBody(const QString &body) { - QString content("%1"); - - body_ = new TextLabel(content.arg(replaceEmoji(body)), this); + body_ = new TextLabel(replaceEmoji(body), this); body_->setFont(font_); body_->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextBrowserInteraction); } diff --git a/src/timeline/TimelineView.cpp b/src/timeline/TimelineView.cpp index 08d506bf..90e116c1 100644 --- a/src/timeline/TimelineView.cpp +++ b/src/timeline/TimelineView.cpp @@ -1236,8 +1236,10 @@ toRoomMessage(const PendingMessage &m) auto html = utils::markdownToHtml(m.body); mtx::events::msg::Emote emote; - emote.body = utils::stripHtml(html); - emote.formatted_body = html.toStdString(); + emote.body = m.body.trimmed().toStdString(); + + if (html != m.body.trimmed().toHtmlEscaped()) + emote.formatted_body = html.toStdString(); return emote; } @@ -1261,8 +1263,10 @@ toRoomMessage(const PendingMessage &m) auto html = utils::markdownToHtml(m.body); mtx::events::msg::Text text; - text.body = utils::stripHtml(html); - text.formatted_body = html.toStdString(); + text.body = m.body.trimmed().toStdString(); + + if (html != m.body.trimmed().toHtmlEscaped()) + text.formatted_body = html.toStdString(); return text; }