From 3284722993773ab7d9a54bfcef36dab0c804f83d Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 3 Feb 2017 14:24:29 +0100 Subject: Listing rules in Blocker dialog Slightly better rule parsing --- src/webengine/blockerrule.cpp | 82 +++++++++++++++++++++++++++++++++++++++---- src/webengine/blockerrule.h | 17 +++++++-- 2 files changed, 91 insertions(+), 8 deletions(-) (limited to 'src/webengine') diff --git a/src/webengine/blockerrule.cpp b/src/webengine/blockerrule.cpp index 3a35ab6..3146854 100644 --- a/src/webengine/blockerrule.cpp +++ b/src/webengine/blockerrule.cpp @@ -20,20 +20,55 @@ #include "blockerrule.h" +/* AdBlock filter reference + * https://adblockplus.org/en/filters + * https://adblockplus.org/en/filter-cheatsheet + */ + BlockerRule::BlockerRule(QString rule, QObject *parent) : QObject(parent) { QString pattern = rule; - if(rule.startsWith("@@")) { + // Empty rule or comment + if(pattern.trimmed().isEmpty() || pattern.startsWith("!")) { + m_type = RuleType::Invalid; + return; + } + + // Exception + if(pattern.startsWith("@@")) { m_exception = true; pattern = pattern.remove(0, 2); - } else { - m_exception = false; } - ruleExpression.setPattern(pattern); - m_valid = true; + // Domain + if(pattern.startsWith("||")) { + m_type = RuleType::DomainMatch; + m_domain = pattern.mid(2, pattern.indexOf("^")-2); + return; + } + + // Regular expression + if(rule.startsWith("/") && rule.endsWith("/")) { + m_type = RuleType::RegularExpressionMatch; + ruleExpression.setPattern(pattern); + return; + } + + // Regular rule + ruleExpression.setPattern(fromWildcardMatch(pattern)); + m_type = RuleType::WildcardMatch; +} + +QString BlockerRule::pattern() const +{ + return ruleExpression.pattern(); +} + +QString BlockerRule::domain() const +{ + return m_domain; } bool BlockerRule::match(const QUrl &url) @@ -43,9 +78,44 @@ bool BlockerRule::match(const QUrl &url) bool BlockerRule::isValid() { - return m_valid; + if(m_type == RuleType::Invalid) { + return false; + } else { + return true; + } } bool BlockerRule::isException() { return m_exception; } + +QString BlockerRule::fromWildcardMatch(const QString &pattern) +{ + QString parsed; + + for(int i=0; i