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

Delta Between Two Patch Sets: compiled/ElemHideBase.cpp

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Left Patch Set: Fixed annotation for hash buffers Created Jan. 28, 2016, 5:07 p.m.
Right Patch Set: Addressed comments from Patch Set 28 Created March 21, 2017, 10:04 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « compiled/ElemHideBase.h ('k') | compiled/ElemHideException.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #include "ElemHideBase.h" 1 #include "ElemHideBase.h"
2 #include "ElemHideFilter.h"
3 #include "ElemHideException.h"
4 #include "CSSPropertyFilter.h" 2 #include "CSSPropertyFilter.h"
5 #include "InvalidFilter.h"
6 #include "StringScanner.h" 3 #include "StringScanner.h"
7 4
8 ElemHideBase::ElemHideBase(const String& text, 5 namespace
9 String::size_type domainsEnd, String::size_type selectorStart)
10 : ActiveFilter(text, false),
11 mSelector(String(mText, selectorStart))
12 { 6 {
13 if (domainsEnd) 7 void NormalizeWhitespace(DependentString& text, String::size_type& domainsEnd,
14 ParseDomains(String(mText, 0, domainsEnd), u','); 8 String::size_type& selectorStart)
9 {
10 // For element hiding filters we only want to remove spaces preceding the
11 // selector part. The positions we've determined already have to be adjusted
12 // accordingly.
13
14 String::size_type delta = 0;
15 String::size_type len = text.length();
16
17 // The first character is guaranteed to be a non-space, the string has been
18 // trimmed earlier.
19 for (String::size_type pos = 1; pos < len; pos++)
20 {
21 if (pos == domainsEnd)
22 domainsEnd -= delta;
23
24 // Only spaces before selectorStart position should be removed.
25 if (pos < selectorStart && text[pos] == ' ')
26 delta++;
27 else
28 text[pos - delta] = text[pos];
29 }
30 selectorStart -= delta;
31
32 text.reset(text, 0, len - delta);
33 }
15 } 34 }
16 35
17 Filter::Type ElemHideBase::Parse(const String& text, 36 ElemHideBase::ElemHideBase(Type type, const String& text, const ElemHideBaseData & data)
18 String::size_type* domainsEnd, 37 : ActiveFilter(type, text, false), mData(data)
19 String::size_type* selectorStart, 38 {
20 String::size_type* prefixEnd, 39 if (mData.HasDomains())
21 String::size_type* regexpStart, 40 ParseDomains(mData.GetDomainsSource(mText), u',');
22 String::size_type* regexpEnd, 41 }
23 String::size_type* suffixStart) 42
43 Filter::Type ElemHideBase::Parse(DependentString& text, ElemHideData& data)
24 { 44 {
25 StringScanner scanner(text); 45 StringScanner scanner(text);
26 46
27 // Domains part 47 // Domains part
28 loop: 48 bool seenSpaces = false;
29 while (!scanner.done()) 49 while (!scanner.done())
30 { 50 {
31 String::value_type next = scanner.next(); 51 String::value_type next = scanner.next();
32 if (next == u'#') 52 if (next == u'#')
33 { 53 {
34 *domainsEnd = scanner.position(); 54 data.mDomainsEnd = scanner.position();
35 break; 55 break;
36 } 56 }
37 57
38 switch (next) 58 switch (next)
39 { 59 {
40 case u'/': 60 case u'/':
41 case u'*': 61 case u'*':
42 case u'|': 62 case u'|':
43 case u'@': 63 case u'@':
44 case u'"': 64 case u'"':
45 case u'!': 65 case u'!':
46 return Type::UNKNOWN; 66 return Type::UNKNOWN;
67 case u' ':
68 seenSpaces = true;
69 break;
47 } 70 }
48 } 71 }
49 72
50 bool exception = false; 73 seenSpaces |= scanner.skip(u' ');
74 bool exception = scanner.skipOne(u'@');
75 if (exception)
76 seenSpaces |= scanner.skip(u' ');
77
51 String::value_type next = scanner.next(); 78 String::value_type next = scanner.next();
52 if (next == u'@')
53 {
54 exception = true;
55 next = scanner.next();
56 }
57
58 if (next != u'#') 79 if (next != u'#')
59 return Type::UNKNOWN; 80 return Type::UNKNOWN;
60 81
61 // Selector part 82 // Selector part
62 83
63 // Selector shouldn't be empty 84 // Selector shouldn't be empty
85 seenSpaces |= scanner.skip(u' ');
64 if (scanner.done()) 86 if (scanner.done())
65 return Type::UNKNOWN; 87 return Type::UNKNOWN;
66 88
67 *selectorStart = scanner.position() + 1; 89 data.mSelectorStart = scanner.position() + 1;
68 while (!scanner.done()) 90 while (!scanner.done())
69 { 91 {
70 switch (scanner.next()) 92 switch (scanner.next())
71 { 93 {
72 case u'{': 94 case u'{':
73 case u'}': 95 case u'}':
74 return Type::UNKNOWN; 96 return Type::UNKNOWN;
75 } 97 }
76 } 98 }
77 99
100 // We are done validating, now we can normalize whitespace and the domain part
101 if (seenSpaces)
102 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart);
103 DependentString(text, 0, data.mDomainsEnd).toLower();
104
78 if (exception) 105 if (exception)
79 return Type::ELEMHIDEEXCEPTION; 106 return Type::ELEMHIDEEXCEPTION;
80 107
81 do 108 do
82 { 109 {
83 // Is this a CSS property rule maybe? 110 // Is this a CSS property rule maybe?
84 if (!prefixEnd) 111 DependentString searchString(u"[-abp-properties="_str);
85 { 112 data.mPrefixEnd = text.find(searchString, data.mSelectorStart);
86 // Caller doesn't care 113 if (data.mPrefixEnd == text.npos ||
87 break; 114 data.mPrefixEnd + searchString.length() + 1 >= text.length())
88 }
89
90 String searchString(u"[-abp-properties="_str);
91 *prefixEnd = text.find(searchString, *selectorStart);
92 if (*prefixEnd == text.npos ||
93 *prefixEnd + searchString.length() + 1 >= text.length())
94 { 115 {
95 break; 116 break;
96 } 117 }
97 118
98 *regexpStart = *prefixEnd + searchString.length() + 1; 119 data.mRegexpStart = data.mPrefixEnd + searchString.length() + 1;
99 char16_t quotation = text[*regexpStart - 1]; 120 char16_t quotation = text[data.mRegexpStart - 1];
100 if (quotation != u'\'' && quotation != u'"') 121 if (quotation != u'\'' && quotation != u'"')
101 break; 122 break;
102 123
103 *regexpEnd = text.find(quotation, *regexpStart); 124 data.mRegexpEnd = text.find(quotation, data.mRegexpStart);
104 if (*regexpEnd == text.npos || *regexpEnd + 1 >= text.length() || 125 if (data.mRegexpEnd == text.npos || data.mRegexpEnd + 1 >= text.length() ||
105 text[*regexpEnd + 1] != u']') 126 text[data.mRegexpEnd + 1] != u']')
106 { 127 {
107 break; 128 break;
108 } 129 }
109 130
110 *suffixStart = *regexpEnd + 2; 131 data.mSuffixStart = data.mRegexpEnd + 2;
111 return Type::CSSPROPERTY; 132 return Type::CSSPROPERTY;
112 } while (false); 133 } while (false);
113 134
114 return Type::ELEMHIDE; 135 return Type::ELEMHIDE;
115 } 136 }
116 137
117 Filter* ElemHideBase::Create(const String& text) 138 OwnedString ElemHideBase::GetSelectorDomain() const
118 {
119 size_t domainsEnd;
120 size_t selectorStart;
121 size_t prefixEnd;
122 size_t regexpStart;
123 size_t regexpEnd;
124 size_t suffixStart;
125 Type type = Parse(text, &domainsEnd, &selectorStart, &prefixEnd, &regexpStart,
126 &regexpEnd, &suffixStart);
127 if (type == Type::UNKNOWN)
128 return nullptr;
129
130 try
131 {
132 if (type == Type::ELEMHIDEEXCEPTION)
133 return new ElemHideException(text, domainsEnd, selectorStart);
134 else if (type == Type::CSSPROPERTY)
135 {
136 return new CSSPropertyFilter(text, domainsEnd, selectorStart, prefixEnd,
137 regexpStart, regexpEnd, suffixStart);
138 }
139 else
140 return new ElemHideFilter(text, domainsEnd, selectorStart);
141 }
142 catch (const String& e)
143 {
144 return new InvalidFilter(text, e);
145 }
146 }
147
148 String ElemHideBase::GetSelectorDomain() const
149 { 139 {
150 /* TODO this is inefficient */ 140 /* TODO this is inefficient */
151 String result; 141 OwnedString result;
152 if (mDomains) 142 if (mDomains)
153 { 143 {
154 for (auto it = mDomains->begin(); it != mDomains->end(); ++it) 144 for (auto it = mDomains->begin(); it != mDomains->end(); ++it)
155 { 145 {
156 if (it->second && !it->first.empty()) 146 if (it->second && !it->first.empty())
157 { 147 {
158 if (!result.empty()) 148 if (!result.empty())
159 result.append(u','); 149 result.append(u',');
160 result.append(it->first); 150 result.append(it->first);
161 } 151 }
162 } 152 }
163 } 153 }
164 return std::move(result); 154 return result;
165 } 155 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld