Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/elemHide.js

Issue 29784555: Issue 6665 - Abstract element hiding container logic into its own module Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Move base class into lib/contentFilterModule.js Created Aug. 15, 2018, 8:56 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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
27 /** 28 /**
28 * Lookup table, active flag, by filter by domain.
29 * (Only contains filters that aren't unconditionally matched for all domains.)
30 * @type {Map.<string,Map.<Filter,boolean>>}
31 */
32 let filtersByDomain = new Map();
33
34 /**
35 * Lookup table, filter by selector. (Only used for selectors that are
36 * unconditionally matched for all domains.)
37 * @type {Map.<string,Filter>}
38 */
39 let filterBySelector = new Map();
40
41 /**
42 * This array caches the keys of filterBySelector table (selectors
43 * which unconditionally apply on all domains). It will be null if the
44 * cache needs to be rebuilt.
45 * @type {?string[]}
46 */
47 let unconditionalSelectors = null;
48
49 /**
50 * Map to be used instead when a filter has a blank domains property.
51 * @type {Map.<string,boolean>}
52 * @const
53 */
54 let defaultDomains = new Map([["", true]]);
55
56 /**
57 * Set containing known element hiding filters
58 * @type {Set.<ElemHideFilter>}
59 */
60 let knownFilters = new Set();
61
62 /**
63 * Adds a filter to the lookup table of filters by domain.
64 * @param {Filter} filter
65 */
66 function addToFiltersByDomain(filter)
67 {
68 let domains = filter.domains || defaultDomains;
69 for (let [domain, isIncluded] of domains)
70 {
71 // There's no need to note that a filter is generically disabled.
72 if (!isIncluded && domain == "")
73 continue;
74
75 let filters = filtersByDomain.get(domain);
76 if (!filters)
77 filtersByDomain.set(domain, filters = new Map());
78 filters.set(filter, isIncluded);
79 }
80 }
81
82 /**
83 * Returns a list of selectors that apply on each website unconditionally.
84 * @returns {string[]}
85 */
86 function getUnconditionalSelectors()
87 {
88 if (!unconditionalSelectors)
89 unconditionalSelectors = [...filterBySelector.keys()];
90
91 return unconditionalSelectors;
92 }
93
94 ElemHideExceptions.on("added", ({selector}) =>
95 {
96 // If this is the first exception for a previously unconditionally applied
97 // element hiding selector we need to take care to update the lookups.
98 let unconditionalFilterForSelector = filterBySelector.get(selector);
99 if (unconditionalFilterForSelector)
100 {
101 addToFiltersByDomain(unconditionalFilterForSelector);
102 filterBySelector.delete(selector);
103 unconditionalSelectors = null;
104 }
105 });
106
107 /**
108 * Container for element hiding filters 29 * Container for element hiding filters
109 * @class 30 * @class
110 */ 31 */
111 exports.ElemHide = { 32 exports.ElemHide = Object.assign(new ContentFilterModule(), {
112 /**
113 * Removes all known filters
114 */
115 clear() 33 clear()
116 { 34 {
117 for (let collection of [filtersByDomain, filterBySelector, knownFilters]) 35 ContentFilterModule.prototype.clear.call(this);
118 collection.clear();
119 36
120 unconditionalSelectors = null;
121 FilterNotifier.emit("elemhideupdate"); 37 FilterNotifier.emit("elemhideupdate");
122 }, 38 },
123 39
124 /**
125 * Add a new element hiding filter
126 * @param {ElemHideFilter} filter
127 */
128 add(filter) 40 add(filter)
129 { 41 {
130 if (knownFilters.has(filter)) 42 let {size} = this._knownFilters;
131 return;
132 43
133 let {selector} = filter; 44 ContentFilterModule.prototype.add.call(this, filter);
134 45
135 if (!(filter.domains || ElemHideExceptions.hasExceptions(selector))) 46 if (size != this._knownFilters.size)
136 { 47 FilterNotifier.emit("elemhideupdate");
137 // The new filter's selector is unconditionally applied to all domains
138 filterBySelector.set(selector, filter);
139 unconditionalSelectors = null;
140 }
141 else
142 {
143 // The new filter's selector only applies to some domains
144 addToFiltersByDomain(filter);
145 }
146
147 knownFilters.add(filter);
148 FilterNotifier.emit("elemhideupdate");
149 }, 48 },
150 49
151 /**
152 * Removes an element hiding filter
153 * @param {ElemHideFilter} filter
154 */
155 remove(filter) 50 remove(filter)
156 { 51 {
157 if (!knownFilters.has(filter)) 52 let {size} = this._knownFilters;
158 return;
159 53
160 let {selector} = filter; 54 ContentFilterModule.prototype.remove.call(this, filter);
161 55
162 // Unconditially applied element hiding filters 56 if (size != this._knownFilters.size)
163 if (filterBySelector.get(selector) == filter) 57 FilterNotifier.emit("elemhideupdate");
164 {
165 filterBySelector.delete(selector);
166 unconditionalSelectors = null;
167 }
168 // Conditionally applied element hiding filters
169 else
170 {
171 let domains = filter.domains || defaultDomains;
172 for (let domain of domains.keys())
173 {
174 let filters = filtersByDomain.get(domain);
175 if (filters)
176 filters.delete(filter);
177 }
178 }
179
180 knownFilters.delete(filter);
181 FilterNotifier.emit("elemhideupdate");
182 }, 58 },
183 59
184 /** 60 /**
185 * Determines from the current filter list which selectors should be applied 61 * Determines from the current filter list which selectors should be applied
186 * on a particular host name. 62 * on a particular host name.
187 * @param {string} domain 63 * @param {string} domain
188 * @param {boolean} [specificOnly] true if generic filters should not apply. 64 * @param {boolean} [specificOnly] true if generic filters should not apply.
189 * @returns {string[]} List of selectors. 65 * @returns {string[]} List of selectors.
190 */ 66 */
191 getSelectorsForDomain(domain, specificOnly = false) 67 getSelectorsForDomain(domain, specificOnly = false)
192 { 68 {
193 let selectors = []; 69 let selectors = [];
194 70
195 let excluded = new Set(); 71 let excluded = new Set();
196 let currentDomain = domain ? domain.replace(/\.+$/, "").toLowerCase() : ""; 72 let currentDomain = domain ? domain.replace(/\.+$/, "").toLowerCase() : "";
197 73
198 // This code is a performance hot-spot, which is why we've made certain 74 // This code is a performance hot-spot, which is why we've made certain
199 // micro-optimisations. Please be careful before making changes. 75 // micro-optimisations. Please be careful before making changes.
200 while (true) 76 while (true)
201 { 77 {
202 if (specificOnly && currentDomain == "") 78 if (specificOnly && currentDomain == "")
203 break; 79 break;
204 80
205 let filters = filtersByDomain.get(currentDomain); 81 let filters = this._filtersByDomain.get(currentDomain);
206 if (filters) 82 if (filters)
207 { 83 {
208 for (let [filter, isIncluded] of filters) 84 for (let [filter, isIncluded] of filters)
209 { 85 {
210 if (!isIncluded) 86 if (!isIncluded)
211 { 87 {
212 excluded.add(filter); 88 excluded.add(filter);
213 } 89 }
214 else 90 else
215 { 91 {
216 let {selector} = filter; 92 let {selector} = filter;
217 if ((excluded.size == 0 || !excluded.has(filter)) && 93 if ((excluded.size == 0 || !excluded.has(filter)) &&
218 !ElemHideExceptions.getException(selector, domain)) 94 !ElemHideExceptions.getException(selector, domain))
219 { 95 {
220 selectors.push(selector); 96 selectors.push(selector);
221 } 97 }
222 } 98 }
223 } 99 }
224 } 100 }
225 101
226 if (currentDomain == "") 102 if (currentDomain == "")
227 break; 103 break;
228 104
229 let nextDot = currentDomain.indexOf("."); 105 let nextDot = currentDomain.indexOf(".");
230 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 106 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
231 } 107 }
232 108
233 if (!specificOnly) 109 if (!specificOnly)
234 selectors = getUnconditionalSelectors().concat(selectors); 110 selectors = this._getUnconditionalSelectors().concat(selectors);
235 111
236 return selectors; 112 return selectors;
237 } 113 }
238 }; 114 });
OLDNEW

Powered by Google App Engine
This is Rietveld