OLD | NEW |
(Empty) | |
| 1 #include "Filter.h" |
| 2 #include "CommentFilter.h" |
| 3 #include "RegExpFilter.h" |
| 4 #include "ElemHideFilter.h" |
| 5 #include "ElemHideException.h" |
| 6 #include "StringMap.h" |
| 7 |
| 8 namespace |
| 9 { |
| 10 StringMap<Filter*> knownFilters(8192); |
| 11 |
| 12 void trim_spaces(String& str) |
| 13 { |
| 14 String::size_type pos; |
| 15 |
| 16 // Remove leading whitespace |
| 17 for (pos = 0; pos < str.length(); ++pos) |
| 18 if (str[pos] != u' ') |
| 19 break; |
| 20 str.reset(str, pos); |
| 21 |
| 22 // Remove trailing whitespace |
| 23 for (pos = str.length(); pos > 0; --pos) |
| 24 if (str[pos - 1] != u' ') |
| 25 break; |
| 26 str.reset(str, 0, pos); |
| 27 } |
| 28 |
| 29 void remove_spaces(String& str) |
| 30 { |
| 31 String::size_type pos; |
| 32 |
| 33 for (String::size_type i = 0; i < str.length(); ++i) |
| 34 if (str[i] != u' ') |
| 35 str[pos++] = str[i]; |
| 36 |
| 37 str.reset(str, 0, pos); |
| 38 } |
| 39 } |
| 40 |
| 41 Filter::Filter(const String& text) |
| 42 : ref_counted(), mText(text) |
| 43 { |
| 44 annotate_address(this, "Filter"); |
| 45 mText.ensure_own_buffer(); |
| 46 } |
| 47 |
| 48 Filter::~Filter() |
| 49 { |
| 50 // TODO: This should be removing from knownFilters |
| 51 } |
| 52 |
| 53 String Filter::Serialize() const |
| 54 { |
| 55 String result(u"[Filter]\ntext="_str); |
| 56 result.append(mText); |
| 57 result.append(u'\n'); |
| 58 return std::move(result); |
| 59 } |
| 60 |
| 61 Filter* Filter::FromText(const String& text) |
| 62 { |
| 63 auto it = knownFilters.find(text); |
| 64 if (it != knownFilters.end()) |
| 65 return it->second; |
| 66 |
| 67 FilterPtr filter(CommentFilter::Create(text)); |
| 68 if (!filter) |
| 69 filter.reset(ElemHideBase::Create(text)); |
| 70 if (!filter) |
| 71 filter.reset(RegExpFilter::Create(text)); |
| 72 |
| 73 enter_context("Adding to known filters"); |
| 74 knownFilters[filter->mText] = filter.get(); |
| 75 exit_context(); |
| 76 |
| 77 // TODO: We intentionally leak the filter here - currently it won't be used |
| 78 // for anything and would be deleted immediately. |
| 79 filter->AddRef(); |
| 80 |
| 81 return filter; |
| 82 } |
| 83 |
| 84 String Filter::Normalize(String& text) |
| 85 { |
| 86 // Removing special characters like line breaks |
| 87 String::size_type delta = 0; |
| 88 for (String::size_type i = 0; i < text.length(); ++i) |
| 89 { |
| 90 if (text[i] >= u' ') |
| 91 text[i - delta] = text[i]; |
| 92 else |
| 93 ++delta; |
| 94 } |
| 95 text.reset(text, 0, text.length() - delta); |
| 96 |
| 97 trim_spaces(text); |
| 98 |
| 99 { |
| 100 String::size_type domainsEnd; |
| 101 String::size_type selectorStart; |
| 102 Filter::Type type = ElemHideBase::Parse(text, &domainsEnd, &selectorStart); |
| 103 if (type != Filter::Type::UNKNOWN) |
| 104 { |
| 105 String domains(text, 0, domainsEnd); |
| 106 String selector(text, selectorStart); |
| 107 remove_spaces(domains); |
| 108 trim_spaces(selector); |
| 109 |
| 110 String::size_type domainsDelta = domainsEnd - domains.length(); |
| 111 String::size_type selectorDelta = text.length() - selectorStart - |
| 112 selector.length(); |
| 113 |
| 114 if (domainsDelta) |
| 115 for (String::size_type i = domainsEnd; i < selectorStart; ++i) |
| 116 text[i - domainsDelta] = text[i]; |
| 117 |
| 118 if (domainsDelta + selectorDelta) |
| 119 for (String::size_type i = 0; i < selector.length(); ++i) |
| 120 text[selectorStart - domainsDelta + i] = selector[i]; |
| 121 |
| 122 text.reset(text, 0, text.length() - domainsDelta - selectorDelta); |
| 123 return text; |
| 124 } |
| 125 } |
| 126 |
| 127 if (CommentFilter::Parse(text) == Filter::Type::UNKNOWN) |
| 128 remove_spaces(text); |
| 129 return text; |
| 130 } |
OLD | NEW |