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

Delta Between Two Patch Sets: lib/elemHide.js

Issue 29783618: Issue 6665 - Split out element hiding exceptions into their own module (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created May 17, 2018, 3:46 a.m.
Right Patch Set: Add lib/elemHideExceptions.js Created Aug. 7, 2018, 2:18 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 | lib/elemHideEmulation.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-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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 { 95 {
96 // If this is the first exception for a previously unconditionally applied 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. 97 // element hiding selector we need to take care to update the lookups.
98 let unconditionalFilterForSelector = filterBySelector.get(selector); 98 let unconditionalFilterForSelector = filterBySelector.get(selector);
99 if (unconditionalFilterForSelector) 99 if (unconditionalFilterForSelector)
100 { 100 {
101 addToFiltersByDomain(unconditionalFilterForSelector); 101 addToFiltersByDomain(unconditionalFilterForSelector);
102 filterBySelector.delete(selector); 102 filterBySelector.delete(selector);
103 unconditionalSelectors = null; 103 unconditionalSelectors = null;
104 } 104 }
105
106 FilterNotifier.emit("elemhideupdate");
107 });
108
109 ElemHideExceptions.on("removed", () =>
110 {
111 FilterNotifier.emit("elemhideupdate");
112 }); 105 });
113 106
114 /** 107 /**
115 * Container for element hiding filters 108 * Container for element hiding filters
116 * @class 109 * @class
117 */ 110 */
118 exports.ElemHide = { 111 exports.ElemHide = {
119 /** 112 /**
120 * Removes all known filters 113 * Removes all known filters
121 */ 114 */
122 clear() 115 clear()
123 { 116 {
124 for (let collection of [filtersByDomain, filterBySelector, 117 for (let collection of [filtersByDomain, filterBySelector, knownFilters])
125 knownFilters])
126 {
127 collection.clear(); 118 collection.clear();
128 } 119
129 unconditionalSelectors = null; 120 unconditionalSelectors = null;
130 FilterNotifier.emit("elemhideupdate"); 121 FilterNotifier.emit("elemhideupdate");
131 }, 122 },
132 123
133 /** 124 /**
134 * Add a new element hiding filter 125 * Add a new element hiding filter
135 * @param {ElemHideFilter} filter 126 * @param {ElemHideFilter} filter
136 */ 127 */
137 add(filter) 128 add(filter)
138 { 129 {
139 if (knownFilters.has(filter)) 130 if (knownFilters.has(filter))
140 return; 131 return;
141 132
142 if (!(filter.domains || ElemHideExceptions.hasExceptions(filter.selector))) 133 let {selector} = filter;
134
135 if (!(filter.domains || ElemHideExceptions.hasExceptions(selector)))
143 { 136 {
144 // The new filter's selector is unconditionally applied to all domains 137 // The new filter's selector is unconditionally applied to all domains
145 filterBySelector.set(filter.selector, filter); 138 filterBySelector.set(selector, filter);
146 unconditionalSelectors = null; 139 unconditionalSelectors = null;
147 } 140 }
148 else 141 else
149 { 142 {
150 // The new filter's selector only applies to some domains 143 // The new filter's selector only applies to some domains
151 addToFiltersByDomain(filter); 144 addToFiltersByDomain(filter);
152 } 145 }
153 146
154 knownFilters.add(filter); 147 knownFilters.add(filter);
155 FilterNotifier.emit("elemhideupdate"); 148 FilterNotifier.emit("elemhideupdate");
156 }, 149 },
157 150
158 /** 151 /**
159 * Removes an element hiding filter 152 * Removes an element hiding filter
160 * @param {ElemHideFilter} filter 153 * @param {ElemHideFilter} filter
161 */ 154 */
162 remove(filter) 155 remove(filter)
163 { 156 {
164 if (!knownFilters.has(filter)) 157 if (!knownFilters.has(filter))
165 return; 158 return;
166 159
160 let {selector} = filter;
161
167 // Unconditially applied element hiding filters 162 // Unconditially applied element hiding filters
168 if (filterBySelector.get(filter.selector) == filter) 163 if (filterBySelector.get(selector) == filter)
169 { 164 {
170 filterBySelector.delete(filter.selector); 165 filterBySelector.delete(selector);
171 unconditionalSelectors = null; 166 unconditionalSelectors = null;
172 } 167 }
173 // Conditionally applied element hiding filters 168 // Conditionally applied element hiding filters
174 else 169 else
175 { 170 {
176 let domains = filter.domains || defaultDomains; 171 let domains = filter.domains || defaultDomains;
177 for (let domain of domains.keys()) 172 for (let domain of domains.keys())
178 { 173 {
179 let filters = filtersByDomain.get(domain); 174 let filters = filtersByDomain.get(domain);
180 if (filters) 175 if (filters)
(...skipping 10 matching lines...) Expand all
191 * on a particular host name. 186 * on a particular host name.
192 * @param {string} domain 187 * @param {string} domain
193 * @param {boolean} [specificOnly] true if generic filters should not apply. 188 * @param {boolean} [specificOnly] true if generic filters should not apply.
194 * @returns {string[]} List of selectors. 189 * @returns {string[]} List of selectors.
195 */ 190 */
196 getSelectorsForDomain(domain, specificOnly = false) 191 getSelectorsForDomain(domain, specificOnly = false)
197 { 192 {
198 let selectors = []; 193 let selectors = [];
199 194
200 let excluded = new Set(); 195 let excluded = new Set();
201 let currentDomain = domain ? domain.toUpperCase() : ""; 196 let currentDomain = domain ? domain.replace(/\.+$/, "").toLowerCase() : "";
202 197
203 // This code is a performance hot-spot, which is why we've made certain 198 // This code is a performance hot-spot, which is why we've made certain
204 // micro-optimisations. Please be careful before making changes. 199 // micro-optimisations. Please be careful before making changes.
205 while (true) 200 while (true)
206 { 201 {
207 if (specificOnly && currentDomain == "") 202 if (specificOnly && currentDomain == "")
208 break; 203 break;
209 204
210 let filters = filtersByDomain.get(currentDomain); 205 let filters = filtersByDomain.get(currentDomain);
211 if (filters) 206 if (filters)
212 { 207 {
213 for (let [filter, isIncluded] of filters) 208 for (let [filter, isIncluded] of filters)
214 { 209 {
215 if (!isIncluded) 210 if (!isIncluded)
216 { 211 {
217 excluded.add(filter); 212 excluded.add(filter);
218 } 213 }
219 else if ((excluded.size == 0 || !excluded.has(filter)) && 214 else
220 !ElemHideExceptions.getException(filter, domain))
221 { 215 {
222 selectors.push(filter.selector); 216 let {selector} = filter;
217 if ((excluded.size == 0 || !excluded.has(filter)) &&
218 !ElemHideExceptions.getException(selector, domain))
219 {
220 selectors.push(selector);
221 }
223 } 222 }
224 } 223 }
225 } 224 }
226 225
227 if (currentDomain == "") 226 if (currentDomain == "")
228 break; 227 break;
229 228
230 let nextDot = currentDomain.indexOf("."); 229 let nextDot = currentDomain.indexOf(".");
231 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 230 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
232 } 231 }
233 232
234 if (!specificOnly) 233 if (!specificOnly)
235 selectors = getUnconditionalSelectors().concat(selectors); 234 selectors = getUnconditionalSelectors().concat(selectors);
236 235
237 return selectors; 236 return selectors;
238 } 237 }
239 }; 238 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld