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

Side by Side Diff: compiled/Filter.cpp

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Merged filter parsing and normalization Created Feb. 4, 2016, 3:01 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/Filter.h ('k') | compiled/InvalidFilter.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 "Filter.h"
2 #include "CommentFilter.h"
3 #include "InvalidFilter.h"
4 #include "RegExpFilter.h"
5 #include "WhitelistFilter.h"
6 #include "ElemHideBase.h"
7 #include "ElemHideFilter.h"
8 #include "ElemHideException.h"
9 #include "CSSPropertyFilter.h"
10 #include "StringMap.h"
11
12 namespace
13 {
14 StringMap<Filter*> knownFilters(8192);
15
16 void NormalizeWhitespace(String& text)
17 {
18 String::size_type start = 0;
19 String::size_type end = text.length();
20
21 // Remove leading spaces and special characters like line breaks
22 for (; start < end; start++)
23 if (text[start] > ' ')
24 break;
25
26 // Now look for invalid characters inside the string
27 String::size_type pos;
28 for (pos = start; pos < end; pos++)
29 if (text[pos] < ' ')
30 break;
31
32 if (pos < end)
33 {
34 // Found invalid characters, copy all the valid characters while skipping
35 // the invalid ones.
36 String::size_type delta = 1;
37 for (pos = pos + 1; pos < end; pos++)
38 {
39 if (text[pos] < ' ')
40 delta++;
41 else
42 text[pos - delta] = text[pos];
43 }
44 end -= delta;
45 }
46
47 // Remove trailing spaces
48 for (; end > 0; end--)
49 if (text[end - 1] != ' ')
50 break;
51
52 // Set new string boundaries
53 text.reset(text, start, end - start);
54 }
55 }
56
57 Filter::Filter(const String& text)
58 : ref_counted(), mText(text)
59 {
60 annotate_address(this, "Filter");
61 mText.ensure_own_buffer();
62 }
63
64 Filter::~Filter()
65 {
66 // TODO: This should be removing from knownFilters
67 }
68
69 String Filter::Serialize() const
70 {
71 String result(u"[Filter]\ntext="_str);
72 result.append(mText);
73 result.append(u'\n');
74 return std::move(result.ensure_own_buffer());
75 }
76
77 Filter* Filter::FromText(String& text)
78 {
79 NormalizeWhitespace(text);
80 if (text.empty())
81 return nullptr;
82
83 // Parsing also normalizes the filter text, so it has to be done before the
84 // lookup in knownFilters.
85 union
86 {
87 RegExpFilterData regexp;
88 ElemHideData elemhide;
89 } data;
90 String error;
91
92 Filter::Type type = CommentFilter::Parse(text);
93 if (type == Filter::Type::UNKNOWN)
94 type = ElemHideBase::Parse(text, data.elemhide);
95 if (type == Filter::Type::UNKNOWN)
96 type = RegExpFilter::Parse(text, error, data.regexp);
97
98 FilterPtr filter(GetKnownFilter(text));
99 if (filter)
100 return filter;
101
102 switch (type)
103 {
104 case Filter::Type::COMMENT:
105 filter = new CommentFilter(text);
106 break;
107 case Filter::Type::INVALID:
108 filter = new InvalidFilter(text, error);
109 break;
110 case Filter::Type::BLOCKING:
111 filter = new RegExpFilter(text, data.regexp);
112 break;
113 case Filter::Type::WHITELIST:
114 filter = new WhitelistFilter(text, data.regexp);
115 break;
116 case Filter::Type::ELEMHIDE:
117 filter = new ElemHideFilter(text, data.elemhide);
118 break;
119 case Filter::Type::ELEMHIDEEXCEPTION:
120 filter = new ElemHideException(text, data.elemhide);
121 break;
122 case Filter::Type::CSSPROPERTY:
123 filter = new CSSPropertyFilter(text, data.elemhide);
124 if (reinterpret_cast<CSSPropertyFilter*>(filter.get())->IsGeneric())
125 filter = new InvalidFilter(text,
126 u"No active domain specified for CSS property filter"_str);
127 break;
128 default:
129 // This should never happen but just in case
130 return nullptr;
131 }
132
133 enter_context("Adding to known filters");
134 knownFilters[filter->mText] = filter.get();
135 exit_context();
136
137 // TODO: We intentionally leak the filter here - currently it won't be used
138 // for anything and would be deleted immediately.
139 filter->AddRef();
140
141 return filter;
142 }
143
144 Filter* Filter::GetKnownFilter(const String& text)
145 {
146 auto it = knownFilters.find(text);
147 if (it != knownFilters.end())
148 return it->second;
149 else
150 return nullptr;
151 }
OLDNEW
« no previous file with comments | « compiled/Filter.h ('k') | compiled/InvalidFilter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld