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

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

Issue 29580558: Issue 5174 - Allow '{' and '}' in selectors (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: adressed comments Created Oct. 18, 2017, 12:53 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.h ('k') | test/filterClasses.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 "ElemHideBase.h" 20 #include "ElemHideBase.h"
19 #include "../StringScanner.h" 21 #include "../StringScanner.h"
20 22
21 namespace 23 namespace
22 { 24 {
23 void NormalizeWhitespace(DependentString& text, String::size_type& domainsEnd, 25 void NormalizeWhitespace(DependentString& text, String::size_type& domainsEnd,
24 String::size_type& selectorStart) 26 String::size_type& selectorStart)
25 { 27 {
26 // For element hiding filters we only want to remove spaces preceding the 28 // For element hiding filters we only want to remove spaces preceding the
27 // selector part. The positions we've determined already have to be adjusted 29 // selector part. The positions we've determined already have to be adjusted
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 return Type::UNKNOWN; 98 return Type::UNKNOWN;
97 99
98 // Selector part 100 // Selector part
99 101
100 // Selector shouldn't be empty 102 // Selector shouldn't be empty
101 seenSpaces |= scanner.skip(u' '); 103 seenSpaces |= scanner.skip(u' ');
102 if (scanner.done()) 104 if (scanner.done())
103 return Type::UNKNOWN; 105 return Type::UNKNOWN;
104 106
105 data.mSelectorStart = scanner.position() + 1; 107 data.mSelectorStart = scanner.position() + 1;
106 while (!scanner.done())
107 {
108 switch (scanner.next())
109 {
110 case u'{':
111 case u'}':
112 return Type::UNKNOWN;
113 }
114 }
115 108
116 // We are done validating, now we can normalize whitespace and the domain part 109 // We are done validating, now we can normalize whitespace and the domain part
117 if (seenSpaces) 110 if (seenSpaces)
118 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart); 111 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart);
119 DependentString(text, 0, data.mDomainsEnd).toLower(); 112 DependentString(text, 0, data.mDomainsEnd).toLower();
120 113
121 if (exception) 114 if (exception)
122 return Type::ELEMHIDEEXCEPTION; 115 return Type::ELEMHIDEEXCEPTION;
123 116
124 if (text.find(u"[-abp-properties="_str, data.mSelectorStart) != text.npos) 117 if (text.find(u"[-abp-properties="_str, data.mSelectorStart) != text.npos)
125 return Type::ELEMHIDEEMULATION; 118 return Type::ELEMHIDEEMULATION;
126 119
127 return Type::ELEMHIDE; 120 return Type::ELEMHIDE;
128 } 121 }
129 122
123 namespace
124 {
125
126 static constexpr String::value_type OPENING_CURLY_REPLACEMENT[] = u"\\x7B ";
127 static constexpr String::value_type CLOSING_CURLY_REPLACEMENT[] = u"\\x7D ";
128 static constexpr String::size_type CURLY_REPLACEMENT_SIZE = sizeof(OPENING_CURLY _REPLACEMENT) / sizeof(OPENING_CURLY_REPLACEMENT[0]) - 1;
sergei 2017/10/18 14:01:37 What about a constexpr function calculating the le
129
130 OwnedString EscapeCurlies(String::size_type replacementCount,
131 const DependentString& str)
132 {
133 OwnedString result(str.length() + replacementCount * (CURLY_REPLACEMENT_SIZE - 1));
134
135 String::value_type* current = result.data();
136 for (String::size_type i = 0; i < str.length(); i++)
137 {
138 switch(str[i])
139 {
140 case u'}':
141 std::memcpy(current, CLOSING_CURLY_REPLACEMENT,
142 sizeof(String::value_type) * CURLY_REPLACEMENT_SIZE);
143 current += CURLY_REPLACEMENT_SIZE;
sergei 2017/10/18 14:01:37 What about moving of memcpy into the String class?
144 break;
145 case u'{':
146 std::memcpy(current, OPENING_CURLY_REPLACEMENT,
147 sizeof(String::value_type) * CURLY_REPLACEMENT_SIZE);
148 current += CURLY_REPLACEMENT_SIZE;
149 break;
150 default:
151 *current = str[i];
152 current++;
153 break;
154 }
155 }
156
157 return result;
158 }
159
160 }
161
162 OwnedString ElemHideBase::GetSelector() const
163 {
164 DependentString selector = mData.GetSelector(mText);
165 String::size_type replacementCount = 0;
166 for (String::size_type i = 0; i < selector.length(); i++)
167 if (selector[i] == '}' || selector[i] == '{')
168 replacementCount++;
169 if (replacementCount)
170 return EscapeCurlies(replacementCount, selector);
171
172 return OwnedString(selector);
173 }
174
130 OwnedString ElemHideBase::GetSelectorDomain() const 175 OwnedString ElemHideBase::GetSelectorDomain() const
131 { 176 {
132 /* TODO this is inefficient */ 177 /* TODO this is inefficient */
133 OwnedString result; 178 OwnedString result;
134 if (mDomains) 179 if (mDomains)
135 { 180 {
136 for (const auto& item : *mDomains) 181 for (const auto& item : *mDomains)
137 { 182 {
138 if (item.second && !item.first.empty()) 183 if (item.second && !item.first.empty())
139 { 184 {
140 if (!result.empty()) 185 if (!result.empty())
141 result.append(u','); 186 result.append(u',');
142 result.append(item.first); 187 result.append(item.first);
143 } 188 }
144 } 189 }
145 } 190 }
146 return result; 191 return result;
147 } 192 }
OLDNEW
« no previous file with comments | « compiled/filter/ElemHideBase.h ('k') | test/filterClasses.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld