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-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 implementation. | 21 * @fileOverview Element hiding implementation. |
22 */ | 22 */ |
23 | 23 |
| 24 const {ContentFilterModule} = require("./contentFilterModule"); |
24 const {ElemHideExceptions} = require("./elemHideExceptions"); | 25 const {ElemHideExceptions} = require("./elemHideExceptions"); |
25 const {FilterNotifier} = require("./filterNotifier"); | 26 const {FilterNotifier} = require("./filterNotifier"); |
26 | |
27 /** | |
28 * Map to be used instead when a filter has a blank domains property. | |
29 * @type {Map.<string,boolean>} | |
30 * @const | |
31 */ | |
32 let defaultDomains = new Map([["", true]]); | |
33 | |
34 /** | |
35 * Template for element hiding and element hiding emulation filter containers | |
36 * @class | |
37 */ | |
38 function ElemHideTemplate() | |
39 { | |
40 this._filtersByDomain = new Map(); | |
41 this._filterBySelector = new Map(); | |
42 this._unconditionalSelectors = null; | |
43 this._knownFilters = new Set(); | |
44 | |
45 ElemHideExceptions.on("added", this._onExceptionAdded.bind(this)); | |
46 } | |
47 | |
48 ElemHideTemplate.prototype = { | |
49 /** | |
50 * Lookup table, active flag, by filter by domain. | |
51 * (Only contains filters that aren't unconditionally matched for all | |
52 * domains.) | |
53 * @type {Map.<string,Map.<Filter,boolean>>} | |
54 */ | |
55 _filtersByDomain: null, | |
56 | |
57 /** | |
58 * Lookup table, filter by selector. (Only used for selectors that are | |
59 * unconditionally matched for all domains.) | |
60 * @type {Map.<string,Filter>} | |
61 */ | |
62 _filterBySelector: null, | |
63 | |
64 /** | |
65 * This array caches the keys of filterBySelector table (selectors | |
66 * which unconditionally apply on all domains). It will be null if the | |
67 * cache needs to be rebuilt. | |
68 * @type {?string[]} | |
69 */ | |
70 _unconditionalSelectors: null, | |
71 | |
72 /** | |
73 * Set containing known element hiding filters | |
74 * @type {Set.<ElemHideFilter>} | |
75 */ | |
76 _knownFilters: null, | |
77 | |
78 /** | |
79 * Adds a filter to the lookup table of filters by domain. | |
80 * @param {Filter} filter | |
81 */ | |
82 _addToFiltersByDomain(filter) | |
83 { | |
84 let domains = filter.domains || defaultDomains; | |
85 for (let [domain, isIncluded] of domains) | |
86 { | |
87 // There's no need to note that a filter is generically disabled. | |
88 if (!isIncluded && domain == "") | |
89 continue; | |
90 | |
91 let filters = this._filtersByDomain.get(domain); | |
92 if (!filters) | |
93 this._filtersByDomain.set(domain, filters = new Map()); | |
94 filters.set(filter, isIncluded); | |
95 } | |
96 }, | |
97 | |
98 /** | |
99 * Returns a list of selectors that apply on each website unconditionally. | |
100 * @returns {string[]} | |
101 */ | |
102 _getUnconditionalSelectors() | |
103 { | |
104 if (!this._unconditionalSelectors) | |
105 this._unconditionalSelectors = [...this._filterBySelector.keys()]; | |
106 | |
107 return this._unconditionalSelectors; | |
108 }, | |
109 | |
110 /** | |
111 * Handles the event when a new element hiding exception has been added | |
112 * @param {ElemHideException} exception | |
113 */ | |
114 _onExceptionAdded(exception) | |
115 { | |
116 let {selector} = exception; | |
117 | |
118 // If this is the first exception for a previously unconditionally applied | |
119 // element hiding selector we need to take care to update the lookups. | |
120 let unconditionalFilterForSelector = this._filterBySelector.get(selector); | |
121 if (unconditionalFilterForSelector) | |
122 { | |
123 this._addToFiltersByDomain(unconditionalFilterForSelector); | |
124 this._filterBySelector.delete(selector); | |
125 this._unconditionalSelectors = null; | |
126 } | |
127 }, | |
128 | |
129 /** | |
130 * Removes all known filters | |
131 */ | |
132 clear() | |
133 { | |
134 for (let collection of [this._filtersByDomain, this._filterBySelector, | |
135 this._knownFilters]) | |
136 { | |
137 collection.clear(); | |
138 } | |
139 | |
140 this._unconditionalSelectors = null; | |
141 FilterNotifier.emit("elemhideupdate"); | |
142 }, | |
143 | |
144 /** | |
145 * Add a new element hiding filter | |
146 * @param {ElemHideFilter} filter | |
147 */ | |
148 add(filter) | |
149 { | |
150 if (this._knownFilters.has(filter)) | |
151 return; | |
152 | |
153 let {selector} = filter; | |
154 | |
155 if (!(filter.domains || ElemHideExceptions.hasExceptions(selector))) | |
156 { | |
157 // The new filter's selector is unconditionally applied to all domains | |
158 this._filterBySelector.set(selector, filter); | |
159 this._unconditionalSelectors = null; | |
160 } | |
161 else | |
162 { | |
163 // The new filter's selector only applies to some domains | |
164 this._addToFiltersByDomain(filter); | |
165 } | |
166 | |
167 this._knownFilters.add(filter); | |
168 FilterNotifier.emit("elemhideupdate"); | |
169 }, | |
170 | |
171 /** | |
172 * Removes an element hiding filter | |
173 * @param {ElemHideFilter} filter | |
174 */ | |
175 remove(filter) | |
176 { | |
177 if (!this._knownFilters.has(filter)) | |
178 return; | |
179 | |
180 let {selector} = filter; | |
181 | |
182 // Unconditially applied element hiding filters | |
183 if (this._filterBySelector.get(selector) == filter) | |
184 { | |
185 this._filterBySelector.delete(selector); | |
186 this._unconditionalSelectors = null; | |
187 } | |
188 // Conditionally applied element hiding filters | |
189 else | |
190 { | |
191 let domains = filter.domains || defaultDomains; | |
192 for (let domain of domains.keys()) | |
193 { | |
194 let filters = this._filtersByDomain.get(domain); | |
195 if (filters) | |
196 filters.delete(filter); | |
197 } | |
198 } | |
199 | |
200 this._knownFilters.delete(filter); | |
201 FilterNotifier.emit("elemhideupdate"); | |
202 } | |
203 }; | |
204 | |
205 exports.ElemHideTemplate = ElemHideTemplate; | |
206 | 27 |
207 /** | 28 /** |
208 * Container for element hiding filters | 29 * Container for element hiding filters |
209 * @class | 30 * @class |
210 */ | 31 */ |
211 exports.ElemHide = Object.assign(new ElemHideTemplate(), { | 32 exports.ElemHide = Object.assign(new ContentFilterModule(), { |
| 33 clear() |
| 34 { |
| 35 ContentFilterModule.prototype.clear.call(this); |
| 36 |
| 37 FilterNotifier.emit("elemhideupdate"); |
| 38 }, |
| 39 |
| 40 add(filter) |
| 41 { |
| 42 let {size} = this._knownFilters; |
| 43 |
| 44 ContentFilterModule.prototype.add.call(this, filter); |
| 45 |
| 46 if (size != this._knownFilters.size) |
| 47 FilterNotifier.emit("elemhideupdate"); |
| 48 }, |
| 49 |
| 50 remove(filter) |
| 51 { |
| 52 let {size} = this._knownFilters; |
| 53 |
| 54 ContentFilterModule.prototype.remove.call(this, filter); |
| 55 |
| 56 if (size != this._knownFilters.size) |
| 57 FilterNotifier.emit("elemhideupdate"); |
| 58 }, |
| 59 |
212 /** | 60 /** |
213 * Determines from the current filter list which selectors should be applied | 61 * Determines from the current filter list which selectors should be applied |
214 * on a particular host name. | 62 * on a particular host name. |
215 * @param {string} domain | 63 * @param {string} domain |
216 * @param {boolean} [specificOnly] true if generic filters should not apply. | 64 * @param {boolean} [specificOnly] true if generic filters should not apply. |
217 * @returns {string[]} List of selectors. | 65 * @returns {string[]} List of selectors. |
218 */ | 66 */ |
219 getSelectorsForDomain(domain, specificOnly = false) | 67 getSelectorsForDomain(domain, specificOnly = false) |
220 { | 68 { |
221 let selectors = []; | 69 let selectors = []; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 let nextDot = currentDomain.indexOf("."); | 105 let nextDot = currentDomain.indexOf("."); |
258 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | 106 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); |
259 } | 107 } |
260 | 108 |
261 if (!specificOnly) | 109 if (!specificOnly) |
262 selectors = this._getUnconditionalSelectors().concat(selectors); | 110 selectors = this._getUnconditionalSelectors().concat(selectors); |
263 | 111 |
264 return selectors; | 112 return selectors; |
265 } | 113 } |
266 }); | 114 }); |
LEFT | RIGHT |