diff options
Diffstat (limited to 'src/adblock/adblockrule.cpp')
-rw-r--r-- | src/adblock/adblockrule.cpp | 86 |
1 files changed, 43 insertions, 43 deletions
diff --git a/src/adblock/adblockrule.cpp b/src/adblock/adblockrule.cpp index 9f86ffee..7c91a692 100644 --- a/src/adblock/adblockrule.cpp +++ b/src/adblock/adblockrule.cpp @@ -30,7 +30,7 @@ * * This file is a part of the rekonq project * - * Copyright (C) 2009 by Andrea Diamantini <adjam7 at gmail dot com> + * Copyright (C) 2010 by Andrea Diamantini <adjam7 at gmail dot com> * * * This program is free software; you can redistribute it and/or @@ -38,9 +38,9 @@ * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved - * by the membership of KDE e.V.), which shall act as a proxy + * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. - * + * * 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 @@ -57,44 +57,38 @@ // Qt Includes #include <QStringList> -#include <QDebug> -#include <QRegExp> #include <QUrl> -// Defines -#define QL1S(x) QLatin1String(x) -#define QL1C(x) QLatin1Char(x) - AdBlockRule::AdBlockRule(const QString &filter) - : m_optionMatchRule(false) + : m_optionMatchRule(false) { bool isRegExpRule = false; QString parsedLine = filter; - - if ( parsedLine.startsWith( QL1C('/') ) && parsedLine.endsWith( QL1C('/') ) ) + + 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); + + int optionsNumber = parsedLine.indexOf(QL1C('$'), 0); QStringList options; - - if (optionsNumber >= 0) + + if (optionsNumber >= 0) { options = parsedLine.mid(optionsNumber + 1).split(QL1C(',')); parsedLine = parsedLine.left(optionsNumber); } - if(!isRegExpRule) + if (!isRegExpRule) parsedLine = convertPatternToRegExp(parsedLine); - + m_regExp = QRegExp(parsedLine, Qt::CaseInsensitive, QRegExp::RegExp2); - if ( options.contains( QL1S("match-case") )) + if (options.contains(QL1S("match-case"))) { m_regExp.setCaseSensitivity(Qt::CaseSensitive); m_optionMatchRule = true; @@ -111,19 +105,19 @@ bool AdBlockRule::match(const QString &encodedUrl) const // TODO: Reimplement this in rekonq 0.5 :) // -// if (matched && !m_options.isEmpty()) +// if (matched && !m_options.isEmpty()) // { // // we only support domain right now // if (m_options.count() == 1) // { -// foreach (const QString &option, m_options) +// foreach (const QString &option, m_options) // { -// if (option.startsWith( QL1S("domain=") )) +// if (option.startsWith( QL1S("domain=") )) // { // QUrl url = QUrl::fromEncoded(encodedUrl.toUtf8()); // QString host = url.host(); // QStringList domainOptions = option.mid(7).split( QL1C('|') ); -// foreach (QString domainOption, domainOptions) +// foreach (QString domainOption, domainOptions) // { // bool negate = domainOption.at(0) == QL1C('~'); // if (negate) @@ -147,37 +141,43 @@ bool AdBlockRule::match(const QString &encodedUrl) const QString AdBlockRule::convertPatternToRegExp(const QString &wildcardPattern) { QString pattern = wildcardPattern; - + // remove multiple wildcards - pattern.replace(QRegExp( QL1S("\\*+") ), QL1S("*") ); - + pattern.replace(QRegExp(QL1S("\\*+")), QL1S("*")); + // remove anchors following separator placeholder - pattern.replace(QRegExp( QL1S("\\^\\|$") ), QL1S("^") ); - + pattern.replace(QRegExp(QL1S("\\^\\|$")), QL1S("^")); + // remove leading wildcards - pattern.replace(QRegExp( QL1S("^(\\*)") ), QL1S("") ); - + pattern.replace(QRegExp(QL1S("^(\\*)")), QL1S("")); + // remove trailing wildcards - pattern.replace(QRegExp( QL1S("(\\*)$") ), QL1S("") ); - + pattern.replace(QRegExp(QL1S("(\\*)$")), QL1S("")); + // escape special symbols - pattern.replace(QRegExp( QL1S("(\\W)") ), QL1S("\\\\1") ); - + pattern.replace(QRegExp(QL1S("(\\W)")), QL1S("\\\\1")); + // process extended anchor at expression start - pattern.replace(QRegExp( QL1S("^\\\\\\|\\\\\\|") ), QL1S("^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?") ); - + pattern.replace(QRegExp(QL1S("^\\\\\\|\\\\\\|")), QL1S("^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?")); + // process separator placeholders - pattern.replace(QRegExp( QL1S("\\\\\\^") ), QL1S("(?:[^\\w\\d\\-.%]|$)") ); - + pattern.replace(QRegExp(QL1S("\\\\\\^")), QL1S("(?:[^\\w\\d\\-.%]|$)")); + // process anchor at expression start - pattern.replace(QRegExp( QL1S("^\\\\\\|") ), QL1S("^") ); - + pattern.replace(QRegExp(QL1S("^\\\\\\|")), QL1S("^")); + // process anchor at expression end - pattern.replace(QRegExp( QL1S("\\\\\\|$") ), QL1S("$") ); - + pattern.replace(QRegExp(QL1S("\\\\\\|$")), QL1S("$")); + // replace wildcards by .* - pattern.replace(QRegExp( QL1S("\\\\\\*") ), QL1S(".*") ); + pattern.replace(QRegExp(QL1S("\\\\\\*")), QL1S(".*")); // Finally, return... return pattern; } + + +QString AdBlockRule::pattern() const +{ + return m_regExp.pattern(); +} |