OLD | NEW |
(Empty) | |
| 1 #include "ElemHideBase.h" |
| 2 #include "ElemHideFilter.h" |
| 3 #include "ElemHideException.h" |
| 4 #include "StringScanner.h" |
| 5 |
| 6 ElemHideBase::ElemHideBase(const std::u16string& text, |
| 7 const std::u16string& domains, const std::u16string& selector) |
| 8 : ActiveFilter(text, false), selector(selector) |
| 9 { |
| 10 if (!domains.empty()) |
| 11 ParseDomains(domains, u','); |
| 12 } |
| 13 |
| 14 Filter::Type ElemHideBase::Parse(const std::u16string& text, size_t* domainsEnd, |
| 15 size_t* selectorStart) |
| 16 { |
| 17 StringScanner scanner(text); |
| 18 |
| 19 // Domains part |
| 20 loop: |
| 21 while (!scanner.done()) |
| 22 { |
| 23 char16_t next = scanner.next(); |
| 24 if (next == u'#') |
| 25 { |
| 26 *domainsEnd = scanner.position(); |
| 27 break; |
| 28 } |
| 29 |
| 30 switch (next) |
| 31 { |
| 32 case u'/': |
| 33 case u'*': |
| 34 case u'|': |
| 35 case u'@': |
| 36 case u'"': |
| 37 case u'!': |
| 38 return Type::UNKNOWN; |
| 39 } |
| 40 } |
| 41 |
| 42 bool exception = false; |
| 43 char16_t next = scanner.next(); |
| 44 if (next == u'@') |
| 45 { |
| 46 exception = true; |
| 47 next = scanner.next(); |
| 48 } |
| 49 |
| 50 if (next != u'#') |
| 51 return Type::UNKNOWN; |
| 52 |
| 53 // Selector part |
| 54 |
| 55 // Selector shouldn't be empty |
| 56 if (scanner.done()) |
| 57 return Type::UNKNOWN; |
| 58 |
| 59 *selectorStart = scanner.position() + 1; |
| 60 while (!scanner.done()) |
| 61 { |
| 62 switch (scanner.next()) |
| 63 { |
| 64 case u'{': |
| 65 case u'}': |
| 66 return Type::UNKNOWN; |
| 67 } |
| 68 } |
| 69 |
| 70 return exception ? Type::ELEMHIDEEXCEPTION : Type::ELEMHIDE; |
| 71 } |
| 72 |
| 73 ElemHideBase* ElemHideBase::Create(const std::u16string& text) |
| 74 { |
| 75 size_t domainsEnd; |
| 76 size_t selectorStart; |
| 77 Type type = Parse(text, &domainsEnd, &selectorStart); |
| 78 if (type == Type::UNKNOWN) |
| 79 return nullptr; |
| 80 |
| 81 std::u16string domains(text.substr(0, domainsEnd)); |
| 82 std::u16string selector(text.substr(selectorStart)); |
| 83 if (type == Type::ELEMHIDEEXCEPTION) |
| 84 return new ElemHideException(text, domains, selector); |
| 85 else |
| 86 return new ElemHideFilter(text, domains, selector); |
| 87 } |
| 88 |
| 89 const std::u16string ElemHideBase::GetSelectorDomain() const |
| 90 { |
| 91 std::u16string result; |
| 92 for (auto it = domains.begin(); it != domains.end(); ++it) |
| 93 { |
| 94 if (it->second && !it->first.empty()) |
| 95 { |
| 96 if (!result.empty()) |
| 97 result.append(u","); |
| 98 result.append(it->first); |
| 99 } |
| 100 } |
| 101 return result; |
| 102 } |
OLD | NEW |