| 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 ABP_NS_USING | 23 ABP_NS_USING |
| 22 | 24 |
| 23 namespace | 25 namespace |
| 24 { | 26 { |
| 25 void NormalizeWhitespace(DependentString& text, String::size_type& domainsEnd, | 27 void NormalizeWhitespace(DependentString& text, String::size_type& domainsEnd, |
| 26 String::size_type& selectorStart) | 28 String::size_type& selectorStart) |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 return Type::UNKNOWN; | 100 return Type::UNKNOWN; |
| 99 | 101 |
| 100 // Selector part | 102 // Selector part |
| 101 | 103 |
| 102 // Selector shouldn't be empty | 104 // Selector shouldn't be empty |
| 103 seenSpaces |= scanner.skip(u' '); | 105 seenSpaces |= scanner.skip(u' '); |
| 104 if (scanner.done()) | 106 if (scanner.done()) |
| 105 return Type::UNKNOWN; | 107 return Type::UNKNOWN; |
| 106 | 108 |
| 107 data.mSelectorStart = scanner.position() + 1; | 109 data.mSelectorStart = scanner.position() + 1; |
| 108 while (!scanner.done()) | |
| 109 { | |
| 110 switch (scanner.next()) | |
| 111 { | |
| 112 case u'{': | |
| 113 case u'}': | |
| 114 return Type::UNKNOWN; | |
| 115 } | |
| 116 } | |
| 117 | 110 |
| 118 // We are done validating, now we can normalize whitespace and the domain part | 111 // We are done validating, now we can normalize whitespace and the domain part |
| 119 if (seenSpaces) | 112 if (seenSpaces) |
| 120 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart); | 113 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart); |
| 121 DependentString(text, 0, data.mDomainsEnd).toLower(); | 114 DependentString(text, 0, data.mDomainsEnd).toLower(); |
| 122 | 115 |
| 123 if (exception) | 116 if (exception) |
| 124 return Type::ELEMHIDEEXCEPTION; | 117 return Type::ELEMHIDEEXCEPTION; |
| 125 | 118 |
| 126 if (text.find(u"[-abp-properties="_str, data.mSelectorStart) != text.npos) | 119 if (text.find(u"[-abp-properties="_str, data.mSelectorStart) != text.npos) |
| 127 return Type::ELEMHIDEEMULATION; | 120 return Type::ELEMHIDEEMULATION; |
| 128 | 121 |
| 129 return Type::ELEMHIDE; | 122 return Type::ELEMHIDE; |
| 123 } |
| 124 |
| 125 namespace |
| 126 { |
| 127 static constexpr String::value_type OPENING_CURLY_REPLACEMENT[] = u"\\7B "; |
| 128 static constexpr String::value_type CLOSING_CURLY_REPLACEMENT[] = u"\\7D "; |
| 129 static constexpr String::size_type CURLY_REPLACEMENT_SIZE = sizeof(OPENING_CUR
LY_REPLACEMENT) / sizeof(OPENING_CURLY_REPLACEMENT[0]) - 1; |
| 130 |
| 131 OwnedString EscapeCurlies(String::size_type replacementCount, |
| 132 const DependentString& str) |
| 133 { |
| 134 OwnedString result(str.length() + replacementCount * (CURLY_REPLACEMENT_SIZE
- 1)); |
| 135 |
| 136 String::value_type* current = result.data(); |
| 137 for (String::size_type i = 0; i < str.length(); i++) |
| 138 { |
| 139 switch(str[i]) |
| 140 { |
| 141 case u'}': |
| 142 std::memcpy(current, CLOSING_CURLY_REPLACEMENT, |
| 143 sizeof(String::value_type) * CURLY_REPLACEMENT_SIZE); |
| 144 current += CURLY_REPLACEMENT_SIZE; |
| 145 break; |
| 146 case u'{': |
| 147 std::memcpy(current, OPENING_CURLY_REPLACEMENT, |
| 148 sizeof(String::value_type) * CURLY_REPLACEMENT_SIZE); |
| 149 current += CURLY_REPLACEMENT_SIZE; |
| 150 break; |
| 151 default: |
| 152 *current = str[i]; |
| 153 current++; |
| 154 break; |
| 155 } |
| 156 } |
| 157 |
| 158 return result; |
| 159 } |
| 160 } |
| 161 |
| 162 OwnedString ElemHideBase::GetSelector() const |
| 163 { |
| 164 const 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); |
| 130 } | 173 } |
| 131 | 174 |
| 132 OwnedString ElemHideBase::GetSelectorDomain() const | 175 OwnedString ElemHideBase::GetSelectorDomain() const |
| 133 { | 176 { |
| 134 /* TODO this is inefficient */ | 177 /* TODO this is inefficient */ |
| 135 OwnedString result; | 178 OwnedString result; |
| 136 if (mDomains) | 179 if (mDomains) |
| 137 { | 180 { |
| 138 for (const auto& item : *mDomains) | 181 for (const auto& item : *mDomains) |
| 139 { | 182 { |
| 140 if (item.second && !item.first.empty()) | 183 if (item.second && !item.first.empty()) |
| 141 { | 184 { |
| 142 if (!result.empty()) | 185 if (!result.empty()) |
| 143 result.append(u','); | 186 result.append(u','); |
| 144 result.append(item.first); | 187 result.append(item.first); |
| 145 } | 188 } |
| 146 } | 189 } |
| 147 } | 190 } |
| 148 return result; | 191 return result; |
| 149 } | 192 } |
| LEFT | RIGHT |