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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 if (text.empty()) | 99 if (text.empty()) |
98 return nullptr; | 100 return nullptr; |
99 | 101 |
100 // 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 |
101 // lookup in knownFilters. | 103 // lookup in knownFilters. |
102 union | 104 union |
103 { | 105 { |
104 RegExpFilterData regexp; | 106 RegExpFilterData regexp; |
105 ElemHideData elemhide; | 107 ElemHideData elemhide; |
106 } data; | 108 } data; |
| 109 bool needConversion = false; |
107 DependentString error; | 110 DependentString error; |
108 | 111 |
109 Filter::Type type = CommentFilter::Parse(text); | 112 Filter::Type type = CommentFilter::Parse(text); |
110 if (type == Filter::Type::UNKNOWN) | 113 if (type == Filter::Type::UNKNOWN) |
111 type = ElemHideBase::Parse(text, data.elemhide); | 114 type = ElemHideBase::Parse(text, data.elemhide, needConversion); |
112 if (type == Filter::Type::UNKNOWN) | 115 if (type == Filter::Type::UNKNOWN) |
113 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; |
114 | 124 |
115 auto knownFilter = knownFilters.find(text); | 125 auto knownFilter = knownFilters.find(text); |
116 if (knownFilter) | 126 if (knownFilter) |
117 { | 127 { |
118 knownFilter->second->AddRef(); | 128 knownFilter->second->AddRef(); |
119 return knownFilter->second; | 129 return knownFilter->second; |
120 } | 130 } |
121 | 131 |
122 FilterPtr filter; | 132 FilterPtr filter; |
123 switch (type) | 133 switch (type) |
124 { | 134 { |
125 case CommentFilter::classType: | 135 case CommentFilter::classType: |
126 filter = FilterPtr(new CommentFilter(text), false); | 136 filter = FilterPtr(new CommentFilter(text), false); |
127 break; | 137 break; |
128 case InvalidFilter::classType: | 138 case InvalidFilter::classType: |
129 filter = FilterPtr(new InvalidFilter(text, error), false); | 139 filter = FilterPtr(new InvalidFilter(text, error), false); |
130 break; | 140 break; |
131 case BlockingFilter::classType: | 141 case BlockingFilter::classType: |
132 filter = FilterPtr(new BlockingFilter(text, data.regexp), false); | 142 filter = FilterPtr(new BlockingFilter(text, data.regexp), false); |
133 break; | 143 break; |
134 case WhitelistFilter::classType: | 144 case WhitelistFilter::classType: |
135 filter = FilterPtr(new WhitelistFilter(text, data.regexp), false); | 145 filter = FilterPtr(new WhitelistFilter(text, data.regexp), false); |
136 break; | 146 break; |
137 case ElemHideFilter::classType: | 147 case ElemHideFilter::classType: |
138 filter = FilterPtr(new ElemHideFilter(text, data.elemhide), false); | 148 filter = FilterPtr(new ElemHideFilter(text, data.elemhide), false); |
139 break; | 149 break; |
140 case ElemHideException::classType: | 150 case ElemHideException::classType: |
141 filter = FilterPtr( | 151 filter = FilterPtr(new ElemHideException(text, data.elemhide), false); |
142 new ElemHideException( | |
143 data.elemhide.mNeedConversion ? | |
144 DependentString(ElemHideBase::ConvertFilter(text, data.elemhide.mSelec
torStart)) : | |
145 text, data.elemhide), false); | |
146 break; | 152 break; |
147 case ElemHideEmulationFilter::classType: | 153 case ElemHideEmulationFilter::classType: |
148 filter = FilterPtr( | 154 filter = FilterPtr(new ElemHideEmulationFilter(text, data.elemhide), false
); |
149 new ElemHideEmulationFilter( | |
150 data.elemhide.mNeedConversion ? | |
151 DependentString(ElemHideBase::ConvertFilter(text, data.elemhide.mSelec
torStart)) : | |
152 text, data.elemhide), false); | |
153 if (static_cast<ElemHideEmulationFilter*>(filter.get())->IsGeneric()) | 155 if (static_cast<ElemHideEmulationFilter*>(filter.get())->IsGeneric()) |
154 filter = FilterPtr(new InvalidFilter(text, u"filter_elemhideemulation_no
domain"_str), false); | 156 filter = FilterPtr(new InvalidFilter(text, u"filter_elemhideemulation_no
domain"_str), false); |
155 break; | 157 break; |
156 default: | 158 default: |
157 // This should never happen but just in case | 159 // This should never happen but just in case |
158 return nullptr; | 160 return nullptr; |
159 } | 161 } |
160 | 162 |
161 enter_context("Adding to known filters"); | 163 enter_context("Adding to known filters"); |
162 if (text != filter->mText) | 164 if (text != filter->mText) |
163 knownFilters[filter->mText] = filter.get(); | 165 knownFilters[filter->mText] = filter.get(); |
164 else | 166 else |
165 // 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 |
166 // 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, |
167 // text refers to a temporary buffer which will go away. | 169 // text refers to a temporary buffer which will go away. |
168 knownFilter.assign(filter->mText, filter.get()); | 170 knownFilter.assign(filter->mText, filter.get()); |
169 exit_context(); | 171 exit_context(); |
170 | 172 |
171 return filter.release(); | 173 return filter.release(); |
172 } | 174 } |
LEFT | RIGHT |