LEFT | RIGHT |
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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
20 /** | 20 /** |
21 * @fileOverview Element hiding implementation. | 21 * @fileOverview Element hiding implementation. |
22 */ | 22 */ |
23 | 23 |
24 let {Utils} = require("utils"); | 24 const {ElemHideException} = require("filterClasses"); |
25 let {ElemHideException} = require("filterClasses"); | 25 const {FilterNotifier} = require("filterNotifier"); |
26 let {FilterNotifier} = require("filterNotifier"); | |
27 | 26 |
28 /** | 27 /** |
29 * Lookup table, filters by their associated key | 28 * Lookup table, filters by their associated key |
30 * @type {Object} | 29 * @type {Object} |
31 */ | 30 */ |
32 let filterByKey = []; | 31 let filterByKey = []; |
33 | 32 |
34 /** | 33 /** |
35 * Lookup table, keys of the filters by filter text | 34 * Lookup table, keys of the filters by filter text |
36 * @type {Object} | 35 * @type {Object} |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 this._removeFilterKey(key, filter); | 218 this._removeFilterKey(key, filter); |
220 } | 219 } |
221 | 220 |
222 FilterNotifier.emit("elemhideupdate"); | 221 FilterNotifier.emit("elemhideupdate"); |
223 }, | 222 }, |
224 | 223 |
225 /** | 224 /** |
226 * Checks whether an exception rule is registered for a filter on a particular | 225 * Checks whether an exception rule is registered for a filter on a particular |
227 * domain. | 226 * domain. |
228 * @param {Filter} filter | 227 * @param {Filter} filter |
229 * @param {String} docDomain | 228 * @param {string} docDomain |
230 * @return {ElemHideException} | 229 * @return {ElemHideException} |
231 */ | 230 */ |
232 getException(filter, docDomain) | 231 getException(filter, docDomain) |
233 { | 232 { |
234 if (!(filter.selector in exceptions)) | 233 if (!(filter.selector in exceptions)) |
235 return null; | 234 return null; |
236 | 235 |
237 let list = exceptions[filter.selector]; | 236 let list = exceptions[filter.selector]; |
238 for (let i = list.length - 1; i >= 0; i--) | 237 for (let i = list.length - 1; i >= 0; i--) |
239 { | 238 { |
240 if (list[i].isActiveOnDomain(docDomain)) | 239 if (list[i].isActiveOnDomain(docDomain)) |
241 return list[i]; | 240 return list[i]; |
242 } | 241 } |
243 | 242 |
244 return null; | 243 return null; |
245 }, | 244 }, |
246 | 245 |
247 /** | 246 /** |
248 * Retrieves an element hiding filter by the corresponding protocol key | 247 * Retrieves an element hiding filter by the corresponding protocol key |
249 * @param {Number} key | 248 * @param {number} key |
250 * @return {Filter} | 249 * @return {Filter} |
251 */ | 250 */ |
252 getFilterByKey(key) | 251 getFilterByKey(key) |
253 { | 252 { |
254 return (key in filterByKey ? filterByKey[key] : null); | 253 return (key in filterByKey ? filterByKey[key] : null); |
255 }, | 254 }, |
256 | 255 |
257 /** | 256 /** |
258 * Returns a list of all selectors as a nested map. On first level, the keys | 257 * Returns a list of all selectors as a nested map. On first level, the keys |
259 * are all values of `ElemHideBase.selectorDomain` (domains on which these | 258 * are all values of `ElemHideBase.selectorDomain` (domains on which these |
260 * selectors should apply, ignoring exceptions). The values are maps again, | 259 * selectors should apply, ignoring exceptions). The values are maps again, |
261 * with the keys being selectors and values the corresponding filter keys. | 260 * with the keys being selectors and values the corresponding filter keys. |
262 * @returns {Map.<String,Map<String,String>>} | 261 * @returns {Map.<String,Map<String,String>>} |
263 */ | 262 */ |
264 getSelectors() | 263 getSelectors() |
265 { | 264 { |
266 let domains = new Map(); | 265 let domains = new Map(); |
267 for (let key in filterByKey) | 266 for (let key in filterByKey) |
268 { | 267 { |
269 let filter = filterByKey[key]; | 268 let filter = filterByKey[key]; |
270 let {selector} = filter; | 269 if (!filter.selector) |
271 if (!selector) | |
272 continue; | 270 continue; |
273 | 271 |
274 let domain = filter.selectorDomain || ""; | 272 let domain = filter.selectorDomain || ""; |
275 | 273 |
276 if (!domains.has(domain)) | 274 if (!domains.has(domain)) |
277 domains.set(domain, new Map()); | 275 domains.set(domain, new Map()); |
278 domains.get(domain).set(selector, key); | 276 domains.get(domain).set(filter.selector, key); |
279 } | 277 } |
280 | 278 |
281 return domains; | 279 return domains; |
282 }, | 280 }, |
283 | 281 |
284 /** | 282 /** |
285 * Returns a list of selectors that apply on each website unconditionally. | 283 * Returns a list of selectors that apply on each website unconditionally. |
286 * @returns {String[]} | 284 * @returns {string[]} |
287 */ | 285 */ |
288 getUnconditionalSelectors() | 286 getUnconditionalSelectors() |
289 { | 287 { |
290 if (!unconditionalSelectors) | 288 if (!unconditionalSelectors) |
291 unconditionalSelectors = Object.keys(filterKeyBySelector); | 289 unconditionalSelectors = Object.keys(filterKeyBySelector); |
292 return unconditionalSelectors.slice(); | 290 return unconditionalSelectors.slice(); |
293 }, | 291 }, |
294 | 292 |
295 /** | 293 /** |
296 * Returns a list of filter keys for selectors which apply to all websites | 294 * Returns a list of filter keys for selectors which apply to all websites |
297 * without exception. | 295 * without exception. |
298 * @returns {Number[]} | 296 * @returns {number[]} |
299 */ | 297 */ |
300 getUnconditionalFilterKeys() | 298 getUnconditionalFilterKeys() |
301 { | 299 { |
302 if (!unconditionalFilterKeys) | 300 if (!unconditionalFilterKeys) |
303 { | 301 { |
304 let selectors = this.getUnconditionalSelectors(); | 302 let selectors = this.getUnconditionalSelectors(); |
305 unconditionalFilterKeys = []; | 303 unconditionalFilterKeys = []; |
306 for (let selector of selectors) | 304 for (let selector of selectors) |
307 unconditionalFilterKeys.push(filterKeyBySelector[selector]); | 305 unconditionalFilterKeys.push(filterKeyBySelector[selector]); |
308 } | 306 } |
(...skipping 16 matching lines...) Expand all Loading... |
325 /** | 323 /** |
326 * Constant used by getSelectorsForDomain to return only selectors for filters | 324 * Constant used by getSelectorsForDomain to return only selectors for filters |
327 * which specifically match the given host name. | 325 * which specifically match the given host name. |
328 */ | 326 */ |
329 SPECIFIC_ONLY: 2, | 327 SPECIFIC_ONLY: 2, |
330 | 328 |
331 /** | 329 /** |
332 * Determines from the current filter list which selectors should be applied | 330 * Determines from the current filter list which selectors should be applied |
333 * on a particular host name. Optionally returns the corresponding filter | 331 * on a particular host name. Optionally returns the corresponding filter |
334 * keys. | 332 * keys. |
335 * @param {String} domain | 333 * @param {string} domain |
336 * @param {Number} [criteria] | 334 * @param {number} [criteria] |
337 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or | 335 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or |
338 * ElemHide.SPECIFIC_ONLY. | 336 * ElemHide.SPECIFIC_ONLY. |
339 * @param {Boolean} [provideFilterKeys] | 337 * @param {boolean} [provideFilterKeys] |
340 * If true, the function will return a list of corresponding filter keys in | 338 * If true, the function will return a list of corresponding filter keys in |
341 * addition to selectors. | 339 * addition to selectors. |
342 * @returns {string[]|Array.<string[]>} | 340 * @returns {string[]|Array.<string[]>} |
343 * List of selectors or an array with two elements (list of selectors and | 341 * List of selectors or an array with two elements (list of selectors and |
344 * list of corresponding keys) if provideFilterKeys is true. | 342 * list of corresponding keys) if provideFilterKeys is true. |
345 */ | 343 */ |
346 getSelectorsForDomain(domain, criteria, provideFilterKeys) | 344 getSelectorsForDomain(domain, criteria, provideFilterKeys) |
347 { | 345 { |
348 let filterKeys = []; | 346 let filterKeys = []; |
349 let selectors = []; | 347 let selectors = []; |
350 | 348 |
351 if (typeof criteria == "undefined") | 349 if (typeof criteria == "undefined") |
352 criteria = ElemHide.ALL_MATCHING; | 350 criteria = ElemHide.ALL_MATCHING; |
353 if (criteria < ElemHide.NO_UNCONDITIONAL) | 351 if (criteria < ElemHide.NO_UNCONDITIONAL) |
354 { | 352 { |
355 selectors = this.getUnconditionalSelectors(); | 353 selectors = this.getUnconditionalSelectors(); |
356 if (provideFilterKeys) | 354 if (provideFilterKeys) |
357 filterKeys = this.getUnconditionalFilterKeys(); | 355 filterKeys = this.getUnconditionalFilterKeys(); |
358 } | 356 } |
359 | 357 |
360 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); | 358 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); |
361 let seenFilters = Object.create(null); | 359 let seenFilters = Object.create(null); |
362 let currentDomain = domain ? domain.toUpperCase() : ""; | 360 let currentDomain = domain ? domain.toUpperCase() : ""; |
363 /* eslint-disable no-constant-condition */ | |
364 while (true) | 361 while (true) |
365 { | 362 { |
366 if (specificOnly && currentDomain == "") | 363 if (specificOnly && currentDomain == "") |
367 break; | 364 break; |
368 | 365 |
369 let filters = filtersByDomain[currentDomain]; | 366 let filters = filtersByDomain[currentDomain]; |
370 if (filters) | 367 if (filters) |
371 { | 368 { |
372 for (let filterKey in filters) | 369 for (let filterKey in filters) |
373 { | 370 { |
(...skipping 10 matching lines...) Expand all Loading... |
384 } | 381 } |
385 } | 382 } |
386 } | 383 } |
387 | 384 |
388 if (currentDomain == "") | 385 if (currentDomain == "") |
389 break; | 386 break; |
390 | 387 |
391 let nextDot = currentDomain.indexOf("."); | 388 let nextDot = currentDomain.indexOf("."); |
392 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | 389 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); |
393 } | 390 } |
394 /* eslint-enable no-constant-condition */ | |
395 | 391 |
396 if (provideFilterKeys) | 392 if (provideFilterKeys) |
397 return [selectors, filterKeys]; | 393 return [selectors, filterKeys]; |
398 return selectors; | 394 return selectors; |
399 } | 395 } |
400 }; | 396 }; |
LEFT | RIGHT |