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

Side by Side Diff: compiled/filter/ElemHideEmulationFilter.cpp

Issue 29595633: Issue 5870 - Implement the new ElemHideEmulation filter type (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Nov. 2, 2017, 11:42 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « compiled/filter/ElemHideBase.cpp ('k') | test/elemHideEmulation.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
19
18 #include "ElemHideEmulationFilter.h" 20 #include "ElemHideEmulationFilter.h"
19 21
22 namespace {
23 // Convert filter from the old syntax to the new.
24 OwnedString ConvertFilter(const String& text, String::size_type at)
hub 2017/11/02 23:47:49 this is a bit rough conversion, but no RegExp. It
25 {
26 static const auto propsSelector = u"[-abp-properties="_str;
27 static const auto newPropsSelector = u":-abp-properties("_str;
28 auto selectorPos = text.find(propsSelector, at);
29 if (selectorPos != text.npos)
30 {
31 auto length = text.length();
32 auto properties = selectorPos + propsSelector.length();
33 String::value_type quote = 0;
34 bool escape = false;
35 String::size_type removed = 0; // how many chars we remove
36 String::size_type end = properties;
37 String::size_type quote_start = 0;
38 String::size_type quote_end = 0;
39 for (auto index = properties;
40 index < length && end == properties; index++)
41 {
42 if (escape)
43 {
44 escape = false;
45 continue;
46 }
47
48 auto c = text[index];
49 switch (c)
50 {
51 case '\\':
52 escape = true;
53 break;
54 case '"':
55 case '\'':
56 if (quote == 0)
57 {
58 quote = c;
59 quote_start = index + 1;
60 }
61 else if (quote == c)
62 {
63 // end of quoted.
64 quote = 0;
65 removed += 2;
66 quote_end = index;
67 }
68 break;
69 case ']':
70 if (quote == 0)
71 end = index + 1; // end of properties (after ])
72 break;
73 default:
74 break;
75 }
76 }
77
78 if (quote != 0)
79 quote_end = end - 1;
80 else if (quote_end <= quote_start)
81 {
82 // we likely didn't find a quoted content so we just take it as is.
83 quote_start = properties;
84 quote_end = end - 1;
85 }
86
87 OwnedString converted(length - removed);
88 String::size_type offset = 0;
89 std::memcpy(converted.data(), text.data(),
90 selectorPos * sizeof(String::value_type));
91 offset += selectorPos;
92
93 std::memcpy(converted.data() + offset, newPropsSelector.data(),
94 newPropsSelector.length() * sizeof(String::value_type));
95 offset += newPropsSelector.length();
96
97 std::memcpy(converted.data() + offset, text.data() + quote_start,
98 (quote_end - quote_start) * sizeof(String::value_type));
99 offset += quote_end - quote_start;
100
101 std::memcpy(converted.data() + offset, u")", sizeof(String::value_type));
102 offset++;
103
104 std::memcpy(converted.data() + offset, text.data() + end,
105 (length - end) * sizeof(String::value_type));
106 offset += (length - end) * sizeof(String::value_type);
107
108 return converted;
109 }
110
111 return OwnedString(text);
112 }
113 }
114
20 ElemHideEmulationFilter::ElemHideEmulationFilter(const String& text, 115 ElemHideEmulationFilter::ElemHideEmulationFilter(const String& text,
21 const ElemHideData& data) 116 const ElemHideData& data)
22 : ElemHideBase(classType, text, data) 117 : ElemHideBase(classType, ConvertFilter(text, data.mSelectorStart), data)
hub 2017/11/02 23:47:49 I think the conversion should be done in the elemh
23 { 118 {
24 } 119 }
OLDNEW
« no previous file with comments | « compiled/filter/ElemHideBase.cpp ('k') | test/elemHideEmulation.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld