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

Side by Side Diff: lib/elemHide.js

Issue 29328165: Issue 3095 - Use a JavaScript Map in ElemHide
Patch Set: Created Sept. 17, 2015, 7:06 p.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-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 12 matching lines...) Expand all
23 23
24 let {Utils} = require("utils"); 24 let {Utils} = require("utils");
25 let {IO} = require("io"); 25 let {IO} = require("io");
26 let {Prefs} = require("prefs"); 26 let {Prefs} = require("prefs");
27 let {ElemHideException} = require("filterClasses"); 27 let {ElemHideException} = require("filterClasses");
28 let {FilterNotifier} = require("filterNotifier"); 28 let {FilterNotifier} = require("filterNotifier");
29 let {AboutHandler} = require("elemHideHitRegistration"); 29 let {AboutHandler} = require("elemHideHitRegistration");
30 30
31 /** 31 /**
32 * Lookup table, filters by their associated key 32 * Lookup table, filters by their associated key
33 * @type Object 33 * @type Map
34 */ 34 */
35 let filterByKey = Object.create(null); 35 let filterByKey = new Map();
36 36
37 /** 37 /**
38 * Lookup table, keys of the filters by filter text 38 * Lookup table, keys of the filters by filter
39 * @type Object 39 * @type WeakMap
40 */ 40 */
41 let keyByFilter = Object.create(null); 41 let keyByFilter = new Map();
42 42
43 /** 43 /**
44 * Lookup table, keys are known element hiding exceptions 44 * Lookup table, keys are known element hiding exceptions
45 * @type Object 45 * @type Object
46 */ 46 */
47 let knownExceptions = Object.create(null); 47 let knownExceptions = Object.create(null);
48 48
49 /** 49 /**
50 * Lookup table, lists of element hiding exceptions by selector 50 * Lookup table, lists of element hiding exceptions by selector
51 * @type Object 51 * @type Object
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 let styleFile = IO.resolveFilePath(Prefs.data_directory); 94 let styleFile = IO.resolveFilePath(Prefs.data_directory);
95 styleFile.append("elemhide.css"); 95 styleFile.append("elemhide.css");
96 styleURL = Services.io.newFileURI(styleFile).QueryInterface(Ci.nsIFileURL); 96 styleURL = Services.io.newFileURI(styleFile).QueryInterface(Ci.nsIFileURL);
97 }, 97 },
98 98
99 /** 99 /**
100 * Removes all known filters 100 * Removes all known filters
101 */ 101 */
102 clear: function() 102 clear: function()
103 { 103 {
104 filterByKey = Object.create(null); 104 filterByKey = new Map();
105 keyByFilter = Object.create(null); 105 keyByFilter = new Map();
106 knownExceptions = Object.create(null); 106 knownExceptions = Object.create(null);
107 exceptions = Object.create(null); 107 exceptions = Object.create(null);
108 ElemHide.isDirty = false; 108 ElemHide.isDirty = false;
109 ElemHide.unapply(); 109 ElemHide.unapply();
110 }, 110 },
111 111
112 /** 112 /**
113 * Add a new element hiding filter 113 * Add a new element hiding filter
114 * @param {ElemHideFilter} filter 114 * @param {ElemHideFilter} filter
115 */ 115 */
116 add: function(filter) 116 add: function(filter)
117 { 117 {
118 if (filter instanceof ElemHideException) 118 if (filter instanceof ElemHideException)
119 { 119 {
120 if (filter.text in knownExceptions) 120 if (filter.text in knownExceptions)
121 return; 121 return;
122 122
123 let selector = filter.selector; 123 let selector = filter.selector;
124 if (!(selector in exceptions)) 124 if (!(selector in exceptions))
125 exceptions[selector] = []; 125 exceptions[selector] = [];
126 exceptions[selector].push(filter); 126 exceptions[selector].push(filter);
127 knownExceptions[filter.text] = true; 127 knownExceptions[filter.text] = true;
128 } 128 }
129 else 129 else
130 { 130 {
131 if (filter.text in keyByFilter) 131 if (keyByFilter.has(filter.text))
132 return; 132 return;
133 133
134 let key; 134 let key;
135 do { 135 do {
136 key = Math.random().toFixed(15).substr(5); 136 key = Math.random().toFixed(15).substr(5);
137 } while (key in filterByKey); 137 } while (filterByKey.has(key));
138 138
139 filterByKey[key] = filter; 139 filterByKey.set(key, filter);
140 keyByFilter[filter.text] = key; 140 keyByFilter.set(filter.text, key);
141 ElemHide.isDirty = true; 141 ElemHide.isDirty = true;
142 } 142 }
143 }, 143 },
144 144
145 /** 145 /**
146 * Removes an element hiding filter 146 * Removes an element hiding filter
147 * @param {ElemHideFilter} filter 147 * @param {ElemHideFilter} filter
148 */ 148 */
149 remove: function(filter) 149 remove: function(filter)
150 { 150 {
151 if (filter instanceof ElemHideException) 151 if (filter instanceof ElemHideException)
152 { 152 {
153 if (!(filter.text in knownExceptions)) 153 if (!(filter.text in knownExceptions))
154 return; 154 return;
155 155
156 let list = exceptions[filter.selector]; 156 let list = exceptions[filter.selector];
157 let index = list.indexOf(filter); 157 let index = list.indexOf(filter);
158 if (index >= 0) 158 if (index >= 0)
159 list.splice(index, 1); 159 list.splice(index, 1);
160 delete knownExceptions[filter.text]; 160 delete knownExceptions[filter.text];
161 } 161 }
162 else 162 else
163 { 163 {
164 if (!(filter.text in keyByFilter)) 164 if (!keyByFilter.has(filter.text))
165 return; 165 return;
166 166
167 let key = keyByFilter[filter.text]; 167 let key = keyByFilter.get(filter.text);
168 delete filterByKey[key]; 168 filterByKey.delete(key);
169 delete keyByFilter[filter.text]; 169 keyByFilter.delete(filter.text);
170 ElemHide.isDirty = true; 170 ElemHide.isDirty = true;
171 } 171 }
172 }, 172 },
173 173
174 /** 174 /**
175 * Checks whether an exception rule is registered for a filter on a particular 175 * Checks whether an exception rule is registered for a filter on a particular
176 * domain. 176 * domain.
177 */ 177 */
178 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE xception*/ 178 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE xception*/
179 { 179 {
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 }.bind(this)); 280 }.bind(this));
281 281
282 this._applying = true; 282 this._applying = true;
283 }, 283 },
284 284
285 _generateCSSContent: function*() 285 _generateCSSContent: function*()
286 { 286 {
287 // Grouping selectors by domains 287 // Grouping selectors by domains
288 let domains = Object.create(null); 288 let domains = Object.create(null);
289 let hasFilters = false; 289 let hasFilters = false;
290 for (let key in filterByKey) 290 for (let [key, filter] of filterByKey.entries())
291 { 291 {
292 let filter = filterByKey[key];
293 let domain = filter.selectorDomain || ""; 292 let domain = filter.selectorDomain || "";
294 293
295 let list; 294 let list;
296 if (domain in domains) 295 if (domain in domains)
297 list = domains[domain]; 296 list = domains[domain];
298 else 297 else
299 { 298 {
300 list = Object.create(null); 299 list = Object.create(null);
301 domains[domain] = list; 300 domains[domain] = list;
302 } 301 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 get styleURL() 360 get styleURL()
362 { 361 {
363 return ElemHide.applied ? styleURL.spec : null; 362 return ElemHide.applied ? styleURL.spec : null;
364 }, 363 },
365 364
366 /** 365 /**
367 * Retrieves an element hiding filter by the corresponding protocol key 366 * Retrieves an element hiding filter by the corresponding protocol key
368 */ 367 */
369 getFilterByKey: function(/**String*/ key) /**Filter*/ 368 getFilterByKey: function(/**String*/ key) /**Filter*/
370 { 369 {
371 return (key in filterByKey ? filterByKey[key] : null); 370 return (filterByKey.has(key) ? filterByKey.get(key) : null);
372 }, 371 },
373 372
374 /** 373 /**
375 * Returns a list of all selectors active on a particular domain (currently 374 * Returns a list of all selectors active on a particular domain (currently
376 * used only in Chrome, Opera and Safari). 375 * used only in Chrome, Opera and Safari).
377 */ 376 */
378 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) 377 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly)
379 { 378 {
380 let result = []; 379 let result = [];
381 let keys = Object.getOwnPropertyNames(filterByKey); 380 for (let filter of filterByKey.values())
382 for (let key of keys)
383 { 381 {
384 let filter = filterByKey[key];
385 if (specificOnly && (!filter.domains || filter.domains[""])) 382 if (specificOnly && (!filter.domains || filter.domains[""]))
386 continue; 383 continue;
387 384
388 if (filter.isActiveOnDomain(domain) && !this.getException(filter, domain)) 385 if (filter.isActiveOnDomain(domain) && !this.getException(filter, domain))
389 result.push(filter.selector); 386 result.push(filter.selector);
390 } 387 }
391 return result; 388 return result;
392 } 389 }
393 }; 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