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

Delta Between Two Patch Sets: lib/elemHide.js

Issue 29774573: Issue 6652 - Do not push to unconditional selectors array (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Rebase Created May 11, 2018, 7:21 a.m.
Right Patch Set: Rebase on patch #29778572 Created May 11, 2018, 12:23 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 | no next file » | 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-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
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 63 * Lookup table, lists of element hiding exceptions by selector
64 * @type {Map.<string,Filter>} 64 * @type {Map.<string,Filter>}
65 */ 65 */
66 let exceptions = new Map(); 66 let exceptions = new Map();
67 67
68 /** 68 /**
69 * Adds a filter to the lookup table of filters by domain.
70 * @param {Filter}
71 */
72 function addToFiltersByDomain(filter)
73 {
74 let domains = filter.domains || defaultDomains;
75 for (let [domain, isIncluded] of domains)
76 {
77 // There's no need to note that a filter is generically disabled.
78 if (!isIncluded && domain == "")
79 continue;
80
81 let filters = filtersByDomain.get(domain);
82 if (!filters)
83 filtersByDomain.set(domain, filters = new Map());
84 filters.set(filter, isIncluded);
85 }
86 }
87
88 /**
69 * Returns a list of selectors that apply on each website unconditionally. 89 * Returns a list of selectors that apply on each website unconditionally.
70 * @returns {string[]} 90 * @returns {string[]}
71 */ 91 */
72 function getUnconditionalSelectors() 92 function getUnconditionalSelectors()
73 { 93 {
74 if (!unconditionalSelectors) 94 if (!unconditionalSelectors)
75 unconditionalSelectors = [...filterBySelector.keys()]; 95 unconditionalSelectors = [...filterBySelector.keys()];
76 96
77 return unconditionalSelectors; 97 return unconditionalSelectors;
78 } 98 }
79 99
80 /** 100 /**
81 * Container for element hiding filters 101 * Container for element hiding filters
82 * @class 102 * @class
83 */ 103 */
84 let ElemHide = exports.ElemHide = { 104 let ElemHide = exports.ElemHide = {
85 /** 105 /**
86 * Removes all known filters 106 * Removes all known filters
87 */ 107 */
88 clear() 108 clear()
89 { 109 {
90 for (let collection of [filtersByDomain, filterBySelector, 110 for (let collection of [filtersByDomain, filterBySelector,
91 knownFilters, exceptions]) 111 knownFilters, exceptions])
92 { 112 {
93 collection.clear(); 113 collection.clear();
94 } 114 }
95 unconditionalSelectors = null; 115 unconditionalSelectors = null;
96 FilterNotifier.emit("elemhideupdate"); 116 FilterNotifier.emit("elemhideupdate");
97 },
98
99 _addToFiltersByDomain(filter)
100 {
101 let domains = filter.domains || defaultDomains;
102 for (let [domain, isIncluded] of domains)
103 {
104 // There's no need to note that a filter is generically disabled.
105 if (!isIncluded && domain == "")
106 continue;
107
108 let filters = filtersByDomain.get(domain);
109 if (!filters)
110 filtersByDomain.set(domain, filters = new Map());
111 filters.set(filter, isIncluded);
112 }
113 }, 117 },
114 118
115 /** 119 /**
116 * Add a new element hiding filter 120 * Add a new element hiding filter
117 * @param {ElemHideBase} filter 121 * @param {ElemHideBase} filter
118 */ 122 */
119 add(filter) 123 add(filter)
120 { 124 {
121 if (knownFilters.has(filter)) 125 if (knownFilters.has(filter))
122 return; 126 return;
123 127
124 if (filter instanceof ElemHideException) 128 if (filter instanceof ElemHideException)
125 { 129 {
126 let {selector} = filter; 130 let {selector} = filter;
127 let list = exceptions.get(selector); 131 let list = exceptions.get(selector);
128 if (list) 132 if (list)
129 list.push(filter); 133 list.push(filter);
130 else 134 else
131 exceptions.set(selector, [filter]); 135 exceptions.set(selector, [filter]);
132 136
133 // If this is the first exception for a previously unconditionally 137 // If this is the first exception for a previously unconditionally
134 // applied element hiding selector we need to take care to update the 138 // applied element hiding selector we need to take care to update the
135 // lookups. 139 // lookups.
136 let unconditionalFilterForSelector = filterBySelector.get(selector); 140 let unconditionalFilterForSelector = filterBySelector.get(selector);
137 if (unconditionalFilterForSelector) 141 if (unconditionalFilterForSelector)
138 { 142 {
139 this._addToFiltersByDomain(unconditionalFilterForSelector); 143 addToFiltersByDomain(unconditionalFilterForSelector);
140 filterBySelector.delete(selector); 144 filterBySelector.delete(selector);
141 unconditionalSelectors = null; 145 unconditionalSelectors = null;
142 } 146 }
143 } 147 }
144 else if (!(filter.domains || exceptions.has(filter.selector))) 148 else if (!(filter.domains || exceptions.has(filter.selector)))
145 { 149 {
146 // The new filter's selector is unconditionally applied to all domains 150 // The new filter's selector is unconditionally applied to all domains
147 filterBySelector.set(filter.selector, filter); 151 filterBySelector.set(filter.selector, filter);
148 unconditionalSelectors = null; 152 unconditionalSelectors = null;
149 } 153 }
150 else 154 else
151 { 155 {
152 // The new filter's selector only applies to some domains 156 // The new filter's selector only applies to some domains
153 this._addToFiltersByDomain(filter); 157 addToFiltersByDomain(filter);
154 } 158 }
155 159
156 knownFilters.add(filter); 160 knownFilters.add(filter);
157 FilterNotifier.emit("elemhideupdate"); 161 FilterNotifier.emit("elemhideupdate");
158 }, 162 },
159 163
160 /** 164 /**
161 * Removes an element hiding filter 165 * Removes an element hiding filter
162 * @param {ElemHideBase} filter 166 * @param {ElemHideBase} filter
163 */ 167 */
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 let nextDot = currentDomain.indexOf("."); 294 let nextDot = currentDomain.indexOf(".");
291 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 295 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
292 } 296 }
293 297
294 if (criteria < ElemHide.NO_UNCONDITIONAL) 298 if (criteria < ElemHide.NO_UNCONDITIONAL)
295 selectors = getUnconditionalSelectors().concat(selectors); 299 selectors = getUnconditionalSelectors().concat(selectors);
296 300
297 return selectors; 301 return selectors;
298 } 302 }
299 }; 303 };
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld