OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 118 |
119 /** | 119 /** |
120 * Add a new element hiding filter | 120 * Add a new element hiding filter |
121 * @param {ElemHideBase} filter | 121 * @param {ElemHideBase} filter |
122 */ | 122 */ |
123 add(filter) | 123 add(filter) |
124 { | 124 { |
125 if (knownFilters.has(filter)) | 125 if (knownFilters.has(filter)) |
126 return; | 126 return; |
127 | 127 |
| 128 let {selector} = filter; |
| 129 |
128 if (filter instanceof ElemHideException) | 130 if (filter instanceof ElemHideException) |
129 { | 131 { |
130 let {selector} = filter; | |
131 let list = exceptions.get(selector); | 132 let list = exceptions.get(selector); |
132 if (list) | 133 if (list) |
133 list.push(filter); | 134 list.push(filter); |
134 else | 135 else |
135 exceptions.set(selector, [filter]); | 136 exceptions.set(selector, [filter]); |
136 | 137 |
137 // If this is the first exception for a previously unconditionally | 138 // If this is the first exception for a previously unconditionally |
138 // applied element hiding selector we need to take care to update the | 139 // applied element hiding selector we need to take care to update the |
139 // lookups. | 140 // lookups. |
140 let unconditionalFilterForSelector = filterBySelector.get(selector); | 141 let unconditionalFilterForSelector = filterBySelector.get(selector); |
141 if (unconditionalFilterForSelector) | 142 if (unconditionalFilterForSelector) |
142 { | 143 { |
143 addToFiltersByDomain(unconditionalFilterForSelector); | 144 addToFiltersByDomain(unconditionalFilterForSelector); |
144 filterBySelector.delete(selector); | 145 filterBySelector.delete(selector); |
145 unconditionalSelectors = null; | 146 unconditionalSelectors = null; |
146 } | 147 } |
147 } | 148 } |
148 else if (!(filter.domains || exceptions.has(filter.selector))) | 149 else if (!(filter.domains || exceptions.has(selector))) |
149 { | 150 { |
150 // The new filter's selector is unconditionally applied to all domains | 151 // The new filter's selector is unconditionally applied to all domains |
151 filterBySelector.set(filter.selector, filter); | 152 filterBySelector.set(selector, filter); |
152 unconditionalSelectors = null; | 153 unconditionalSelectors = null; |
153 } | 154 } |
154 else | 155 else |
155 { | 156 { |
156 // The new filter's selector only applies to some domains | 157 // The new filter's selector only applies to some domains |
157 addToFiltersByDomain(filter); | 158 addToFiltersByDomain(filter); |
158 } | 159 } |
159 | 160 |
160 knownFilters.add(filter); | 161 knownFilters.add(filter); |
161 FilterNotifier.emit("elemhideupdate"); | 162 FilterNotifier.emit("elemhideupdate"); |
162 }, | 163 }, |
163 | 164 |
164 /** | 165 /** |
165 * Removes an element hiding filter | 166 * Removes an element hiding filter |
166 * @param {ElemHideBase} filter | 167 * @param {ElemHideBase} filter |
167 */ | 168 */ |
168 remove(filter) | 169 remove(filter) |
169 { | 170 { |
170 if (!knownFilters.has(filter)) | 171 if (!knownFilters.has(filter)) |
171 return; | 172 return; |
172 | 173 |
| 174 let {selector} = filter; |
| 175 |
173 // Whitelisting filters | 176 // Whitelisting filters |
174 if (filter instanceof ElemHideException) | 177 if (filter instanceof ElemHideException) |
175 { | 178 { |
176 let list = exceptions.get(filter.selector); | 179 let list = exceptions.get(selector); |
177 let index = list.indexOf(filter); | 180 let index = list.indexOf(filter); |
178 if (index >= 0) | 181 if (index >= 0) |
179 list.splice(index, 1); | 182 list.splice(index, 1); |
180 } | 183 } |
181 // Unconditially applied element hiding filters | 184 // Unconditially applied element hiding filters |
182 else if (filterBySelector.get(filter.selector) == filter) | 185 else if (filterBySelector.get(selector) == filter) |
183 { | 186 { |
184 filterBySelector.delete(filter.selector); | 187 filterBySelector.delete(selector); |
185 unconditionalSelectors = null; | 188 unconditionalSelectors = null; |
186 } | 189 } |
187 // Conditionally applied element hiding filters | 190 // Conditionally applied element hiding filters |
188 else | 191 else |
189 { | 192 { |
190 let domains = filter.domains || defaultDomains; | 193 let domains = filter.domains || defaultDomains; |
191 for (let domain of domains.keys()) | 194 for (let domain of domains.keys()) |
192 { | 195 { |
193 let filters = filtersByDomain.get(domain); | 196 let filters = filtersByDomain.get(domain); |
194 if (filters) | 197 if (filters) |
195 filters.delete(filter); | 198 filters.delete(filter); |
196 } | 199 } |
197 } | 200 } |
198 | 201 |
199 knownFilters.delete(filter); | 202 knownFilters.delete(filter); |
200 FilterNotifier.emit("elemhideupdate"); | 203 FilterNotifier.emit("elemhideupdate"); |
201 }, | 204 }, |
202 | 205 |
203 /** | 206 /** |
204 * Checks whether an exception rule is registered for a filter on a particular | 207 * Checks whether an exception rule is registered for a selector on a |
205 * domain. | 208 * particular domain. |
206 * @param {Filter} filter | 209 * @param {string} selector |
207 * @param {?string} docDomain | 210 * @param {?string} docDomain |
208 * @return {?ElemHideException} | 211 * @return {?ElemHideException} |
209 */ | 212 */ |
210 getException(filter, docDomain) | 213 getException(selector, docDomain) |
211 { | 214 { |
212 let list = exceptions.get(filter.selector); | 215 let list = exceptions.get(selector); |
213 if (!list) | 216 if (!list) |
214 return null; | 217 return null; |
215 | 218 |
216 for (let i = list.length - 1; i >= 0; i--) | 219 for (let i = list.length - 1; i >= 0; i--) |
217 { | 220 { |
218 if (list[i].isActiveOnDomain(docDomain)) | 221 if (list[i].isActiveOnDomain(docDomain)) |
219 return list[i]; | 222 return list[i]; |
220 } | 223 } |
221 | 224 |
222 return null; | 225 return null; |
(...skipping 22 matching lines...) Expand all Loading... |
245 | 248 |
246 let filters = filtersByDomain.get(currentDomain); | 249 let filters = filtersByDomain.get(currentDomain); |
247 if (filters) | 250 if (filters) |
248 { | 251 { |
249 for (let [filter, isIncluded] of filters) | 252 for (let [filter, isIncluded] of filters) |
250 { | 253 { |
251 if (!isIncluded) | 254 if (!isIncluded) |
252 { | 255 { |
253 excluded.add(filter); | 256 excluded.add(filter); |
254 } | 257 } |
255 else if ((excluded.size == 0 || !excluded.has(filter)) && | 258 else |
256 !this.getException(filter, domain)) | |
257 { | 259 { |
258 selectors.push(filter.selector); | 260 let {selector} = filter; |
| 261 if ((excluded.size == 0 || !excluded.has(filter)) && |
| 262 !this.getException(selector, domain)) |
| 263 { |
| 264 selectors.push(selector); |
| 265 } |
259 } | 266 } |
260 } | 267 } |
261 } | 268 } |
262 | 269 |
263 if (currentDomain == "") | 270 if (currentDomain == "") |
264 break; | 271 break; |
265 | 272 |
266 let nextDot = currentDomain.indexOf("."); | 273 let nextDot = currentDomain.indexOf("."); |
267 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | 274 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); |
268 } | 275 } |
269 | 276 |
270 if (!specificOnly) | 277 if (!specificOnly) |
271 selectors = getUnconditionalSelectors().concat(selectors); | 278 selectors = getUnconditionalSelectors().concat(selectors); |
272 | 279 |
273 return selectors; | 280 return selectors; |
274 } | 281 } |
275 }; | 282 }; |
OLD | NEW |