Index: lib/elemHideEmulation.js |
=================================================================== |
--- a/lib/elemHideEmulation.js |
+++ b/lib/elemHideEmulation.js |
@@ -17,64 +17,122 @@ |
"use strict"; |
/** |
* @fileOverview Element hiding emulation implementation. |
*/ |
const {ElemHide} = require("./elemHide"); |
-const {Filter} = require("./filterClasses"); |
+const {filterIt, mapIt, flattenIt, regExpIt} = require("./coreUtils"); |
+ |
+/** |
+ * Map of element hiding emulation filters by domain |
+ * @type {Map.<string,Set.<ElemHideEmulationFilter>>} |
+ */ |
+let filtersByDomain = new Map(); |
+ |
+/** |
+ * Set containing known element hiding emulation filters |
+ * @type {Set.<ElemHideEmulation>} |
+ */ |
+let knownFilters = new Set(); |
-let filters = new Set(); |
+function* filterDomains(filter) |
+{ |
+ yield* mapIt(filterIt(filter.domains || [], ([domain]) => domain != ""), |
+ ([domain, include]) => [domain, include, |
+ filtersByDomain.get(domain)]); |
+} |
+ |
+function* filtersForDomain(domain) |
+{ |
+ yield* filtersByDomain.get(domain) || []; |
+ |
+ let subdomainIt = |
+ mapIt(regExpIt(/([^|]*)\|/g, domain.replace(/([^.]+\.)/g, "$'|")), |
+ ([, subdomain]) => filtersByDomain.get(subdomain) || []); |
+ yield* flattenIt(subdomainIt); |
+} |
/** |
* Container for element hiding emulation filters |
* @class |
*/ |
let ElemHideEmulation = { |
/** |
* Removes all known filters |
*/ |
clear() |
{ |
- filters.clear(); |
+ filtersByDomain.clear(); |
}, |
/** |
* Add a new element hiding emulation filter |
* @param {ElemHideEmulationFilter} filter |
*/ |
add(filter) |
{ |
- filters.add(filter.text); |
+ if (knownFilters.has(filter)) |
+ return; |
+ |
+ for (let [domain, include, filters] of filterDomains(filter)) |
+ { |
+ if (filters) |
+ filters.set(filter, include); |
+ else |
+ filtersByDomain.set(domain, new Map([[filter, include]])); |
+ } |
+ |
+ knownFilters.add(filter); |
}, |
/** |
* Removes an element hiding emulation filter |
* @param {ElemHideEmulationFilter} filter |
*/ |
remove(filter) |
{ |
- filters.delete(filter.text); |
+ if (!knownFilters.has(filter)) |
+ return; |
+ |
+ for (let [domain, , filters] of filterDomains(filter)) |
+ { |
+ if (filters) |
+ { |
+ filters.delete(filter); |
+ |
+ if (filters.size == 0) |
+ filtersByDomain.delete(domain); |
+ } |
+ } |
+ |
+ knownFilters.delete(filter); |
}, |
/** |
* Returns a list of all rules active on a particular domain |
* @param {string} domain |
* @return {ElemHideEmulationFilter[]} |
*/ |
getRulesForDomain(domain) |
{ |
let result = []; |
- for (let text of filters.values()) |
+ |
+ let excludeSet = new Set(); |
+ for (let [filter, include] of filtersForDomain(domain.toUpperCase())) |
{ |
- let filter = Filter.fromText(text); |
- if (filter.isActiveOnDomain(domain) && |
- !ElemHide.getException(filter, domain)) |
+ if (!include) |
+ { |
+ excludeSet.add(filter); |
+ } |
+ else if ((excludeSet.size == 0 || !excludeSet.has(filter)) && |
+ !ElemHide.getException(filter, domain)) |
{ |
result.push(filter); |
} |
} |
+ |
return result; |
} |
}; |
exports.ElemHideEmulation = ElemHideEmulation; |