 Issue 29580558:
  Issue 5174 - Allow '{' and '}' in selectors  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluscore/
    
  
    Issue 29580558:
  Issue 5174 - Allow '{' and '}' in selectors  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluscore/| Index: compiled/filter/ElemHideBase.cpp | 
| =================================================================== | 
| --- a/compiled/filter/ElemHideBase.cpp | 
| +++ b/compiled/filter/ElemHideBase.cpp | 
| @@ -10,16 +10,18 @@ | 
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
| * GNU General Public License for more details. | 
| * | 
| * You should have received a copy of the GNU General Public License | 
| * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 
| */ | 
| +#include <cstring> | 
| + | 
| #include "ElemHideBase.h" | 
| #include "../StringScanner.h" | 
| namespace | 
| { | 
| void NormalizeWhitespace(DependentString& text, String::size_type& domainsEnd, | 
| String::size_type& selectorStart) | 
| { | 
| @@ -98,40 +100,83 @@ | 
| // Selector part | 
| // Selector shouldn't be empty | 
| seenSpaces |= scanner.skip(u' '); | 
| if (scanner.done()) | 
| return Type::UNKNOWN; | 
| data.mSelectorStart = scanner.position() + 1; | 
| - while (!scanner.done()) | 
| - { | 
| - switch (scanner.next()) | 
| - { | 
| - case u'{': | 
| - case u'}': | 
| - return Type::UNKNOWN; | 
| - } | 
| - } | 
| // We are done validating, now we can normalize whitespace and the domain part | 
| if (seenSpaces) | 
| NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart); | 
| DependentString(text, 0, data.mDomainsEnd).toLower(); | 
| if (exception) | 
| return Type::ELEMHIDEEXCEPTION; | 
| if (text.find(u"[-abp-properties="_str, data.mSelectorStart) != text.npos) | 
| return Type::ELEMHIDEEMULATION; | 
| return Type::ELEMHIDE; | 
| } | 
| +namespace | 
| +{ | 
| + | 
| 
Wladimir Palant
2017/10/19 07:59:28
Nit: This newline is unnecessary. And the block co
 
hub
2017/10/19 12:48:26
Done.
 | 
| +static constexpr String::value_type OPENING_CURLY_REPLACEMENT[] = u"\\7B "; | 
| +static constexpr String::value_type CLOSING_CURLY_REPLACEMENT[] = u"\\7D "; | 
| +static constexpr String::size_type CURLY_REPLACEMENT_SIZE = sizeof(OPENING_CURLY_REPLACEMENT) / sizeof(OPENING_CURLY_REPLACEMENT[0]) - 1; | 
| + | 
| +OwnedString EscapeCurlies(String::size_type replacementCount, | 
| + const DependentString& str) | 
| +{ | 
| + OwnedString result(str.length() + replacementCount * (CURLY_REPLACEMENT_SIZE - 1)); | 
| + | 
| + String::value_type* current = result.data(); | 
| + for (String::size_type i = 0; i < str.length(); i++) | 
| + { | 
| + switch(str[i]) | 
| + { | 
| + case u'}': | 
| + std::memcpy(current, CLOSING_CURLY_REPLACEMENT, | 
| + sizeof(String::value_type) * CURLY_REPLACEMENT_SIZE); | 
| + current += CURLY_REPLACEMENT_SIZE; | 
| + break; | 
| + case u'{': | 
| + std::memcpy(current, OPENING_CURLY_REPLACEMENT, | 
| + sizeof(String::value_type) * CURLY_REPLACEMENT_SIZE); | 
| + current += CURLY_REPLACEMENT_SIZE; | 
| + break; | 
| + default: | 
| + *current = str[i]; | 
| + current++; | 
| + break; | 
| + } | 
| + } | 
| + | 
| + return result; | 
| +} | 
| + | 
| +} | 
| + | 
| +OwnedString ElemHideBase::GetSelector() const | 
| +{ | 
| + DependentString selector = mData.GetSelector(mText); | 
| + String::size_type replacementCount = 0; | 
| + for (String::size_type i = 0; i < selector.length(); i++) | 
| + if (selector[i] == '}' || selector[i] == '{') | 
| + replacementCount++; | 
| + if (replacementCount) | 
| + return EscapeCurlies(replacementCount, selector); | 
| + | 
| + return OwnedString(selector); | 
| +} | 
| + | 
| OwnedString ElemHideBase::GetSelectorDomain() const | 
| { | 
| /* TODO this is inefficient */ | 
| OwnedString result; | 
| if (mDomains) | 
| { | 
| for (const auto& item : *mDomains) | 
| { |