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

Side by Side Diff: lib/elemHide.js

Issue 29737558: Issue 6538, 6781 - Implement support for snippet filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Make snippet filters similar to element hiding filters Created April 25, 2018, 5:15 p.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/filterClasses.js » ('j') | lib/filterClasses.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
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 * Add a new element hiding filter 110 * Add a new element hiding filter
111 * @param {ElemHideFilter} filter 111 * @param {ElemHideFilter} filter
112 */ 112 */
113 add(filter) 113 add(filter)
114 { 114 {
115 if (filter instanceof ElemHideException) 115 if (filter instanceof ElemHideException)
116 { 116 {
117 if (knownExceptions.has(filter.text)) 117 if (knownExceptions.has(filter.text))
118 return; 118 return;
119 119
120 let {selector} = filter; 120 let {code} = filter;
121 let list = exceptions.get(selector); 121 let list = exceptions.get(code);
122 if (list) 122 if (list)
123 list.push(filter); 123 list.push(filter);
124 else 124 else
125 exceptions.set(selector, [filter]); 125 exceptions.set(code, [filter]);
126 126
127 // If this is the first exception for a previously unconditionally 127 // If this is the first exception for a previously unconditionally
128 // applied element hiding selector we need to take care to update the 128 // applied element hiding selector we need to take care to update the
129 // lookups. 129 // lookups.
130 let filterKey = filterKeyBySelector.get(selector); 130 let filterKey = filterKeyBySelector.get(code);
131 if (typeof filterKey != "undefined") 131 if (typeof filterKey != "undefined")
132 { 132 {
133 this._addToFiltersByDomain(filterKey, filterByKey[filterKey]); 133 this._addToFiltersByDomain(filterKey, filterByKey[filterKey]);
134 filterKeyBySelector.delete(selector); 134 filterKeyBySelector.delete(code);
135 unconditionalSelectors = null; 135 unconditionalSelectors = null;
136 } 136 }
137 137
138 knownExceptions.add(filter.text); 138 knownExceptions.add(filter.text);
139 } 139 }
140 else 140 else
141 { 141 {
142 if (keyByFilter.has(filter)) 142 if (keyByFilter.has(filter))
143 return; 143 return;
144 144
145 let key = filterByKey.push(filter) - 1; 145 let key = filterByKey.push(filter) - 1;
146 keyByFilter.set(filter, key); 146 keyByFilter.set(filter, key);
147 147
148 if (!(filter.domains || exceptions.has(filter.selector))) 148 if (!(filter.domains || exceptions.has(filter.code)))
149 { 149 {
150 // The new filter's selector is unconditionally applied to all domains 150 // The new filter's selector is unconditionally applied to all domains
151 filterKeyBySelector.set(filter.selector, key); 151 filterKeyBySelector.set(filter.code, key);
152 unconditionalSelectors = null; 152 unconditionalSelectors = null;
153 } 153 }
154 else 154 else
155 { 155 {
156 // The new filter's selector only applies to some domains 156 // The new filter's selector only applies to some domains
157 this._addToFiltersByDomain(key, filter); 157 this._addToFiltersByDomain(key, filter);
158 } 158 }
159 } 159 }
160 160
161 FilterNotifier.emit("elemhideupdate"); 161 FilterNotifier.emit("elemhideupdate");
162 }, 162 },
163 163
164 _removeFilterKey(key, filter) 164 _removeFilterKey(key, filter)
165 { 165 {
166 if (filterKeyBySelector.get(filter.selector) == key) 166 if (filterKeyBySelector.get(filter.code) == key)
167 { 167 {
168 filterKeyBySelector.delete(filter.selector); 168 filterKeyBySelector.delete(filter.code);
169 unconditionalSelectors = null; 169 unconditionalSelectors = null;
170 return; 170 return;
171 } 171 }
172 172
173 // We haven't found this filter in unconditional filters, look in 173 // We haven't found this filter in unconditional filters, look in
174 // filtersByDomain. 174 // filtersByDomain.
175 let domains = filter.domains || defaultDomains; 175 let domains = filter.domains || defaultDomains;
176 for (let domain of domains.keys()) 176 for (let domain of domains.keys())
177 { 177 {
178 let filters = filtersByDomain.get(domain); 178 let filters = filtersByDomain.get(domain);
179 if (filters) 179 if (filters)
180 filters.delete(key); 180 filters.delete(key);
181 } 181 }
182 }, 182 },
183 183
184 /** 184 /**
185 * Removes an element hiding filter 185 * Removes an element hiding filter
186 * @param {ElemHideFilter} filter 186 * @param {ElemHideFilter} filter
187 */ 187 */
188 remove(filter) 188 remove(filter)
189 { 189 {
190 if (filter instanceof ElemHideException) 190 if (filter instanceof ElemHideException)
191 { 191 {
192 if (!knownExceptions.has(filter.text)) 192 if (!knownExceptions.has(filter.text))
193 return; 193 return;
194 194
195 let list = exceptions.get(filter.selector); 195 let list = exceptions.get(filter.code);
196 let index = list.indexOf(filter); 196 let index = list.indexOf(filter);
197 if (index >= 0) 197 if (index >= 0)
198 list.splice(index, 1); 198 list.splice(index, 1);
199 knownExceptions.delete(filter.text); 199 knownExceptions.delete(filter.text);
200 } 200 }
201 else 201 else
202 { 202 {
203 let key = keyByFilter.get(filter); 203 let key = keyByFilter.get(filter);
204 if (typeof key == "undefined") 204 if (typeof key == "undefined")
205 return; 205 return;
206 206
207 delete filterByKey[key]; 207 delete filterByKey[key];
208 keyByFilter.delete(filter); 208 keyByFilter.delete(filter);
209 this._removeFilterKey(key, filter); 209 this._removeFilterKey(key, filter);
210 } 210 }
211 211
212 FilterNotifier.emit("elemhideupdate"); 212 FilterNotifier.emit("elemhideupdate");
213 }, 213 },
214 214
215 /** 215 /**
216 * Checks whether an exception rule is registered for a filter on a particular 216 * Checks whether an exception rule is registered for a filter on a particular
217 * domain. 217 * domain.
218 * @param {Filter} filter 218 * @param {Filter} filter
219 * @param {string} docDomain 219 * @param {string} docDomain
220 * @return {ElemHideException} 220 * @return {ElemHideException}
221 */ 221 */
222 getException(filter, docDomain) 222 getException(filter, docDomain)
223 { 223 {
224 let list = exceptions.get(filter.selector); 224 let list = exceptions.get(filter.code);
225 if (!list) 225 if (!list)
226 return null; 226 return null;
227 227
228 for (let i = list.length - 1; i >= 0; i--) 228 for (let i = list.length - 1; i >= 0; i--)
229 { 229 {
230 if (list[i].isActiveOnDomain(docDomain)) 230 if (list[i].isActiveOnDomain(docDomain))
231 return list[i]; 231 return list[i];
232 } 232 }
233 233
234 return null; 234 return null;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 let filters = filtersByDomain.get(currentDomain); 303 let filters = filtersByDomain.get(currentDomain);
304 if (filters) 304 if (filters)
305 { 305 {
306 for (let [filterKey, filter] of filters) 306 for (let [filterKey, filter] of filters)
307 { 307 {
308 if (seenFilters.has(filterKey)) 308 if (seenFilters.has(filterKey))
309 continue; 309 continue;
310 seenFilters.add(filterKey); 310 seenFilters.add(filterKey);
311 311
312 if (filter && !this.getException(filter, domain)) 312 if (filter && !this.getException(filter, domain))
313 selectors.push(filter.selector); 313 selectors.push(filter.code);
314 } 314 }
315 } 315 }
316 316
317 if (currentDomain == "") 317 if (currentDomain == "")
318 break; 318 break;
319 319
320 let nextDot = currentDomain.indexOf("."); 320 let nextDot = currentDomain.indexOf(".");
321 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 321 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
322 } 322 }
323 323
324 return selectors; 324 return selectors;
325 } 325 }
326 }; 326 };
OLDNEW
« no previous file with comments | « no previous file | lib/filterClasses.js » ('j') | lib/filterClasses.js » ('J')

Powered by Google App Engine
This is Rietveld