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

Side by Side Diff: lib/elemHide.js

Issue 29935564: Issue 7452 - Do not cache element hiding filter objects by default Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Rename filter* to filterText* Created April 8, 2019, 9:59 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/filterClasses.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 {Filter} = require("./filterClasses");
24 const {ElemHideExceptions} = require("./elemHideExceptions"); 25 const {ElemHideExceptions} = require("./elemHideExceptions");
25 const {filterNotifier} = require("./filterNotifier"); 26 const {filterNotifier} = require("./filterNotifier");
26 const {normalizeHostname, domainSuffixes} = require("./url"); 27 const {normalizeHostname, domainSuffixes} = require("./url");
27 const {Cache} = require("./caching"); 28 const {Cache} = require("./caching");
28 29
29 /** 30 /**
30 * The maximum number of selectors in a CSS rule. This is used by 31 * The maximum number of selectors in a CSS rule. This is used by
31 * <code>{@link createStyleSheet}</code> to split up a long list of selectors 32 * <code>{@link createStyleSheet}</code> to split up a long list of selectors
32 * into multiple rules. 33 * into multiple rules.
33 * @const {number} 34 * @const {number}
34 * @default 35 * @default
35 */ 36 */
36 const selectorGroupSize = 1024; 37 const selectorGroupSize = 1024;
37 38
38 /** 39 /**
39 * Lookup table, active flag, by filter by domain. 40 * Lookup table, selector by filter text by domain.
40 * (Only contains filters that aren't unconditionally matched for all domains.) 41 * (Only contains filter text for filters that aren't unconditionally matched
41 * @type {Map.<string,Map.<Filter,boolean>>} 42 * for all domains.)
43 * @type {Map.<string,Map.<string,string>>}
42 */ 44 */
43 let filtersByDomain = new Map(); 45 let filterTextByDomain = new Map();
44 46
45 /** 47 /**
46 * Lookup table, filter by selector. (Only used for selectors that are 48 * Lookup table, filter text by selector. (Only used for selectors that are
47 * unconditionally matched for all domains.) 49 * unconditionally matched for all domains.)
48 * @type {Map.<string,Filter>} 50 * @type {Map.<string,string>}
49 */ 51 */
50 let filterBySelector = new Map(); 52 let filterTextBySelector = new Map();
51 53
52 /** 54 /**
53 * This array caches the keys of filterBySelector table (selectors 55 * This array caches the keys of filterTextBySelector table (selectors
54 * which unconditionally apply on all domains). It will be null if the 56 * which unconditionally apply on all domains). It will be null if the
55 * cache needs to be rebuilt. 57 * cache needs to be rebuilt.
56 * @type {?string[]} 58 * @type {?string[]}
57 */ 59 */
58 let unconditionalSelectors = null; 60 let unconditionalSelectors = null;
59 61
60 /** 62 /**
61 * The default style sheet that applies on all domains. This is based on the 63 * The default style sheet that applies on all domains. This is based on the
62 * value of <code>{@link unconditionalSelectors}</code>. 64 * value of <code>{@link unconditionalSelectors}</code>.
63 * @type {?string} 65 * @type {?string}
(...skipping 18 matching lines...) Expand all
82 let styleSheetCache = new Cache(100); 84 let styleSheetCache = new Cache(100);
83 85
84 /** 86 /**
85 * Map to be used instead when a filter has a blank domains property. 87 * Map to be used instead when a filter has a blank domains property.
86 * @type {Map.<string,boolean>} 88 * @type {Map.<string,boolean>}
87 * @const 89 * @const
88 */ 90 */
89 let defaultDomains = new Map([["", true]]); 91 let defaultDomains = new Map([["", true]]);
90 92
91 /** 93 /**
92 * Set containing known element hiding filters 94 * Set containing text of known element hiding filters
93 * @type {Set.<ElemHideFilter>} 95 * @type {Set.<string>}
94 */ 96 */
95 let knownFilters = new Set(); 97 let knownFilterText = new Set();
96 98
97 /** 99 /**
98 * All domains known to occur in exceptions 100 * All domains known to occur in exceptions
99 * @type {Set.<string>} 101 * @type {Set.<string>}
100 */ 102 */
101 let knownExceptionDomains = new Set(); 103 let knownExceptionDomains = new Set();
102 104
103 /** 105 /**
104 * Returns the suffix of the given domain that is known. If no suffix is known, 106 * Returns the suffix of the given domain that is known. If no suffix is known,
105 * an empty string is returned. 107 * an empty string is returned.
106 * @param {?string} domain 108 * @param {?string} domain
107 * @returns {string} 109 * @returns {string}
108 */ 110 */
109 function getKnownSuffix(domain) 111 function getKnownSuffix(domain)
110 { 112 {
111 while (domain && !filtersByDomain.has(domain) && 113 while (domain && !filterTextByDomain.has(domain) &&
112 !knownExceptionDomains.has(domain)) 114 !knownExceptionDomains.has(domain))
113 { 115 {
114 let index = domain.indexOf("."); 116 let index = domain.indexOf(".");
115 domain = index == -1 ? "" : domain.substring(index + 1); 117 domain = index == -1 ? "" : domain.substring(index + 1);
116 } 118 }
117 119
118 return domain; 120 return domain;
119 } 121 }
120 122
121 /** 123 /**
122 * Adds a filter to the lookup table of filters by domain. 124 * Adds a filter to the lookup table of filters by domain.
123 * @param {Filter} filter 125 * @param {Filter} filter
124 * @param {?Map.<string,boolean>} [domains] 126 * @param {?Map.<string,boolean>} [domains]
125 */ 127 */
126 function addToFiltersByDomain(filter, domains = filter.domains) 128 function addToFiltersByDomain(filter, domains = filter.domains)
127 { 129 {
128 for (let [domain, isIncluded] of domains || defaultDomains) 130 for (let [domain, isIncluded] of domains || defaultDomains)
129 { 131 {
130 // There's no need to note that a filter is generically disabled. 132 // There's no need to note that a filter is generically disabled.
131 if (!isIncluded && domain == "") 133 if (!isIncluded && domain == "")
132 continue; 134 continue;
133 135
134 let filters = filtersByDomain.get(domain); 136 let filters = filterTextByDomain.get(domain);
135 if (!filters) 137 if (!filters)
136 filtersByDomain.set(domain, filters = new Map()); 138 filterTextByDomain.set(domain, filters = new Map());
137 filters.set(filter, isIncluded); 139 filters.set(filter.text, isIncluded ? filter.selector : null);
138 } 140 }
139 } 141 }
140 142
141 /** 143 /**
142 * Returns a list of selectors that apply on each website unconditionally. 144 * Returns a list of selectors that apply on each website unconditionally.
143 * @returns {string[]} 145 * @returns {string[]}
144 */ 146 */
145 function getUnconditionalSelectors() 147 function getUnconditionalSelectors()
146 { 148 {
147 if (!unconditionalSelectors) 149 if (!unconditionalSelectors)
148 unconditionalSelectors = [...filterBySelector.keys()]; 150 unconditionalSelectors = [...filterTextBySelector.keys()];
149 151
150 return unconditionalSelectors; 152 return unconditionalSelectors;
151 } 153 }
152 154
153 /** 155 /**
154 * Returns the list of selectors that apply on a given domain from the subset 156 * Returns the list of selectors that apply on a given domain from the subset
155 * of filters that do not apply unconditionally on all domains. 157 * of filters that do not apply unconditionally on all domains.
156 * 158 *
157 * @param {string} domain The domain. 159 * @param {string} domain The domain.
158 * @param {boolean} specificOnly Whether selectors from generic filters should 160 * @param {boolean} specificOnly Whether selectors from generic filters should
159 * be included. 161 * be included.
160 * 162 *
161 * @returns {Array.<string>} The list of selectors. 163 * @returns {Array.<string>} The list of selectors.
162 */ 164 */
163 function getConditionalSelectors(domain, specificOnly) 165 function getConditionalSelectors(domain, specificOnly)
164 { 166 {
165 let selectors = []; 167 let selectors = [];
166 168
167 let excluded = new Set(); 169 let excluded = new Set();
168 170
169 for (let currentDomain of domainSuffixes(domain, !specificOnly)) 171 for (let currentDomain of domainSuffixes(domain, !specificOnly))
170 { 172 {
171 let filters = filtersByDomain.get(currentDomain); 173 let filters = filterTextByDomain.get(currentDomain);
172 if (filters) 174 if (filters)
173 { 175 {
174 for (let [filter, isIncluded] of filters) 176 for (let [text, selector] of filters)
175 { 177 {
176 if (!isIncluded) 178 if (!selector)
177 { 179 {
178 excluded.add(filter); 180 excluded.add(text);
179 } 181 }
180 else 182 else if ((excluded.size == 0 || !excluded.has(text)) &&
183 !ElemHideExceptions.getException(selector, domain))
181 { 184 {
182 let {selector} = filter; 185 selectors.push(selector);
183 if ((excluded.size == 0 || !excluded.has(filter)) &&
184 !ElemHideExceptions.getException(selector, domain))
185 {
186 selectors.push(selector);
187 }
188 } 186 }
189 } 187 }
190 } 188 }
191 } 189 }
192 190
193 return selectors; 191 return selectors;
194 } 192 }
195 193
196 /** 194 /**
197 * Returns the list of selectors and the list of exceptions that apply on a 195 * Returns the list of selectors and the list of exceptions that apply on a
198 * given domain from the subset of filters that do not apply unconditionally 196 * given domain from the subset of filters that do not apply unconditionally
199 * on all domains. 197 * on all domains.
200 * 198 *
201 * @param {string} domain The domain. 199 * @param {string} domain The domain.
202 * @param {boolean} specificOnly Whether selectors from generic filters should 200 * @param {boolean} specificOnly Whether selectors from generic filters should
203 * be included. 201 * be included.
204 * 202 *
205 * @returns {{selectors: Array.<string>, exceptions: Array.<ElemHideException>}} 203 * @returns {{selectors: Array.<string>, exceptions: Array.<ElemHideException>}}
206 * An object containing the lists of selectors and exceptions. 204 * An object containing the lists of selectors and exceptions.
207 */ 205 */
208 function getConditionalSelectorsWithExceptions(domain, specificOnly) 206 function getConditionalSelectorsWithExceptions(domain, specificOnly)
209 { 207 {
210 let selectors = []; 208 let selectors = [];
211 let exceptions = []; 209 let exceptions = [];
212 210
213 let excluded = new Set(); 211 let excluded = new Set();
214 212
215 for (let currentDomain of domainSuffixes(domain, !specificOnly)) 213 for (let currentDomain of domainSuffixes(domain, !specificOnly))
216 { 214 {
217 let filters = filtersByDomain.get(currentDomain); 215 let filters = filterTextByDomain.get(currentDomain);
218 if (filters) 216 if (filters)
219 { 217 {
220 for (let [filter, isIncluded] of filters) 218 for (let [text, selector] of filters)
221 { 219 {
222 if (!isIncluded) 220 if (!selector)
223 { 221 {
224 excluded.add(filter); 222 excluded.add(text);
225 } 223 }
226 else if (excluded.size == 0 || !excluded.has(filter)) 224 else if (excluded.size == 0 || !excluded.has(text))
227 { 225 {
228 let {selector} = filter;
229 let exception = ElemHideExceptions.getException(selector, domain); 226 let exception = ElemHideExceptions.getException(selector, domain);
230 227
231 if (exception) 228 if (exception)
232 exceptions.push(exception); 229 exceptions.push(exception);
233 else 230 else
234 selectors.push(selector); 231 selectors.push(selector);
235 } 232 }
236 } 233 }
237 } 234 }
238 } 235 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 // Note: Once an exception domain is known it never becomes unknown, even 294 // Note: Once an exception domain is known it never becomes unknown, even
298 // when all the exceptions containing that domain are removed. This is a 295 // when all the exceptions containing that domain are removed. This is a
299 // best-case optimization. 296 // best-case optimization.
300 if (domain != "") 297 if (domain != "")
301 knownExceptionDomains.add(domain); 298 knownExceptionDomains.add(domain);
302 } 299 }
303 } 300 }
304 301
305 // If this is the first exception for a previously unconditionally applied 302 // If this is the first exception for a previously unconditionally applied
306 // element hiding selector we need to take care to update the lookups. 303 // element hiding selector we need to take care to update the lookups.
307 let unconditionalFilterForSelector = filterBySelector.get(selector); 304 let unconditionalFilterForSelector = filterTextBySelector.get(selector);
308 if (unconditionalFilterForSelector) 305 if (unconditionalFilterForSelector)
309 { 306 {
310 addToFiltersByDomain(unconditionalFilterForSelector); 307 addToFiltersByDomain(Filter.fromText(unconditionalFilterForSelector,
311 filterBySelector.delete(selector); 308 false));
309 filterTextBySelector.delete(selector);
312 unconditionalSelectors = null; 310 unconditionalSelectors = null;
313 defaultStyleSheet = null; 311 defaultStyleSheet = null;
314 } 312 }
315 }); 313 });
316 314
317 /** 315 /**
318 * Container for element hiding filters 316 * Container for element hiding filters
319 * @class 317 * @class
320 */ 318 */
321 exports.ElemHide = { 319 exports.ElemHide = {
322 /** 320 /**
323 * Removes all known filters 321 * Removes all known filters
324 */ 322 */
325 clear() 323 clear()
326 { 324 {
327 commonStyleSheet = null; 325 commonStyleSheet = null;
328 326
329 for (let collection of [styleSheetCache, filtersByDomain, filterBySelector, 327 for (let collection of [styleSheetCache, filterTextByDomain,
330 knownFilters, knownExceptionDomains]) 328 filterTextBySelector, knownFilterText,
329 knownExceptionDomains])
331 { 330 {
332 collection.clear(); 331 collection.clear();
333 } 332 }
334 333
335 unconditionalSelectors = null; 334 unconditionalSelectors = null;
336 defaultStyleSheet = null; 335 defaultStyleSheet = null;
337 336
338 filterNotifier.emit("elemhideupdate"); 337 filterNotifier.emit("elemhideupdate");
339 }, 338 },
340 339
341 /** 340 /**
342 * Add a new element hiding filter 341 * Add a new element hiding filter
343 * @param {ElemHideFilter} filter 342 * @param {ElemHideFilter} filter
344 */ 343 */
345 add(filter) 344 add(filter)
346 { 345 {
347 if (knownFilters.has(filter)) 346 let {text} = filter;
347
348 if (knownFilterText.has(text))
348 return; 349 return;
349 350
350 styleSheetCache.clear(); 351 styleSheetCache.clear();
351 commonStyleSheet = null; 352 commonStyleSheet = null;
352 353
353 let {domains, selector} = filter; 354 let {domains, selector} = filter;
354 355
355 if (!(domains || ElemHideExceptions.hasExceptions(selector))) 356 if (!(domains || ElemHideExceptions.hasExceptions(selector)))
356 { 357 {
357 // The new filter's selector is unconditionally applied to all domains 358 // The new filter's selector is unconditionally applied to all domains
358 filterBySelector.set(selector, filter); 359 filterTextBySelector.set(selector, text);
359 unconditionalSelectors = null; 360 unconditionalSelectors = null;
360 defaultStyleSheet = null; 361 defaultStyleSheet = null;
361 } 362 }
362 else 363 else
363 { 364 {
364 // The new filter's selector only applies to some domains 365 // The new filter's selector only applies to some domains
365 addToFiltersByDomain(filter, domains); 366 addToFiltersByDomain(filter, domains);
366 } 367 }
367 368
368 knownFilters.add(filter); 369 knownFilterText.add(text);
369 filterNotifier.emit("elemhideupdate"); 370 filterNotifier.emit("elemhideupdate");
370 }, 371 },
371 372
372 /** 373 /**
373 * Removes an element hiding filter 374 * Removes an element hiding filter
374 * @param {ElemHideFilter} filter 375 * @param {ElemHideFilter} filter
375 */ 376 */
376 remove(filter) 377 remove(filter)
377 { 378 {
378 if (!knownFilters.has(filter)) 379 let {text} = filter;
380
381 if (!knownFilterText.has(text))
379 return; 382 return;
380 383
381 styleSheetCache.clear(); 384 styleSheetCache.clear();
382 commonStyleSheet = null; 385 commonStyleSheet = null;
383 386
384 let {selector} = filter; 387 let {selector} = filter;
385 388
386 // Unconditially applied element hiding filters 389 // Unconditially applied element hiding filters
387 if (filterBySelector.get(selector) == filter) 390 if (filterTextBySelector.get(selector) == text)
388 { 391 {
389 filterBySelector.delete(selector); 392 filterTextBySelector.delete(selector);
390 unconditionalSelectors = null; 393 unconditionalSelectors = null;
391 defaultStyleSheet = null; 394 defaultStyleSheet = null;
392 } 395 }
393 // Conditionally applied element hiding filters 396 // Conditionally applied element hiding filters
394 else 397 else
395 { 398 {
396 let domains = filter.domains || defaultDomains; 399 let domains = filter.domains || defaultDomains;
397 for (let domain of domains.keys()) 400 for (let domain of domains.keys())
398 { 401 {
399 let filters = filtersByDomain.get(domain); 402 let filters = filterTextByDomain.get(domain);
400 if (filters) 403 if (filters)
401 { 404 {
402 filters.delete(filter); 405 filters.delete(text);
403 406
404 if (filters.size == 0) 407 if (filters.size == 0)
405 filtersByDomain.delete(domain); 408 filterTextByDomain.delete(domain);
406 } 409 }
407 } 410 }
408 } 411 }
409 412
410 knownFilters.delete(filter); 413 knownFilterText.delete(text);
411 filterNotifier.emit("elemhideupdate"); 414 filterNotifier.emit("elemhideupdate");
412 }, 415 },
413 416
414 /** 417 /**
415 * @typedef {object} ElemHideStyleSheet 418 * @typedef {object} ElemHideStyleSheet
416 * @property {string} code CSS code. 419 * @property {string} code CSS code.
417 * @property {Array.<string>} selectors List of selectors. 420 * @property {Array.<string>} selectors List of selectors.
418 */ 421 */
419 422
420 /** 423 /**
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
577 { 580 {
578 let styleSheet = ""; 581 let styleSheet = "";
579 582
580 for (let selectorGroup of splitSelectors(selectors)) 583 for (let selectorGroup of splitSelectors(selectors))
581 styleSheet += createRule(selectorGroup); 584 styleSheet += createRule(selectorGroup);
582 585
583 return styleSheet; 586 return styleSheet;
584 } 587 }
585 588
586 exports.createStyleSheet = createStyleSheet; 589 exports.createStyleSheet = createStyleSheet;
OLDNEW
« no previous file with comments | « no previous file | lib/filterClasses.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld