summaryrefslogtreecommitdiff
path: root/src/adblock/adblockrule.cpp
diff options
context:
space:
mode:
authorBenjamin Poulain <benjamin.poulain@nokia.com>2010-08-18 01:28:43 +0200
committerBenjamin Poulain <benjamin.poulain@nokia.com>2010-08-18 05:04:39 +0200
commitfc216c5bd5181191c867eb6b6ec45bf252c4a074 (patch)
treee1869bef163f9d2ff7bcc4cb04ac0cda02fd9239 /src/adblock/adblockrule.cpp
parentremove superfluous space (diff)
downloadrekonq-fc216c5bd5181191c867eb6b6ec45bf252c4a074.tar.xz
Cleaning of the constructor of AdBlockRule
Basic cleaning and bug fixing of AdBlockRule. The options shoud be parsed before checking if the filter is a regexp, otherwhise a regexp with option will never match the rule. For example: -"/.*/$xmlhttprequest" is a valid regexp that would not be matched. -"/.*$xmlhttprequest/" is a valid regexp without options and the option should not have been parsed. The matching of the option should match the last index of "$", not the first, for the same reason as above. Use mid() to split the vector at once instead QString::mid() + QString::lef(). Clean the coding style to follow the conventions of KDE.
Diffstat (limited to 'src/adblock/adblockrule.cpp')
-rw-r--r--src/adblock/adblockrule.cpp36
1 files changed, 15 insertions, 21 deletions
diff --git a/src/adblock/adblockrule.cpp b/src/adblock/adblockrule.cpp
index 6ff98f03..b47f8bb8 100644
--- a/src/adblock/adblockrule.cpp
+++ b/src/adblock/adblockrule.cpp
@@ -59,38 +59,32 @@
#include <QStringList>
#include <QUrl>
+static inline bool isRegExpFilter(const QString &filter)
+{
+ return filter.startsWith(QL1C('/')) && filter.endsWith(QL1C('/'));
+}
AdBlockRule::AdBlockRule(const QString &filter)
{
- bool isRegExpRule = false;
+ m_regExp.setCaseSensitivity(Qt::CaseInsensitive);
+ m_regExp.setPatternSyntax(QRegExp::RegExp2);
QString parsedLine = filter;
- if (parsedLine.startsWith(QL1C('/')) && parsedLine.endsWith(QL1C('/')))
- {
- parsedLine = parsedLine.mid(1);
- parsedLine = parsedLine.left(parsedLine.size() - 1);
- isRegExpRule = true;
- }
-
- int optionsNumber = parsedLine.indexOf(QL1C('$'), 0);
- QStringList options;
-
- if (optionsNumber >= 0)
- {
- options = parsedLine.mid(optionsNumber + 1).split(QL1C(','));
+ const int optionsNumber = parsedLine.lastIndexOf(QL1C('$'));
+ if (optionsNumber >= 0 && !isRegExpFilter(parsedLine)) {
+ const QStringList options(parsedLine.mid(optionsNumber + 1).split(QL1C(',')));
+ if (options.contains(QL1S("match-case")))
+ m_regExp.setCaseSensitivity(Qt::CaseSensitive);
parsedLine = parsedLine.left(optionsNumber);
}
- if (!isRegExpRule)
+ if (isRegExpFilter(parsedLine))
+ parsedLine = parsedLine.mid(1, parsedLine.length() - 2);
+ else
parsedLine = convertPatternToRegExp(parsedLine);
- m_regExp = QRegExp(parsedLine, Qt::CaseInsensitive, QRegExp::RegExp2);
-
- if (options.contains(QL1S("match-case")))
- {
- m_regExp.setCaseSensitivity(Qt::CaseSensitive);
- }
+ m_regExp.setPattern(parsedLine);
}