aboutsummaryrefslogtreecommitdiff
path: root/lib/urlfilter/formats/adblockrule.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/urlfilter/formats/adblockrule.cpp')
-rw-r--r--lib/urlfilter/formats/adblockrule.cpp136
1 files changed, 68 insertions, 68 deletions
diff --git a/lib/urlfilter/formats/adblockrule.cpp b/lib/urlfilter/formats/adblockrule.cpp
index c5d6b58..79a6dc8 100644
--- a/lib/urlfilter/formats/adblockrule.cpp
+++ b/lib/urlfilter/formats/adblockrule.cpp
@@ -16,30 +16,28 @@
// QString::chop(len) - Removes n characters from the end of the string.
// QString::remove(pos, len) - Removes n characters from the string, starting at the given position index.
-AdBlockRule::AdBlockRule(const QString &filter)
+AdBlockRule *loadRule(const QString &filter)
{
QString parsedLine = filter.trimmed();
// there is no rule, or it's a comment
if(parsedLine.isEmpty() || parsedLine.startsWith("!")) {
- return;
+ return nullptr;
}
- // css rule - ignore for now
+ // css rule -> filterleaves cannot do element blocking
if(parsedLine.contains(QLatin1Literal("##")) || parsedLine.contains(QLatin1Literal("#@#"))) {
- return;
+ return nullptr;
}
- m_isEnabled = true;
-
// exception rules
- if(parsedLine.startsWith(QLatin1Literal("@@"))) {
- m_isBlocking = false;
+ const bool isBlocking = parsedLine.startsWith(QLatin1Literal("@@"));
+ if(isBlocking)
parsedLine.remove(0, 2);
- } else
- m_isBlocking = true;
// parse options
+ QStringList enabledOn, disabledOn;
+ QHash<QWebEngineUrlRequestInfo::ResourceType, bool> optionsHash;
{
const int sepPos = parsedLine.indexOf(QLatin1Literal("$"));
if(sepPos != -1) {
@@ -49,102 +47,102 @@ AdBlockRule::AdBlockRule(const QString &filter)
for(const QString &option : options) {
if(option.startsWith(QLatin1Literal("domain"))) {
const auto domainList = option.mid(7).split(QLatin1Literal("|"));
+
for(const QString &domain : domainList) {
if(domain.startsWith(QLatin1Literal("~"))) {
- blockedDomains_hashes.append(qHash(domain.mid(1)));
+ disabledOn.append(domain.mid(1));
} else {
- allowedDomains_hashes.append(qHash(domain));
+ enabledOn.append(domain);
}
}
} else {
- parseOption(option);
+ const auto pair = parseOption(option);
+ if(pair)
+ optionsHash.insert(pair.value().first, pair.value().second);
}
}
}
}
- // regular expression rule
- if(parsedLine.startsWith(QLatin1Literal("/")) && parsedLine.endsWith(QLatin1Literal("/"))) {
- parsedLine = parsedLine.mid(1, parsedLine.length() - 2);
-
- urlMatchType = RegularExpressionMatch;
- regexp.setPattern(parsedLine);
- return;
- }
-
- // string equals rule
- if(parsedLine.startsWith(QLatin1Literal("|")) && parsedLine.endsWith(QLatin1Literal("|"))) {
- urlMatchType = StringEquals;
- match = parsedLine.mid(1, parsedLine.length() - 2);
- return;
- }
-
- // Basic filter rules can use wildcards, which were supported by QRegExp,
- // but were deprecated in QRegularExpression.
-
- // remove beginning and ending wildcards
- if(parsedLine.startsWith(QLatin1Literal("*")))
- parsedLine = parsedLine.mid(1);
+ FilterLeaf::UrlMatchType matchType;
+ QString pattern;
- if(parsedLine.endsWith(QLatin1Literal("*")))
- parsedLine.chop(1);
-
- if(parsedLine.startsWith(QLatin1Literal("||")) && parsedLine.endsWith(QLatin1Literal("^"))) {
- urlMatchType = DomainMatch;
- match = parsedLine.mid(2, parsedLine.length() - 3);
- return;
- }
-
- // check for wildcards and translate to regexp
- // wildcard "*" - any number of characters
- // 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);
- return;
+ if(parsedLine.startsWith(QLatin1Literal("/")) && parsedLine.endsWith(QLatin1Literal("/"))) {
+ // regular expression rule
+ matchType = FilterLeaf::RegularExpressionMatch;
+ pattern = parsedLine.mid(1, parsedLine.length() - 2);
+
+ } else if(parsedLine.startsWith(QLatin1Literal("|")) && parsedLine.endsWith(QLatin1Literal("|"))) {
+ // string equals rule
+ matchType = FilterLeaf::StringEquals;
+ pattern = parsedLine.mid(1, parsedLine.length() - 2);
+
+ } else {
+
+ // Basic filter rules can use wildcards, which were supported by QRegExp,
+ // but were deprecated in QRegularExpression.
+
+ // remove beginning and ending wildcards
+ if(parsedLine.startsWith(QLatin1Literal("*")))
+ parsedLine = parsedLine.mid(1);
+
+ if(parsedLine.endsWith(QLatin1Literal("*")))
+ parsedLine.chop(1);
+
+ if(parsedLine.startsWith(QLatin1Literal("||")) && parsedLine.endsWith(QLatin1Literal("^"))) {
+ matchType = FilterLeaf::DomainMatch;
+ pattern = parsedLine.mid(2, parsedLine.length() - 3);
+
+ } else if(parsedLine.contains(QLatin1Literal("*")) || parsedLine.contains(QLatin1Literal("^"))) {
+ // check for wildcards and translate to regexp
+ // wildcard "*" - any number of characters
+ // separator "^" - end, ? or /
+ matchType = FilterLeaf::RegularExpressionMatch;
+ parsedLine.replace(QLatin1Literal("||"), QLatin1Literal("^\\w+://"));
+ parsedLine.replace(QLatin1Literal("|"), QLatin1Literal("\\|"));
+ parsedLine.replace(QLatin1Literal("*"), QLatin1Literal(".*"));
+ parsedLine.replace(QLatin1Literal("^"), QLatin1Literal("($|\\?|\\/)"));
+ pattern = parsedLine;
+ }
}
-
- match = parsedLine;
+ return nullptr;
}
-void AdBlockRule::parseOption(const QString &option)
+
+std::optional<QPair<QWebEngineUrlRequestInfo::ResourceType, bool>> parseOption(const QString &option)
{
const bool exception = !option.startsWith(QLatin1Literal("~"));
if(option.endsWith(QLatin1Literal("script"))) {
// external scripts loaded via HTML script tag
- m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeScript, exception);
+ return qMakePair(QWebEngineUrlRequestInfo::ResourceTypeScript, exception);
} else if(option.endsWith(QLatin1Literal("image"))) {
// regular images, typically loaded via HTML img tag
- m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeImage, exception);
+ return qMakePair(QWebEngineUrlRequestInfo::ResourceTypeImage, exception);
} else if(option.endsWith(QLatin1Literal("stylesheet"))) {
// external CSS stylesheet files
- m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeStylesheet, exception);
+ return qMakePair(QWebEngineUrlRequestInfo::ResourceTypeStylesheet, exception);
} else if(option.endsWith(QLatin1Literal("object"))) {
// content handled by browser plugins, e.g. Flash or Java
- m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeObject, exception);
+ return qMakePair(QWebEngineUrlRequestInfo::ResourceTypeObject, exception);
} else if(option.endsWith(QLatin1Literal("xmlhttprequest"))) {
// requests started using the XMLHttpRequest object or fetch() API
- m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeXhr, exception);
+ return qMakePair(QWebEngineUrlRequestInfo::ResourceTypeXhr, exception);
} else if(option.endsWith(QLatin1Literal("object-subrequest"))) {
// requests started by plugins like Flash
- m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypePluginResource, exception);
+ return qMakePair(QWebEngineUrlRequestInfo::ResourceTypePluginResource, exception);
} else if(option.endsWith(QLatin1Literal("subdocument"))) {
// embedded pages, usually included via HTML frames
- m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeSubFrame, exception);
+ return qMakePair(QWebEngineUrlRequestInfo::ResourceTypeSubFrame, exception);
} else if(option.endsWith(QLatin1Literal("ping"))) {
// requests started by <a ping> or navigator.sendBeacon()
- m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypePing, exception);
+ return qMakePair(QWebEngineUrlRequestInfo::ResourceTypePing, exception);
} else if(option.endsWith(QLatin1Literal("websocket"))) {
// requests initiated via WebSocket object
@@ -156,9 +154,11 @@ void AdBlockRule::parseOption(const QString &option)
} else if(option.endsWith(QLatin1Literal("document"))) {
// the page itself
- m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeMainFrame, exception);
+ return qMakePair(QWebEngineUrlRequestInfo::ResourceTypeMainFrame, exception);
} else if(option.endsWith(QLatin1Literal("other"))) {
- m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeUnknown, exception);
+ return qMakePair(QWebEngineUrlRequestInfo::ResourceTypeUnknown, exception);
}
+
+ return std::nullopt;
}