Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: compiled/filter/ElemHideBase.cpp

Issue 29595633: Issue 5870 - Implement the new ElemHideEmulation filter type (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Address review comment. Reworked the conversion Created Feb. 7, 2018, 4:13 a.m.
Right Patch Set: Deal with ill formed filters. Created Feb. 14, 2018, 5:05 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « compiled/filter/ElemHideBase.h ('k') | compiled/filter/Filter.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 <cstring> 18 #include <cstring>
19 19
20 #include "ElemHideBase.h" 20 #include "ElemHideBase.h"
21 #include "../StringScanner.h" 21 #include "../StringScanner.h"
22 #include "../Utils.h" 22 #include "../Utils.h"
23 23
24 ABP_NS_USING
25
24 namespace 26 namespace
25 { 27 {
26 void NormalizeWhitespace(DependentString& text, String::size_type& domainsEnd, 28 void NormalizeWhitespace(DependentString& text, String::size_type& domainsEnd,
27 String::size_type& selectorStart) 29 String::size_type& selectorStart)
28 { 30 {
29 // For element hiding filters we only want to remove spaces preceding the 31 // For element hiding filters we only want to remove spaces preceding the
30 // selector part. The positions we've determined already have to be adjusted 32 // selector part. The positions we've determined already have to be adjusted
31 // accordingly. 33 // accordingly.
32 34
33 String::size_type delta = 0; 35 String::size_type delta = 0;
(...skipping 11 matching lines...) Expand all
45 delta++; 47 delta++;
46 else 48 else
47 text[pos - delta] = text[pos]; 49 text[pos - delta] = text[pos];
48 } 50 }
49 selectorStart -= delta; 51 selectorStart -= delta;
50 52
51 text.reset(text, 0, len - delta); 53 text.reset(text, 0, len - delta);
52 } 54 }
53 55
54 static constexpr String::value_type ELEM_HIDE_DELIMITER[] = u"##"; 56 static constexpr String::value_type ELEM_HIDE_DELIMITER[] = u"##";
55 static constexpr String::size_type ELEM_HIDE_DELIMITER_LEN = LENGTH_OF(ELEM_HI DE_DELIMITER); 57 static constexpr String::size_type ELEM_HIDE_DELIMITER_LEN = str_length_of(ELE M_HIDE_DELIMITER);
56 58
57 static constexpr String::value_type ELEM_HIDE_EMULATION_DELIMITER[] = u"#?#"; 59 static constexpr String::value_type ELEM_HIDE_EMULATION_DELIMITER[] = u"#?#";
58 static constexpr String::size_type ELEM_HIDE_EMULATION_DELIMITER_LEN = LENGTH_ OF(ELEM_HIDE_EMULATION_DELIMITER); 60 static constexpr String::size_type ELEM_HIDE_EMULATION_DELIMITER_LEN = str_len gth_of(ELEM_HIDE_EMULATION_DELIMITER);
59 61
60 static constexpr String::value_type OLD_PROPS_SELECTOR[] = u"[-abp-properties= "; 62 static constexpr String::value_type OLD_PROPS_SELECTOR[] = u"[-abp-properties= ";
61 static constexpr String::size_type OLD_PROPS_SELECTOR_LEN = LENGTH_OF(OLD_PROP S_SELECTOR); 63 static constexpr String::size_type OLD_PROPS_SELECTOR_LEN = str_length_of(OLD_ PROPS_SELECTOR);
62 64
63 static constexpr String::value_type PROPS_SELECTOR[] = u":-abp-properties("; 65 static constexpr String::value_type PROPS_SELECTOR[] = u":-abp-properties(";
64 static constexpr String::size_type PROPS_SELECTOR_LEN = LENGTH_OF(PROPS_SELECT OR); 66 static constexpr String::size_type PROPS_SELECTOR_LEN = str_length_of(PROPS_SE LECTOR);
65 } 67 }
66 68
67 ElemHideBase::ElemHideBase(Type type, const String& text, const ElemHideData& da ta) 69 ElemHideBase::ElemHideBase(Type type, const String& text, const ElemHideData& da ta)
68 : ActiveFilter(type, text, false), mData(data) 70 : ActiveFilter(type, text, false), mData(data)
69 { 71 {
70 if (mData.HasDomains()) 72 if (mData.HasDomains())
71 ParseDomains(mData.GetDomainsSource(mText), u','); 73 ParseDomains(mData.GetDomainsSource(mText), u',');
72 } 74 }
73 75
74 Filter::Type ElemHideBase::Parse(DependentString& text, ElemHideData& data, bool & needConversion) 76 Filter::Type ElemHideBase::Parse(DependentString& text, ElemHideData& data, bool & needConversion)
75 { 77 {
78 needConversion = false;
79
76 StringScanner scanner(text); 80 StringScanner scanner(text);
77 81
78 // Domains part 82 // Domains part
79 bool seenSpaces = false; 83 bool seenSpaces = false;
80 while (!scanner.done()) 84 while (!scanner.done())
81 { 85 {
82 String::value_type next = scanner.next(); 86 String::value_type next = scanner.next();
83 if (next == u'#') 87 if (next == u'#')
84 { 88 {
85 data.mDomainsEnd = scanner.position(); 89 data.mDomainsEnd = scanner.position();
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 String::size_type byte_len() const 162 String::size_type byte_len() const
159 { 163 {
160 return len() * sizeof(String::value_type); 164 return len() * sizeof(String::value_type);
161 } 165 }
162 }; 166 };
163 } 167 }
164 168
165 // Convert filter from the old syntax to the new. 169 // Convert filter from the old syntax to the new.
166 DependentString ElemHideBase::ConvertFilter(String& text, String::size_type& at) 170 DependentString ElemHideBase::ConvertFilter(String& text, String::size_type& at)
167 { 171 {
172 Range prefix = {at, text.find(OLD_PROPS_SELECTOR, at, OLD_PROPS_SELECTOR_LEN)} ;
173 if (prefix.end == text.npos)
174 return DependentString(text);
175
168 auto length = text.length(); 176 auto length = text.length();
169 Range prefix = {at, 0};
170 Range suffix = {at, length}; 177 Range suffix = {at, length};
171 prefix.end = text.find(OLD_PROPS_SELECTOR, at, OLD_PROPS_SELECTOR_LEN); 178 Range properties = { prefix.end + OLD_PROPS_SELECTOR_LEN, 0 };
172 if (prefix.end != text.npos) 179 String::value_type quote = 0;
173 { 180 for (auto index = properties.start;
174 Range properties = { prefix.end + OLD_PROPS_SELECTOR_LEN, 0 }; 181 index < length && (suffix.start == at); index++)
175 String::value_type quote = 0; 182 {
176 bool closing = false; 183 auto c = text[index];
177 for (auto index = properties.start; 184 switch (c)
178 index < length && !closing; index++) 185 {
179 { 186 case u'"':
180 auto c = text[index]; 187 case u'\'':
181 switch (c) 188 if (quote == 0)
182 { 189 {
183 case u'"': 190 // syntax error: we already have a quoted section.
184 case u'\'': 191 if (properties.end)
185 if (quote == 0) 192 return DependentString();
186 { 193
187 // syntax error: we already have a quoted section. 194 if (properties.start != index)
188 if (properties.end) 195 return DependentString();
189 return DependentString(); 196
190 197 quote = c;
191 quote = c; 198 properties.start = index + 1;
192 properties.start = index + 1; 199 }
193 } 200 else if (quote == c)
194 else if (quote == c) 201 {
195 { 202 // end of quoted.
196 // end of quoted. 203 quote = 0;
197 quote = 0; 204 properties.end = index;
198 properties.end = index; 205 }
199 } 206 break;
200 break; 207 case u']':
201 case u']': 208 if (quote == 0)
202 if (quote == 0) 209 {
203 { 210 if (properties.end == 0)
204 if (properties.end == 0) 211 return DependentString();
205 return DependentString(); 212 if (properties.end + 1 != index)
206 suffix.start = index + 1; 213 return DependentString();
207 closing = true; 214 suffix.start = index + 1;
208 } 215 }
209 break; 216 break;
210 default: 217 default:
211 break; 218 break;
212 } 219 }
213 } 220 }
214 221
215 String::size_type delimiter = text.find(ELEM_HIDE_DELIMITER, 0, 222 if (suffix.start == at)
sergei 2018/02/27 10:56:53 Just for reference, I think it (what the whole for
216 ELEM_HIDE_DELIMITER_LEN); 223 return DependentString();
217 // +1 for the replacement of "##" by "#?#" 224
218 if (delimiter != text.npos) 225 String::size_type delimiter = text.find(ELEM_HIDE_DELIMITER, 0,
219 at++; 226 ELEM_HIDE_DELIMITER_LEN);
220 auto new_len = prefix.len() + suffix.len() + properties.len() 227 // +1 for the replacement of "##" by "#?#"
221 + PROPS_SELECTOR_LEN + 1 + at; 228 if (delimiter != text.npos)
222 229 at++;
223 DependentString converted(text, 0, new_len); 230 auto new_len = at + prefix.len() + PROPS_SELECTOR_LEN + properties.len() + 1 / * ) */ + suffix.len();
224 231
225 if (suffix.len()) 232 assert2(new_len + 1 == length || (delimiter == text.npos && new_len + 2 == len gth), u"Inconsistent length in filter conversion."_str);
sergei 2018/02/27 10:56:53 not important just for reference length == new_len
hub 2018/02/27 13:32:30 Acknowledged.
226 { 233
227 new_len -= suffix.len(); 234 DependentString converted(text, 0, new_len);
228 std::memmove(converted.data() + new_len, 235
229 text.data() + suffix.start, 236 if (suffix.len())
230 suffix.byte_len()); 237 {
231 } 238 new_len -= suffix.len();
232 new_len--;
233 // here we need to move the properties before inserting the ')'
234 auto parens = new_len;
235 if (properties.len())
236 {
237 new_len -= properties.len();
238 std::memmove(converted.data() + new_len,
239 text.data() + properties.start, properties.byte_len());
240 }
241 converted[parens] = u')';
242
243 new_len -= PROPS_SELECTOR_LEN;
244 std::memmove(converted.data() + new_len, 239 std::memmove(converted.data() + new_len,
245 PROPS_SELECTOR, 240 text.data() + suffix.start,
246 PROPS_SELECTOR_LEN * sizeof(String::value_type)); 241 suffix.byte_len());
247 if (prefix.len()) 242 }
248 { 243 new_len--;
249 new_len -= prefix.len(); 244 // here we need to move the properties before inserting the ')'
250 std::memmove(converted.data() + new_len, 245 auto parens = new_len;
251 text.data() + prefix.start, prefix.byte_len()); 246 if (properties.len())
252 } 247 {
253 248 new_len -= properties.len();
254 if (delimiter != String::npos) 249 std::memmove(converted.data() + new_len,
255 { 250 text.data() + properties.start, properties.byte_len());
256 std::memmove(converted.data() + delimiter, ELEM_HIDE_EMULATION_DELIMITER, 251 }
257 ELEM_HIDE_EMULATION_DELIMITER_LEN * sizeof(String::value_type )); 252 converted[parens] = u')';
258 at++; 253
259 } 254 new_len -= PROPS_SELECTOR_LEN;
260 255 std::memcpy(converted.data() + new_len,
261 return converted; 256 PROPS_SELECTOR,
262 } 257 PROPS_SELECTOR_LEN * sizeof(String::value_type));
263 258 if (prefix.len())
264 return DependentString(text); 259 {
260 new_len -= prefix.len();
261 std::memmove(converted.data() + new_len,
262 text.data() + prefix.start, prefix.byte_len());
263 }
264
265 if (delimiter != String::npos)
266 {
267 std::memcpy(converted.data() + delimiter, ELEM_HIDE_EMULATION_DELIMITER,
268 ELEM_HIDE_EMULATION_DELIMITER_LEN * sizeof(String::value_type));
269 }
270
271 return converted;
265 } 272 }
266 273
267 namespace 274 namespace
268 { 275 {
269 static constexpr String::value_type OPENING_CURLY_REPLACEMENT[] = u"\\7B "; 276 static constexpr String::value_type OPENING_CURLY_REPLACEMENT[] = u"\\7B ";
270 static constexpr String::value_type CLOSING_CURLY_REPLACEMENT[] = u"\\7D "; 277 static constexpr String::value_type CLOSING_CURLY_REPLACEMENT[] = u"\\7D ";
271 static constexpr String::size_type CURLY_REPLACEMENT_SIZE = LENGTH_OF(OPENING_ CURLY_REPLACEMENT); 278 static constexpr String::size_type CURLY_REPLACEMENT_SIZE = str_length_of(OPEN ING_CURLY_REPLACEMENT);
272 279
273 OwnedString EscapeCurlies(String::size_type replacementCount, 280 OwnedString EscapeCurlies(String::size_type replacementCount,
274 const DependentString& str) 281 const DependentString& str)
275 { 282 {
276 OwnedString result(str.length() + replacementCount * (CURLY_REPLACEMENT_SIZE - 1)); 283 OwnedString result(str.length() + replacementCount * (CURLY_REPLACEMENT_SIZE - 1));
277 284
278 String::value_type* current = result.data(); 285 String::value_type* current = result.data();
279 for (String::size_type i = 0; i < str.length(); i++) 286 for (String::size_type i = 0; i < str.length(); i++)
280 { 287 {
281 switch(str[i]) 288 switch(str[i])
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 if (item.second && !item.first.empty()) 332 if (item.second && !item.first.empty())
326 { 333 {
327 if (!result.empty()) 334 if (!result.empty())
328 result.append(u','); 335 result.append(u',');
329 result.append(item.first); 336 result.append(item.first);
330 } 337 }
331 } 338 }
332 } 339 }
333 return result; 340 return result;
334 } 341 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld