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

Unified Diff: lib/elemHide.js

Issue 29328165: Issue 3095 - Use a JavaScript Map in ElemHide
Patch Set: Created Sept. 17, 2015, 7:06 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/elemHide.js
===================================================================
--- a/lib/elemHide.js
+++ b/lib/elemHide.js
@@ -25,25 +25,25 @@ let {Utils} = require("utils");
let {IO} = require("io");
let {Prefs} = require("prefs");
let {ElemHideException} = require("filterClasses");
let {FilterNotifier} = require("filterNotifier");
let {AboutHandler} = require("elemHideHitRegistration");
/**
* Lookup table, filters by their associated key
- * @type Object
+ * @type Map
*/
-let filterByKey = Object.create(null);
+let filterByKey = new Map();
/**
- * Lookup table, keys of the filters by filter text
- * @type Object
+ * Lookup table, keys of the filters by filter
+ * @type WeakMap
*/
-let keyByFilter = Object.create(null);
+let keyByFilter = new Map();
/**
* Lookup table, keys are known element hiding exceptions
* @type Object
*/
let knownExceptions = Object.create(null);
/**
@@ -96,18 +96,18 @@ let ElemHide = exports.ElemHide =
styleURL = Services.io.newFileURI(styleFile).QueryInterface(Ci.nsIFileURL);
},
/**
* Removes all known filters
*/
clear: function()
{
- filterByKey = Object.create(null);
- keyByFilter = Object.create(null);
+ filterByKey = new Map();
+ keyByFilter = new Map();
knownExceptions = Object.create(null);
exceptions = Object.create(null);
ElemHide.isDirty = false;
ElemHide.unapply();
},
/**
* Add a new element hiding filter
@@ -123,26 +123,26 @@ let ElemHide = exports.ElemHide =
let selector = filter.selector;
if (!(selector in exceptions))
exceptions[selector] = [];
exceptions[selector].push(filter);
knownExceptions[filter.text] = true;
}
else
{
- if (filter.text in keyByFilter)
+ if (keyByFilter.has(filter.text))
return;
let key;
do {
key = Math.random().toFixed(15).substr(5);
- } while (key in filterByKey);
+ } while (filterByKey.has(key));
- filterByKey[key] = filter;
- keyByFilter[filter.text] = key;
+ filterByKey.set(key, filter);
+ keyByFilter.set(filter.text, key);
ElemHide.isDirty = true;
}
},
/**
* Removes an element hiding filter
* @param {ElemHideFilter} filter
*/
@@ -156,22 +156,22 @@ let ElemHide = exports.ElemHide =
let list = exceptions[filter.selector];
let index = list.indexOf(filter);
if (index >= 0)
list.splice(index, 1);
delete knownExceptions[filter.text];
}
else
{
- if (!(filter.text in keyByFilter))
+ if (!keyByFilter.has(filter.text))
return;
- let key = keyByFilter[filter.text];
- delete filterByKey[key];
- delete keyByFilter[filter.text];
+ let key = keyByFilter.get(filter.text);
+ filterByKey.delete(key);
+ keyByFilter.delete(filter.text);
ElemHide.isDirty = true;
}
},
/**
* Checks whether an exception rule is registered for a filter on a particular
* domain.
*/
@@ -282,19 +282,18 @@ let ElemHide = exports.ElemHide =
this._applying = true;
},
_generateCSSContent: function*()
{
// Grouping selectors by domains
let domains = Object.create(null);
let hasFilters = false;
- for (let key in filterByKey)
+ for (let [key, filter] of filterByKey.entries())
{
- let filter = filterByKey[key];
let domain = filter.selectorDomain || "";
let list;
if (domain in domains)
list = domains[domain];
else
{
list = Object.create(null);
@@ -363,30 +362,28 @@ let ElemHide = exports.ElemHide =
return ElemHide.applied ? styleURL.spec : null;
},
/**
* Retrieves an element hiding filter by the corresponding protocol key
*/
getFilterByKey: function(/**String*/ key) /**Filter*/
{
- return (key in filterByKey ? filterByKey[key] : null);
+ return (filterByKey.has(key) ? filterByKey.get(key) : null);
},
/**
* Returns a list of all selectors active on a particular domain (currently
* used only in Chrome, Opera and Safari).
*/
getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly)
{
let result = [];
- let keys = Object.getOwnPropertyNames(filterByKey);
- for (let key of keys)
+ for (let filter of filterByKey.values())
{
- let filter = filterByKey[key];
if (specificOnly && (!filter.domains || filter.domains[""]))
continue;
if (filter.isActiveOnDomain(domain) && !this.getException(filter, domain))
result.push(filter.selector);
}
return result;
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld