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

Powered by Google App Engine
This is Rietveld