#include "adblockrule.h" inline std::pair 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("~"))); } 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); } } // adblock format documentation // https://adblockplus.org/filters // QString::mid(pos, len) - Returns a string starting at the specified position index. // 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) { QString parsedLine = filter.trimmed(); // there is no rule, or it"s a comment if(parsedLine.isEmpty() || parsedLine.startsWith("!")) { return; } // css rule - ignore for now if(parsedLine.contains(QLatin1Literal("##")) || parsedLine.contains(QLatin1Literal("#@#"))) { return; } m_isEnabled = true; // exception rules if(parsedLine.startsWith(QLatin1Literal("@@"))) { m_isException = true; parsedLine.remove(0, 2); } // parse options { const int sepPos = parsedLine.indexOf(QLatin1Literal("$")); if(sepPos != -1) { const auto options = parsedLine.mid(sepPos + 1).split(QLatin1Literal(",")); parsedLine = parsedLine.mid(0, sepPos); 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.append(domain.mid(1)); else allowedDomains.append(domain); } } else { auto optPair = parseOption(option); m_resourceTypeOptions.insert(optPair.first, optPair.second); } } } } // regular expression rules if(parsedLine.startsWith(QLatin1Literal("/")) && parsedLine.endsWith(QLatin1Literal("/"))) { parsedLine = parsedLine.mid(1, parsedLine.length() - 2); urlMatchType = RegularExpressionMatch; regexp.setPattern(parsedLine); return; } // basic filter rules 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); 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(".*")); parsedLine.replace(QLatin1Literal("^"), QLatin1Literal("($|\\?|\\/)")); regexp.setPattern(parsedLine); return; } match = parsedLine; }