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

Unified Diff: compiled/filter/ElemHideBase.cpp

Issue 29595633: Issue 5870 - Implement the new ElemHideEmulation filter type (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Reworked the logic and added tests Created Jan. 31, 2018, 9:07 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') | compiled/filter/Filter.cpp » ('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
@@ -47,17 +47,18 @@
}
selectorStart -= delta;
text.reset(text, 0, len - delta);
}
}
ElemHideBase::ElemHideBase(Type type, const String& text, const ElemHideData& data)
- : ActiveFilter(type, text, false), mData(data)
+ : ActiveFilter(type, text, false),
+ mData(data)
{
if (mData.HasDomains())
ParseDomains(mData.GetDomainsSource(mText), u',');
}
Filter::Type ElemHideBase::Parse(DependentString& text, ElemHideData& data)
{
StringScanner scanner(text);
@@ -84,47 +85,175 @@
return Type::UNKNOWN;
case u' ':
seenSpaces = true;
break;
}
}
seenSpaces |= scanner.skip(u' ');
+ bool emulation = false;
bool exception = scanner.skipOne(u'@');
if (exception)
seenSpaces |= scanner.skip(u' ');
+ else
+ emulation = scanner.skipOne(u'?');
String::value_type next = scanner.next();
if (next != u'#')
return Type::UNKNOWN;
// Selector part
// Selector shouldn't be empty
seenSpaces |= scanner.skip(u' ');
if (scanner.done())
return Type::UNKNOWN;
data.mSelectorStart = scanner.position() + 1;
+ data.mNeedConversion = false;
// 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();
+ // We still need to check the old syntax. It will be converted when
+ // we instantiate the filter.
+ if (!emulation &&
+ text.find(u"[-abp-properties="_str, data.mSelectorStart) != text.npos)
+ {
+ data.mNeedConversion = true;
+ emulation = !exception;
sergei 2018/02/05 14:51:04 Should it be an invalid filter if it's exception w
hub 2018/02/07 04:13:35 no. This is actually how exceptions for element hi
+ }
+
if (exception)
return Type::ELEMHIDEEXCEPTION;
- if (text.find(u"[-abp-properties="_str, data.mSelectorStart) != text.npos)
+ if (emulation)
return Type::ELEMHIDEEMULATION;
return Type::ELEMHIDE;
}
+// Convert filter from the old syntax to the new.
+OwnedString ElemHideBase::ConvertFilter(const String& text, String::size_type& at)
+{
+ static const auto propsSelector = u"[-abp-properties="_str;
+ static const auto newPropsSelector = u":-abp-properties("_str;
+ static const auto elemHideDelimiter = u"##"_str;
+ auto selectorPos = text.find(propsSelector, at);
+ if (selectorPos != text.npos)
+ {
+ auto length = text.length();
+ auto properties = selectorPos + propsSelector.length();
+ String::value_type quote = 0;
+ bool escape = false;
+ String::size_type removed = 0; // how many chars we remove
+ String::size_type end = properties;
+ String::size_type quote_start = 0;
+ String::size_type quote_end = 0;
+ for (auto index = properties;
+ index < length && end == properties; index++)
+ {
+ if (escape)
+ {
+ escape = false;
+ continue;
+ }
+
+ auto c = text[index];
+ switch (c)
+ {
+ case '\\':
+ escape = true;
+ break;
+ case '"':
+ case '\'':
+ if (quote == 0)
+ {
+ quote = c;
+ quote_start = index + 1;
+ }
+ else if (quote == c)
+ {
+ // end of quoted.
+ quote = 0;
+ removed += 2;
+ quote_end = index;
+ }
+ break;
+ case ']':
+ if (quote == 0)
+ end = index + 1; // end of properties (after ])
+ break;
+ default:
+ break;
+ }
+ }
+
+ if (quote != 0)
+ quote_end = end - 1;
+ else if (quote_end <= quote_start)
+ {
+ // we likely didn't find a quoted content so we just take it as is.
+ quote_start = properties;
+ quote_end = end - 1;
+ }
+
+ // +1 for the replacement of "##" by "#?#"
+ String::size_type offset = 0;
+
+ String::size_type delimiter = text.find(elemHideDelimiter);
+ OwnedString converted(length + ((delimiter != text.npos) ? 1 : 0) - removed);
+ if (delimiter != text.npos)
+ {
+ if (delimiter >= selectorPos)
+ return OwnedString(text);
+
+ at++;
+ std::memcpy(converted.data(), text.data(),
+ delimiter * sizeof(String::value_type));
+ offset += delimiter;
+ std::memcpy(converted.data() + offset, u"#?#",
+ 3 * sizeof(String::value_type));
+ offset += 3;
+ delimiter += 2;
+ // we have already parsed to past the delimiter.
+ selectorPos -= delimiter;
+ }
+ else
+ delimiter = 0;
+
+
+ std::memcpy(converted.data() + offset, text.data() + delimiter,
+ selectorPos * sizeof(String::value_type));
+ offset += selectorPos;
+
+ std::memcpy(converted.data() + offset, newPropsSelector.data(),
+ newPropsSelector.length() * sizeof(String::value_type));
+ offset += newPropsSelector.length();
+
+ std::memcpy(converted.data() + offset, text.data() + quote_start,
+ (quote_end - quote_start) * sizeof(String::value_type));
+ offset += quote_end - quote_start;
+
+ std::memcpy(converted.data() + offset, u")", sizeof(String::value_type));
+ offset++;
+
+ std::memcpy(converted.data() + offset, text.data() + end,
+ (length - end) * sizeof(String::value_type));
+ offset += (length - end) * sizeof(String::value_type);
+
+ return converted;
+ }
+
+ return OwnedString(text);
+}
+
namespace
{
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)
« no previous file with comments | « compiled/filter/ElemHideBase.h ('k') | compiled/filter/Filter.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld