Left: | ||
Right: |
OLD | NEW |
---|---|
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 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 return Type::UNKNOWN; | 96 return Type::UNKNOWN; |
97 | 97 |
98 // Selector part | 98 // Selector part |
99 | 99 |
100 // Selector shouldn't be empty | 100 // Selector shouldn't be empty |
101 seenSpaces |= scanner.skip(u' '); | 101 seenSpaces |= scanner.skip(u' '); |
102 if (scanner.done()) | 102 if (scanner.done()) |
103 return Type::UNKNOWN; | 103 return Type::UNKNOWN; |
104 | 104 |
105 data.mSelectorStart = scanner.position() + 1; | 105 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 | 106 |
116 // We are done validating, now we can normalize whitespace and the domain part | 107 // We are done validating, now we can normalize whitespace and the domain part |
117 if (seenSpaces) | 108 if (seenSpaces) |
118 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart); | 109 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart); |
119 DependentString(text, 0, data.mDomainsEnd).toLower(); | 110 DependentString(text, 0, data.mDomainsEnd).toLower(); |
120 | 111 |
121 if (exception) | 112 if (exception) |
122 return Type::ELEMHIDEEXCEPTION; | 113 return Type::ELEMHIDEEXCEPTION; |
123 | 114 |
124 if (text.find(u"[-abp-properties="_str, data.mSelectorStart) != text.npos) | 115 if (text.find(u"[-abp-properties="_str, data.mSelectorStart) != text.npos) |
125 return Type::ELEMHIDEEMULATION; | 116 return Type::ELEMHIDEEMULATION; |
126 | 117 |
127 return Type::ELEMHIDE; | 118 return Type::ELEMHIDE; |
128 } | 119 } |
129 | 120 |
121 namespace { | |
Wladimir Palant
2017/10/18 09:24:02
Please put the opening curly on the next line and
hub
2017/10/18 12:53:47
Done.
| |
122 | |
123 static constexpr size_t REPLACE_SIZE = 5; | |
Wladimir Palant
2017/10/18 09:24:02
This name doesn't quite make it clear what this co
hub
2017/10/18 12:53:48
Done.
| |
124 | |
125 OwnedString EscapeCurlies(String::size_type count, const DependentString& str) | |
126 { | |
127 OwnedString result(str.length() + (count * (REPLACE_SIZE - 1))); | |
Wladimir Palant
2017/10/18 09:24:02
Nit: the parentheses around `count * ...` are unne
hub
2017/10/18 12:53:47
Done.
| |
128 | |
129 String::size_type start = 0; | |
130 String::size_type i = 0; | |
131 String::value_type* current = result.data(); | |
132 for (; i < str.length(); i++) | |
133 { | |
134 if (str[i] == '}' || str[i] == '{') | |
135 { | |
136 if (i != start) | |
137 { | |
138 std::memcpy(current, str.data() + start, | |
139 sizeof(String::value_type) * (i - start)); | |
140 current += i - start; | |
Wladimir Palant
2017/10/18 09:24:02
The logic here got unnecessarily complicated. I'd
hub
2017/10/18 12:53:48
Done.
| |
141 } | |
142 start = i + 1; | |
143 switch(str[i]) | |
144 { | |
145 case '}': | |
Wladimir Palant
2017/10/18 09:24:02
Nit: I'd probably say u'}' to indicate that we are
hub
2017/10/18 12:53:47
Done.
| |
146 std::memcpy(current, u"\\x7D ", | |
147 sizeof(String::value_type) * REPLACE_SIZE); | |
Wladimir Palant
2017/10/18 09:24:02
You need to include <cstring> explicitly to use st
hub
2017/10/18 12:53:47
Done.
| |
148 current += REPLACE_SIZE; | |
149 break; | |
150 case '{': | |
151 std::memcpy(current, u"\\x7B ", | |
152 sizeof(String::value_type) * REPLACE_SIZE); | |
153 current += REPLACE_SIZE; | |
154 break; | |
155 default: | |
156 break; | |
157 } | |
158 } | |
159 } | |
160 if (start < i) | |
161 std::memcpy(current, str.data() + start, | |
162 sizeof(String::value_type) * (i - start)); | |
163 return result; | |
164 } | |
165 | |
166 } | |
167 | |
168 OwnedString ElemHideBase::GetSelector() const | |
169 { | |
170 DependentString selector = mData.GetSelector(mText); | |
171 String::size_type count = 0; | |
Wladimir Palant
2017/10/18 09:24:02
Nit: Somewhat less generic name such as replacemen
hub
2017/10/18 12:53:47
Done.
| |
172 for (String::size_type i = 0; i < selector.length(); i++) | |
173 if (selector[i] == '}' || selector[i] == '{') | |
174 count++; | |
175 if (count) | |
176 return EscapeCurlies(count, selector); | |
177 | |
178 return OwnedString(selector); | |
179 } | |
180 | |
130 OwnedString ElemHideBase::GetSelectorDomain() const | 181 OwnedString ElemHideBase::GetSelectorDomain() const |
131 { | 182 { |
132 /* TODO this is inefficient */ | 183 /* TODO this is inefficient */ |
133 OwnedString result; | 184 OwnedString result; |
134 if (mDomains) | 185 if (mDomains) |
135 { | 186 { |
136 for (const auto& item : *mDomains) | 187 for (const auto& item : *mDomains) |
137 { | 188 { |
138 if (item.second && !item.first.empty()) | 189 if (item.second && !item.first.empty()) |
139 { | 190 { |
140 if (!result.empty()) | 191 if (!result.empty()) |
141 result.append(u','); | 192 result.append(u','); |
142 result.append(item.first); | 193 result.append(item.first); |
143 } | 194 } |
144 } | 195 } |
145 } | 196 } |
146 return result; | 197 return result; |
147 } | 198 } |
OLD | NEW |