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 |
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 emulation implementation. | 21 * @fileOverview Element hiding emulation implementation. |
22 */ | 22 */ |
23 | 23 |
24 const {ElemHide} = require("./elemHide"); | 24 const {ElemHide} = require("./elemHide"); |
25 const {Filter} = require("./filterClasses"); | 25 const {filterIt, mapIt, flattenIt, regExpIt} = require("./coreUtils"); |
26 | 26 |
27 let filters = new Set(); | 27 /** |
| 28 * Map of element hiding emulation filters by domain |
| 29 * @type {Map.<string,Set.<ElemHideEmulationFilter>>} |
| 30 */ |
| 31 let filtersByDomain = new Map(); |
| 32 |
| 33 /** |
| 34 * Set containing known element hiding emulation filters |
| 35 * @type {Set.<ElemHideEmulation>} |
| 36 */ |
| 37 let knownFilters = new Set(); |
| 38 |
| 39 function* filterDomains(filter) |
| 40 { |
| 41 yield* mapIt(filterIt(filter.domains || [], ([domain]) => domain != ""), |
| 42 ([domain, include]) => [domain, include, |
| 43 filtersByDomain.get(domain)]); |
| 44 } |
| 45 |
| 46 function* subdomains(domain) |
| 47 { |
| 48 yield* mapIt(regExpIt(/([^|]*)\|/g, domain.replace(/([^.]+\.)/g, "$'|")), |
| 49 ([, subdomain]) => subdomain); |
| 50 } |
| 51 |
| 52 function* filtersForDomain(domain) |
| 53 { |
| 54 yield* filtersByDomain.get(domain) || []; |
| 55 |
| 56 yield* flattenIt(mapIt(subdomains(domain), |
| 57 subdomain => filtersByDomain.get(subdomain) || [])); |
| 58 } |
28 | 59 |
29 /** | 60 /** |
30 * Container for element hiding emulation filters | 61 * Container for element hiding emulation filters |
31 * @class | 62 * @class |
32 */ | 63 */ |
33 let ElemHideEmulation = { | 64 let ElemHideEmulation = { |
34 /** | 65 /** |
35 * Removes all known filters | 66 * Removes all known filters |
36 */ | 67 */ |
37 clear() | 68 clear() |
38 { | 69 { |
39 filters.clear(); | 70 filtersByDomain.clear(); |
40 }, | 71 }, |
41 | 72 |
42 /** | 73 /** |
43 * Add a new element hiding emulation filter | 74 * Add a new element hiding emulation filter |
44 * @param {ElemHideEmulationFilter} filter | 75 * @param {ElemHideEmulationFilter} filter |
45 */ | 76 */ |
46 add(filter) | 77 add(filter) |
47 { | 78 { |
48 filters.add(filter.text); | 79 if (knownFilters.has(filter)) |
| 80 return; |
| 81 |
| 82 for (let [domain, include, filters] of filterDomains(filter)) |
| 83 { |
| 84 if (filters) |
| 85 filters.set(filter, include); |
| 86 else |
| 87 filtersByDomain.set(domain, new Map([[filter, include]])); |
| 88 } |
| 89 |
| 90 knownFilters.add(filter); |
49 }, | 91 }, |
50 | 92 |
51 /** | 93 /** |
52 * Removes an element hiding emulation filter | 94 * Removes an element hiding emulation filter |
53 * @param {ElemHideEmulationFilter} filter | 95 * @param {ElemHideEmulationFilter} filter |
54 */ | 96 */ |
55 remove(filter) | 97 remove(filter) |
56 { | 98 { |
57 filters.delete(filter.text); | 99 if (!knownFilters.has(filter)) |
| 100 return; |
| 101 |
| 102 for (let [domain, , filters] of filterDomains(filter)) |
| 103 { |
| 104 if (filters) |
| 105 { |
| 106 filters.delete(filter); |
| 107 |
| 108 if (filters.size == 0) |
| 109 filtersByDomain.delete(domain); |
| 110 } |
| 111 } |
| 112 |
| 113 knownFilters.delete(filter); |
58 }, | 114 }, |
59 | 115 |
60 /** | 116 /** |
61 * Returns a list of all rules active on a particular domain | 117 * Returns a list of all rules active on a particular domain |
62 * @param {string} domain | 118 * @param {string} domain |
63 * @return {ElemHideEmulationFilter[]} | 119 * @return {ElemHideEmulationFilter[]} |
64 */ | 120 */ |
65 getRulesForDomain(domain) | 121 getRulesForDomain(domain) |
66 { | 122 { |
67 let result = []; | 123 let result = []; |
68 for (let text of filters.values()) | 124 |
| 125 let excludeSet = new Set(); |
| 126 for (let [filter, include] of filtersForDomain(domain.toUpperCase())) |
69 { | 127 { |
70 let filter = Filter.fromText(text); | 128 if (!include) |
71 if (filter.isActiveOnDomain(domain) && | 129 { |
72 !ElemHide.getException(filter, domain)) | 130 excludeSet.add(filter); |
| 131 } |
| 132 else if ((excludeSet.size == 0 || !excludeSet.has(filter)) && |
| 133 !ElemHide.getException(filter, domain)) |
73 { | 134 { |
74 result.push(filter); | 135 result.push(filter); |
75 } | 136 } |
76 } | 137 } |
| 138 |
77 return result; | 139 return result; |
78 } | 140 } |
79 }; | 141 }; |
80 exports.ElemHideEmulation = ElemHideEmulation; | 142 exports.ElemHideEmulation = ElemHideEmulation; |
OLD | NEW |