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: Rebase on GitLab MR #24 Created April 7, 2019, 6:08 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') | lib/filterClasses.js » ('J')
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");
Manish Jethani 2019/04/07 18:14:11 I have minimized the changes in this file for now
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}
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 { 128 {
128 for (let [domain, isIncluded] of domains || defaultDomains) 129 for (let [domain, isIncluded] of domains || defaultDomains)
129 { 130 {
130 // There's no need to note that a filter is generically disabled. 131 // There's no need to note that a filter is generically disabled.
131 if (!isIncluded && domain == "") 132 if (!isIncluded && domain == "")
132 continue; 133 continue;
133 134
134 let filters = filtersByDomain.get(domain); 135 let filters = filtersByDomain.get(domain);
135 if (!filters) 136 if (!filters)
136 filtersByDomain.set(domain, filters = new Map()); 137 filtersByDomain.set(domain, filters = new Map());
137 filters.set(filter, isIncluded); 138 filters.set(filter.text, isIncluded ? filter.selector : null);
138 } 139 }
139 } 140 }
140 141
141 /** 142 /**
142 * Returns a list of selectors that apply on each website unconditionally. 143 * Returns a list of selectors that apply on each website unconditionally.
143 * @returns {string[]} 144 * @returns {string[]}
144 */ 145 */
145 function getUnconditionalSelectors() 146 function getUnconditionalSelectors()
146 { 147 {
147 if (!unconditionalSelectors) 148 if (!unconditionalSelectors)
(...skipping 16 matching lines...) Expand all
164 { 165 {
165 let selectors = []; 166 let selectors = [];
166 167
167 let excluded = new Set(); 168 let excluded = new Set();
168 169
169 for (let currentDomain of domainSuffixes(domain, !specificOnly)) 170 for (let currentDomain of domainSuffixes(domain, !specificOnly))
170 { 171 {
171 let filters = filtersByDomain.get(currentDomain); 172 let filters = filtersByDomain.get(currentDomain);
172 if (filters) 173 if (filters)
173 { 174 {
174 for (let [filter, isIncluded] of filters) 175 for (let [text, selector] of filters)
175 { 176 {
176 if (!isIncluded) 177 if (!selector)
177 { 178 {
178 excluded.add(filter); 179 excluded.add(text);
179 } 180 }
180 else 181 else if ((excluded.size == 0 || !excluded.has(text)) &&
182 !ElemHideExceptions.getException(selector, domain))
181 { 183 {
182 let {selector} = filter; 184 selectors.push(selector);
183 if ((excluded.size == 0 || !excluded.has(filter)) &&
184 !ElemHideExceptions.getException(selector, domain))
185 {
186 selectors.push(selector);
187 }
188 } 185 }
189 } 186 }
190 } 187 }
191 } 188 }
192 189
193 return selectors; 190 return selectors;
194 } 191 }
195 192
196 /** 193 /**
197 * Returns the list of selectors and the list of exceptions that apply on a 194 * Returns the list of selectors and the list of exceptions that apply on a
(...skipping 12 matching lines...) Expand all
210 let selectors = []; 207 let selectors = [];
211 let exceptions = []; 208 let exceptions = [];
212 209
213 let excluded = new Set(); 210 let excluded = new Set();
214 211
215 for (let currentDomain of domainSuffixes(domain, !specificOnly)) 212 for (let currentDomain of domainSuffixes(domain, !specificOnly))
216 { 213 {
217 let filters = filtersByDomain.get(currentDomain); 214 let filters = filtersByDomain.get(currentDomain);
218 if (filters) 215 if (filters)
219 { 216 {
220 for (let [filter, isIncluded] of filters) 217 for (let [text, selector] of filters)
221 { 218 {
222 if (!isIncluded) 219 if (!selector)
223 { 220 {
224 excluded.add(filter); 221 excluded.add(text);
225 } 222 }
226 else if (excluded.size == 0 || !excluded.has(filter)) 223 else if (excluded.size == 0 || !excluded.has(text))
227 { 224 {
228 { 225 let exception = ElemHideExceptions.getException(selector, domain);
229 let {selector} = filter;
230 let exception = ElemHideExceptions.getException(selector, domain);
231 226
232 if (exception) 227 if (exception)
233 exceptions.push(exception); 228 exceptions.push(exception);
234 else 229 else
235 selectors.push(selector); 230 selectors.push(selector);
236 }
237 } 231 }
238 } 232 }
239 } 233 }
240 } 234 }
241 235
242 return {selectors, exceptions}; 236 return {selectors, exceptions};
243 } 237 }
244 238
245 /** 239 /**
246 * Returns the default style sheet that applies on all domains. 240 * Returns the default style sheet that applies on all domains.
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 if (domain != "") 296 if (domain != "")
303 knownExceptionDomains.add(domain); 297 knownExceptionDomains.add(domain);
304 } 298 }
305 } 299 }
306 300
307 // If this is the first exception for a previously unconditionally applied 301 // If this is the first exception for a previously unconditionally applied
308 // element hiding selector we need to take care to update the lookups. 302 // element hiding selector we need to take care to update the lookups.
309 let unconditionalFilterForSelector = filterBySelector.get(selector); 303 let unconditionalFilterForSelector = filterBySelector.get(selector);
310 if (unconditionalFilterForSelector) 304 if (unconditionalFilterForSelector)
311 { 305 {
312 addToFiltersByDomain(unconditionalFilterForSelector); 306 addToFiltersByDomain(Filter.fromText(unconditionalFilterForSelector,
307 false));
313 filterBySelector.delete(selector); 308 filterBySelector.delete(selector);
314 unconditionalSelectors = null; 309 unconditionalSelectors = null;
315 defaultStyleSheet = null; 310 defaultStyleSheet = null;
316 } 311 }
317 }); 312 });
318 313
319 /** 314 /**
320 * Container for element hiding filters 315 * Container for element hiding filters
321 * @class 316 * @class
322 */ 317 */
(...skipping 16 matching lines...) Expand all
339 334
340 filterNotifier.emit("elemhideupdate"); 335 filterNotifier.emit("elemhideupdate");
341 }, 336 },
342 337
343 /** 338 /**
344 * Add a new element hiding filter 339 * Add a new element hiding filter
345 * @param {ElemHideFilter} filter 340 * @param {ElemHideFilter} filter
346 */ 341 */
347 add(filter) 342 add(filter)
348 { 343 {
349 if (knownFilters.has(filter)) 344 let {text} = filter;
345
346 if (knownFilters.has(text))
350 return; 347 return;
351 348
352 styleSheetCache.clear(); 349 styleSheetCache.clear();
353 commonStyleSheet = null; 350 commonStyleSheet = null;
354 351
355 let {domains, selector} = filter; 352 let {domains, selector} = filter;
356 353
357 if (!(domains || ElemHideExceptions.hasExceptions(selector))) 354 if (!(domains || ElemHideExceptions.hasExceptions(selector)))
358 { 355 {
359 // The new filter's selector is unconditionally applied to all domains 356 // The new filter's selector is unconditionally applied to all domains
360 filterBySelector.set(selector, filter); 357 filterBySelector.set(selector, text);
361 unconditionalSelectors = null; 358 unconditionalSelectors = null;
362 defaultStyleSheet = null; 359 defaultStyleSheet = null;
363 } 360 }
364 else 361 else
365 { 362 {
366 // The new filter's selector only applies to some domains 363 // The new filter's selector only applies to some domains
367 addToFiltersByDomain(filter, domains); 364 addToFiltersByDomain(filter, domains);
368 } 365 }
369 366
370 knownFilters.add(filter); 367 knownFilters.add(text);
371 filterNotifier.emit("elemhideupdate"); 368 filterNotifier.emit("elemhideupdate");
372 }, 369 },
373 370
374 /** 371 /**
375 * Removes an element hiding filter 372 * Removes an element hiding filter
376 * @param {ElemHideFilter} filter 373 * @param {ElemHideFilter} filter
377 */ 374 */
378 remove(filter) 375 remove(filter)
379 { 376 {
380 if (!knownFilters.has(filter)) 377 let {text} = filter;
378
379 if (!knownFilters.has(text))
381 return; 380 return;
382 381
383 styleSheetCache.clear(); 382 styleSheetCache.clear();
384 commonStyleSheet = null; 383 commonStyleSheet = null;
385 384
386 let {selector} = filter; 385 let {selector} = filter;
387 386
388 // Unconditially applied element hiding filters 387 // Unconditially applied element hiding filters
389 if (filterBySelector.get(selector) == filter) 388 if (filterBySelector.get(selector) == text)
390 { 389 {
391 filterBySelector.delete(selector); 390 filterBySelector.delete(selector);
392 unconditionalSelectors = null; 391 unconditionalSelectors = null;
393 defaultStyleSheet = null; 392 defaultStyleSheet = null;
394 } 393 }
395 // Conditionally applied element hiding filters 394 // Conditionally applied element hiding filters
396 else 395 else
397 { 396 {
398 let domains = filter.domains || defaultDomains; 397 let domains = filter.domains || defaultDomains;
399 for (let domain of domains.keys()) 398 for (let domain of domains.keys())
400 { 399 {
401 let filters = filtersByDomain.get(domain); 400 let filters = filtersByDomain.get(domain);
402 if (filters) 401 if (filters)
403 { 402 {
404 filters.delete(filter); 403 filters.delete(text);
405 404
406 if (filters.size == 0) 405 if (filters.size == 0)
407 filtersByDomain.delete(domain); 406 filtersByDomain.delete(domain);
408 } 407 }
409 } 408 }
410 } 409 }
411 410
412 knownFilters.delete(filter); 411 knownFilters.delete(text);
413 filterNotifier.emit("elemhideupdate"); 412 filterNotifier.emit("elemhideupdate");
414 }, 413 },
415 414
416 /** 415 /**
417 * @typedef {object} ElemHideStyleSheet 416 * @typedef {object} ElemHideStyleSheet
418 * @property {string} code CSS code. 417 * @property {string} code CSS code.
419 * @property {Array.<string>} selectors List of selectors. 418 * @property {Array.<string>} selectors List of selectors.
420 */ 419 */
421 420
422 /** 421 /**
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
579 { 578 {
580 let styleSheet = ""; 579 let styleSheet = "";
581 580
582 for (let selectorGroup of splitSelectors(selectors)) 581 for (let selectorGroup of splitSelectors(selectors))
583 styleSheet += createRule(selectorGroup); 582 styleSheet += createRule(selectorGroup);
584 583
585 return styleSheet; 584 return styleSheet;
586 } 585 }
587 586
588 exports.createStyleSheet = createStyleSheet; 587 exports.createStyleSheet = createStyleSheet;
OLDNEW
« no previous file with comments | « no previous file | lib/filterClasses.js » ('j') | lib/filterClasses.js » ('J')

Powered by Google App Engine
This is Rietveld