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

Side by Side Diff: lib/elemHide.js

Issue 29783618: Issue 6665 - Split out element hiding exceptions into their own module (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Emit own elemhideupdate event Created May 17, 2018, 5:19 a.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 | lib/elemHideEmulation.js » ('j') | lib/elemHideExceptions.js » ('J')
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-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 {ElemHideException} = require("./filterClasses"); 24 const {ElemHideExceptions} = require("./elemHideExceptions");
25 const {FilterNotifier} = require("./filterNotifier"); 25 const {FilterNotifier} = require("./filterNotifier");
26 26
27 /** 27 /**
28 * Lookup table, active flag, by filter by domain. 28 * Lookup table, active flag, by filter by domain.
29 * (Only contains filters that aren't unconditionally matched for all domains.) 29 * (Only contains filters that aren't unconditionally matched for all domains.)
30 * @type {Map.<string,Map.<Filter,boolean>>} 30 * @type {Map.<string,Map.<Filter,boolean>>}
31 */ 31 */
32 let filtersByDomain = new Map(); 32 let filtersByDomain = new Map();
33 33
34 /** 34 /**
(...skipping 12 matching lines...) Expand all
47 let unconditionalSelectors = null; 47 let unconditionalSelectors = null;
48 48
49 /** 49 /**
50 * Map to be used instead when a filter has a blank domains property. 50 * Map to be used instead when a filter has a blank domains property.
51 * @type {Map.<string,boolean>} 51 * @type {Map.<string,boolean>}
52 * @const 52 * @const
53 */ 53 */
54 let defaultDomains = new Map([["", true]]); 54 let defaultDomains = new Map([["", true]]);
55 55
56 /** 56 /**
57 * Set containing known element hiding and exception filters 57 * Set containing known element hiding filters
58 * @type {Set.<ElemHideBase>} 58 * @type {Set.<ElemHideFilter>}
Manish Jethani 2018/05/17 05:42:48 We can use the specific type now in the documentat
59 */ 59 */
60 let knownFilters = new Set(); 60 let knownFilters = new Set();
61 61
62 /** 62 /**
63 * Lookup table, lists of element hiding exceptions by selector
64 * @type {Map.<string,Filter[]>}
65 */
66 let exceptions = new Map();
67
68 /**
69 * Adds a filter to the lookup table of filters by domain. 63 * Adds a filter to the lookup table of filters by domain.
70 * @param {Filter} filter 64 * @param {Filter} filter
71 */ 65 */
72 function addToFiltersByDomain(filter) 66 function addToFiltersByDomain(filter)
73 { 67 {
74 let domains = filter.domains || defaultDomains; 68 let domains = filter.domains || defaultDomains;
75 for (let [domain, isIncluded] of domains) 69 for (let [domain, isIncluded] of domains)
76 { 70 {
77 // There's no need to note that a filter is generically disabled. 71 // There's no need to note that a filter is generically disabled.
78 if (!isIncluded && domain == "") 72 if (!isIncluded && domain == "")
(...skipping 11 matching lines...) Expand all
90 * @returns {string[]} 84 * @returns {string[]}
91 */ 85 */
92 function getUnconditionalSelectors() 86 function getUnconditionalSelectors()
93 { 87 {
94 if (!unconditionalSelectors) 88 if (!unconditionalSelectors)
95 unconditionalSelectors = [...filterBySelector.keys()]; 89 unconditionalSelectors = [...filterBySelector.keys()];
96 90
97 return unconditionalSelectors; 91 return unconditionalSelectors;
98 } 92 }
99 93
94 ElemHideExceptions.on("added", ({selector}) =>
95 {
96 // If this is the first exception for a previously unconditionally applied
97 // element hiding selector we need to take care to update the lookups.
98 let unconditionalFilterForSelector = filterBySelector.get(selector);
99 if (unconditionalFilterForSelector)
100 {
101 addToFiltersByDomain(unconditionalFilterForSelector);
102 filterBySelector.delete(selector);
103 unconditionalSelectors = null;
104 }
105 });
106
100 /** 107 /**
101 * Container for element hiding filters 108 * Container for element hiding filters
102 * @class 109 * @class
103 */ 110 */
104 exports.ElemHide = { 111 exports.ElemHide = {
105 /** 112 /**
106 * Removes all known filters 113 * Removes all known filters
107 */ 114 */
108 clear() 115 clear()
109 { 116 {
110 for (let collection of [filtersByDomain, filterBySelector, 117 for (let collection of [filtersByDomain, filterBySelector,
111 knownFilters, exceptions]) 118 knownFilters])
112 { 119 {
113 collection.clear(); 120 collection.clear();
114 } 121 }
115 unconditionalSelectors = null; 122 unconditionalSelectors = null;
116 FilterNotifier.emit("elemhideupdate"); 123 FilterNotifier.emit("elemhideupdate");
117 }, 124 },
118 125
119 /** 126 /**
120 * Add a new element hiding filter 127 * Add a new element hiding filter
121 * @param {ElemHideBase} filter 128 * @param {ElemHideFilter} filter
122 */ 129 */
123 add(filter) 130 add(filter)
124 { 131 {
125 if (knownFilters.has(filter)) 132 if (knownFilters.has(filter))
126 return; 133 return;
127 134
128 if (filter instanceof ElemHideException) 135 if (!(filter.domains || ElemHideExceptions.hasExceptions(filter.selector)))
129 {
130 let {selector} = filter;
131 let list = exceptions.get(selector);
132 if (list)
133 list.push(filter);
134 else
135 exceptions.set(selector, [filter]);
136
137 // If this is the first exception for a previously unconditionally
138 // applied element hiding selector we need to take care to update the
139 // lookups.
140 let unconditionalFilterForSelector = filterBySelector.get(selector);
141 if (unconditionalFilterForSelector)
142 {
143 addToFiltersByDomain(unconditionalFilterForSelector);
144 filterBySelector.delete(selector);
145 unconditionalSelectors = null;
146 }
147 }
148 else if (!(filter.domains || exceptions.has(filter.selector)))
149 { 136 {
150 // The new filter's selector is unconditionally applied to all domains 137 // The new filter's selector is unconditionally applied to all domains
151 filterBySelector.set(filter.selector, filter); 138 filterBySelector.set(filter.selector, filter);
152 unconditionalSelectors = null; 139 unconditionalSelectors = null;
153 } 140 }
154 else 141 else
155 { 142 {
156 // The new filter's selector only applies to some domains 143 // The new filter's selector only applies to some domains
157 addToFiltersByDomain(filter); 144 addToFiltersByDomain(filter);
158 } 145 }
159 146
160 knownFilters.add(filter); 147 knownFilters.add(filter);
161 FilterNotifier.emit("elemhideupdate"); 148 FilterNotifier.emit("elemhideupdate");
162 }, 149 },
163 150
164 /** 151 /**
165 * Removes an element hiding filter 152 * Removes an element hiding filter
166 * @param {ElemHideBase} filter 153 * @param {ElemHideFilter} filter
167 */ 154 */
168 remove(filter) 155 remove(filter)
169 { 156 {
170 if (!knownFilters.has(filter)) 157 if (!knownFilters.has(filter))
171 return; 158 return;
172 159
173 // Whitelisting filters
174 if (filter instanceof ElemHideException)
175 {
176 let list = exceptions.get(filter.selector);
177 let index = list.indexOf(filter);
178 if (index >= 0)
179 list.splice(index, 1);
180 }
181 // Unconditially applied element hiding filters 160 // Unconditially applied element hiding filters
182 else if (filterBySelector.get(filter.selector) == filter) 161 if (filterBySelector.get(filter.selector) == filter)
183 { 162 {
184 filterBySelector.delete(filter.selector); 163 filterBySelector.delete(filter.selector);
185 unconditionalSelectors = null; 164 unconditionalSelectors = null;
186 } 165 }
187 // Conditionally applied element hiding filters 166 // Conditionally applied element hiding filters
188 else 167 else
189 { 168 {
190 let domains = filter.domains || defaultDomains; 169 let domains = filter.domains || defaultDomains;
191 for (let domain of domains.keys()) 170 for (let domain of domains.keys())
192 { 171 {
193 let filters = filtersByDomain.get(domain); 172 let filters = filtersByDomain.get(domain);
194 if (filters) 173 if (filters)
195 filters.delete(filter); 174 filters.delete(filter);
196 } 175 }
197 } 176 }
198 177
199 knownFilters.delete(filter); 178 knownFilters.delete(filter);
200 FilterNotifier.emit("elemhideupdate"); 179 FilterNotifier.emit("elemhideupdate");
201 }, 180 },
202 181
203 /** 182 /**
204 * Checks whether an exception rule is registered for a filter on a particular
205 * domain.
206 * @param {Filter} filter
207 * @param {?string} docDomain
208 * @return {?ElemHideException}
209 */
210 getException(filter, docDomain)
211 {
212 let list = exceptions.get(filter.selector);
213 if (!list)
214 return null;
215
216 for (let i = list.length - 1; i >= 0; i--)
217 {
218 if (list[i].isActiveOnDomain(docDomain))
219 return list[i];
220 }
221
222 return null;
223 },
224
225 /**
226 * Determines from the current filter list which selectors should be applied 183 * Determines from the current filter list which selectors should be applied
227 * on a particular host name. 184 * on a particular host name.
228 * @param {string} domain 185 * @param {string} domain
229 * @param {boolean} [specificOnly] true if generic filters should not apply. 186 * @param {boolean} [specificOnly] true if generic filters should not apply.
230 * @returns {string[]} List of selectors. 187 * @returns {string[]} List of selectors.
231 */ 188 */
232 getSelectorsForDomain(domain, specificOnly = false) 189 getSelectorsForDomain(domain, specificOnly = false)
233 { 190 {
234 let selectors = []; 191 let selectors = [];
235 192
(...skipping 10 matching lines...) Expand all
246 let filters = filtersByDomain.get(currentDomain); 203 let filters = filtersByDomain.get(currentDomain);
247 if (filters) 204 if (filters)
248 { 205 {
249 for (let [filter, isIncluded] of filters) 206 for (let [filter, isIncluded] of filters)
250 { 207 {
251 if (!isIncluded) 208 if (!isIncluded)
252 { 209 {
253 excluded.add(filter); 210 excluded.add(filter);
254 } 211 }
255 else if ((excluded.size == 0 || !excluded.has(filter)) && 212 else if ((excluded.size == 0 || !excluded.has(filter)) &&
256 !this.getException(filter, domain)) 213 !ElemHideExceptions.getException(filter, domain))
257 { 214 {
258 selectors.push(filter.selector); 215 selectors.push(filter.selector);
259 } 216 }
260 } 217 }
261 } 218 }
262 219
263 if (currentDomain == "") 220 if (currentDomain == "")
264 break; 221 break;
265 222
266 let nextDot = currentDomain.indexOf("."); 223 let nextDot = currentDomain.indexOf(".");
267 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 224 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
268 } 225 }
269 226
270 if (!specificOnly) 227 if (!specificOnly)
271 selectors = getUnconditionalSelectors().concat(selectors); 228 selectors = getUnconditionalSelectors().concat(selectors);
272 229
273 return selectors; 230 return selectors;
274 } 231 }
275 }; 232 };
OLDNEW
« no previous file with comments | « no previous file | lib/elemHideEmulation.js » ('j') | lib/elemHideExceptions.js » ('J')

Powered by Google App Engine
This is Rietveld