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: Remove unrelated changes Created May 9, 2018, 3:42 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();
67
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 }
64 87
65 /** 88 /**
66 * Returns a list of selectors that apply on each website unconditionally. 89 * Returns a list of selectors that apply on each website unconditionally.
67 * @returns {string[]} 90 * @returns {string[]}
68 */ 91 */
69 function getUnconditionalSelectors() 92 function getUnconditionalSelectors()
70 { 93 {
71 if (!unconditionalSelectors) 94 if (!unconditionalSelectors)
72 unconditionalSelectors = [...filterBySelector.keys()]; 95 unconditionalSelectors = [...filterBySelector.keys()];
96
73 return unconditionalSelectors; 97 return unconditionalSelectors;
74 } 98 }
75 99
76 /** 100 /**
77 * Container for element hiding filters 101 * Container for element hiding filters
78 * @class 102 * @class
79 */ 103 */
80 let ElemHide = exports.ElemHide = { 104 let ElemHide = exports.ElemHide = {
81 /** 105 /**
82 * Removes all known filters 106 * Removes all known filters
83 */ 107 */
84 clear() 108 clear()
85 { 109 {
86 for (let collection of [filtersByDomain, filterBySelector, 110 for (let collection of [filtersByDomain, filterBySelector,
87 knownFilters, exceptions]) 111 knownFilters, exceptions])
88 { 112 {
89 collection.clear(); 113 collection.clear();
90 } 114 }
91 unconditionalSelectors = null; 115 unconditionalSelectors = null;
92 FilterNotifier.emit("elemhideupdate"); 116 FilterNotifier.emit("elemhideupdate");
93 },
94
95 _addToFiltersByDomain(filter)
96 {
97 let domains = filter.domains || defaultDomains;
98 for (let [domain, isIncluded] of domains)
99 {
100 // There's no need to note that a filter is generically disabled.
101 if (!isIncluded && domain == "")
102 continue;
103
104 let filters = filtersByDomain.get(domain);
105 if (!filters)
106 filtersByDomain.set(domain, filters = new Map());
107 filters.set(filter, isIncluded);
108 }
109 }, 117 },
110 118
111 /** 119 /**
112 * Add a new element hiding filter 120 * Add a new element hiding filter
113 * @param {ElemHideBase} filter 121 * @param {ElemHideBase} filter
114 */ 122 */
115 add(filter) 123 add(filter)
116 { 124 {
117 if (knownFilters.has(filter)) 125 if (knownFilters.has(filter))
118 return; 126 return;
119 127
120 if (filter instanceof ElemHideException) 128 if (filter instanceof ElemHideException)
121 { 129 {
122 let {selector} = filter; 130 let {selector} = filter;
123 let list = exceptions.get(selector); 131 let list = exceptions.get(selector);
124 if (list) 132 if (list)
125 list.push(filter); 133 list.push(filter);
126 else 134 else
127 exceptions.set(selector, [filter]); 135 exceptions.set(selector, [filter]);
128 136
129 // If this is the first exception for a previously unconditionally 137 // If this is the first exception for a previously unconditionally
130 // 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
131 // lookups. 139 // lookups.
132 let unconditionalFilterForSelector = filterBySelector.get(selector); 140 let unconditionalFilterForSelector = filterBySelector.get(selector);
133 if (unconditionalFilterForSelector) 141 if (unconditionalFilterForSelector)
134 { 142 {
135 this._addToFiltersByDomain(unconditionalFilterForSelector); 143 addToFiltersByDomain(unconditionalFilterForSelector);
136 filterBySelector.delete(selector); 144 filterBySelector.delete(selector);
137 unconditionalSelectors = null; 145 unconditionalSelectors = null;
138 } 146 }
139 } 147 }
140 else if (!(filter.domains || exceptions.has(filter.selector))) 148 else if (!(filter.domains || exceptions.has(filter.selector)))
141 { 149 {
142 // The new filter's selector is unconditionally applied to all domains 150 // The new filter's selector is unconditionally applied to all domains
143 filterBySelector.set(filter.selector, filter); 151 filterBySelector.set(filter.selector, filter);
144 unconditionalSelectors = null; 152 unconditionalSelectors = null;
145 } 153 }
146 else 154 else
147 { 155 {
148 // The new filter's selector only applies to some domains 156 // The new filter's selector only applies to some domains
149 this._addToFiltersByDomain(filter); 157 addToFiltersByDomain(filter);
150 } 158 }
151 159
152 knownFilters.add(filter); 160 knownFilters.add(filter);
153 FilterNotifier.emit("elemhideupdate"); 161 FilterNotifier.emit("elemhideupdate");
154 }, 162 },
155 163
156 /** 164 /**
157 * Removes an element hiding filter 165 * Removes an element hiding filter
158 * @param {ElemHideBase} filter 166 * @param {ElemHideBase} filter
159 */ 167 */
(...skipping 29 matching lines...) Expand all
189 } 197 }
190 198
191 knownFilters.delete(filter); 199 knownFilters.delete(filter);
192 FilterNotifier.emit("elemhideupdate"); 200 FilterNotifier.emit("elemhideupdate");
193 }, 201 },
194 202
195 /** 203 /**
196 * 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
197 * domain. 205 * domain.
198 * @param {Filter} filter 206 * @param {Filter} filter
199 * @param {string} docDomain 207 * @param {?string} docDomain
200 * @return {ElemHideException} 208 * @return {?ElemHideException}
201 */ 209 */
202 getException(filter, docDomain) 210 getException(filter, docDomain)
203 { 211 {
204 let list = exceptions.get(filter.selector); 212 let list = exceptions.get(filter.selector);
205 if (!list) 213 if (!list)
206 return null; 214 return null;
207 215
208 for (let i = list.length - 1; i >= 0; i--) 216 for (let i = list.length - 1; i >= 0; i--)
209 { 217 {
210 if (list[i].isActiveOnDomain(docDomain)) 218 if (list[i].isActiveOnDomain(docDomain))
211 return list[i]; 219 return list[i];
212 } 220 }
213 221
214 return null; 222 return null;
215 }, 223 },
216 224
217 /** 225 /**
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 262
249 if (typeof criteria == "undefined")
250 criteria = ElemHide.ALL_MATCHING;
251
252 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); 263 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY);
253
254 let excluded = new Set(); 264 let excluded = new Set();
255 let currentDomain = domain ? domain.toUpperCase() : ""; 265 let currentDomain = domain ? domain.toUpperCase() : "";
256 266
257 // 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
258 // micro-optimisations. Please be careful before making changes. 268 // micro-optimisations. Please be careful before making changes.
259 while (true) 269 while (true)
260 { 270 {
261 if (specificOnly && currentDomain == "") 271 if (specificOnly && currentDomain == "")
262 break; 272 break;
263 273
(...skipping 20 matching lines...) Expand all
284 let nextDot = currentDomain.indexOf("."); 294 let nextDot = currentDomain.indexOf(".");
285 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 295 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
286 } 296 }
287 297
288 if (criteria < ElemHide.NO_UNCONDITIONAL) 298 if (criteria < ElemHide.NO_UNCONDITIONAL)
289 selectors = getUnconditionalSelectors().concat(selectors); 299 selectors = getUnconditionalSelectors().concat(selectors);
290 300
291 return selectors; 301 return selectors;
292 } 302 }
293 }; 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