| 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 |
| 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 12 matching lines...) Expand all Loading... |
| 40 // Only spaces before selectorStart position should be removed. | 42 // Only spaces before selectorStart position should be removed. |
| 41 if (pos < selectorStart && text[pos] == ' ') | 43 if (pos < selectorStart && text[pos] == ' ') |
| 42 delta++; | 44 delta++; |
| 43 else | 45 else |
| 44 text[pos - delta] = text[pos]; | 46 text[pos - delta] = text[pos]; |
| 45 } | 47 } |
| 46 selectorStart -= delta; | 48 selectorStart -= delta; |
| 47 | 49 |
| 48 text.reset(text, 0, len - delta); | 50 text.reset(text, 0, len - delta); |
| 49 } | 51 } |
| 52 |
| 53 // Convert filter from the old syntax to the new. |
| 54 OwnedString ConvertFilter(const String& text, String::size_type at) |
| 55 { |
| 56 static const auto propsSelector = u"[-abp-properties="_str; |
| 57 static const auto newPropsSelector = u":-abp-properties("_str; |
| 58 auto selectorPos = text.find(propsSelector, at); |
| 59 if (selectorPos != text.npos) |
| 60 { |
| 61 auto length = text.length(); |
| 62 auto properties = selectorPos + propsSelector.length(); |
| 63 String::value_type quote = 0; |
| 64 bool escape = false; |
| 65 String::size_type removed = 0; // how many chars we remove |
| 66 String::size_type end = properties; |
| 67 String::size_type quote_start = 0; |
| 68 String::size_type quote_end = 0; |
| 69 for (auto index = properties; |
| 70 index < length && end == properties; index++) |
| 71 { |
| 72 if (escape) |
| 73 { |
| 74 escape = false; |
| 75 continue; |
| 76 } |
| 77 |
| 78 auto c = text[index]; |
| 79 switch (c) |
| 80 { |
| 81 case '\\': |
| 82 escape = true; |
| 83 break; |
| 84 case '"': |
| 85 case '\'': |
| 86 if (quote == 0) |
| 87 { |
| 88 quote = c; |
| 89 quote_start = index + 1; |
| 90 } |
| 91 else if (quote == c) |
| 92 { |
| 93 // end of quoted. |
| 94 quote = 0; |
| 95 removed += 2; |
| 96 quote_end = index; |
| 97 } |
| 98 break; |
| 99 case ']': |
| 100 if (quote == 0) |
| 101 end = index + 1; // end of properties (after ]) |
| 102 break; |
| 103 default: |
| 104 break; |
| 105 } |
| 106 } |
| 107 |
| 108 if (quote != 0) |
| 109 quote_end = end - 1; |
| 110 else if (quote_end <= quote_start) |
| 111 { |
| 112 // we likely didn't find a quoted content so we just take it as is. |
| 113 quote_start = properties; |
| 114 quote_end = end - 1; |
| 115 } |
| 116 |
| 117 OwnedString converted(length - removed); |
| 118 String::size_type offset = 0; |
| 119 std::memcpy(converted.data(), text.data(), |
| 120 selectorPos * sizeof(String::value_type)); |
| 121 offset += selectorPos; |
| 122 |
| 123 std::memcpy(converted.data() + offset, newPropsSelector.data(), |
| 124 newPropsSelector.length() * sizeof(String::value_type)); |
| 125 offset += newPropsSelector.length(); |
| 126 |
| 127 std::memcpy(converted.data() + offset, text.data() + quote_start, |
| 128 (quote_end - quote_start) * sizeof(String::value_type)); |
| 129 offset += quote_end - quote_start; |
| 130 |
| 131 std::memcpy(converted.data() + offset, u")", sizeof(String::value_type)); |
| 132 offset++; |
| 133 |
| 134 std::memcpy(converted.data() + offset, text.data() + end, |
| 135 (length - end) * sizeof(String::value_type)); |
| 136 offset += (length - end) * sizeof(String::value_type); |
| 137 |
| 138 return converted; |
| 139 } |
| 140 |
| 141 return OwnedString(text); |
| 142 } |
| 50 } | 143 } |
| 51 | 144 |
| 52 ElemHideBase::ElemHideBase(Type type, const String& text, const ElemHideData& da
ta) | 145 ElemHideBase::ElemHideBase(Type type, const String& text, const ElemHideData& da
ta) |
| 53 : ActiveFilter(type, text, false), mData(data) | 146 : ActiveFilter(type, ConvertFilter(text, data.mSelectorStart), false), |
| 147 mData(data) |
| 54 { | 148 { |
| 55 if (mData.HasDomains()) | 149 if (mData.HasDomains()) |
| 56 ParseDomains(mData.GetDomainsSource(mText), u','); | 150 ParseDomains(mData.GetDomainsSource(mText), u','); |
| 57 } | 151 } |
| 58 | 152 |
| 59 Filter::Type ElemHideBase::Parse(DependentString& text, ElemHideData& data) | 153 Filter::Type ElemHideBase::Parse(DependentString& text, ElemHideData& data) |
| 60 { | 154 { |
| 61 StringScanner scanner(text); | 155 StringScanner scanner(text); |
| 62 | 156 |
| 63 // Domains part | 157 // Domains part |
| (...skipping 16 matching lines...) Expand all Loading... |
| 80 case u'"': | 174 case u'"': |
| 81 case u'!': | 175 case u'!': |
| 82 return Type::UNKNOWN; | 176 return Type::UNKNOWN; |
| 83 case u' ': | 177 case u' ': |
| 84 seenSpaces = true; | 178 seenSpaces = true; |
| 85 break; | 179 break; |
| 86 } | 180 } |
| 87 } | 181 } |
| 88 | 182 |
| 89 seenSpaces |= scanner.skip(u' '); | 183 seenSpaces |= scanner.skip(u' '); |
| 184 bool emulation = false; |
| 90 bool exception = scanner.skipOne(u'@'); | 185 bool exception = scanner.skipOne(u'@'); |
| 91 if (exception) | 186 if (exception) |
| 92 seenSpaces |= scanner.skip(u' '); | 187 seenSpaces |= scanner.skip(u' '); |
| 188 else |
| 189 emulation = scanner.skipOne(u'?'); |
| 93 | 190 |
| 94 String::value_type next = scanner.next(); | 191 String::value_type next = scanner.next(); |
| 95 if (next != u'#') | 192 if (next != u'#') |
| 96 return Type::UNKNOWN; | 193 return Type::UNKNOWN; |
| 97 | 194 |
| 98 // Selector part | 195 // Selector part |
| 99 | 196 |
| 100 // Selector shouldn't be empty | 197 // Selector shouldn't be empty |
| 101 seenSpaces |= scanner.skip(u' '); | 198 seenSpaces |= scanner.skip(u' '); |
| 102 if (scanner.done()) | 199 if (scanner.done()) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 114 } | 211 } |
| 115 | 212 |
| 116 // We are done validating, now we can normalize whitespace and the domain part | 213 // We are done validating, now we can normalize whitespace and the domain part |
| 117 if (seenSpaces) | 214 if (seenSpaces) |
| 118 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart); | 215 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart); |
| 119 DependentString(text, 0, data.mDomainsEnd).toLower(); | 216 DependentString(text, 0, data.mDomainsEnd).toLower(); |
| 120 | 217 |
| 121 if (exception) | 218 if (exception) |
| 122 return Type::ELEMHIDEEXCEPTION; | 219 return Type::ELEMHIDEEXCEPTION; |
| 123 | 220 |
| 124 if (text.find(u"[-abp-properties="_str, data.mSelectorStart) != text.npos) | 221 if (emulation) |
| 125 return Type::ELEMHIDEEMULATION; | 222 return Type::ELEMHIDEEMULATION; |
| 126 | 223 |
| 127 return Type::ELEMHIDE; | 224 return Type::ELEMHIDE; |
| 128 } | 225 } |
| 129 | 226 |
| 130 OwnedString ElemHideBase::GetSelectorDomain() const | 227 OwnedString ElemHideBase::GetSelectorDomain() const |
| 131 { | 228 { |
| 132 /* TODO this is inefficient */ | 229 /* TODO this is inefficient */ |
| 133 OwnedString result; | 230 OwnedString result; |
| 134 if (mDomains) | 231 if (mDomains) |
| 135 { | 232 { |
| 136 for (const auto& item : *mDomains) | 233 for (const auto& item : *mDomains) |
| 137 { | 234 { |
| 138 if (item.second && !item.first.empty()) | 235 if (item.second && !item.first.empty()) |
| 139 { | 236 { |
| 140 if (!result.empty()) | 237 if (!result.empty()) |
| 141 result.append(u','); | 238 result.append(u','); |
| 142 result.append(item.first); | 239 result.append(item.first); |
| 143 } | 240 } |
| 144 } | 241 } |
| 145 } | 242 } |
| 146 return result; | 243 return result; |
| 147 } | 244 } |
| OLD | NEW |