[SCM] ktp-common-internals packaging branch, master, updated. debian/15.12.1-2-1839-gf0635e9
Maximiliano Curia
maxy at moszumanska.debian.org
Mon May 9 09:08:03 UTC 2016
Gitweb-URL: http://git.debian.org/?p=pkg-kde/applications/ktp-common-internals.git;a=commitdiff;h=a7df31b
The following commit has been merged in the master branch:
commit a7df31b729f85cb7381263789010b6bc4aba9e88
Author: Martin Klapetek <mklapetek at kde.org>
Date: Thu Feb 27 18:36:55 2014 +0100
Merge escape and URL filters into one
This is needed to avoid the URL filter detecting wrong link boundaries
as the escape filter would change eg. "<http://kde.org/>" to
<http://kde.org/> and then the URL filter would detect
"http://kde.org/>" as the actual link.
Switching the order wouldn't help because the escape filter would escape
the newly inserted <a href...> links and the user would
get <a href.../a> and no clickable links.
Therefore we first detect the links, replace them with placeholders,
then escape everything, then replace placeholders with actual links.
Reviewed-by: David Edmundson
BUG: 329875
FIXED-IN: 0.8.0
---
KTp/CMakeLists.txt | 1 -
KTp/message-escape-filter.cpp | 54 +++++++++++++++++++++++++++++++++++++-
KTp/message-processor.cpp | 3 ++-
KTp/message-url-filter.cpp | 60 -------------------------------------------
4 files changed, 55 insertions(+), 63 deletions(-)
diff --git a/KTp/CMakeLists.txt b/KTp/CMakeLists.txt
index 01a24b4..535639f 100644
--- a/KTp/CMakeLists.txt
+++ b/KTp/CMakeLists.txt
@@ -23,7 +23,6 @@ set (ktp_common_internals_private_SRCS
message-escape-filter.cpp
message-filter-config-manager.cpp
message-processor.cpp
- message-url-filter.cpp
outgoing-message.cpp
persistent-contact.cpp
presence.cpp
diff --git a/KTp/message-escape-filter.cpp b/KTp/message-escape-filter.cpp
index 310e465..93513f2 100644
--- a/KTp/message-escape-filter.cpp
+++ b/KTp/message-escape-filter.cpp
@@ -20,6 +20,9 @@
#include <QtGui/QTextDocument> //needed for Qt::escape
+#include <KTp/text-parser.h>
+#include <KUrl>
+
MessageEscapeFilter::MessageEscapeFilter(QObject *parent)
: KTp::AbstractMessageFilter(parent)
{
@@ -28,7 +31,51 @@ MessageEscapeFilter::MessageEscapeFilter(QObject *parent)
void MessageEscapeFilter::filterMessage(KTp::Message &message, const KTp::MessageContext &context)
{
Q_UNUSED(context)
- QString escapedMessage = Qt::escape(message.mainMessagePart());
+
+ // Here we do URL detection first, before any escaping is done.
+ // If escaping would be done first, the link detection of eg. "<http://kde.org/>"
+ // would become <http://kde.org/> and the URL filter would
+ // match "http://kde.org/>" as the link. On the other hand if it's escaped afterwards,
+ // it would also escape the newly inserted <a href...> links and the user would
+ // get <a href.../a> and no clickable links.
+ //
+ // Therefore we first detect the links, replace them with placeholders,
+ // then escape everything, then replace placeholders with actual links.
+
+ QString messageText = message.mainMessagePart();
+
+ QVariantList urls = message.property("Urls").toList();
+
+ // link detection
+ KTp::TextUrlData parsedUrl = KTp::TextParser::instance()->extractUrlData(messageText);
+
+ QList<QPair<QString, QString> > placeholders;
+
+ int offset = 0;
+ for (int i = 0; i < parsedUrl.fixedUrls.size(); i++) {
+ KUrl url(parsedUrl.fixedUrls.at(i));
+ if (url.protocol() != QLatin1String("mailto")) {
+ QString originalText = messageText.mid(parsedUrl.urlRanges.at(i).first + offset, parsedUrl.urlRanges.at(i).second);
+ QString link = QString::fromLatin1("<a href=\"%1\">%2</a>").arg(QString::fromAscii(url.toEncoded()), originalText);
+
+ QString placeholder = QString::fromLatin1("#K#T#P%1").arg(i);
+
+ // replace the link with a placeholder^ so it passes through the escaping phase,
+ // then it will be replaced back for the actual link
+ messageText.replace(parsedUrl.urlRanges.at(i).first + offset, parsedUrl.urlRanges.at(i).second, placeholder);
+
+ placeholders << qMakePair(placeholder, link);
+
+ urls.append(url);
+
+ //after the first replacement is made, the original position values are not valid anymore, this adjusts them
+ offset += placeholder.length() - originalText.length();
+ }
+ }
+
+ message.setProperty("Urls", urls);
+
+ QString escapedMessage = Qt::escape(messageText);
escapedMessage.replace(QLatin1String("
"), QLatin1String("<br/> ")); //keep leading whitespaces
escapedMessage.replace(QLatin1Char('
'), QLatin1String("<br/>"));
@@ -36,5 +83,10 @@ void MessageEscapeFilter::filterMessage(KTp::Message &message, const KTp::Messag
escapedMessage.replace(QLatin1Char(' '), QLatin1String(" ")); // replace tabs by 4 spaces
escapedMessage.replace(QLatin1String(" "), QLatin1String(" ")); // keep multiple whitespaces
+ // replace link placeholders with actual links
+ for (int i = 0; i < placeholders.size(); i++) {
+ escapedMessage.replace(placeholders.at(i).first, placeholders.at(i).second);
+ }
+
message.setMainMessagePart(escapedMessage);
}
diff --git a/KTp/message-processor.cpp b/KTp/message-processor.cpp
index 6630bd3..906f8e6 100644
--- a/KTp/message-processor.cpp
+++ b/KTp/message-processor.cpp
@@ -131,8 +131,9 @@ MessageProcessor::MessageProcessor():
{
// Default weight is 100. Make sure these two plugins are always above those
// which don't have weight specified and in this exact order.
+ //
+ // The escape filter also has the URL filter in it, see message-escape-filter.cpp for details
d->filters << FilterPlugin(QLatin1String("__messageEscapeFilter"), 98, new MessageEscapeFilter(this));
- d->filters << FilterPlugin(QLatin1String("__messageUrlFilter"), 99, new MessageUrlFilter(this));
d->loadFilters();
}
diff --git a/KTp/message-url-filter.cpp b/KTp/message-url-filter.cpp
deleted file mode 100644
index b3fd9de..0000000
--- a/KTp/message-url-filter.cpp
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- Copyright (C) 2012 Lasath Fernando <kde at lasath.org>
- Copyright (C) 2012 David Edmundson <kde at davidedmundson.co.uk>
- Copyright (C) 2012 Rohan Garg <rohangarg at kubuntu.org>
-
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
-
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-*/
-
-#include "message-filters-private.h"
-
-#include <KUrl>
-#include <KTp/text-parser.h>
-
-MessageUrlFilter::MessageUrlFilter(QObject *parent)
- : KTp::AbstractMessageFilter(parent)
-{
-}
-
-void MessageUrlFilter::filterMessage(KTp::Message &info, const KTp::MessageContext &context)
-{
- Q_UNUSED(context);
- QString message = info.mainMessagePart();
- //FIXME: make "Urls" into a constant
- QVariantList urls = info.property("Urls").toList();
-
- // link detection
- KTp::TextUrlData parsedUrl = KTp::TextParser::instance()->extractUrlData(message);
-
- int offset = 0;
- for (int i = 0; i < parsedUrl.fixedUrls.size(); i++) {
- KUrl url(parsedUrl.fixedUrls.at(i));
- if (url.protocol() != QLatin1String("mailto")) {
- QString originalText = message.mid(parsedUrl.urlRanges.at(i).first + offset, parsedUrl.urlRanges.at(i).second);
- QString link = QString::fromLatin1("<a href=\"%1\">%2</a>").arg(QString::fromAscii(url.toEncoded()), originalText);
- message.replace(parsedUrl.urlRanges.at(i).first + offset, parsedUrl.urlRanges.at(i).second, link);
-
- urls.append(url);
-
- //after the first replacement is made, the original position values are not valid anymore, this adjusts them
- offset += link.length() - originalText.length();
- }
- }
-
- info.setProperty("Urls", urls);
- info.setMainMessagePart(message);
-}
-
-
--
ktp-common-internals packaging
More information about the pkg-kde-commits
mailing list