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: Indent namespace content. Created Oct. 19, 2017, 12:48 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 static constexpr String::value_type OPENING_CURLY_REPLACEMENT[] = u"\\7B ";
126 static constexpr String::value_type CLOSING_CURLY_REPLACEMENT[] = u"\\7D ";
127 static constexpr String::size_type CURLY_REPLACEMENT_SIZE = sizeof(OPENING_CUR LY_REPLACEMENT) / sizeof(OPENING_CURLY_REPLACEMENT[0]) - 1;
128
129 OwnedString EscapeCurlies(String::size_type replacementCount,
130 const DependentString& str)
131 {
132 OwnedString result(str.length() + replacementCount * (CURLY_REPLACEMENT_SIZE - 1));
sergei 2017/11/21 11:42:10 I think we should not subtract 1 here because it's
hub 2017/11/21 15:58:21 the -1 is because we are replacing. str.length() a
sergei 2017/11/21 16:18:00 Acknowledged.
133
134 String::value_type* current = result.data();
135 for (String::size_type i = 0; i < str.length(); i++)
136 {
137 switch(str[i])
138 {
139 case u'}':
140 std::memcpy(current, CLOSING_CURLY_REPLACEMENT,
141 sizeof(String::value_type) * CURLY_REPLACEMENT_SIZE);
142 current += CURLY_REPLACEMENT_SIZE;
143 break;
144 case u'{':
145 std::memcpy(current, OPENING_CURLY_REPLACEMENT,
146 sizeof(String::value_type) * CURLY_REPLACEMENT_SIZE);
147 current += CURLY_REPLACEMENT_SIZE;
148 break;
149 default:
150 *current = str[i];
151 current++;
152 break;
153 }
154 }
155
156 return result;
157 }
158 }
159
160 OwnedString ElemHideBase::GetSelector() const
161 {
162 DependentString selector = mData.GetSelector(mText);
163 String::size_type replacementCount = 0;
164 for (String::size_type i = 0; i < selector.length(); i++)
165 if (selector[i] == '}' || selector[i] == '{')
166 replacementCount++;
167 if (replacementCount)
168 return EscapeCurlies(replacementCount, selector);
169
170 return OwnedString(selector);
171 }
172
130 OwnedString ElemHideBase::GetSelectorDomain() const 173 OwnedString ElemHideBase::GetSelectorDomain() const
131 { 174 {
132 /* TODO this is inefficient */ 175 /* TODO this is inefficient */
133 OwnedString result; 176 OwnedString result;
134 if (mDomains) 177 if (mDomains)
135 { 178 {
136 for (const auto& item : *mDomains) 179 for (const auto& item : *mDomains)
137 { 180 {
138 if (item.second && !item.first.empty()) 181 if (item.second && !item.first.empty())
139 { 182 {
140 if (!result.empty()) 183 if (!result.empty())
141 result.append(u','); 184 result.append(u',');
142 result.append(item.first); 185 result.append(item.first);
143 } 186 }
144 } 187 }
145 } 188 }
146 return result; 189 return result;
147 } 190 }
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