| LEFT | RIGHT |
| 1 #include <unordered_map> | |
| 2 | |
| 3 #include "Filter.h" | 1 #include "Filter.h" |
| 4 #include "CommentFilter.h" | 2 #include "CommentFilter.h" |
| 3 #include "InvalidFilter.h" |
| 5 #include "RegExpFilter.h" | 4 #include "RegExpFilter.h" |
| 5 #include "BlockingFilter.h" |
| 6 #include "WhitelistFilter.h" |
| 7 #include "ElemHideBase.h" |
| 6 #include "ElemHideFilter.h" | 8 #include "ElemHideFilter.h" |
| 7 #include "ElemHideException.h" | 9 #include "ElemHideException.h" |
| 10 #include "CSSPropertyFilter.h" |
| 11 #include "StringMap.h" |
| 8 | 12 |
| 9 namespace | 13 namespace |
| 10 { | 14 { |
| 11 std::unordered_map<std::u16string,FilterPtr> knownFilters; | 15 StringMap<Filter*> knownFilters(8192); |
| 12 | 16 |
| 13 void trim_spaces(std::u16string& str) | 17 void NormalizeWhitespace(DependentString& text) |
| 14 { | 18 { |
| 15 size_t pos; | 19 String::size_type start = 0; |
| 20 String::size_type end = text.length(); |
| 16 | 21 |
| 17 // Remove leading whitespace | 22 // Remove leading spaces and special characters like line breaks |
| 18 pos = str.find_first_not_of(u' '); | 23 for (; start < end; start++) |
| 19 if (pos > 0) | 24 if (text[start] > ' ') |
| 20 str.erase(0, pos); | 25 break; |
| 21 | 26 |
| 22 // Remove trailing whitespace | 27 // Now look for invalid characters inside the string |
| 23 pos = str.find_last_not_of(u' '); | 28 String::size_type pos; |
| 24 if (pos < str.length() - 1) | 29 for (pos = start; pos < end; pos++) |
| 25 str.erase(pos + 1); | 30 if (text[pos] < ' ') |
| 26 } | 31 break; |
| 27 | 32 |
| 28 void remove_spaces(std::u16string& str) | 33 if (pos < end) |
| 29 { | |
| 30 for (size_t i = 0, l = str.length(); i < l; ++i) | |
| 31 { | 34 { |
| 32 if (str[i] == u' ') | 35 // Found invalid characters, copy all the valid characters while skipping |
| 36 // the invalid ones. |
| 37 String::size_type delta = 1; |
| 38 for (pos = pos + 1; pos < end; pos++) |
| 33 { | 39 { |
| 34 str.erase(i, 1); | 40 if (text[pos] < ' ') |
| 35 --i; | 41 delta++; |
| 36 --l; | 42 else |
| 43 text[pos - delta] = text[pos]; |
| 37 } | 44 } |
| 45 end -= delta; |
| 38 } | 46 } |
| 47 |
| 48 // Remove trailing spaces |
| 49 for (; end > 0; end--) |
| 50 if (text[end - 1] != ' ') |
| 51 break; |
| 52 |
| 53 // Set new string boundaries |
| 54 text.reset(text, start, end - start); |
| 39 } | 55 } |
| 40 } | 56 } |
| 41 | 57 |
| 42 Filter::Filter(const std::u16string& text) | 58 Filter::Filter(Type type, const String& text) |
| 59 : mType(type), mText(text) |
| 43 { | 60 { |
| 44 this->text = text; | 61 annotate_address(this, "Filter"); |
| 45 } | 62 } |
| 46 | 63 |
| 47 const std::u16string Filter::Serialize() | 64 Filter::~Filter() |
| 48 { | 65 { |
| 49 return ( | 66 knownFilters.erase(mText); |
| 50 u"[Filter]\n" | |
| 51 u"text=" + text + u"\n" | |
| 52 ); | |
| 53 } | 67 } |
| 54 | 68 |
| 55 FilterPtr Filter::FromText(const std::u16string& text) | 69 OwnedString Filter::Serialize() const |
| 56 { | 70 { |
| 57 auto it = knownFilters.find(text); | 71 OwnedString result(u"[Filter]\ntext="_str); |
| 58 if (it != knownFilters.end()) | 72 result.append(mText); |
| 59 return it->second; | 73 result.append(u'\n'); |
| 60 | 74 return result; |
| 61 FilterPtr filter = FilterPtr(CommentFilter::Create(text)); | |
| 62 if (!filter) | |
| 63 filter.reset(ElemHideBase::Create(text)); | |
| 64 if (!filter) | |
| 65 filter.reset(RegExpFilter::Create(text)); | |
| 66 return knownFilters[text] = filter; | |
| 67 } | 75 } |
| 68 | 76 |
| 69 const std::u16string Filter::Normalize(const std::u16string& text) | 77 Filter* Filter::FromText(DependentString& text) |
| 70 { | 78 { |
| 71 std::u16string result(text); | 79 NormalizeWhitespace(text); |
| 80 if (text.empty()) |
| 81 return nullptr; |
| 72 | 82 |
| 73 // Remove special characters like line breaks | 83 // Parsing also normalizes the filter text, so it has to be done before the |
| 74 for (size_t i = 0, l = result.length(); i < l; ++i) | 84 // lookup in knownFilters. |
| 85 union |
| 75 { | 86 { |
| 76 if (result[i] < u' ') | 87 RegExpFilterData regexp; |
| 77 { | 88 ElemHideData elemhide; |
| 78 result.erase(i, 1); | 89 } data; |
| 79 --i; | 90 DependentString error; |
| 80 --l; | 91 |
| 81 } | 92 Filter::Type type = CommentFilter::Parse(text); |
| 93 if (type == Filter::Type::UNKNOWN) |
| 94 type = ElemHideBase::Parse(text, data.elemhide); |
| 95 if (type == Filter::Type::UNKNOWN) |
| 96 type = RegExpFilter::Parse(text, error, data.regexp); |
| 97 |
| 98 auto knownFilter = knownFilters.find(text); |
| 99 if (knownFilter) |
| 100 { |
| 101 knownFilter->second->AddRef(); |
| 102 return knownFilter->second; |
| 82 } | 103 } |
| 83 | 104 |
| 84 trim_spaces(result); | 105 FilterPtr filter; |
| 85 | 106 switch (type) |
| 86 { | 107 { |
| 87 size_t domainsEnd; | 108 case Filter::Type::COMMENT: |
| 88 size_t selectorStart; | 109 filter = new CommentFilter(text); |
| 89 Filter::Type type = ElemHideBase::Parse(result, &domainsEnd, &selectorStart)
; | 110 break; |
| 90 if (type != Filter::Type::UNKNOWN) | 111 case Filter::Type::INVALID: |
| 91 { | 112 filter = new InvalidFilter(text, error); |
| 92 std::u16string domains = result.substr(0, domainsEnd); | 113 break; |
| 93 std::u16string selector = result.substr(selectorStart); | 114 case Filter::Type::BLOCKING: |
| 94 remove_spaces(domains); | 115 filter = new BlockingFilter(text, data.regexp); |
| 95 trim_spaces(selector); | 116 break; |
| 96 return domains + ( | 117 case Filter::Type::WHITELIST: |
| 97 type == Filter::Type::ELEMHIDEEXCEPTION ? u"#@#" : u"##" | 118 filter = new WhitelistFilter(text, data.regexp); |
| 98 ) + selector; | 119 break; |
| 99 } | 120 case Filter::Type::ELEMHIDE: |
| 121 filter = new ElemHideFilter(text, data.elemhide); |
| 122 break; |
| 123 case Filter::Type::ELEMHIDEEXCEPTION: |
| 124 filter = new ElemHideException(text, data.elemhide); |
| 125 break; |
| 126 case Filter::Type::CSSPROPERTY: |
| 127 filter = new CSSPropertyFilter(text, data.elemhide); |
| 128 if (static_cast<CSSPropertyFilter*>(filter.get())->IsGeneric()) |
| 129 filter = new InvalidFilter(text, u"filter_cssproperty_nodomain"_str); |
| 130 break; |
| 131 default: |
| 132 // This should never happen but just in case |
| 133 return nullptr; |
| 100 } | 134 } |
| 101 | 135 |
| 102 if (CommentFilter::Parse(result) == Filter::Type::UNKNOWN) | 136 // This is a hack: we looked up the entry using text but create it using |
| 103 remove_spaces(result); | 137 // filter->mText. This works because both are equal at this point. However, |
| 104 return result; | 138 // text refers to a temporary buffer which will go away. |
| 139 enter_context("Adding to known filters"); |
| 140 knownFilter.assign(filter->mText, filter.get()); |
| 141 exit_context(); |
| 142 |
| 143 return filter.release(); |
| 105 } | 144 } |
| LEFT | RIGHT |