LEFT | RIGHT |
1 #include "ElemHideBase.h" | 1 #include "ElemHideBase.h" |
2 #include "CSSPropertyFilter.h" | 2 #include "CSSPropertyFilter.h" |
3 #include "StringScanner.h" | 3 #include "StringScanner.h" |
4 | 4 |
5 namespace | 5 namespace |
6 { | 6 { |
7 void NormalizeWhitespace(String& text, String::size_type& domainsEnd, | 7 void NormalizeWhitespace(DependentString& text, String::size_type& domainsEnd, |
8 String::size_type& selectorStart) | 8 String::size_type& selectorStart) |
9 { | 9 { |
10 // For element hiding filters we only want to remove spaces preceding the | 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 | 11 // selector part. The positions we've determined already have to be adjusted |
12 // accordingly. | 12 // accordingly. |
13 | 13 |
14 String::size_type delta = 0; | 14 String::size_type delta = 0; |
15 String::size_type len = text.length(); | 15 String::size_type len = text.length(); |
16 | 16 |
17 // The first character is guaranteed to be a non-space, the string has been | 17 // The first character is guaranteed to be a non-space, the string has been |
18 // trimmed earlier. | 18 // trimmed earlier. |
19 for (String::size_type pos = 1; pos < len; pos++) | 19 for (String::size_type pos = 1; pos < len; pos++) |
20 { | 20 { |
21 if (pos == domainsEnd) | 21 if (pos == domainsEnd) |
22 domainsEnd -= delta; | 22 domainsEnd -= delta; |
23 | 23 |
24 // Only spaces before selectorStart position should be removed. | 24 // Only spaces before selectorStart position should be removed. |
25 if (pos < selectorStart && text[pos] == ' ') | 25 if (pos < selectorStart && text[pos] == ' ') |
26 delta++; | 26 delta++; |
27 else | 27 else |
28 text[pos - delta] = text[pos]; | 28 text[pos - delta] = text[pos]; |
29 } | 29 } |
30 selectorStart -= delta; | 30 selectorStart -= delta; |
31 | 31 |
32 text.reset(text, 0, len - delta); | 32 text.reset(text, 0, len - delta); |
33 } | 33 } |
34 } | 34 } |
35 | 35 |
36 ElemHideBase::ElemHideBase(const String& text, const ElemHideBaseData& data) | 36 ElemHideBase::ElemHideBase(Type type, const String& text, const ElemHideBaseData
& data) |
37 : ActiveFilter(text, false), ElemHideBaseData(data) | 37 : ActiveFilter(type, text, false), mData(data) |
38 { | 38 { |
39 if (HasDomains()) | 39 if (mData.HasDomains()) |
40 ParseDomains(GetDomainsSource(mText), u','); | 40 ParseDomains(mData.GetDomainsSource(mText), u','); |
41 } | 41 } |
42 | 42 |
43 Filter::Type ElemHideBase::Parse(String& text, ElemHideData& data) | 43 Filter::Type ElemHideBase::Parse(DependentString& text, ElemHideData& data) |
44 { | 44 { |
45 StringScanner scanner(text); | 45 StringScanner scanner(text); |
46 | 46 |
47 // Domains part | 47 // Domains part |
48 bool seenSpaces = false; | 48 bool seenSpaces = false; |
49 while (!scanner.done()) | 49 while (!scanner.done()) |
50 { | 50 { |
51 String::value_type next = scanner.next(); | 51 String::value_type next = scanner.next(); |
52 if (next == u'#') | 52 if (next == u'#') |
53 { | 53 { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 { | 93 { |
94 case u'{': | 94 case u'{': |
95 case u'}': | 95 case u'}': |
96 return Type::UNKNOWN; | 96 return Type::UNKNOWN; |
97 } | 97 } |
98 } | 98 } |
99 | 99 |
100 // We are done validating, now we can normalize whitespace and the domain part | 100 // We are done validating, now we can normalize whitespace and the domain part |
101 if (seenSpaces) | 101 if (seenSpaces) |
102 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart); | 102 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart); |
103 ToLower(text, 0, data.mDomainsEnd); | 103 DependentString(text, 0, data.mDomainsEnd).toLower(); |
104 | 104 |
105 if (exception) | 105 if (exception) |
106 return Type::ELEMHIDEEXCEPTION; | 106 return Type::ELEMHIDEEXCEPTION; |
107 | 107 |
108 do | 108 do |
109 { | 109 { |
110 // Is this a CSS property rule maybe? | 110 // Is this a CSS property rule maybe? |
111 String searchString(u"[-abp-properties="_str); | 111 DependentString searchString(u"[-abp-properties="_str); |
112 data.mPrefixEnd = text.find(searchString, data.mSelectorStart); | 112 data.mPrefixEnd = text.find(searchString, data.mSelectorStart); |
113 if (data.mPrefixEnd == text.npos || | 113 if (data.mPrefixEnd == text.npos || |
114 data.mPrefixEnd + searchString.length() + 1 >= text.length()) | 114 data.mPrefixEnd + searchString.length() + 1 >= text.length()) |
115 { | 115 { |
116 break; | 116 break; |
117 } | 117 } |
118 | 118 |
119 data.mRegexpStart = data.mPrefixEnd + searchString.length() + 1; | 119 data.mRegexpStart = data.mPrefixEnd + searchString.length() + 1; |
120 char16_t quotation = text[data.mRegexpStart - 1]; | 120 char16_t quotation = text[data.mRegexpStart - 1]; |
121 if (quotation != u'\'' && quotation != u'"') | 121 if (quotation != u'\'' && quotation != u'"') |
122 break; | 122 break; |
123 | 123 |
124 data.mRegexpEnd = text.find(quotation, data.mRegexpStart); | 124 data.mRegexpEnd = text.find(quotation, data.mRegexpStart); |
125 if (data.mRegexpEnd == text.npos || data.mRegexpEnd + 1 >= text.length() || | 125 if (data.mRegexpEnd == text.npos || data.mRegexpEnd + 1 >= text.length() || |
126 text[data.mRegexpEnd + 1] != u']') | 126 text[data.mRegexpEnd + 1] != u']') |
127 { | 127 { |
128 break; | 128 break; |
129 } | 129 } |
130 | 130 |
131 data.mSuffixStart = data.mRegexpEnd + 2; | 131 data.mSuffixStart = data.mRegexpEnd + 2; |
132 return Type::CSSPROPERTY; | 132 return Type::CSSPROPERTY; |
133 } while (false); | 133 } while (false); |
134 | 134 |
135 return Type::ELEMHIDE; | 135 return Type::ELEMHIDE; |
136 } | 136 } |
137 | 137 |
138 String ElemHideBase::GetSelectorDomain() const | 138 OwnedString ElemHideBase::GetSelectorDomain() const |
139 { | 139 { |
140 /* TODO this is inefficient */ | 140 /* TODO this is inefficient */ |
141 String result; | 141 OwnedString result; |
142 if (mDomains) | 142 if (mDomains) |
143 { | 143 { |
144 for (auto it = mDomains->begin(); it != mDomains->end(); ++it) | 144 for (auto it = mDomains->begin(); it != mDomains->end(); ++it) |
145 { | 145 { |
146 if (it->second && !it->first.empty()) | 146 if (it->second && !it->first.empty()) |
147 { | 147 { |
148 if (!result.empty()) | 148 if (!result.empty()) |
149 result.append(u','); | 149 result.append(u','); |
150 result.append(it->first); | 150 result.append(it->first); |
151 } | 151 } |
152 } | 152 } |
153 } | 153 } |
154 return std::move(result.ensure_own_buffer()); | 154 return result; |
155 } | 155 } |
LEFT | RIGHT |