2012-07-11 18:30:00 +02:00
|
|
|
/* ============================================================
|
|
|
|
* GreaseMonkey plugin for QupZilla
|
2013-02-15 12:36:25 +01:00
|
|
|
* Copyright (C) 2012-2013 David Rosca <nowrep@gmail.com>
|
2012-07-11 18:30:00 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "gm_urlmatcher.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QStringList>
|
|
|
|
|
2013-02-15 12:36:25 +01:00
|
|
|
static bool wildcardMatch(const QString &string, const QString &pattern)
|
2012-07-11 18:30:00 +02:00
|
|
|
{
|
|
|
|
int stringSize = string.size();
|
|
|
|
int patternSize = pattern.size();
|
|
|
|
|
2012-09-04 11:29:47 +02:00
|
|
|
bool startsWithWildcard = pattern[0] == QLatin1Char('*');
|
|
|
|
bool endsWithWildcard = pattern[patternSize - 1] == QLatin1Char('*');
|
2012-07-11 18:30:00 +02:00
|
|
|
|
2013-12-30 13:43:48 +01:00
|
|
|
const QStringList parts = pattern.split(QLatin1Char('*'));
|
2012-07-11 18:30:00 +02:00
|
|
|
int pos = 0;
|
|
|
|
|
|
|
|
if (startsWithWildcard) {
|
|
|
|
pos = string.indexOf(parts.at(1));
|
|
|
|
if (pos == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const QString &part, parts) {
|
2012-07-11 18:30:00 +02:00
|
|
|
pos = string.indexOf(part, pos);
|
|
|
|
if (pos == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!endsWithWildcard && stringSize - pos != parts.last().size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-26 12:56:11 +01:00
|
|
|
GM_UrlMatcher::GM_UrlMatcher()
|
|
|
|
: m_useRegExp(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-07-11 18:30:00 +02:00
|
|
|
GM_UrlMatcher::GM_UrlMatcher(const QString &pattern)
|
|
|
|
: m_pattern(pattern)
|
|
|
|
, m_useRegExp(false)
|
|
|
|
{
|
|
|
|
parsePattern(m_pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GM_UrlMatcher::pattern() const
|
|
|
|
{
|
|
|
|
return m_pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GM_UrlMatcher::match(const QString &urlString) const
|
|
|
|
{
|
|
|
|
if (m_useRegExp) {
|
|
|
|
return m_regExp.indexIn(urlString) != -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return wildcardMatch(urlString, m_matchString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GM_UrlMatcher::parsePattern(QString pattern)
|
|
|
|
{
|
2012-09-04 11:29:47 +02:00
|
|
|
if (pattern.startsWith(QLatin1Char('/')) && pattern.endsWith(QLatin1Char('/'))) {
|
2012-07-11 18:30:00 +02:00
|
|
|
pattern = pattern.mid(1);
|
|
|
|
pattern = pattern.left(pattern.size() - 1);
|
|
|
|
|
2013-02-24 10:57:58 +01:00
|
|
|
m_regExp = QzRegExp(pattern, Qt::CaseInsensitive);
|
2012-07-11 18:30:00 +02:00
|
|
|
m_useRegExp = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-04 11:29:47 +02:00
|
|
|
if (pattern.contains(QLatin1String(".tld"))) {
|
2012-07-11 18:30:00 +02:00
|
|
|
|
2013-02-24 10:57:58 +01:00
|
|
|
pattern.replace(QzRegExp("(\\W)"), QLatin1String("\\\\1"))
|
|
|
|
.replace(QzRegExp("\\*+"), QLatin1String("*"))
|
|
|
|
.replace(QzRegExp("^\\\\\\|"), QLatin1String("^"))
|
|
|
|
.replace(QzRegExp("\\\\\\|$"), QLatin1String("$"))
|
|
|
|
.replace(QzRegExp("\\\\\\*"), QLatin1String(".*"))
|
2012-09-04 12:42:45 +02:00
|
|
|
.replace(QLatin1String("\\.tld"), QLatin1String("\\.[a-z.]{2,6}"));
|
2012-07-11 18:30:00 +02:00
|
|
|
|
|
|
|
m_useRegExp = true;
|
2013-02-24 10:57:58 +01:00
|
|
|
m_regExp = QzRegExp(pattern, Qt::CaseInsensitive);
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_matchString = pattern;
|
|
|
|
}
|
|
|
|
}
|