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

Delta Between Two Patch Sets: lib/elemHide.js

Issue 29349187: Issue 4167 - getSelectorsForDomain criteria + keys (Closed)
Left Patch Set: Improved tests, fixed bugs spotted in the process Created Sept. 20, 2016, 2:17 p.m.
Right Patch Set: Improved comments Created Sept. 27, 2016, 1:52 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
« no previous file with change/comment | « no previous file | test/elemHide.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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 else 173 else
174 { 174 {
175 // The new filter's selector only applies to some domains 175 // The new filter's selector only applies to some domains
176 this._addToFiltersByDomain(key, filter); 176 this._addToFiltersByDomain(key, filter);
177 } 177 }
178 } 178 }
179 179
180 FilterNotifier.emit("elemhideupdate"); 180 FilterNotifier.emit("elemhideupdate");
181 }, 181 },
182 182
183 _removeFilterKey: function(key, filter)
184 {
185 let filterKeys = filterKeysBySelector[filter.selector];
186 if (filterKeys)
187 {
188 let index = filterKeys.indexOf(key);
189 if (index >= 0)
190 {
191 if (filterKeys.length > 1)
192 {
193 filterKeys.splice(index, 1);
194 if (index == 0)
195 unconditionalFilterKeys = null;
196 }
197 else
198 {
199 delete filterKeysBySelector[filter.selector];
200 unconditionalSelectors = unconditionalFilterKeys = null;
201 }
202 return;
203 }
204 }
205
206 // We haven't found this filter in unconditional filters, look in
207 // filtersByDomain.
208 let domains = filter.domains || defaultDomains;
209 for (let domain in domains)
210 {
211 let filters = filtersByDomain[domain];
212 if (filters)
213 delete filters[key];
214 }
215 },
216
183 /** 217 /**
184 * Removes an element hiding filter 218 * Removes an element hiding filter
185 * @param {ElemHideFilter} filter 219 * @param {ElemHideFilter} filter
186 */ 220 */
187 remove: function(filter) 221 remove: function(filter)
188 { 222 {
189 if (filter instanceof ElemHideException) 223 if (filter instanceof ElemHideException)
190 { 224 {
191 if (!(filter.text in knownExceptions)) 225 if (!(filter.text in knownExceptions))
192 return; 226 return;
193 227
194 let list = exceptions[filter.selector]; 228 let list = exceptions[filter.selector];
195 let index = list.indexOf(filter); 229 let index = list.indexOf(filter);
196 if (index >= 0) 230 if (index >= 0)
197 list.splice(index, 1); 231 list.splice(index, 1);
198 delete knownExceptions[filter.text]; 232 delete knownExceptions[filter.text];
199 } 233 }
200 else 234 else
201 { 235 {
202 if (!(filter.text in keyByFilter)) 236 if (!(filter.text in keyByFilter))
203 return; 237 return;
204 238
205 let key = keyByFilter[filter.text]; 239 let key = keyByFilter[filter.text];
206 delete filterByKey[key]; 240 delete filterByKey[key];
207 delete keyByFilter[filter.text]; 241 delete keyByFilter[filter.text];
208 242 this._removeFilterKey(key, filter);
209 let unconditional = !(filter.domains || filter.selector in exceptions);
210 if (unconditional)
211 {
212 let filterKeys = filterKeysBySelector[filter.selector];
213 if (filterKeys)
214 {
215 if (filterKeys.length > 1)
216 {
217 let index = filterKeys.indexOf(key);
218 filterKeys.splice(index, 1);
219 if (index == 0)
220 unconditionalFilterKeys = null;
221 }
222 else
223 {
224 delete filterKeysBySelector[filter.selector];
225 unconditionalSelectors = unconditionalFilterKeys = null;
226 }
227 }
228 else
229 {
230 // Even if this filter appears unconditional its selector might not
231 // be. In that case we need to take care to remove it from
232 // filtersByDomain instead.
233 unconditional = false;
234 }
235 }
236
237 if (!unconditional)
238 {
239 let domains = filter.domains || defaultDomains;
240 for (let domain in domains)
241 {
242 let filters = filtersByDomain[domain];
243 if (filters)
244 delete filters[key];
245 }
246 }
247 } 243 }
248 244
249 FilterNotifier.emit("elemhideupdate"); 245 FilterNotifier.emit("elemhideupdate");
250 }, 246 },
251 247
252 /** 248 /**
253 * Checks whether an exception rule is registered for a filter on a particular 249 * Checks whether an exception rule is registered for a filter on a particular
254 * domain. 250 * domain.
255 */ 251 */
256 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE xception*/ 252 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE xception*/
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 if (!unconditionalFilterKeys) 319 if (!unconditionalFilterKeys)
324 { 320 {
325 let selectors = this.getUnconditionalSelectors(); 321 let selectors = this.getUnconditionalSelectors();
326 unconditionalFilterKeys = []; 322 unconditionalFilterKeys = [];
327 for (let selector of selectors) 323 for (let selector of selectors)
328 unconditionalFilterKeys.push(filterKeysBySelector[selector][0]); 324 unconditionalFilterKeys.push(filterKeysBySelector[selector][0]);
329 } 325 }
330 return unconditionalFilterKeys.slice(); 326 return unconditionalFilterKeys.slice();
331 }, 327 },
332 328
333 /** 329
334 * Returns a list of all selectors active on a particular domain. Optionally 330 /**
335 * a list of corresponding filter keys for the selectors can be returned too. 331 * Constant used by getSelectorsForDomain to return all selectors applying to
336 * The optional criteria parameter restricts the results as follows: 332 * a particular hostname.
337 * 0 - All selectors allowed (default) 333 */
338 * 1 - No unconditionally matching selectors allowed 334 ALL_MATCHING: 0,
339 * 2 - Only specifically matching selectors allowed 335
340 */ 336 /**
341 getSelectorsForDomain: function(/**String*/ domain, 337 * Constant used by getSelectorsForDomain to exclude selectors which apply to
342 /**Number*/ criteria, 338 * all websites without exception.
343 /**Boolean*/ provideFilterKeys) 339 */
340 NO_UNCONDITIONAL: 1,
341
342 /**
343 * Constant used by getSelectorsForDomain to return only selectors for filters
344 * which specifically match the given host name.
345 */
346 SPECIFIC_ONLY: 2,
347
348 /**
349 * Determines from the current filter list which selectors should be applied
350 * on a particular host name. Optionally returns the corresponding filter
351 * keys.
352 * @param {String} domain
353 * @param {Number} [criteria]
354 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or
355 * ElemHide.SPECIFIC_ONLY.
356 * @param {Boolean} [provideFilterKeys]
357 * If true, the function will return a list of corresponding filter keys in
358 * addition to selectors.
359 * @returns {string[]|Array.<string[]>}
360 * List of selectors or an array with two elements (list of selectors and
361 * list of corresponding keys) if provideFilterKeys is true.
362 */
363 getSelectorsForDomain: function(domain, criteria, provideFilterKeys)
344 { 364 {
345 let filterKeys = []; 365 let filterKeys = [];
346 let selectors = []; 366 let selectors = [];
347 if (!criteria) 367
368 if (typeof criteria == "undefined")
369 criteria = ElemHide.ALL_MATCHING;
370 if (criteria < ElemHide.NO_UNCONDITIONAL)
348 { 371 {
349 selectors = this.getUnconditionalSelectors(); 372 selectors = this.getUnconditionalSelectors();
350 if (provideFilterKeys) 373 if (provideFilterKeys)
351 filterKeys = this.getUnconditionalFilterKeys(); 374 filterKeys = this.getUnconditionalFilterKeys();
352 } 375 }
353 376
354 let specificOnly = criteria == 2; 377 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY);
355 let seenFilters = Object.create(null); 378 let seenFilters = Object.create(null);
356 let currentDomain = domain ? domain.toUpperCase() : ""; 379 let currentDomain = domain ? domain.toUpperCase() : "";
357 while (true) 380 while (true)
358 { 381 {
359 if (specificOnly && currentDomain == "") 382 if (specificOnly && currentDomain == "")
360 break; 383 break;
361 384
362 let filters = filtersByDomain[currentDomain]; 385 let filters = filtersByDomain[currentDomain];
363 if (filters) 386 if (filters)
364 { 387 {
(...skipping 14 matching lines...) Expand all
379 } 402 }
380 403
381 if (currentDomain == "") 404 if (currentDomain == "")
382 break; 405 break;
383 406
384 let nextDot = currentDomain.indexOf("."); 407 let nextDot = currentDomain.indexOf(".");
385 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 408 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
386 } 409 }
387 410
388 if (provideFilterKeys) 411 if (provideFilterKeys)
389 return [selectors, filterKeys.map(key => parseInt(key, 10))]; 412 return [selectors, filterKeys];
kzar 2016/09/20 14:23:49 (Since filtersByDomain[domain] is an Object we end
390 else 413 else
391 return selectors; 414 return selectors;
392 } 415 }
393 }; 416 };
LEFTRIGHT
« no previous file | test/elemHide.js » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld