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.cpp70
1 files changed, 35 insertions, 35 deletions
diff --git a/lib/web/urlfilter/adblockrule.cpp b/lib/web/urlfilter/adblockrule.cpp
index 60262b7..58b1941 100644
--- a/lib/web/urlfilter/adblockrule.cpp
+++ b/lib/web/urlfilter/adblockrule.cpp
@@ -1,30 +1,13 @@
-#include "adblockrule.h"
-
-inline std::pair<QWebEngineUrlRequestInfo::ResourceType, bool> parseOption(const QString &option)
-{
- if(option.endsWith(QLatin1Literal("script"))) {
- return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeScript, !option.startsWith(QLatin1Literal("~")));
-
- } else if(option.endsWith(QLatin1Literal("image"))) {
- return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeImage, !option.startsWith(QLatin1Literal("~")));
-
- } else if(option.endsWith(QLatin1Literal("stylesheet"))) {
- return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeStylesheet, !option.startsWith(QLatin1Literal("~")));
+/*
+ * This file is part of smolbote. It's copyrighted by the contributors recorded
+ * in the version control history of the file, available from its original
+ * location: https://neueland.iserlohn-fortress.net/smolbote.hg
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+// Based on Falkon's AdBlockRule class
- } else if(option.endsWith(QLatin1Literal("object"))) {
- return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeObject, !option.startsWith(QLatin1Literal("~")));
-
- } else if(option.endsWith(QLatin1Literal("xmlhttprequest"))) {
- return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeXhr, !option.startsWith(QLatin1Literal("~")));
-
- } else if(option.endsWith(QLatin1Literal("other"))) {
- return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeUnknown, !option.startsWith(QLatin1Literal("~")));
-
- } else {
- // unhandled pair
- Q_ASSERT(false);
- }
-}
+#include "adblockrule.h"
// adblock format documentation
// https://adblockplus.org/filters
@@ -35,9 +18,10 @@ inline std::pair<QWebEngineUrlRequestInfo::ResourceType, bool> parseOption(const
AdBlockRule::AdBlockRule(const QString &filter)
{
+ originalFilter = filter;
QString parsedLine = filter.trimmed();
- // there is no rule, or it"s a comment
+ // there is no rule, or it's a comment
if(parsedLine.isEmpty() || parsedLine.startsWith("!")) {
return;
}
@@ -51,9 +35,10 @@ AdBlockRule::AdBlockRule(const QString &filter)
// exception rules
if(parsedLine.startsWith(QLatin1Literal("@@"))) {
- m_isException = true;
+ m_isBlocking = false;
parsedLine.remove(0, 2);
- }
+ } else
+ m_isBlocking = true;
// parse options
{
@@ -71,16 +56,29 @@ AdBlockRule::AdBlockRule(const QString &filter)
else
allowedDomains.append(domain);
}
- } else {
- auto optPair = parseOption(option);
- m_resourceTypeOptions.insert(optPair.first, optPair.second);
+ } else if(option.endsWith(QLatin1Literal("script"))) {
+ m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeScript, !option.startsWith(QLatin1Literal("~")));
+
+ } else if(option.endsWith(QLatin1Literal("image"))) {
+ m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeImage, !option.startsWith(QLatin1Literal("~")));
+
+ } else if(option.endsWith(QLatin1Literal("stylesheet"))) {
+ m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeStylesheet, !option.startsWith(QLatin1Literal("~")));
+
+ } else if(option.endsWith(QLatin1Literal("object"))) {
+ m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeObject, !option.startsWith(QLatin1Literal("~")));
+
+ } else if(option.endsWith(QLatin1Literal("xmlhttprequest"))) {
+ m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeXhr, !option.startsWith(QLatin1Literal("~")));
+
+ } else if(option.endsWith(QLatin1Literal("other"))) {
+ m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeUnknown, !option.startsWith(QLatin1Literal("~")));
}
}
-
}
}
- // regular expression rules
+ // regular expression rule
if(parsedLine.startsWith(QLatin1Literal("/")) && parsedLine.endsWith(QLatin1Literal("/"))) {
parsedLine = parsedLine.mid(1, parsedLine.length() - 2);
@@ -89,7 +87,7 @@ AdBlockRule::AdBlockRule(const QString &filter)
return;
}
- // basic filter rules
+ // string equals rule
if(parsedLine.startsWith(QLatin1Literal("|")) && parsedLine.endsWith(QLatin1Literal("|"))) {
urlMatchType = StringEquals;
match = parsedLine.mid(1, parsedLine.length() - 2);
@@ -117,6 +115,8 @@ AdBlockRule::AdBlockRule(const QString &filter)
// separator "^" - end, ? or /
if(parsedLine.contains(QLatin1Literal("*")) || parsedLine.contains(QLatin1Literal("^"))) {
urlMatchType = RegularExpressionMatch;
+ parsedLine.replace(QLatin1Literal("||"), QLatin1Literal("^\\w+://"));
+ parsedLine.replace(QLatin1Literal("|"), QLatin1Literal("\\|"));
parsedLine.replace(QLatin1Literal("*"), QLatin1Literal(".*"));
parsedLine.replace(QLatin1Literal("^"), QLatin1Literal("($|\\?|\\/)"));
regexp.setPattern(parsedLine);