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: Minor improvements Created Jan. 20, 2016, 2:41 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 std::u16string& text,
9 const std::u16string& domains, const std::u16string& selector)
10 : ActiveFilter(text, false), selector(selector)
11 {
12 if (!domains.empty())
13 ParseDomains(domains, u',');
14 }
15
16 Filter::Type ElemHideBase::Parse(const std::u16string& text, size_t* domainsEnd,
17 size_t* selectorStart, size_t* prefixEnd, size_t* regexpStart,
18 size_t* regexpEnd, size_t* suffixStart)
19 {
20 StringScanner scanner(text);
21
22 // Domains part
23 loop:
24 while (!scanner.done())
25 {
26 char16_t next = scanner.next();
27 if (next == u'#')
28 {
29 *domainsEnd = scanner.position();
30 break;
31 }
32
33 switch (next)
34 {
35 case u'/':
36 case u'*':
37 case u'|':
38 case u'@':
39 case u'"':
40 case u'!':
41 return Type::UNKNOWN;
42 }
43 }
44
45 bool exception = false;
46 char16_t next = scanner.next();
47 if (next == u'@')
48 {
49 exception = true;
50 next = scanner.next();
51 }
52
53 if (next != u'#')
54 return Type::UNKNOWN;
55
56 // Selector part
57
58 // Selector shouldn't be empty
59 if (scanner.done())
60 return Type::UNKNOWN;
61
62 *selectorStart = scanner.position() + 1;
63 while (!scanner.done())
64 {
65 switch (scanner.next())
66 {
67 case u'{':
68 case u'}':
69 return Type::UNKNOWN;
70 }
71 }
72
73 if (exception)
74 return Type::ELEMHIDEEXCEPTION;
75
76 do
77 {
78 // Is this a CSS property rule maybe?
79 if (!prefixEnd)
80 {
81 // Caller doesn't care
82 break;
83 }
84
85 const std::u16string searchString(u"[-abp-properties=");
86 *prefixEnd = text.find(searchString, *selectorStart);
87 if (*prefixEnd == text.npos ||
88 *prefixEnd + searchString.length() + 1 >= text.length())
89 {
90 break;
91 }
92
93 *regexpStart = *prefixEnd + searchString.length() + 1;
94 char16_t quotation = text[*regexpStart - 1];
95 if (quotation != u'\'' && quotation != u'"')
96 break;
97
98 *regexpEnd = text.find(quotation, *regexpStart);
99 if (*regexpEnd == text.npos || *regexpEnd + 1 >= text.length() ||
100 text[*regexpEnd + 1] != u']')
101 {
102 break;
103 }
104
105 *suffixStart = *regexpEnd + 2;
106 return Type::CSSPROPERTY;
107 } while (false);
108
109 return Type::ELEMHIDE;
110 }
111
112 Filter* ElemHideBase::Create(const std::u16string& text)
113 {
114 size_t domainsEnd;
115 size_t selectorStart;
116 size_t prefixEnd;
117 size_t regexpStart;
118 size_t regexpEnd;
119 size_t suffixStart;
120 Type type = Parse(text, &domainsEnd, &selectorStart, &prefixEnd, &regexpStart,
121 &regexpEnd, &suffixStart);
122 if (type == Type::UNKNOWN)
123 return nullptr;
124
125 try
126 {
127 std::u16string domains(text.substr(0, domainsEnd));
128 std::u16string selector(text.substr(selectorStart));
129 if (type == Type::ELEMHIDEEXCEPTION)
130 return new ElemHideException(text, domains, selector);
131 else if (type == Type::CSSPROPERTY)
132 {
133 std::u16string prefix(text.substr(selectorStart, prefixEnd - selectorStart ));
134 std::u16string regexpSource(text.substr(regexpStart, regexpEnd - regexpSta rt));
135 std::u16string suffix(text.substr(suffixStart));
136 return new CSSPropertyFilter(text, domains, selector, regexpSource, prefix ,
137 suffix);
138 }
139 else
140 return new ElemHideFilter(text, domains, selector);
141 }
142 catch (const std::u16string& e)
143 {
144 return new InvalidFilter(text, e);
145 }
146 }
147
148 const std::u16string ElemHideBase::GetSelectorDomain() const
149 {
150 std::u16string result;
151 for (auto it = domains.begin(); it != domains.end(); ++it)
152 {
153 if (it->second && !it->first.empty())
154 {
155 if (!result.empty())
156 result.append(u",");
157 result.append(it->first);
158 }
159 }
160 return result;
161 }
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