Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 | |
18 #include <cstring> | |
17 | 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 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 | 113 |
112 if (exception) | 114 if (exception) |
113 return Type::ELEMHIDEEXCEPTION; | 115 return Type::ELEMHIDEEXCEPTION; |
114 | 116 |
115 if (text.find(u"[-abp-properties="_str, data.mSelectorStart) != text.npos) | 117 if (text.find(u"[-abp-properties="_str, data.mSelectorStart) != text.npos) |
116 return Type::ELEMHIDEEMULATION; | 118 return Type::ELEMHIDEEMULATION; |
117 | 119 |
118 return Type::ELEMHIDE; | 120 return Type::ELEMHIDE; |
119 } | 121 } |
120 | 122 |
121 namespace { | 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; | |
122 | 128 |
123 OwnedString EscapeCurlies(String::size_type first, const DependentString& str) | 129 OwnedString EscapeCurlies(String::size_type replacementCount, |
124 { | 130 const DependentString& str) |
125 OwnedString result; | 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.
| |
126 | 133 |
127 String::size_type start = 0; | 134 String::value_type* current = result.data(); |
128 for (String::size_type i = first; i < str.length(); i++) | 135 for (String::size_type i = 0; i < str.length(); i++) |
129 { | |
130 if (str[i] == '}' || str[i] == '{') | |
131 { | 136 { |
132 if (i != start) | |
133 result.append(str.data() + start, i - start); | |
134 start = i + 1; | |
135 switch(str[i]) | 137 switch(str[i]) |
136 { | 138 { |
137 case '}': | 139 case u'}': |
138 result.append("\\x7D ", 5); | 140 std::memcpy(current, CLOSING_CURLY_REPLACEMENT, |
141 sizeof(String::value_type) * CURLY_REPLACEMENT_SIZE); | |
142 current += CURLY_REPLACEMENT_SIZE; | |
139 break; | 143 break; |
140 case '{': | 144 case u'{': |
141 result.append("\\x7B ", 5); | 145 std::memcpy(current, OPENING_CURLY_REPLACEMENT, |
146 sizeof(String::value_type) * CURLY_REPLACEMENT_SIZE); | |
147 current += CURLY_REPLACEMENT_SIZE; | |
142 break; | 148 break; |
143 default: | 149 default: |
150 *current = str[i]; | |
151 current++; | |
144 break; | 152 break; |
145 } | 153 } |
146 } | 154 } |
155 | |
156 return result; | |
147 } | 157 } |
148 return result; | |
149 } | |
150 | |
151 } | 158 } |
152 | 159 |
153 OwnedString ElemHideBase::GetSelector() const | 160 OwnedString ElemHideBase::GetSelector() const |
154 { | 161 { |
155 DependentString selector = mData.GetSelector(mText); | 162 DependentString selector = mData.GetSelector(mText); |
156 | 163 String::size_type replacementCount = 0; |
157 for (String::size_type i = 0; i < selector.length(); i++) | 164 for (String::size_type i = 0; i < selector.length(); i++) |
158 if (selector[i] == '}' || selector[i] == '{') | 165 if (selector[i] == '}' || selector[i] == '{') |
159 return EscapeCurlies(i, selector); | 166 replacementCount++; |
167 if (replacementCount) | |
168 return EscapeCurlies(replacementCount, selector); | |
160 | 169 |
161 return OwnedString(selector); | 170 return OwnedString(selector); |
162 } | 171 } |
163 | 172 |
164 OwnedString ElemHideBase::GetSelectorDomain() const | 173 OwnedString ElemHideBase::GetSelectorDomain() const |
165 { | 174 { |
166 /* TODO this is inefficient */ | 175 /* TODO this is inefficient */ |
167 OwnedString result; | 176 OwnedString result; |
168 if (mDomains) | 177 if (mDomains) |
169 { | 178 { |
170 for (const auto& item : *mDomains) | 179 for (const auto& item : *mDomains) |
171 { | 180 { |
172 if (item.second && !item.first.empty()) | 181 if (item.second && !item.first.empty()) |
173 { | 182 { |
174 if (!result.empty()) | 183 if (!result.empty()) |
175 result.append(u','); | 184 result.append(u','); |
176 result.append(item.first); | 185 result.append(item.first); |
177 } | 186 } |
178 } | 187 } |
179 } | 188 } |
180 return result; | 189 return result; |
181 } | 190 } |
LEFT | RIGHT |