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

Delta Between Two Patch Sets: lib/elemHide.js

Issue 29774573: Issue 6652 - Do not push to unconditional selectors array (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created May 8, 2018, 4:53 p.m.
Right Patch Set: Rebase on patch #29778572 Created May 11, 2018, 12:23 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
(...skipping 24 matching lines...) Expand all
35 * Lookup table, filter by selector. (Only used for selectors that are 35 * Lookup table, filter by selector. (Only used for selectors that are
36 * unconditionally matched for all domains.) 36 * unconditionally matched for all domains.)
37 * @type {Map.<string,Filter>} 37 * @type {Map.<string,Filter>}
38 */ 38 */
39 let filterBySelector = new Map(); 39 let filterBySelector = new Map();
40 40
41 /** 41 /**
42 * This array caches the keys of filterBySelector table (selectors 42 * This array caches the keys of filterBySelector table (selectors
43 * which unconditionally apply on all domains). It will be null if the 43 * which unconditionally apply on all domains). It will be null if the
44 * cache needs to be rebuilt. 44 * cache needs to be rebuilt.
45 * @type {?string[]}
45 */ 46 */
46 let unconditionalSelectors = null; 47 let unconditionalSelectors = null;
47 48
48 /** 49 /**
49 * Map to be used instead when a filter has a blank domains property. 50 * Map to be used instead when a filter has a blank domains property.
51 * @type {Map.<string,boolean>}
52 * @const
50 */ 53 */
51 let defaultDomains = new Map([["", true]]); 54 let defaultDomains = new Map([["", true]]);
52 55
53 /** 56 /**
54 * Set containing known element hiding and exception filters 57 * Set containing known element hiding and exception filters
55 * @type {Set.<ElemHideBase>} 58 * @type {Set.<ElemHideBase>}
56 */ 59 */
57 let knownFilters = new Set(); 60 let knownFilters = new Set();
58 61
59 /** 62 /**
60 * Lookup table, lists of element hiding exceptions by selector 63 * Lookup table, lists of element hiding exceptions by selector
61 * @type {Map.<string,Filter>} 64 * @type {Map.<string,Filter>}
62 */ 65 */
63 let exceptions = new Map(); 66 let exceptions = new Map();
64 67
65 /** 68 /**
69 * Adds a filter to the lookup table of filters by domain.
70 * @param {Filter}
71 */
72 function addToFiltersByDomain(filter)
73 {
74 let domains = filter.domains || defaultDomains;
75 for (let [domain, isIncluded] of domains)
76 {
77 // There's no need to note that a filter is generically disabled.
78 if (!isIncluded && domain == "")
79 continue;
80
81 let filters = filtersByDomain.get(domain);
82 if (!filters)
83 filtersByDomain.set(domain, filters = new Map());
84 filters.set(filter, isIncluded);
85 }
86 }
87
88 /**
89 * Returns a list of selectors that apply on each website unconditionally.
90 * @returns {string[]}
91 */
92 function getUnconditionalSelectors()
93 {
94 if (!unconditionalSelectors)
95 unconditionalSelectors = [...filterBySelector.keys()];
96
97 return unconditionalSelectors;
98 }
99
100 /**
66 * Container for element hiding filters 101 * Container for element hiding filters
67 * @class 102 * @class
68 */ 103 */
69 let ElemHide = exports.ElemHide = { 104 let ElemHide = exports.ElemHide = {
70 /** 105 /**
71 * Removes all known filters 106 * Removes all known filters
72 */ 107 */
73 clear() 108 clear()
74 { 109 {
75 for (let collection of [filtersByDomain, filterBySelector, 110 for (let collection of [filtersByDomain, filterBySelector,
76 knownFilters, exceptions]) 111 knownFilters, exceptions])
77 { 112 {
78 collection.clear(); 113 collection.clear();
79 } 114 }
80 unconditionalSelectors = null; 115 unconditionalSelectors = null;
81 FilterNotifier.emit("elemhideupdate"); 116 FilterNotifier.emit("elemhideupdate");
82 },
83
84 _addToFiltersByDomain(filter)
85 {
86 let domains = filter.domains || defaultDomains;
87 for (let [domain, isIncluded] of domains)
88 {
89 // There's no need to note that a filter is generically disabled.
90 if (!isIncluded && domain == "")
91 continue;
92
93 let filters = filtersByDomain.get(domain);
94 if (!filters)
95 filtersByDomain.set(domain, filters = new Map());
96 filters.set(filter, isIncluded);
97 }
98 }, 117 },
99 118
100 /** 119 /**
101 * Add a new element hiding filter 120 * Add a new element hiding filter
102 * @param {ElemHideBase} filter 121 * @param {ElemHideBase} filter
103 */ 122 */
104 add(filter) 123 add(filter)
105 { 124 {
106 if (knownFilters.has(filter)) 125 if (knownFilters.has(filter))
107 return; 126 return;
108 127
109 if (filter instanceof ElemHideException) 128 if (filter instanceof ElemHideException)
110 { 129 {
111 let {selector} = filter; 130 let {selector} = filter;
112 let list = exceptions.get(selector); 131 let list = exceptions.get(selector);
113 if (list) 132 if (list)
114 list.push(filter); 133 list.push(filter);
115 else 134 else
116 exceptions.set(selector, [filter]); 135 exceptions.set(selector, [filter]);
117 136
118 // If this is the first exception for a previously unconditionally 137 // If this is the first exception for a previously unconditionally
119 // applied element hiding selector we need to take care to update the 138 // applied element hiding selector we need to take care to update the
120 // lookups. 139 // lookups.
121 let unconditionalFilterForSelector = filterBySelector.get(selector); 140 let unconditionalFilterForSelector = filterBySelector.get(selector);
122 if (unconditionalFilterForSelector) 141 if (unconditionalFilterForSelector)
123 { 142 {
124 this._addToFiltersByDomain(unconditionalFilterForSelector); 143 addToFiltersByDomain(unconditionalFilterForSelector);
125 filterBySelector.delete(selector); 144 filterBySelector.delete(selector);
126 unconditionalSelectors = null; 145 unconditionalSelectors = null;
127 } 146 }
128 } 147 }
129 else if (!(filter.domains || exceptions.has(filter.selector))) 148 else if (!(filter.domains || exceptions.has(filter.selector)))
130 { 149 {
131 // The new filter's selector is unconditionally applied to all domains 150 // The new filter's selector is unconditionally applied to all domains
132 filterBySelector.set(filter.selector, filter); 151 filterBySelector.set(filter.selector, filter);
133 unconditionalSelectors = null; 152 unconditionalSelectors = null;
134 } 153 }
135 else 154 else
136 { 155 {
137 // The new filter's selector only applies to some domains 156 // The new filter's selector only applies to some domains
138 this._addToFiltersByDomain(filter); 157 addToFiltersByDomain(filter);
139 } 158 }
140 159
141 knownFilters.add(filter); 160 knownFilters.add(filter);
142 FilterNotifier.emit("elemhideupdate"); 161 FilterNotifier.emit("elemhideupdate");
143 }, 162 },
144 163
145 /** 164 /**
146 * Removes an element hiding filter 165 * Removes an element hiding filter
147 * @param {ElemHideBase} filter 166 * @param {ElemHideBase} filter
148 */ 167 */
(...skipping 29 matching lines...) Expand all
178 } 197 }
179 198
180 knownFilters.delete(filter); 199 knownFilters.delete(filter);
181 FilterNotifier.emit("elemhideupdate"); 200 FilterNotifier.emit("elemhideupdate");
182 }, 201 },
183 202
184 /** 203 /**
185 * Checks whether an exception rule is registered for a filter on a particular 204 * Checks whether an exception rule is registered for a filter on a particular
186 * domain. 205 * domain.
187 * @param {Filter} filter 206 * @param {Filter} filter
188 * @param {string} docDomain 207 * @param {?string} docDomain
189 * @return {ElemHideException} 208 * @return {?ElemHideException}
190 */ 209 */
191 getException(filter, docDomain) 210 getException(filter, docDomain)
192 { 211 {
193 let list = exceptions.get(filter.selector); 212 let list = exceptions.get(filter.selector);
194 if (!list) 213 if (!list)
195 return null; 214 return null;
196 215
197 for (let i = list.length - 1; i >= 0; i--) 216 for (let i = list.length - 1; i >= 0; i--)
198 { 217 {
199 if (list[i].isActiveOnDomain(docDomain)) 218 if (list[i].isActiveOnDomain(docDomain))
200 return list[i]; 219 return list[i];
201 } 220 }
202 221
203 return null; 222 return null;
204 }, 223 },
205 224
206 /** 225 /**
207 * Returns a list of selectors that apply on each website unconditionally.
208 * @returns {string[]}
209 */
210 getUnconditionalSelectors()
211 {
212 if (!unconditionalSelectors)
213 unconditionalSelectors = [...filterBySelector.keys()];
214 return unconditionalSelectors.slice();
215 },
216
217 /**
218 * Constant used by getSelectorsForDomain to return all selectors applying to 226 * Constant used by getSelectorsForDomain to return all selectors applying to
219 * a particular hostname. 227 * a particular hostname.
228 * @type {number}
229 * @const
220 */ 230 */
221 ALL_MATCHING: 0, 231 ALL_MATCHING: 0,
222 232
223 /** 233 /**
224 * Constant used by getSelectorsForDomain to exclude selectors which apply to 234 * Constant used by getSelectorsForDomain to exclude selectors which apply to
225 * all websites without exception. 235 * all websites without exception.
236 * @type {number}
237 * @const
226 */ 238 */
227 NO_UNCONDITIONAL: 1, 239 NO_UNCONDITIONAL: 1,
228 240
229 /** 241 /**
230 * Constant used by getSelectorsForDomain to return only selectors for filters 242 * Constant used by getSelectorsForDomain to return only selectors for filters
231 * which specifically match the given host name. 243 * which specifically match the given host name.
244 * @type {number}
245 * @const
232 */ 246 */
233 SPECIFIC_ONLY: 2, 247 SPECIFIC_ONLY: 2,
234 248
235 /** 249 /**
236 * Determines from the current filter list which selectors should be applied 250 * Determines from the current filter list which selectors should be applied
237 * on a particular host name. 251 * on a particular host name.
238 * @param {string} domain 252 * @param {string} domain
239 * @param {number} [criteria] 253 * @param {number} [criteria]
240 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or 254 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or
241 * ElemHide.SPECIFIC_ONLY. 255 * ElemHide.SPECIFIC_ONLY.
242 * @returns {string[]} 256 * @returns {string[]}
243 * List of selectors. 257 * List of selectors.
244 */ 258 */
245 getSelectorsForDomain(domain, criteria) 259 getSelectorsForDomain(domain, criteria = ElemHide.ALL_MATCHING)
246 { 260 {
247 let selectors = []; 261 let selectors = [];
248
249 let unconditionalSelectors = null;
250
251 if (typeof criteria == "undefined")
252 criteria = ElemHide.ALL_MATCHING;
253 if (criteria < ElemHide.NO_UNCONDITIONAL)
254 unconditionalSelectors = this.getUnconditionalSelectors();
255 262
256 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); 263 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY);
257 let excluded = new Set(); 264 let excluded = new Set();
258 let currentDomain = domain ? domain.toUpperCase() : ""; 265 let currentDomain = domain ? domain.toUpperCase() : "";
259 266
260 // This code is a performance hot-spot, which is why we've made certain 267 // This code is a performance hot-spot, which is why we've made certain
261 // micro-optimisations. Please be careful before making changes. 268 // micro-optimisations. Please be careful before making changes.
262 while (true) 269 while (true)
263 { 270 {
264 if (specificOnly && currentDomain == "") 271 if (specificOnly && currentDomain == "")
(...skipping 16 matching lines...) Expand all
281 } 288 }
282 } 289 }
283 290
284 if (currentDomain == "") 291 if (currentDomain == "")
285 break; 292 break;
286 293
287 let nextDot = currentDomain.indexOf("."); 294 let nextDot = currentDomain.indexOf(".");
288 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 295 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
289 } 296 }
290 297
291 if (unconditionalSelectors) 298 if (criteria < ElemHide.NO_UNCONDITIONAL)
292 selectors = unconditionalSelectors.concat(selectors); 299 selectors = getUnconditionalSelectors().concat(selectors);
293 300
294 return selectors; 301 return selectors;
295 } 302 }
296 }; 303 };
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld