| Index: compiled/ElemHideBase.cpp |
| =================================================================== |
| new file mode 100644 |
| --- /dev/null |
| +++ b/compiled/ElemHideBase.cpp |
| @@ -0,0 +1,102 @@ |
| +#include "ElemHideBase.h" |
| +#include "ElemHideFilter.h" |
| +#include "ElemHideException.h" |
| +#include "StringScanner.h" |
| + |
| +ElemHideBase::ElemHideBase(const std::u16string& text, |
| + const std::u16string& domains, const std::u16string& selector) |
| + : ActiveFilter(text, false), selector(selector) |
| +{ |
| + if (!domains.empty()) |
| + ParseDomains(domains, u','); |
| +} |
| + |
| +Filter::Type ElemHideBase::Parse(const std::u16string& text, size_t* domainsEnd, |
| + size_t* selectorStart) |
| +{ |
| + StringScanner scanner(text); |
| + |
| + // Domains part |
| + loop: |
| + while (!scanner.done()) |
| + { |
| + char16_t next = scanner.next(); |
| + if (next == u'#') |
| + { |
| + *domainsEnd = scanner.position(); |
| + break; |
| + } |
| + |
| + switch (next) |
| + { |
| + case u'/': |
| + case u'*': |
| + case u'|': |
| + case u'@': |
| + case u'"': |
| + case u'!': |
| + return Type::UNKNOWN; |
| + } |
| + } |
| + |
| + bool exception = false; |
| + char16_t next = scanner.next(); |
| + if (next == u'@') |
| + { |
| + exception = true; |
| + next = scanner.next(); |
| + } |
| + |
| + if (next != u'#') |
| + return Type::UNKNOWN; |
| + |
| + // Selector part |
| + |
| + // Selector shouldn't be empty |
| + if (scanner.done()) |
| + return Type::UNKNOWN; |
| + |
| + *selectorStart = scanner.position() + 1; |
| + while (!scanner.done()) |
| + { |
| + switch (scanner.next()) |
| + { |
| + case u'{': |
| + case u'}': |
| + return Type::UNKNOWN; |
| + } |
| + } |
| + |
| + return exception ? Type::ELEMHIDEEXCEPTION : Type::ELEMHIDE; |
| +} |
| + |
| +ElemHideBase* ElemHideBase::Create(const std::u16string& text) |
| +{ |
| + size_t domainsEnd; |
| + size_t selectorStart; |
| + Type type = Parse(text, &domainsEnd, &selectorStart); |
| + if (type == Type::UNKNOWN) |
| + return nullptr; |
| + |
| + std::u16string domains(text.substr(0, domainsEnd)); |
| + std::u16string selector(text.substr(selectorStart)); |
| + if (type == Type::ELEMHIDEEXCEPTION) |
| + return new ElemHideException(text, domains, selector); |
| + else |
| + return new ElemHideFilter(text, domains, selector); |
| +} |
| + |
| +const std::u16string ElemHideBase::GetSelectorDomain() const |
| +{ |
| + std::u16string result; |
| + for (auto it = domains.begin(); it != domains.end(); ++it) |
| + { |
| + if (it->second && !it->first.empty()) |
| + { |
| + if (!result.empty()) |
| + result.append(u","); |
| + result.append(it->first); |
| + } |
| + } |
| + return result; |
| +} |