1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-24 12:46:35 +01:00

AdBlock: Add support for rules with empty filter (match all)

This commit is contained in:
David Rosca 2017-08-12 16:25:45 +02:00
parent 67c6834a5f
commit c2c49c6b13
3 changed files with 34 additions and 16 deletions

View File

@ -1,6 +1,6 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
* QupZilla - Qt web browser
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
*
* 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
@ -201,7 +201,6 @@ bool AdBlockRule::urlMatch(const QUrl &url) const
return false;
}
const QString encodedUrl = url.toEncoded();
const QString domain = url.host();
@ -538,6 +537,18 @@ void AdBlockRule::parseFilter()
return;
}
// This rule matches all urls
if (parsedLine.isEmpty()) {
if (m_options == NoOption) {
qWarning() << "Disabling unrestricted rule that would block all requests" << m_filter;
m_isInternalDisabled = true;
m_type = Invalid;
return;
}
m_type = MatchAllUrlsRule;
return;
}
// We haven't found anything that needs use of regexp, yay!
m_type = StringContainsMatchRule;
m_matchString = parsedLine;
@ -671,23 +682,28 @@ QList<QStringMatcher> AdBlockRule::createStringMatchers(const QStringList &filte
bool AdBlockRule::stringMatch(const QString &domain, const QString &encodedUrl) const
{
if (m_type == StringContainsMatchRule) {
switch (m_type) {
case StringContainsMatchRule:
return encodedUrl.contains(m_matchString, m_caseSensitivity);
}
else if (m_type == DomainMatchRule) {
case DomainMatchRule:
return isMatchingDomain(domain, m_matchString);
}
else if (m_type == StringEndsMatchRule) {
case StringEndsMatchRule:
return encodedUrl.endsWith(m_matchString, m_caseSensitivity);
}
else if (m_type == RegExpMatchRule) {
case RegExpMatchRule:
if (!isMatchingRegExpStrings(encodedUrl)) {
return false;
}
return (m_regExp->regExp.indexIn(encodedUrl) != -1);
}
return false;
case MatchAllUrlsRule:
return true;
default:
return false;
}
}
bool AdBlockRule::isMatchingDomain(const QString &domain, const QString &filter) const

View File

@ -1,6 +1,6 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
* QupZilla - Qt web browser
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
*
* 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
@ -116,10 +116,12 @@ private:
RegExpMatchRule = 2,
StringEndsMatchRule = 3,
StringContainsMatchRule = 4,
Invalid = 5
MatchAllUrlsRule = 5,
Invalid = 6
};
enum RuleOption {
NoOption = 0,
DomainRestrictedOption = 1,
ThirdPartyOption = 2,
ObjectOption = 4,

View File

@ -46,7 +46,7 @@ bool AdBlockSearchTree::add(const AdBlockRule* rule)
int len = filter.size();
if (len <= 0) {
qDebug() << "AdBlockSearchTree: Inserting rule with filter len <= 0!";
qDebug() << "AdBlockSearchTree: Inserting rule with filter len <= 0!" << rule->filter();
return false;
}