| LEFT | RIGHT |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 #include "Filter.h" | 18 #include "Filter.h" |
| 19 #include "CommentFilter.h" | 19 #include "CommentFilter.h" |
| 20 #include "InvalidFilter.h" | 20 #include "InvalidFilter.h" |
| 21 #include "RegExpFilter.h" | 21 #include "RegExpFilter.h" |
| 22 #include "BlockingFilter.h" | 22 #include "BlockingFilter.h" |
| 23 #include "WhitelistFilter.h" | 23 #include "WhitelistFilter.h" |
| 24 #include "ElemHideBase.h" | 24 #include "ElemHideBase.h" |
| 25 #include "ElemHideFilter.h" | 25 #include "ElemHideFilter.h" |
| 26 #include "ElemHideException.h" | 26 #include "ElemHideException.h" |
| 27 #include "ElemHideEmulationFilter.h" | 27 #include "ElemHideEmulationFilter.h" |
| 28 #include "../StringMap.h" | 28 #include "../StringMap.h" |
| 29 |
| 30 ABP_NS_USING |
| 29 | 31 |
| 30 namespace | 32 namespace |
| 31 { | 33 { |
| 32 StringMap<Filter*> knownFilters(8192); | 34 StringMap<Filter*> knownFilters(8192); |
| 33 | 35 |
| 34 void NormalizeWhitespace(DependentString& text) | 36 void NormalizeWhitespace(DependentString& text) |
| 35 { | 37 { |
| 36 String::size_type start = 0; | 38 String::size_type start = 0; |
| 37 String::size_type end = text.length(); | 39 String::size_type end = text.length(); |
| 38 | 40 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 text.reset(text, start, end - start); | 73 text.reset(text, start, end - start); |
| 72 } | 74 } |
| 73 } | 75 } |
| 74 | 76 |
| 75 Filter::Filter(Type type, const String& text) | 77 Filter::Filter(Type type, const String& text) |
| 76 : mText(text), mType(type) | 78 : mText(text), mType(type) |
| 77 { | 79 { |
| 78 annotate_address(this, "Filter"); | 80 annotate_address(this, "Filter"); |
| 79 } | 81 } |
| 80 | 82 |
| 81 Filter::Filter(Type type, String&& text) | |
| 82 : mText(text), mType(type) | |
| 83 { | |
| 84 annotate_address(this, "Filter"); | |
| 85 } | |
| 86 | |
| 87 Filter::~Filter() | 83 Filter::~Filter() |
| 88 { | 84 { |
| 89 knownFilters.erase(mText); | 85 knownFilters.erase(mText); |
| 90 } | 86 } |
| 91 | 87 |
| 92 OwnedString Filter::Serialize() const | 88 OwnedString Filter::Serialize() const |
| 93 { | 89 { |
| 94 OwnedString result(u"[Filter]\ntext="_str); | 90 OwnedString result(u"[Filter]\ntext="_str); |
| 95 result.append(mText); | 91 result.append(mText); |
| 96 result.append(u'\n'); | 92 result.append(u'\n'); |
| 97 return result; | 93 return result; |
| 98 } | 94 } |
| 99 | 95 |
| 100 Filter* Filter::FromText(DependentString& text) | 96 Filter* Filter::FromText(DependentString& text) |
| 101 { | 97 { |
| 102 NormalizeWhitespace(text); | 98 NormalizeWhitespace(text); |
| 103 if (text.empty()) | 99 if (text.empty()) |
| 104 return nullptr; | 100 return nullptr; |
| 105 | 101 |
| 106 // Parsing also normalizes the filter text, so it has to be done before the | 102 // Parsing also normalizes the filter text, so it has to be done before the |
| 107 // lookup in knownFilters. | 103 // lookup in knownFilters. |
| 108 union | 104 union |
| 109 { | 105 { |
| 110 RegExpFilterData regexp; | 106 RegExpFilterData regexp; |
| 111 ElemHideData elemhide; | 107 ElemHideData elemhide; |
| 112 } data; | 108 } data; |
| 109 bool needConversion = false; |
| 113 DependentString error; | 110 DependentString error; |
| 114 | 111 |
| 115 Filter::Type type = CommentFilter::Parse(text); | 112 Filter::Type type = CommentFilter::Parse(text); |
| 116 if (type == Filter::Type::UNKNOWN) | 113 if (type == Filter::Type::UNKNOWN) |
| 117 type = ElemHideBase::Parse(text, data.elemhide); | 114 type = ElemHideBase::Parse(text, data.elemhide, needConversion); |
| 118 if (type == Filter::Type::UNKNOWN) | 115 if (type == Filter::Type::UNKNOWN) |
| 119 type = RegExpFilter::Parse(text, error, data.regexp); | 116 type = RegExpFilter::Parse(text, error, data.regexp); |
| 117 |
| 118 if (needConversion) |
| 119 text = ElemHideBase::ConvertFilter(text, data.elemhide.mSelectorStart); |
| 120 |
| 121 // At that point we failed the conversion. |
| 122 if (text.empty()) |
| 123 return nullptr; |
| 120 | 124 |
| 121 auto knownFilter = knownFilters.find(text); | 125 auto knownFilter = knownFilters.find(text); |
| 122 if (knownFilter) | 126 if (knownFilter) |
| 123 { | 127 { |
| 124 knownFilter->second->AddRef(); | 128 knownFilter->second->AddRef(); |
| 125 return knownFilter->second; | 129 return knownFilter->second; |
| 126 } | 130 } |
| 127 | 131 |
| 128 FilterPtr filter; | 132 FilterPtr filter; |
| 129 switch (type) | 133 switch (type) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 knownFilters[filter->mText] = filter.get(); | 165 knownFilters[filter->mText] = filter.get(); |
| 162 else | 166 else |
| 163 // This is a hack: we looked up the entry using text but create it using | 167 // This is a hack: we looked up the entry using text but create it using |
| 164 // filter->mText. This works because both are equal at this point. However, | 168 // filter->mText. This works because both are equal at this point. However, |
| 165 // text refers to a temporary buffer which will go away. | 169 // text refers to a temporary buffer which will go away. |
| 166 knownFilter.assign(filter->mText, filter.get()); | 170 knownFilter.assign(filter->mText, filter.get()); |
| 167 exit_context(); | 171 exit_context(); |
| 168 | 172 |
| 169 return filter.release(); | 173 return filter.release(); |
| 170 } | 174 } |
| LEFT | RIGHT |