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

Powered by Google App Engine
This is Rietveld