OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 "use strict"; |
| 19 |
| 20 /** |
| 21 * @fileOverview Element hiding exceptions implementation. |
| 22 */ |
| 23 |
| 24 const {EventEmitter} = require("./events"); |
| 25 const {FilterNotifier} = require("./filterNotifier"); |
| 26 |
| 27 /** |
| 28 * Lookup table, lists of element hiding exceptions by selector |
| 29 * @type {Map.<string,ElemHideException[]>} |
| 30 */ |
| 31 let exceptions = new Map(); |
| 32 |
| 33 /** |
| 34 * Set containing known element exceptions |
| 35 * @type {Set.<ElemHideException>} |
| 36 */ |
| 37 let knownExceptions = new Set(); |
| 38 |
| 39 /** |
| 40 * Container for element hiding exceptions |
| 41 * @class |
| 42 */ |
| 43 exports.ElemHideExceptions = Object.assign(Object.create(new EventEmitter()), { |
| 44 /** |
| 45 * Removes all known exceptions |
| 46 */ |
| 47 clear() |
| 48 { |
| 49 exceptions.clear(); |
| 50 knownExceptions.clear(); |
| 51 |
| 52 FilterNotifier.emit("elemhideupdate"); |
| 53 }, |
| 54 |
| 55 /** |
| 56 * Add a new element hiding exception |
| 57 * @param {ElemHideException} exception |
| 58 */ |
| 59 add(exception) |
| 60 { |
| 61 if (knownExceptions.has(exception)) |
| 62 return; |
| 63 |
| 64 let {selector} = exception; |
| 65 let list = exceptions.get(selector); |
| 66 if (list) |
| 67 list.push(exception); |
| 68 else |
| 69 exceptions.set(selector, [exception]); |
| 70 |
| 71 knownExceptions.add(exception); |
| 72 |
| 73 this.emit("added", exception); |
| 74 |
| 75 FilterNotifier.emit("elemhideupdate"); |
| 76 }, |
| 77 |
| 78 /** |
| 79 * Removes an element hiding exception |
| 80 * @param {ElemHideException} exception |
| 81 */ |
| 82 remove(exception) |
| 83 { |
| 84 if (!knownExceptions.has(exception)) |
| 85 return; |
| 86 |
| 87 let list = exceptions.get(exception.selector); |
| 88 let index = list.indexOf(exception); |
| 89 if (index >= 0) |
| 90 list.splice(index, 1); |
| 91 |
| 92 knownExceptions.delete(exception); |
| 93 |
| 94 this.emit("removed", exception); |
| 95 |
| 96 FilterNotifier.emit("elemhideupdate"); |
| 97 }, |
| 98 |
| 99 /** |
| 100 * Checks whether any exception rules are registered for a selector |
| 101 * @param {string} selector |
| 102 * @returns {boolean} |
| 103 */ |
| 104 hasExceptions(selector) |
| 105 { |
| 106 return exceptions.has(selector); |
| 107 }, |
| 108 |
| 109 /** |
| 110 * Checks whether an exception rule is registered for a filter on a particular |
| 111 * domain. |
| 112 * @param {ElemHideFilter|ElemHideEmulationFilter} filter |
| 113 * @param {?string} docDomain |
| 114 * @return {?ElemHideException} |
| 115 */ |
| 116 getException(filter, docDomain) |
| 117 { |
| 118 let list = exceptions.get(filter.selector); |
| 119 if (!list) |
| 120 return null; |
| 121 |
| 122 for (let i = list.length - 1; i >= 0; i--) |
| 123 { |
| 124 if (list[i].isActiveOnDomain(docDomain)) |
| 125 return list[i]; |
| 126 } |
| 127 |
| 128 return null; |
| 129 } |
| 130 }); |
OLD | NEW |