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

Unified 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: Created Nov. 3, 2018, 7:54 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | test/filterListener.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/elemHide.js
===================================================================
--- a/lib/elemHide.js
+++ b/lib/elemHide.js
@@ -16,39 +16,40 @@
*/
"use strict";
/**
* @fileOverview Element hiding implementation.
*/
+const {Filter} = require("./filterClasses");
const {ElemHideExceptions} = require("./elemHideExceptions");
const {filterNotifier} = require("./filterNotifier");
/**
* The maximum number of selectors in a CSS rule. This is used by
* <code>{@link createStyleSheet}</code> to split up a long list of selectors
* into multiple rules.
* @const {number}
* @default
*/
const selectorGroupSize = 1024;
/**
* Lookup table, active flag, by filter by domain.
Manish Jethani 2018/11/03 20:01:25 Throughout this file, "filter" now means filter te
hub 2018/11/22 15:59:33 I'd update the comment to say `by filter text`
Manish Jethani 2018/11/23 02:03:33 Done.
* (Only contains filters that aren't unconditionally matched for all domains.)
- * @type {Map.<string,Map.<Filter,boolean>>}
+ * @type {Map.<string,Map.<string,boolean>>}
*/
let filtersByDomain = new Map();
/**
* Lookup table, filter by selector. (Only used for selectors that are
* unconditionally matched for all domains.)
- * @type {Map.<string,Filter>}
+ * @type {Map.<string,string>}
*/
let filterBySelector = new Map();
/**
* This array caches the keys of filterBySelector table (selectors
* which unconditionally apply on all domains). It will be null if the
* cache needs to be rebuilt.
* @type {?string[]}
@@ -75,17 +76,17 @@
* Map to be used instead when a filter has a blank domains property.
* @type {Map.<string,boolean>}
* @const
*/
let defaultDomains = new Map([["", true]]);
/**
* Set containing known element hiding filters
- * @type {Set.<ElemHideFilter>}
+ * @type {Set.<string>}
*/
let knownFilters = new Set();
/**
* All domains known to occur in exceptions
* @type {Set.<string>}
*/
let knownExceptionDomains = new Set();
@@ -119,17 +120,17 @@
{
// There's no need to note that a filter is generically disabled.
if (!isIncluded && domain == "")
continue;
let filters = filtersByDomain.get(domain);
if (!filters)
filtersByDomain.set(domain, filters = new Map());
- filters.set(filter, isIncluded);
+ filters.set(filter.text, isIncluded ? filter.selector : null);
Manish Jethani 2018/11/03 20:01:25 Instead of true/false, we now keep the selector or
}
}
/**
* Returns a list of selectors that apply on each website unconditionally.
* @returns {string[]}
*/
function getUnconditionalSelectors()
@@ -162,30 +163,26 @@
while (true)
{
if (specificOnly && currentDomain == "")
break;
let filters = filtersByDomain.get(currentDomain);
if (filters)
{
- for (let [filter, isIncluded] of filters)
+ for (let [text, selector] of filters)
{
- if (!isIncluded)
+ if (selector == null)
{
- excluded.add(filter);
+ excluded.add(text);
}
- else
+ else if ((excluded.size == 0 || !excluded.has(text)) &&
+ !ElemHideExceptions.getException(selector, domain))
{
- let {selector} = filter;
- if ((excluded.size == 0 || !excluded.has(filter)) &&
- !ElemHideExceptions.getException(selector, domain))
- {
- selectors.push(selector);
- }
+ selectors.push(selector);
}
}
}
if (currentDomain == "")
break;
let nextDot = currentDomain.indexOf(".");
@@ -238,17 +235,17 @@
}
}
// If this is the first exception for a previously unconditionally applied
// element hiding selector we need to take care to update the lookups.
let unconditionalFilterForSelector = filterBySelector.get(selector);
if (unconditionalFilterForSelector)
{
- addToFiltersByDomain(unconditionalFilterForSelector);
+ addToFiltersByDomain(Filter.fromText(unconditionalFilterForSelector));
Manish Jethani 2018/11/03 20:01:25 This is the one time where we have to get the filt
filterBySelector.delete(selector);
unconditionalSelectors = null;
defaultStyleSheet = null;
}
});
/**
* Container for element hiding filters
@@ -275,78 +272,78 @@
},
/**
* Add a new element hiding filter
* @param {ElemHideFilter} filter
*/
add(filter)
{
- if (knownFilters.has(filter))
+ if (knownFilters.has(filter.text))
return;
commonStyleSheet = null;
let {domains, selector} = filter;
if (!(domains || ElemHideExceptions.hasExceptions(selector)))
{
// The new filter's selector is unconditionally applied to all domains
- filterBySelector.set(selector, filter);
+ filterBySelector.set(selector, filter.text);
unconditionalSelectors = null;
defaultStyleSheet = null;
}
else
{
// The new filter's selector only applies to some domains
addToFiltersByDomain(filter, domains);
}
- knownFilters.add(filter);
+ knownFilters.add(filter.text);
filterNotifier.emit("elemhideupdate");
},
/**
* Removes an element hiding filter
* @param {ElemHideFilter} filter
*/
remove(filter)
{
- if (!knownFilters.has(filter))
+ if (!knownFilters.has(filter.text))
return;
commonStyleSheet = null;
let {selector} = filter;
// Unconditially applied element hiding filters
- if (filterBySelector.get(selector) == filter)
+ if (filterBySelector.get(selector) == filter.text)
{
filterBySelector.delete(selector);
unconditionalSelectors = null;
defaultStyleSheet = null;
}
// Conditionally applied element hiding filters
else
{
let domains = filter.domains || defaultDomains;
for (let domain of domains.keys())
{
let filters = filtersByDomain.get(domain);
if (filters)
{
- filters.delete(filter);
+ filters.delete(filter.text);
if (filters.size == 0)
filtersByDomain.delete(domain);
}
}
}
- knownFilters.delete(filter);
+ knownFilters.delete(filter.text);
filterNotifier.emit("elemhideupdate");
},
/**
* @typedef {object} ElemHideStyleSheet
* @property {string} code CSS code.
* @property {Array.<string>} selectors List of selectors.
*/
« no previous file with comments | « no previous file | test/filterListener.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld