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

Side by Side Diff: compiled/ElemHideBase.cpp

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Addressed comments, made String class slightly more sane, slightly cleaned up bindings.cpp Created Feb. 2, 2016, 5:48 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 | « compiled/ElemHideBase.h ('k') | compiled/ElemHideException.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include "ElemHideBase.h"
2 #include "ElemHideFilter.h"
3 #include "ElemHideException.h"
4 #include "CSSPropertyFilter.h"
5 #include "InvalidFilter.h"
6 #include "StringScanner.h"
7
8 ElemHideBase::ElemHideBase(const String& text,
9 String::size_type domainsEnd, String::size_type selectorStart)
10 : ActiveFilter(text, false),
11 mSelector(String(mText, selectorStart))
12 {
13 if (domainsEnd)
14 ParseDomains(mText, 0, domainsEnd, u',');
15 }
16
17 Filter::Type ElemHideBase::Parse(const String& text,
18 String::size_type* domainsEnd,
19 String::size_type* selectorStart,
20 String::size_type* prefixEnd,
21 String::size_type* regexpStart,
22 String::size_type* regexpEnd,
23 String::size_type* suffixStart)
24 {
25 StringScanner scanner(text);
26
27 // Domains part
28 while (!scanner.done())
29 {
30 String::value_type next = scanner.next();
31 if (next == u'#')
32 {
33 *domainsEnd = scanner.position();
34 break;
35 }
36
37 switch (next)
38 {
39 case u'/':
40 case u'*':
41 case u'|':
42 case u'@':
43 case u'"':
44 case u'!':
45 return Type::UNKNOWN;
46 }
47 }
48
49 bool exception = false;
50 String::value_type next = scanner.next();
51 if (next == u'@')
52 {
53 exception = true;
54 next = scanner.next();
55 }
56
57 if (next != u'#')
58 return Type::UNKNOWN;
59
60 // Selector part
61
62 // Selector shouldn't be empty
63 if (scanner.done())
64 return Type::UNKNOWN;
65
66 *selectorStart = scanner.position() + 1;
67 while (!scanner.done())
68 {
69 switch (scanner.next())
70 {
71 case u'{':
72 case u'}':
73 return Type::UNKNOWN;
74 }
75 }
76
77 if (exception)
78 return Type::ELEMHIDEEXCEPTION;
79
80 do
81 {
82 // Is this a CSS property rule maybe?
83 if (!prefixEnd)
84 {
85 // Caller doesn't care
86 break;
87 }
88
89 String searchString(u"[-abp-properties="_str);
90 *prefixEnd = text.find(searchString, *selectorStart);
91 if (*prefixEnd == text.npos ||
92 *prefixEnd + searchString.length() + 1 >= text.length())
93 {
94 break;
95 }
96
97 *regexpStart = *prefixEnd + searchString.length() + 1;
98 char16_t quotation = text[*regexpStart - 1];
99 if (quotation != u'\'' && quotation != u'"')
100 break;
101
102 *regexpEnd = text.find(quotation, *regexpStart);
103 if (*regexpEnd == text.npos || *regexpEnd + 1 >= text.length() ||
104 text[*regexpEnd + 1] != u']')
105 {
106 break;
107 }
108
109 *suffixStart = *regexpEnd + 2;
110 return Type::CSSPROPERTY;
111 } while (false);
112
113 return Type::ELEMHIDE;
114 }
115
116 Filter* ElemHideBase::Create(const String& text)
117 {
118 size_t domainsEnd;
119 size_t selectorStart;
120 size_t prefixEnd;
121 size_t regexpStart;
122 size_t regexpEnd;
123 size_t suffixStart;
124 Type type = Parse(text, &domainsEnd, &selectorStart, &prefixEnd, &regexpStart,
125 &regexpEnd, &suffixStart);
126 if (type == Type::UNKNOWN)
127 return nullptr;
128
129 try
130 {
131 if (type == Type::ELEMHIDEEXCEPTION)
132 return new ElemHideException(text, domainsEnd, selectorStart);
133 else if (type == Type::CSSPROPERTY)
134 {
135 return new CSSPropertyFilter(text, domainsEnd, selectorStart, prefixEnd,
136 regexpStart, regexpEnd, suffixStart);
137 }
138 else
139 return new ElemHideFilter(text, domainsEnd, selectorStart);
140 }
141 catch (const String& e)
142 {
143 return new InvalidFilter(text, e);
144 }
145 }
146
147 String ElemHideBase::GetSelectorDomain() const
148 {
149 /* TODO this is inefficient */
150 String result;
151 if (mDomains)
152 {
153 for (auto it = mDomains->begin(); it != mDomains->end(); ++it)
154 {
155 if (it->second && !it->first.empty())
156 {
157 if (!result.empty())
158 result.append(u',');
159 result.append(it->first);
160 }
161 }
162 }
163 return std::move(result.ensure_own_buffer());
164 }
OLDNEW
« no previous file with comments | « compiled/ElemHideBase.h ('k') | compiled/ElemHideException.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld