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