Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: compiled/filter/ElemHideBase.cpp

Issue 29580558: Issue 5174 - Allow '{' and '}' in selectors (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Preallocate the destination Created Oct. 17, 2017, 7:21 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « compiled/filter/ElemHideBase.h ('k') | test/filterClasses.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: compiled/filter/ElemHideBase.cpp
===================================================================
--- a/compiled/filter/ElemHideBase.cpp
+++ b/compiled/filter/ElemHideBase.cpp
@@ -98,40 +98,91 @@
// 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/18 09:24:02 Please put the opening curly on the next line and
hub 2017/10/18 12:53:47 Done.
+
+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.
+
+OwnedString EscapeCurlies(String::size_type count, const DependentString& str)
+{
+ 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.
+
+ String::size_type start = 0;
+ String::size_type i = 0;
+ String::value_type* current = result.data();
+ for (; i < str.length(); i++)
+ {
+ if (str[i] == '}' || str[i] == '{')
+ {
+ if (i != start)
+ {
+ std::memcpy(current, str.data() + start,
+ sizeof(String::value_type) * (i - start));
+ 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.
+ }
+ start = i + 1;
+ switch(str[i])
+ {
+ 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.
+ std::memcpy(current, u"\\x7D ",
+ 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.
+ current += REPLACE_SIZE;
+ break;
+ case '{':
+ std::memcpy(current, u"\\x7B ",
+ sizeof(String::value_type) * REPLACE_SIZE);
+ current += REPLACE_SIZE;
+ break;
+ default:
+ break;
+ }
+ }
+ }
+ if (start < i)
+ std::memcpy(current, str.data() + start,
+ sizeof(String::value_type) * (i - start));
+ return result;
+}
+
+}
+
+OwnedString ElemHideBase::GetSelector() const
+{
+ DependentString selector = mData.GetSelector(mText);
+ 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.
+ for (String::size_type i = 0; i < selector.length(); i++)
+ if (selector[i] == '}' || selector[i] == '{')
+ count++;
+ if (count)
+ return EscapeCurlies(count, selector);
+
+ return OwnedString(selector);
+}
+
OwnedString ElemHideBase::GetSelectorDomain() const
{
/* TODO this is inefficient */
OwnedString result;
if (mDomains)
{
for (const auto& item : *mDomains)
{
« no previous file with comments | « compiled/filter/ElemHideBase.h ('k') | test/filterClasses.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld