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: Created May 17, 2018, 3:46 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') | no next file with comments »
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>}
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 FilterNotifier.emit("elemhideupdate");
107 });
108
109 ElemHideExceptions.on("removed", () =>
110 {
111 FilterNotifier.emit("elemhideupdate");
112 });
113
100 /** 114 /**
101 * Container for element hiding filters 115 * Container for element hiding filters
102 * @class 116 * @class
103 */ 117 */
104 exports.ElemHide = { 118 exports.ElemHide = {
105 /** 119 /**
106 * Removes all known filters 120 * Removes all known filters
107 */ 121 */
108 clear() 122 clear()
109 { 123 {
110 for (let collection of [filtersByDomain, filterBySelector, 124 for (let collection of [filtersByDomain, filterBySelector,
111 knownFilters, exceptions]) 125 knownFilters])
112 { 126 {
113 collection.clear(); 127 collection.clear();
114 } 128 }
115 unconditionalSelectors = null; 129 unconditionalSelectors = null;
116 FilterNotifier.emit("elemhideupdate"); 130 FilterNotifier.emit("elemhideupdate");
117 }, 131 },
118 132
119 /** 133 /**
120 * Add a new element hiding filter 134 * Add a new element hiding filter
121 * @param {ElemHideBase} filter 135 * @param {ElemHideFilter} filter
122 */ 136 */
123 add(filter) 137 add(filter)
124 { 138 {
125 if (knownFilters.has(filter)) 139 if (knownFilters.has(filter))
126 return; 140 return;
127 141
128 if (filter instanceof ElemHideException) 142 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 { 143 {
150 // The new filter's selector is unconditionally applied to all domains 144 // The new filter's selector is unconditionally applied to all domains
151 filterBySelector.set(filter.selector, filter); 145 filterBySelector.set(filter.selector, filter);
152 unconditionalSelectors = null; 146 unconditionalSelectors = null;
153 } 147 }
154 else 148 else
155 { 149 {
156 // The new filter's selector only applies to some domains 150 // The new filter's selector only applies to some domains
157 addToFiltersByDomain(filter); 151 addToFiltersByDomain(filter);
158 } 152 }
159 153
160 knownFilters.add(filter); 154 knownFilters.add(filter);
161 FilterNotifier.emit("elemhideupdate"); 155 FilterNotifier.emit("elemhideupdate");
162 }, 156 },
163 157
164 /** 158 /**
165 * Removes an element hiding filter 159 * Removes an element hiding filter
166 * @param {ElemHideBase} filter 160 * @param {ElemHideFilter} filter
167 */ 161 */
168 remove(filter) 162 remove(filter)
169 { 163 {
170 if (!knownFilters.has(filter)) 164 if (!knownFilters.has(filter))
171 return; 165 return;
172 166
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 167 // Unconditially applied element hiding filters
182 else if (filterBySelector.get(filter.selector) == filter) 168 if (filterBySelector.get(filter.selector) == filter)
183 { 169 {
184 filterBySelector.delete(filter.selector); 170 filterBySelector.delete(filter.selector);
185 unconditionalSelectors = null; 171 unconditionalSelectors = null;
186 } 172 }
187 // Conditionally applied element hiding filters 173 // Conditionally applied element hiding filters
188 else 174 else
189 { 175 {
190 let domains = filter.domains || defaultDomains; 176 let domains = filter.domains || defaultDomains;
191 for (let domain of domains.keys()) 177 for (let domain of domains.keys())
192 { 178 {
193 let filters = filtersByDomain.get(domain); 179 let filters = filtersByDomain.get(domain);
194 if (filters) 180 if (filters)
195 filters.delete(filter); 181 filters.delete(filter);
196 } 182 }
197 } 183 }
198 184
199 knownFilters.delete(filter); 185 knownFilters.delete(filter);
200 FilterNotifier.emit("elemhideupdate"); 186 FilterNotifier.emit("elemhideupdate");
201 }, 187 },
202 188
203 /** 189 /**
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 190 * Determines from the current filter list which selectors should be applied
227 * on a particular host name. 191 * on a particular host name.
228 * @param {string} domain 192 * @param {string} domain
229 * @param {boolean} [specificOnly] true if generic filters should not apply. 193 * @param {boolean} [specificOnly] true if generic filters should not apply.
230 * @returns {string[]} List of selectors. 194 * @returns {string[]} List of selectors.
231 */ 195 */
232 getSelectorsForDomain(domain, specificOnly = false) 196 getSelectorsForDomain(domain, specificOnly = false)
233 { 197 {
234 let selectors = []; 198 let selectors = [];
235 199
(...skipping 10 matching lines...) Expand all
246 let filters = filtersByDomain.get(currentDomain); 210 let filters = filtersByDomain.get(currentDomain);
247 if (filters) 211 if (filters)
248 { 212 {
249 for (let [filter, isIncluded] of filters) 213 for (let [filter, isIncluded] of filters)
250 { 214 {
251 if (!isIncluded) 215 if (!isIncluded)
252 { 216 {
253 excluded.add(filter); 217 excluded.add(filter);
254 } 218 }
255 else if ((excluded.size == 0 || !excluded.has(filter)) && 219 else if ((excluded.size == 0 || !excluded.has(filter)) &&
256 !this.getException(filter, domain)) 220 !ElemHideExceptions.getException(filter, domain))
257 { 221 {
258 selectors.push(filter.selector); 222 selectors.push(filter.selector);
259 } 223 }
260 } 224 }
261 } 225 }
262 226
263 if (currentDomain == "") 227 if (currentDomain == "")
264 break; 228 break;
265 229
266 let nextDot = currentDomain.indexOf("."); 230 let nextDot = currentDomain.indexOf(".");
267 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 231 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
268 } 232 }
269 233
270 if (!specificOnly) 234 if (!specificOnly)
271 selectors = getUnconditionalSelectors().concat(selectors); 235 selectors = getUnconditionalSelectors().concat(selectors);
272 236
273 return selectors; 237 return selectors;
274 } 238 }
275 }; 239 };
OLDNEW
« no previous file with comments | « no previous file | lib/elemHideEmulation.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld