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

Delta Between Two Patch Sets: lib/elemHide.js

Issue 29784555: Issue 6665 - Abstract element hiding container logic into its own module Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Rebase Created May 17, 2018, 5:31 a.m.
Right Patch Set: Move base class into lib/contentFilterModule.js Created Aug. 15, 2018, 8:56 p.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
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-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
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 "use strict"; 18 "use strict";
19 19
20 /** 20 /**
21 * @fileOverview Element hiding implementation. 21 * @fileOverview Element hiding implementation.
22 */ 22 */
23 23
24 const {ContentFilterModule} = require("./contentFilterModule");
24 const {ElemHideExceptions} = require("./elemHideExceptions"); 25 const {ElemHideExceptions} = require("./elemHideExceptions");
25 const {FilterNotifier} = require("./filterNotifier"); 26 const {FilterNotifier} = require("./filterNotifier");
26
27 /**
28 * Map to be used instead when a filter has a blank domains property.
29 * @type {Map.<string,boolean>}
30 * @const
31 */
32 let defaultDomains = new Map([["", true]]);
33
34 /**
35 * Template for element hiding and element hiding emulation filter containers
36 * @class
37 */
38 function ElemHideTemplate()
39 {
40 this._filtersByDomain = new Map();
41 this._filterBySelector = new Map();
42 this._unconditionalSelectors = null;
43 this._knownFilters = new Set();
44
45 ElemHideExceptions.on("added", this._onExceptionAdded.bind(this));
46 }
47
48 ElemHideTemplate.prototype = {
49 /**
50 * Lookup table, active flag, by filter by domain.
51 * (Only contains filters that aren't unconditionally matched for all
52 * domains.)
53 * @type {Map.<string,Map.<Filter,boolean>>}
54 */
55 _filtersByDomain: null,
56
57 /**
58 * Lookup table, filter by selector. (Only used for selectors that are
59 * unconditionally matched for all domains.)
60 * @type {Map.<string,Filter>}
61 */
62 _filterBySelector: null,
63
64 /**
65 * This array caches the keys of _filterBySelector table (selectors
66 * which unconditionally apply on all domains). It will be null if the
67 * cache needs to be rebuilt.
68 * @type {?string[]}
69 */
70 _unconditionalSelectors: null,
71
72 /**
73 * Set containing known element hiding filters
74 * @type {Set.<ElemHideFilter>}
75 */
76 _knownFilters: null,
77
78 /**
79 * Adds a filter to the lookup table of filters by domain.
80 * @param {Filter} filter
81 */
82 _addToFiltersByDomain(filter)
83 {
84 let domains = filter.domains || defaultDomains;
85 for (let [domain, isIncluded] of domains)
86 {
87 // There's no need to note that a filter is generically disabled.
88 if (!isIncluded && domain == "")
89 continue;
90
91 let filters = this._filtersByDomain.get(domain);
92 if (!filters)
93 this._filtersByDomain.set(domain, filters = new Map());
94 filters.set(filter, isIncluded);
95 }
96 },
97
98 /**
99 * Returns a list of selectors that apply on each website unconditionally.
100 * @returns {string[]}
101 */
102 _getUnconditionalSelectors()
103 {
104 if (!this._unconditionalSelectors)
105 this._unconditionalSelectors = [...this._filterBySelector.keys()];
106
107 return this._unconditionalSelectors;
108 },
109
110 /**
111 * Handles the event when a new element hiding exception has been added
112 * @param {ElemHideException} exception
113 */
114 _onExceptionAdded(exception)
115 {
116 let {selector} = exception;
117
118 // If this is the first exception for a previously unconditionally applied
119 // element hiding selector we need to take care to update the lookups.
120 let unconditionalFilterForSelector = this._filterBySelector.get(selector);
121 if (unconditionalFilterForSelector)
122 {
123 this._addToFiltersByDomain(unconditionalFilterForSelector);
124 this._filterBySelector.delete(selector);
125 this._unconditionalSelectors = null;
126 }
127 },
128
129 /**
130 * Removes all known filters
131 */
132 clear()
133 {
134 for (let collection of [this._filtersByDomain, this._filterBySelector,
135 this._knownFilters])
136 {
137 collection.clear();
138 }
139 this._unconditionalSelectors = null;
140 FilterNotifier.emit("elemhideupdate");
141 },
142
143 /**
144 * Add a new element hiding filter
145 * @param {ElemHideFilter} filter
146 */
147 add(filter)
148 {
149 if (this._knownFilters.has(filter))
150 return;
151
152 if (!(filter.domains || ElemHideExceptions.hasExceptions(filter.selector)))
153 {
154 // The new filter's selector is unconditionally applied to all domains
155 this._filterBySelector.set(filter.selector, filter);
156 this._unconditionalSelectors = null;
157 }
158 else
159 {
160 // The new filter's selector only applies to some domains
161 this._addToFiltersByDomain(filter);
162 }
163
164 this._knownFilters.add(filter);
165 FilterNotifier.emit("elemhideupdate");
166 },
167
168 /**
169 * Removes an element hiding filter
170 * @param {ElemHideFilter} filter
171 */
172 remove(filter)
173 {
174 if (!this._knownFilters.has(filter))
175 return;
176
177 // Unconditially applied element hiding filters
178 if (this._filterBySelector.get(filter.selector) == filter)
179 {
180 this._filterBySelector.delete(filter.selector);
181 this._unconditionalSelectors = null;
182 }
183 // Conditionally applied element hiding filters
184 else
185 {
186 let domains = filter.domains || defaultDomains;
187 for (let domain of domains.keys())
188 {
189 let filters = this._filtersByDomain.get(domain);
190 if (filters)
191 filters.delete(filter);
192 }
193 }
194
195 this._knownFilters.delete(filter);
196 FilterNotifier.emit("elemhideupdate");
197 }
198 };
199
200 exports.ElemHideTemplate = ElemHideTemplate;
201 27
202 /** 28 /**
203 * Container for element hiding filters 29 * Container for element hiding filters
204 * @class 30 * @class
205 */ 31 */
206 exports.ElemHide = Object.assign(new ElemHideTemplate(), { 32 exports.ElemHide = Object.assign(new ContentFilterModule(), {
33 clear()
34 {
35 ContentFilterModule.prototype.clear.call(this);
36
37 FilterNotifier.emit("elemhideupdate");
38 },
39
40 add(filter)
41 {
42 let {size} = this._knownFilters;
43
44 ContentFilterModule.prototype.add.call(this, filter);
45
46 if (size != this._knownFilters.size)
47 FilterNotifier.emit("elemhideupdate");
48 },
49
50 remove(filter)
51 {
52 let {size} = this._knownFilters;
53
54 ContentFilterModule.prototype.remove.call(this, filter);
55
56 if (size != this._knownFilters.size)
57 FilterNotifier.emit("elemhideupdate");
58 },
59
207 /** 60 /**
208 * Determines from the current filter list which selectors should be applied 61 * Determines from the current filter list which selectors should be applied
209 * on a particular host name. 62 * on a particular host name.
210 * @param {string} domain 63 * @param {string} domain
211 * @param {boolean} [specificOnly] true if generic filters should not apply. 64 * @param {boolean} [specificOnly] true if generic filters should not apply.
212 * @returns {string[]} List of selectors. 65 * @returns {string[]} List of selectors.
213 */ 66 */
214 getSelectorsForDomain(domain, specificOnly = false) 67 getSelectorsForDomain(domain, specificOnly = false)
Manish Jethani 2018/05/17 05:51:54 Code that is specific to ElemHide is still in the
215 { 68 {
216 let selectors = []; 69 let selectors = [];
217 70
218 let excluded = new Set(); 71 let excluded = new Set();
219 let currentDomain = domain ? domain.toUpperCase() : ""; 72 let currentDomain = domain ? domain.replace(/\.+$/, "").toLowerCase() : "";
220 73
221 // This code is a performance hot-spot, which is why we've made certain 74 // This code is a performance hot-spot, which is why we've made certain
222 // micro-optimisations. Please be careful before making changes. 75 // micro-optimisations. Please be careful before making changes.
223 while (true) 76 while (true)
224 { 77 {
225 if (specificOnly && currentDomain == "") 78 if (specificOnly && currentDomain == "")
226 break; 79 break;
227 80
228 let filters = this._filtersByDomain.get(currentDomain); 81 let filters = this._filtersByDomain.get(currentDomain);
229 if (filters) 82 if (filters)
230 { 83 {
231 for (let [filter, isIncluded] of filters) 84 for (let [filter, isIncluded] of filters)
232 { 85 {
233 if (!isIncluded) 86 if (!isIncluded)
234 { 87 {
235 excluded.add(filter); 88 excluded.add(filter);
236 } 89 }
237 else if ((excluded.size == 0 || !excluded.has(filter)) && 90 else
238 !ElemHideExceptions.getException(filter, domain))
239 { 91 {
240 selectors.push(filter.selector); 92 let {selector} = filter;
93 if ((excluded.size == 0 || !excluded.has(filter)) &&
94 !ElemHideExceptions.getException(selector, domain))
95 {
96 selectors.push(selector);
97 }
241 } 98 }
242 } 99 }
243 } 100 }
244 101
245 if (currentDomain == "") 102 if (currentDomain == "")
246 break; 103 break;
247 104
248 let nextDot = currentDomain.indexOf("."); 105 let nextDot = currentDomain.indexOf(".");
249 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 106 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
250 } 107 }
251 108
252 if (!specificOnly) 109 if (!specificOnly)
253 selectors = this._getUnconditionalSelectors().concat(selectors); 110 selectors = this._getUnconditionalSelectors().concat(selectors);
254 111
255 return selectors; 112 return selectors;
256 } 113 }
257 }); 114 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld