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

Side by Side 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: Rebased Created Dec. 5, 2017, 6:04 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | compiled/filter/Filter.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 // Only spaces before selectorStart position should be removed. 42 // Only spaces before selectorStart position should be removed.
43 if (pos < selectorStart && text[pos] == ' ') 43 if (pos < selectorStart && text[pos] == ' ')
44 delta++; 44 delta++;
45 else 45 else
46 text[pos - delta] = text[pos]; 46 text[pos - delta] = text[pos];
47 } 47 }
48 selectorStart -= delta; 48 selectorStart -= delta;
49 49
50 text.reset(text, 0, len - delta); 50 text.reset(text, 0, len - delta);
51 } 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 }
52 } 143 }
53 144
54 ElemHideBase::ElemHideBase(Type type, const String& text, const ElemHideData& da ta) 145 ElemHideBase::ElemHideBase(Type type, const String& text, const ElemHideData& da ta)
55 : ActiveFilter(type, text, false), mData(data) 146 : ActiveFilter(type, ConvertFilter(text, data.mSelectorStart), false),
147 mData(data)
56 { 148 {
57 if (mData.HasDomains()) 149 if (mData.HasDomains())
58 ParseDomains(mData.GetDomainsSource(mText), u','); 150 ParseDomains(mData.GetDomainsSource(mText), u',');
59 } 151 }
60 152
61 Filter::Type ElemHideBase::Parse(DependentString& text, ElemHideData& data) 153 Filter::Type ElemHideBase::Parse(DependentString& text, ElemHideData& data)
62 { 154 {
63 StringScanner scanner(text); 155 StringScanner scanner(text);
64 156
65 // Domains part 157 // Domains part
(...skipping 16 matching lines...) Expand all
82 case u'"': 174 case u'"':
83 case u'!': 175 case u'!':
84 return Type::UNKNOWN; 176 return Type::UNKNOWN;
85 case u' ': 177 case u' ':
86 seenSpaces = true; 178 seenSpaces = true;
87 break; 179 break;
88 } 180 }
89 } 181 }
90 182
91 seenSpaces |= scanner.skip(u' '); 183 seenSpaces |= scanner.skip(u' ');
184 bool emulation = false;
92 bool exception = scanner.skipOne(u'@'); 185 bool exception = scanner.skipOne(u'@');
93 if (exception) 186 if (exception)
94 seenSpaces |= scanner.skip(u' '); 187 seenSpaces |= scanner.skip(u' ');
188 else
189 emulation = scanner.skipOne(u'?');
95 190
96 String::value_type next = scanner.next(); 191 String::value_type next = scanner.next();
97 if (next != u'#') 192 if (next != u'#')
98 return Type::UNKNOWN; 193 return Type::UNKNOWN;
99 194
100 // Selector part 195 // Selector part
101 196
102 // Selector shouldn't be empty 197 // Selector shouldn't be empty
103 seenSpaces |= scanner.skip(u' '); 198 seenSpaces |= scanner.skip(u' ');
104 if (scanner.done()) 199 if (scanner.done())
105 return Type::UNKNOWN; 200 return Type::UNKNOWN;
106 201
107 data.mSelectorStart = scanner.position() + 1; 202 data.mSelectorStart = scanner.position() + 1;
108 203
109 // We are done validating, now we can normalize whitespace and the domain part 204 // We are done validating, now we can normalize whitespace and the domain part
110 if (seenSpaces) 205 if (seenSpaces)
111 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart); 206 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart);
112 DependentString(text, 0, data.mDomainsEnd).toLower(); 207 DependentString(text, 0, data.mDomainsEnd).toLower();
113 208
114 if (exception) 209 if (exception)
115 return Type::ELEMHIDEEXCEPTION; 210 return Type::ELEMHIDEEXCEPTION;
116 211
117 if (text.find(u"[-abp-properties="_str, data.mSelectorStart) != text.npos) 212 if (emulation)
118 return Type::ELEMHIDEEMULATION; 213 return Type::ELEMHIDEEMULATION;
119 214
120 return Type::ELEMHIDE; 215 return Type::ELEMHIDE;
121 } 216 }
122 217
123 namespace 218 namespace
124 { 219 {
125 static constexpr String::value_type OPENING_CURLY_REPLACEMENT[] = u"\\7B "; 220 static constexpr String::value_type OPENING_CURLY_REPLACEMENT[] = u"\\7B ";
126 static constexpr String::value_type CLOSING_CURLY_REPLACEMENT[] = u"\\7D "; 221 static constexpr String::value_type CLOSING_CURLY_REPLACEMENT[] = u"\\7D ";
127 static constexpr String::size_type CURLY_REPLACEMENT_SIZE = sizeof(OPENING_CUR LY_REPLACEMENT) / sizeof(OPENING_CURLY_REPLACEMENT[0]) - 1; 222 static constexpr String::size_type CURLY_REPLACEMENT_SIZE = sizeof(OPENING_CUR LY_REPLACEMENT) / sizeof(OPENING_CURLY_REPLACEMENT[0]) - 1;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 if (item.second && !item.first.empty()) 276 if (item.second && !item.first.empty())
182 { 277 {
183 if (!result.empty()) 278 if (!result.empty())
184 result.append(u','); 279 result.append(u',');
185 result.append(item.first); 280 result.append(item.first);
186 } 281 }
187 } 282 }
188 } 283 }
189 return result; 284 return result;
190 } 285 }
OLDNEW
« no previous file with comments | « no previous file | compiled/filter/Filter.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld