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("<p>", "");
+ result.replace("</p>", "");
+
+ 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("<em>%1</em>").arg(formatted_body);
@@ -651,9 +652,7 @@ TimelineItem::markReceived(bool isEncrypted)
void
TimelineItem::generateBody(const QString &body)
{
- QString content("<span>%1</span>");
-
- 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<mtx::events::msg::Emote>(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<mtx::events::msg::Text>(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;
}
|