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: Using embind Created Jan. 15, 2016, 8:44 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 <emscripten/bind.h>
2 #include <unordered_map>
3
4 #include "Filter.h"
5 #include "CommentFilter.h"
6 #include "RegExpFilter.h"
7 #include "ElemHideFilter.h"
8 #include "ElemHideException.h"
9
10 namespace
11 {
12 std::unordered_map<std::wstring,FilterPtr> knownFilters;
13
14 void trim_spaces(std::wstring& str)
15 {
16 size_t pos;
17
18 // Remove leading whitespace
19 pos = str.find_first_not_of(L' ');
20 if (pos > 0)
21 str.erase(0, pos);
22
23 // Remove trailing whitespace
24 pos = str.find_last_not_of(L' ');
25 if (pos < str.length() - 1)
26 str.erase(pos + 1);
27 }
28
29 void remove_spaces(std::wstring& str)
30 {
31 for (size_t i = 0, l = str.length(); i < l; ++i)
32 {
33 if (str[i] == L' ')
34 {
35 str.erase(i, 1);
36 --i;
37 --l;
38 }
39 }
40 }
41 }
42
43 Filter::Filter(const std::wstring& text)
44 {
45 this->text = text;
46 }
47
48 const std::wstring Filter::Serialize()
49 {
50 return (
51 L"[Filter]\n"
52 L"text=" + text + L"\n"
53 );
54 }
55
56 FilterPtr Filter::FromText(const std::wstring& text)
57 {
58 auto it = knownFilters.find(text);
59 if (it != knownFilters.end())
60 return it->second;
61
62 FilterPtr filter = FilterPtr(CommentFilter::Create(text));
63 if (!filter)
64 filter.reset(ElemHideBase::Create(text));
65 if (!filter)
66 filter.reset(RegExpFilter::Create(text));
67 return knownFilters[text] = filter;
68 }
69
70 const std::wstring Filter::Normalize(const std::wstring& text)
71 {
72 std::wstring result(text);
73
74 // Remove special characters like line breaks
75 for (size_t i = 0, l = result.length(); i < l; ++i)
76 {
77 if (result[i] < L' ')
78 {
79 result.erase(i, 1);
80 --i;
81 --l;
82 }
83 }
84
85 trim_spaces(result);
86
87 {
88 size_t domainsEnd;
89 size_t selectorStart;
90 Filter::Type type = ElemHideBase::Parse(result, &domainsEnd, &selectorStart) ;
91 if (type != Filter::Type::UNKNOWN)
92 {
93 std::wstring domains = result.substr(0, domainsEnd);
94 std::wstring selector = result.substr(selectorStart);
95 remove_spaces(domains);
96 trim_spaces(selector);
97 return domains + (
98 type == Filter::Type::ELEMHIDEEXCEPTION ? L"#@#" : L"##"
99 ) + selector;
100 }
101 }
102
103 if (CommentFilter::Parse(result) == Filter::Type::UNKNOWN)
104 remove_spaces(result);
105 return result;
106 }
107
108 EMSCRIPTEN_BINDINGS(filter)
109 {
110 using namespace emscripten;
111 enum_<Filter::Type>("FilterType")
112 .value("INVALID", Filter::Type::INVALID)
113 .value("COMMENT", Filter::Type::COMMENT)
114 .value("BLOCKING", Filter::Type::BLOCKING)
115 .value("WHITELIST", Filter::Type::WHITELIST)
116 .value("ELEMHIDE", Filter::Type::ELEMHIDE)
117 .value("ELEMHIDEEXCEPTION", Filter::Type::ELEMHIDEEXCEPTION)
118 .value("CSSPROPERTY", Filter::Type::CSSPROPERTY);
119
120 class_<Filter>("Filter")
121 .property("text", &Filter::GetText)
122 .property("type", &Filter::GetType)
123 .function("serialize", &Filter::Serialize)
124 .class_function("fromText", &Filter::FromText)
125 .class_function("normalize", &Filter::Normalize)
126 .smart_ptr<FilterPtr>("shared_ptr");
127 }
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