| Index: lib/cssRules.js |
| =================================================================== |
| new file mode 100644 |
| --- /dev/null |
| +++ b/lib/cssRules.js |
| @@ -0,0 +1,103 @@ |
| +/* |
| + * This file is part of Adblock Plus <https://adblockplus.org/>, |
| + * Copyright (C) 2006-2015 Eyeo GmbH |
| + * |
| + * Adblock Plus is free software: you can redistribute it and/or modify |
| + * it under the terms of the GNU General Public License version 3 as |
| + * published by the Free Software Foundation. |
| + * |
| + * Adblock Plus is distributed in the hope that it will be useful, |
| + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| + * GNU General Public License for more details. |
| + * |
| + * You should have received a copy of the GNU General Public License |
| + * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| + */ |
| + |
| +/** |
| + * @fileOverview CSS property filtering implementation. |
| + */ |
| + |
| +let {ElemHide: {getException}} = require("elemHide"); |
|
kzar
2015/10/24 17:14:51
This results in a syntax error when building the s
Thomas Greiner
2015/11/25 11:20:00
Done. Since this is valid ES6 syntax, I'd consider
kzar
2015/11/25 13:12:26
Thanks, yea I agree that it looks like more of a p
|
| + |
| +/** |
| + * Lookup table, filters by their associated key |
| + * @type Object |
| + */ |
| +let filterByKey = Object.create(null); |
| + |
| +/** |
| + * Lookup table, keys of the filters by filter text |
| + * @type Object |
| + */ |
| +let keyByFilter = Object.create(null); |
|
Wladimir Palant
2015/11/05 12:46:18
The two maps filterByKey and keyByFilter seem to b
Thomas Greiner
2015/11/25 11:20:00
You're absolutely right.
I'd love to use `Set` fo
|
| + |
| +/** |
| + * CSS rules component |
| + * @class |
| + */ |
| +let CSSRules = exports.CSSRules = |
| +{ |
| + /** |
| + * Removes all known filters |
| + */ |
| + clear: function() |
| + { |
| + filterByKey = Object.create(null); |
| + keyByFilter = Object.create(null); |
| + }, |
| + |
| + /** |
| + * Add a new CSS property filter |
| + * @param {CSSPropertyFilter} filter |
| + */ |
| + add: function(filter) |
| + { |
| + if (filter.text in keyByFilter) |
| + return; |
| + |
| + let key; |
| + do { |
| + key = Math.random().toFixed(15).substr(5); |
| + } while (key in filterByKey); |
| + |
| + filterByKey[key] = filter; |
| + keyByFilter[filter.text] = key; |
| + }, |
| + |
| + /** |
| + * Removes a CSS property filter |
| + * @param {CSSPropertyFilter} filter |
| + */ |
| + remove: function(filter) |
| + { |
| + if (!(filter.text in keyByFilter)) |
| + return; |
| + |
| + let key = keyByFilter[filter.text]; |
| + delete filterByKey[key]; |
| + delete keyByFilter[filter.text]; |
| + }, |
| + |
| + /** |
| + * Returns a list of all rules active on a particular domain |
| + * @param {String} domain |
| + * @param {Boolean} specificOnly |
|
kzar
2015/11/04 13:03:20
Aren't CSS property filters _always_ specific? We
Thomas Greiner
2015/11/25 11:20:00
Done.
|
| + */ |
| + getRulesForDomain: function(domain, specificOnly) |
| + { |
| + let result = []; |
| + let keys = Object.getOwnPropertyNames(filterByKey); |
| + for (let key of keys) |
| + { |
| + let filter = filterByKey[key]; |
| + if (specificOnly && (!filter.domains || filter.domains[""])) |
|
kzar
2015/11/02 19:33:45
Use filter.isGeneric() instead?
Wladimir Palant
2015/11/05 12:46:19
Actually, filters that are generic shouldn't even
kzar
2015/11/05 14:27:47
Yea, I noticed that later, see my above comment :p
|
| + continue; |
| + |
| + if (filter.isActiveOnDomain(domain) && !getException(filter, domain)) |
| + result.push(filter.selector); |
|
kzar
2015/11/02 19:33:45
Mind just pushing `filter` instead of `filter.sele
Thomas Greiner
2015/11/25 11:20:00
Done. I must've overseen that those are required a
|
| + } |
|
Wladimir Palant
2015/11/05 12:46:18
Note: This loop is rather inefficient but that's o
|
| + return result; |
| + } |
| +}; |