aboutsummaryrefslogtreecommitdiff
path: root/staging/adblock/rule.h
diff options
context:
space:
mode:
Diffstat (limited to 'staging/adblock/rule.h')
-rw-r--r--staging/adblock/rule.h92
1 files changed, 73 insertions, 19 deletions
diff --git a/staging/adblock/rule.h b/staging/adblock/rule.h
index a9a9592..03970dc 100644
--- a/staging/adblock/rule.h
+++ b/staging/adblock/rule.h
@@ -6,48 +6,102 @@
* SPDX-License-Identifier: GPL-3.0
*/
-#include "options.h"
-#include <QObject>
-#include <QString>
-
#ifndef SMOLBOTE_ADBLOCK_RULE_H
#define SMOLBOTE_ADBLOCK_RULE_H
-class QStringMatcher;
-class QRegularExpression;
+#include "options.h"
+#include <QObject>
+#include <QRegularExpression>
+#include <QString>
+#include <QStringMatcher>
namespace AdblockPlus
{
class Rule
{
public:
- // virtual bool hasMatch(const QString &url) const = 0;
+ virtual ~Rule() = default;
+ virtual bool hasMatch(const QStringRef &) const = 0;
+ bool shouldRedirect() const
+ {
+ return options.redirect;
+ }
+ bool shouldBlock() const
+ {
+ return !options.exception;
+ }
+
+protected:
+ Rule(const Options &opt)
+ : options(opt)
+ {
+ }
+ const Options options;
};
class MatcherRule : public Rule
{
- Q_DISABLE_COPY(MatcherRule)
-
public:
- MatcherRule(const QString &rule, const Options &opt);
- ~MatcherRule();
+ enum MatchPosition {
+ UrlStartsWith,
+ UrlEndsWith,
+ UrlContains
+ };
+
+ MatcherRule(const QString &pattern, const Options &opt, const MatchPosition pos = UrlContains)
+ : Rule(opt)
+ , position(pos)
+ , matcher(pattern, opt.matchcase ? Qt::CaseSensitive : Qt::CaseInsensitive)
+ , patternLength(pattern.length())
+ {
+ }
+ explicit MatcherRule(const MatcherRule &) = delete;
+ MatcherRule &operator=(const MatcherRule &) = delete;
+
+ ~MatcherRule() = default;
+ bool hasMatch(const QStringRef &url) const override
+ {
+ const auto index = matcher.indexIn(url);
+
+ switch(position) {
+ case UrlStartsWith:
+ return (index == 0);
+ case UrlEndsWith:
+ return (index == url.length() - patternLength);
+ case UrlContains:
+ return (index != -1);
+ }
+ }
private:
- Options options;
- QStringMatcher *matcher;
+ const MatchPosition position;
+ const QStringMatcher matcher;
+ const int patternLength;
};
class RegexRule : public Rule
{
- Q_DISABLE_COPY(RegexRule)
-
public:
- RegexRule(const QString &rule, const Options &opt);
- ~RegexRule();
+ RegexRule(const QString &rule, const Options &opt)
+ : Rule(opt)
+ , regex(rule)
+ {
+ if(!opt.matchcase) {
+ regex.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
+ }
+ }
+ explicit RegexRule(const RegexRule &) = delete;
+ RegexRule &operator=(const RegexRule &) = delete;
+
+ ~RegexRule() = default;
+ bool hasMatch(const QStringRef &url) const override
+ {
+ const auto match = regex.match(url);
+ return match.hasMatch();
+ }
private:
- Options options;
- QRegularExpression *regex;
+ QRegularExpression regex;
};
} // namespace AdblockPlus