LEFT | RIGHT |
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(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 |
| 48 bool seenSpaces = false; |
28 while (!scanner.done()) | 49 while (!scanner.done()) |
29 { | 50 { |
30 String::value_type next = scanner.next(); | 51 String::value_type next = scanner.next(); |
31 if (next == u'#') | 52 if (next == u'#') |
32 { | 53 { |
33 *domainsEnd = scanner.position(); | 54 data.mDomainsEnd = scanner.position(); |
34 break; | 55 break; |
35 } | 56 } |
36 | 57 |
37 switch (next) | 58 switch (next) |
38 { | 59 { |
39 case u'/': | 60 case u'/': |
40 case u'*': | 61 case u'*': |
41 case u'|': | 62 case u'|': |
42 case u'@': | 63 case u'@': |
43 case u'"': | 64 case u'"': |
44 case u'!': | 65 case u'!': |
45 return Type::UNKNOWN; | 66 return Type::UNKNOWN; |
| 67 case u' ': |
| 68 seenSpaces = true; |
| 69 break; |
46 } | 70 } |
47 } | 71 } |
48 | 72 |
49 bool exception = false; | 73 seenSpaces |= scanner.skip(u' '); |
| 74 bool exception = scanner.skipOne(u'@'); |
| 75 if (exception) |
| 76 seenSpaces |= scanner.skip(u' '); |
| 77 |
50 String::value_type next = scanner.next(); | 78 String::value_type next = scanner.next(); |
51 if (next == u'@') | |
52 { | |
53 exception = true; | |
54 next = scanner.next(); | |
55 } | |
56 | |
57 if (next != u'#') | 79 if (next != u'#') |
58 return Type::UNKNOWN; | 80 return Type::UNKNOWN; |
59 | 81 |
60 // Selector part | 82 // Selector part |
61 | 83 |
62 // Selector shouldn't be empty | 84 // Selector shouldn't be empty |
| 85 seenSpaces |= scanner.skip(u' '); |
63 if (scanner.done()) | 86 if (scanner.done()) |
64 return Type::UNKNOWN; | 87 return Type::UNKNOWN; |
65 | 88 |
66 *selectorStart = scanner.position() + 1; | 89 data.mSelectorStart = scanner.position() + 1; |
67 while (!scanner.done()) | 90 while (!scanner.done()) |
68 { | 91 { |
69 switch (scanner.next()) | 92 switch (scanner.next()) |
70 { | 93 { |
71 case u'{': | 94 case u'{': |
72 case u'}': | 95 case u'}': |
73 return Type::UNKNOWN; | 96 return Type::UNKNOWN; |
74 } | 97 } |
75 } | 98 } |
76 | 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 |
77 if (exception) | 105 if (exception) |
78 return Type::ELEMHIDEEXCEPTION; | 106 return Type::ELEMHIDEEXCEPTION; |
79 | 107 |
80 do | 108 do |
81 { | 109 { |
82 // Is this a CSS property rule maybe? | 110 // Is this a CSS property rule maybe? |
83 if (!prefixEnd) | 111 DependentString searchString(u"[-abp-properties="_str); |
84 { | 112 data.mPrefixEnd = text.find(searchString, data.mSelectorStart); |
85 // Caller doesn't care | 113 if (data.mPrefixEnd == text.npos || |
86 break; | 114 data.mPrefixEnd + searchString.length() + 1 >= text.length()) |
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 { | 115 { |
94 break; | 116 break; |
95 } | 117 } |
96 | 118 |
97 *regexpStart = *prefixEnd + searchString.length() + 1; | 119 data.mRegexpStart = data.mPrefixEnd + searchString.length() + 1; |
98 char16_t quotation = text[*regexpStart - 1]; | 120 char16_t quotation = text[data.mRegexpStart - 1]; |
99 if (quotation != u'\'' && quotation != u'"') | 121 if (quotation != u'\'' && quotation != u'"') |
100 break; | 122 break; |
101 | 123 |
102 *regexpEnd = text.find(quotation, *regexpStart); | 124 data.mRegexpEnd = text.find(quotation, data.mRegexpStart); |
103 if (*regexpEnd == text.npos || *regexpEnd + 1 >= text.length() || | 125 if (data.mRegexpEnd == text.npos || data.mRegexpEnd + 1 >= text.length() || |
104 text[*regexpEnd + 1] != u']') | 126 text[data.mRegexpEnd + 1] != u']') |
105 { | 127 { |
106 break; | 128 break; |
107 } | 129 } |
108 | 130 |
109 *suffixStart = *regexpEnd + 2; | 131 data.mSuffixStart = data.mRegexpEnd + 2; |
110 return Type::CSSPROPERTY; | 132 return Type::CSSPROPERTY; |
111 } while (false); | 133 } while (false); |
112 | 134 |
113 return Type::ELEMHIDE; | 135 return Type::ELEMHIDE; |
114 } | 136 } |
115 | 137 |
116 Filter* ElemHideBase::Create(const String& text) | 138 OwnedString ElemHideBase::GetSelectorDomain() const |
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, ®expStart, | |
125 ®expEnd, &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 { | 139 { |
149 /* TODO this is inefficient */ | 140 /* TODO this is inefficient */ |
150 String result; | 141 OwnedString result; |
151 if (mDomains) | 142 if (mDomains) |
152 { | 143 { |
153 for (auto it = mDomains->begin(); it != mDomains->end(); ++it) | 144 for (auto it = mDomains->begin(); it != mDomains->end(); ++it) |
154 { | 145 { |
155 if (it->second && !it->first.empty()) | 146 if (it->second && !it->first.empty()) |
156 { | 147 { |
157 if (!result.empty()) | 148 if (!result.empty()) |
158 result.append(u','); | 149 result.append(u','); |
159 result.append(it->first); | 150 result.append(it->first); |
160 } | 151 } |
161 } | 152 } |
162 } | 153 } |
163 return std::move(result.ensure_own_buffer()); | 154 return result; |
164 } | 155 } |
LEFT | RIGHT |