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

Delta Between Two Patch Sets: lib/cssRules.js

Issue 29324599: Issue 2392/2393 - Created container for CSS property filters (Closed)
Left Patch Set: Created Aug. 25, 2015, 3:35 p.m.
Right Patch Set: Rebased and addressed comments Created Nov. 25, 2015, 11:12 a.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 | lib/filterClasses.js » ('j') | 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-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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 /** 18 /**
19 * @fileOverview CSS property filtering implementation. 19 * @fileOverview CSS property filtering implementation.
20 */ 20 */
21 21
22 let {ElemHide: {getException}} = require("elemHide"); 22 let {ElemHide} = 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
23 let {Filter} = require("filterClasses");
23 24
24 /** 25 let filters = Object.create(null);
25 * Lookup table, filters by their associated key
26 * @type Object
27 */
28 let filterByKey = Object.create(null);
29
30 /**
31 * Lookup table, keys of the filters by filter text
32 * @type Object
33 */
34 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
35 26
36 /** 27 /**
37 * CSS rules component 28 * CSS rules component
38 * @class 29 * @class
39 */ 30 */
40 let CSSRules = exports.CSSRules = 31 let CSSRules = exports.CSSRules =
41 { 32 {
42 /** 33 /**
43 * Removes all known filters 34 * Removes all known filters
44 */ 35 */
45 clear: function() 36 clear: function()
46 { 37 {
47 filterByKey = Object.create(null); 38 filters = Object.create(null);
48 keyByFilter = Object.create(null);
49 }, 39 },
50 40
51 /** 41 /**
52 * Add a new CSS property filter 42 * Add a new CSS property filter
53 * @param {CSSPropertyFilter} filter 43 * @param {CSSPropertyFilter} filter
54 */ 44 */
55 add: function(filter) 45 add: function(filter)
56 { 46 {
57 if (filter.text in keyByFilter) 47 filters[filter.text] = true;
58 return;
59
60 let key;
61 do {
62 key = Math.random().toFixed(15).substr(5);
63 } while (key in filterByKey);
64
65 filterByKey[key] = filter;
66 keyByFilter[filter.text] = key;
67 }, 48 },
68 49
69 /** 50 /**
70 * Removes a CSS property filter 51 * Removes a CSS property filter
71 * @param {CSSPropertyFilter} filter 52 * @param {CSSPropertyFilter} filter
72 */ 53 */
73 remove: function(filter) 54 remove: function(filter)
74 { 55 {
75 if (!(filter.text in keyByFilter)) 56 delete filters[filter.text];
76 return;
77
78 let key = keyByFilter[filter.text];
79 delete filterByKey[key];
80 delete keyByFilter[filter.text];
81 }, 57 },
82 58
83 /** 59 /**
84 * Returns a list of all rules active on a particular domain 60 * Returns a list of all rules active on a particular domain
85 * @param {String} domain 61 * @param {String} domain
86 * @param {Boolean} specificOnly 62 * @return {Array.<CSSPropertyFilter>}
kzar 2015/11/04 13:03:20 Aren't CSS property filters _always_ specific? We
Thomas Greiner 2015/11/25 11:20:00 Done.
Wladimir Palant 2015/11/26 12:52:46 Nit: We usually use CSSPropertyFilter[] which is s
Thomas Greiner 2015/11/30 13:51:16 Done. Will include that change in the commit.
87 */ 63 */
88 getRulesForDomain: function(domain, specificOnly) 64 getRulesForDomain: function(domain)
89 { 65 {
90 let result = []; 66 let result = [];
91 let keys = Object.getOwnPropertyNames(filterByKey); 67 let keys = Object.getOwnPropertyNames(filters);
92 for (let key of keys) 68 for (let key of keys)
93 { 69 {
94 let filter = filterByKey[key]; 70 let filter = Filter.fromText(key);
95 if (specificOnly && (!filter.domains || filter.domains[""])) 71 if (filter.isActiveOnDomain(domain) && !ElemHide.getException(filter, doma in))
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
96 continue; 72 result.push(filter);
97
98 if (filter.isActiveOnDomain(domain) && !getException(filter, domain))
99 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
100 } 73 }
Wladimir Palant 2015/11/05 12:46:18 Note: This loop is rather inefficient but that's o
101 return result; 74 return result;
102 } 75 }
103 }; 76 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld