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