aboutsummaryrefslogtreecommitdiff
path: root/lib/web/urlfilter/adblockrule.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/web/urlfilter/adblockrule.cpp')
-rw-r--r--lib/web/urlfilter/adblockrule.cpp68
1 files changed, 5 insertions, 63 deletions
diff --git a/lib/web/urlfilter/adblockrule.cpp b/lib/web/urlfilter/adblockrule.cpp
index b20ed8f..60262b7 100644
--- a/lib/web/urlfilter/adblockrule.cpp
+++ b/lib/web/urlfilter/adblockrule.cpp
@@ -1,26 +1,5 @@
#include "adblockrule.h"
-bool isMatchingDomain(const QString &domain, const QString &filter)
-{
- // domain and filter are the same
- if(domain == filter) {
- return true;
- }
-
- // domain can't be matched by filter if it doesn't end with filter
- // ex. example2.com isn't matched by example.com
- if(!domain.endsWith(filter)) {
- return false;
- }
-
- // match with subdomains
- // ex. subdomain.example.com is matched by example.com
- int index = domain.indexOf(filter);
-
- // match if (domain ends with filter) && (filter has been found) and (character before filter is '.')
- return index > 0 && domain[index - 1] == QLatin1Char('.');
-}
-
inline std::pair<QWebEngineUrlRequestInfo::ResourceType, bool> parseOption(const QString &option)
{
if(option.endsWith(QLatin1Literal("script"))) {
@@ -105,14 +84,14 @@ AdBlockRule::AdBlockRule(const QString &filter)
if(parsedLine.startsWith(QLatin1Literal("/")) && parsedLine.endsWith(QLatin1Literal("/"))) {
parsedLine = parsedLine.mid(1, parsedLine.length() - 2);
- matchType = RegularExpressionMatch;
+ urlMatchType = RegularExpressionMatch;
regexp.setPattern(parsedLine);
return;
}
// basic filter rules
if(parsedLine.startsWith(QLatin1Literal("|")) && parsedLine.endsWith(QLatin1Literal("|"))) {
- matchType = StringEquals;
+ urlMatchType = StringEquals;
match = parsedLine.mid(1, parsedLine.length() - 2);
return;
}
@@ -128,7 +107,7 @@ AdBlockRule::AdBlockRule(const QString &filter)
parsedLine.chop(1);
if(parsedLine.startsWith(QLatin1Literal("||")) && parsedLine.endsWith(QLatin1Literal("^"))) {
- matchType = DomainMatch;
+ urlMatchType = DomainMatch;
match = parsedLine.mid(2, parsedLine.length() - 3);
return;
}
@@ -137,49 +116,12 @@ AdBlockRule::AdBlockRule(const QString &filter)
// wildcard "*" - any number of characters
// separator "^" - end, ? or /
if(parsedLine.contains(QLatin1Literal("*")) || parsedLine.contains(QLatin1Literal("^"))) {
- matchType = RegularExpressionMatch;
+ urlMatchType = RegularExpressionMatch;
parsedLine.replace(QLatin1Literal("*"), QLatin1Literal(".*"));
parsedLine.replace(QLatin1Literal("^"), QLatin1Literal("($|\\?|\\/)"));
regexp.setPattern(parsedLine);
return;
}
- matcher.setPattern(parsedLine);
-}
-
-bool AdBlockRule::isEnabled() const
-{
- return m_isEnabled;
-}
-
-bool AdBlockRule::matchesType(QWebEngineUrlRequestInfo::ResourceType type) const
-{
- // no options have been specified -> match all resource types
- if(m_resourceTypeOptions.isEmpty())
- return true;
-
- // this resource type has not been specified -> reject it
- if(!m_resourceTypeOptions.contains(type))
- return false;
-
- // resource type has been specified; true to match, false to exception
- return m_resourceTypeOptions.value(type);
-}
-
-bool AdBlockRule::matchesUrl(const QUrl &url) const
-{
- switch (matchType) {
- case RegularExpressionMatch:
- if(regexp.match(url.toString()).hasMatch())
- return !m_isException;
-
- case StringEquals:
- return url.toString() == match;
-
- case DomainMatch:
- return isMatchingDomain(url.host(), match);
-
- default:
- return false;
- }
+ match = parsedLine;
}