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

Side by Side Diff: lib/elemHide.js

Issue 29551635: Issue 5735 - Use JS Map instead of Object for filters which are stored in filtersByDomain in elem... (Closed) Base URL: https://github.com/adblockplus/adblockpluscore.git
Patch Set: Created Sept. 21, 2017, 11:44 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 | no next file » | no next file with comments »
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
(...skipping 21 matching lines...) Expand all
32 32
33 /** 33 /**
34 * Lookup table, keys of the filters by filter text 34 * Lookup table, keys of the filters by filter text
35 * @type {Object} 35 * @type {Object}
36 */ 36 */
37 let keyByFilter = Object.create(null); 37 let keyByFilter = Object.create(null);
38 38
39 /** 39 /**
40 * Nested lookup table, filter (or false if inactive) by filter key by domain. 40 * Nested lookup table, filter (or false if inactive) by filter key by domain.
41 * (Only contains filters that aren't unconditionally matched for all domains.) 41 * (Only contains filters that aren't unconditionally matched for all domains.)
42 * @type {Map.<string,Object>} 42 * @type {Map.<string,Map.<string,(Filter|boolean)>>}
43 */ 43 */
44 let filtersByDomain = new Map(); 44 let filtersByDomain = new Map();
45 45
46 /** 46 /**
47 * Lookup table, filter key by selector. (Only used for selectors that are 47 * Lookup table, filter key by selector. (Only used for selectors that are
48 * unconditionally matched for all domains.) 48 * unconditionally matched for all domains.)
49 */ 49 */
50 let filterKeyBySelector = Object.create(null); 50 let filterKeyBySelector = Object.create(null);
51 51
52 /** 52 /**
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 FilterNotifier.emit("elemhideupdate"); 100 FilterNotifier.emit("elemhideupdate");
101 }, 101 },
102 102
103 _addToFiltersByDomain(key, filter) 103 _addToFiltersByDomain(key, filter)
104 { 104 {
105 let domains = filter.domains || defaultDomains; 105 let domains = filter.domains || defaultDomains;
106 for (let [domain, isIncluded] of domains) 106 for (let [domain, isIncluded] of domains)
107 { 107 {
108 let filters = filtersByDomain.get(domain); 108 let filters = filtersByDomain.get(domain);
109 if (!filters) 109 if (!filters)
110 filtersByDomain.set(domain, filters = Object.create(null)); 110 filtersByDomain.set(domain, filters = new Map());
111 111 filters.set(key, isIncluded ? filter : false);
112 if (isIncluded)
113 filters[key] = filter;
114 else
115 filters[key] = false;
116 } 112 }
117 }, 113 },
118 114
119 /** 115 /**
120 * Add a new element hiding filter 116 * Add a new element hiding filter
121 * @param {ElemHideFilter} filter 117 * @param {ElemHideFilter} filter
122 */ 118 */
123 add(filter) 119 add(filter)
124 { 120 {
125 if (filter instanceof ElemHideException) 121 if (filter instanceof ElemHideException)
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 return; 174 return;
179 } 175 }
180 176
181 // We haven't found this filter in unconditional filters, look in 177 // We haven't found this filter in unconditional filters, look in
182 // filtersByDomain. 178 // filtersByDomain.
183 let domains = filter.domains || defaultDomains; 179 let domains = filter.domains || defaultDomains;
184 for (let domain of domains.keys()) 180 for (let domain of domains.keys())
185 { 181 {
186 let filters = filtersByDomain.get(domain); 182 let filters = filtersByDomain.get(domain);
187 if (filters) 183 if (filters)
188 delete filters[key]; 184 filters.delete(key);
189 } 185 }
190 }, 186 },
191 187
192 /** 188 /**
193 * Removes an element hiding filter 189 * Removes an element hiding filter
194 * @param {ElemHideFilter} filter 190 * @param {ElemHideFilter} filter
195 */ 191 */
196 remove(filter) 192 remove(filter)
197 { 193 {
198 if (filter instanceof ElemHideException) 194 if (filter instanceof ElemHideException)
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 let seenFilters = Object.create(null); 354 let seenFilters = Object.create(null);
359 let currentDomain = domain ? domain.toUpperCase() : ""; 355 let currentDomain = domain ? domain.toUpperCase() : "";
360 while (true) 356 while (true)
361 { 357 {
362 if (specificOnly && currentDomain == "") 358 if (specificOnly && currentDomain == "")
363 break; 359 break;
364 360
365 let filters = filtersByDomain.get(currentDomain); 361 let filters = filtersByDomain.get(currentDomain);
366 if (filters) 362 if (filters)
367 { 363 {
368 for (let filterKey in filters) 364 for (let [filterKey, filter] of filters)
369 { 365 {
370 if (filterKey in seenFilters) 366 if (filterKey in seenFilters)
371 continue; 367 continue;
372 seenFilters[filterKey] = true; 368 seenFilters[filterKey] = true;
373 369
374 let filter = filters[filterKey];
375 if (filter && !this.getException(filter, domain)) 370 if (filter && !this.getException(filter, domain))
376 { 371 {
377 selectors.push(filter.selector); 372 selectors.push(filter.selector);
378 // It is faster to always push the key, even if not required. 373 // It is faster to always push the key, even if not required.
379 filterKeys.push(filterKey); 374 filterKeys.push(filterKey);
380 } 375 }
381 } 376 }
382 } 377 }
383 378
384 if (currentDomain == "") 379 if (currentDomain == "")
385 break; 380 break;
386 381
387 let nextDot = currentDomain.indexOf("."); 382 let nextDot = currentDomain.indexOf(".");
388 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 383 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
389 } 384 }
390 385
391 if (provideFilterKeys) 386 if (provideFilterKeys)
392 return [selectors, filterKeys]; 387 return [selectors, filterKeys];
393 return selectors; 388 return selectors;
394 } 389 }
395 }; 390 };
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld