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

Delta Between Two Patch Sets: lib/content/elemHideEmulation.js

Issue 29585589: Issue 6423 - Observe only relevant mutation types (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created Oct. 22, 2017, 10:51 p.m.
Right Patch Set: Add brackets Created Feb. 22, 2018, 7:13 a.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
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 const {filterToRegExp, splitSelector} = require("common"); 20 const {filterToRegExp, splitSelector} = require("../common");
21 21
22 let MIN_INVOCATION_INTERVAL = 3000; 22 let MIN_INVOCATION_INTERVAL = 3000;
23 const MAX_SYNCHRONOUS_PROCESSING_TIME = 50; 23 const MAX_SYNCHRONOUS_PROCESSING_TIME = 50;
24 const abpSelectorRegexp = /:-abp-([\w-]+)\(/i; 24 const abpSelectorRegexp = /:-abp-([\w-]+)\(/i;
25 25
26 /** Return position of node from parent. 26 /** Return position of node from parent.
27 * @param {Node} node the node to find the position of. 27 * @param {Node} node the node to find the position of.
28 * @return {number} One-based index like for :nth-child(), or 0 on error. 28 * @return {number} One-based index like for :nth-child(), or 0 on error.
29 */ 29 */
30 function positionInParent(node) 30 function positionInParent(node)
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 let priority = rule.style.getPropertyPriority(property); 115 let priority = rule.style.getPropertyPriority(property);
116 styles.push(`${property}: ${value}${priority ? " !" + priority : ""};`); 116 styles.push(`${property}: ${value}${priority ? " !" + priority : ""};`);
117 } 117 }
118 styles.sort(); 118 styles.sort();
119 return { 119 return {
120 style: styles.join(" "), 120 style: styles.join(" "),
121 subSelectors: splitSelector(rule.selectorText) 121 subSelectors: splitSelector(rule.selectorText)
122 }; 122 };
123 } 123 }
124 124
125 let scopeSupported = null;
126
127 function tryQuerySelector(subtree, selector, all)
128 {
129 let elements = null;
130 try
131 {
132 elements = all ? subtree.querySelectorAll(selector) :
133 subtree.querySelector(selector);
134 scopeSupported = true;
135 }
136 catch (e)
137 {
138 // Edge doesn't support ":scope"
139 scopeSupported = false;
140 }
141 return elements;
142 }
143
144 /**
145 * Query selector. If it is relative, will try :scope.
146 * @param {Node} subtree the element to query selector
147 * @param {string} selector the selector to query
148 * @param {bool} [all=false] true to perform querySelectorAll()
149 * @returns {?(Node|NodeList)} result of the query. null in case of error.
150 */
151 function scopedQuerySelector(subtree, selector, all)
152 {
153 if (selector[0] == ">")
154 {
155 selector = ":scope" + selector;
156 if (scopeSupported)
157 {
158 return all ? subtree.querySelectorAll(selector) :
159 subtree.querySelector(selector);
160 }
161 if (scopeSupported == null)
162 return tryQuerySelector(subtree, selector, all);
163 return null;
164 }
165 return all ? subtree.querySelectorAll(selector) :
166 subtree.querySelector(selector);
167 }
168
169 function scopedQuerySelectorAll(subtree, selector)
170 {
171 return scopedQuerySelector(subtree, selector, true);
172 }
173
125 function* evaluate(chain, index, prefix, subtree, styles) 174 function* evaluate(chain, index, prefix, subtree, styles)
126 { 175 {
127 if (index >= chain.length) 176 if (index >= chain.length)
128 { 177 {
129 yield prefix; 178 yield prefix;
130 return; 179 return;
131 } 180 }
132 for (let [selector, element] of 181 for (let [selector, element] of
133 chain[index].getSelectors(prefix, subtree, styles)) 182 chain[index].getSelectors(prefix, subtree, styles))
134 { 183 {
135 if (selector == null) 184 if (selector == null)
136 yield null; 185 yield null;
137 else 186 else
138 yield* evaluate(chain, index + 1, selector, element, styles); 187 yield* evaluate(chain, index + 1, selector, element, styles);
139 } 188 }
140 // Just in case the getSelectors() generator above had to run some heavy 189 // Just in case the getSelectors() generator above had to run some heavy
141 // document.querySelectorAll() call which didn't produce any results, make 190 // document.querySelectorAll() call which didn't produce any results, make
142 // sure there is at least one point where execution can pause. 191 // sure there is at least one point where execution can pause.
143 yield null; 192 yield null;
144 } 193 }
145 194
146 function PlainSelector(selector) 195 function PlainSelector(selector)
147 { 196 {
148 this._selector = selector; 197 this._selector = selector;
198 this.maybeDependsOnAttributes = /[#.]|\[.+\]/.test(selector);
149 } 199 }
150 200
151 PlainSelector.prototype = { 201 PlainSelector.prototype = {
152 /** 202 /**
153 * Generator function returning a pair of selector 203 * Generator function returning a pair of selector
154 * string and subtree. 204 * string and subtree.
155 * @param {string} prefix the prefix for the selector. 205 * @param {string} prefix the prefix for the selector.
156 * @param {Node} subtree the subtree we work on. 206 * @param {Node} subtree the subtree we work on.
157 * @param {StringifiedStyle[]} styles the stringified style objects. 207 * @param {StringifiedStyle[]} styles the stringified style objects.
158 */ 208 */
159 *getSelectors(prefix, subtree, styles) 209 *getSelectors(prefix, subtree, styles)
160 { 210 {
161 yield [prefix + this._selector, subtree]; 211 yield [prefix + this._selector, subtree];
162 } 212 }
163 }; 213 };
164 214
165 const incompletePrefixRegexp = /[\s>+~]$/; 215 const incompletePrefixRegexp = /[\s>+~]$/;
166 const relativeSelectorRegexp = /^[>+~]/;
167 216
168 function HasSelector(selectors) 217 function HasSelector(selectors)
169 { 218 {
170 this._innerSelectors = selectors; 219 this._innerSelectors = selectors;
171 } 220 }
172 221
173 HasSelector.prototype = { 222 HasSelector.prototype = {
174 requiresHiding: true, 223 requiresHiding: true,
175 224
176 get dependsOnStyles() 225 get dependsOnStyles()
177 { 226 {
178 return this._innerSelectors.some(selector => selector.dependsOnStyles); 227 return this._innerSelectors.some(selector => selector.dependsOnStyles);
228 },
229
230 get dependsOnCharacterData()
231 {
232 return this._innerSelectors.some(
233 selector => selector.dependsOnCharacterData
234 );
235 },
236
237 get maybeDependsOnAttributes()
238 {
239 return this._innerSelectors.some(
240 selector => selector.maybeDependsOnAttributes
241 );
179 }, 242 },
180 243
181 *getSelectors(prefix, subtree, styles) 244 *getSelectors(prefix, subtree, styles)
182 { 245 {
183 for (let element of this.getElements(prefix, subtree, styles)) 246 for (let element of this.getElements(prefix, subtree, styles))
184 yield [makeSelector(element, ""), element]; 247 yield [makeSelector(element, ""), element];
185 }, 248 },
186 249
187 /** 250 /**
188 * Generator function returning selected elements. 251 * Generator function returning selected elements.
189 * @param {string} prefix the prefix for the selector. 252 * @param {string} prefix the prefix for the selector.
190 * @param {Node} subtree the subtree we work on. 253 * @param {Node} subtree the subtree we work on.
191 * @param {StringifiedStyle[]} styles the stringified style objects. 254 * @param {StringifiedStyle[]} styles the stringified style objects.
192 */ 255 */
193 *getElements(prefix, subtree, styles) 256 *getElements(prefix, subtree, styles)
194 { 257 {
195 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? 258 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ?
196 prefix + "*" : prefix; 259 prefix + "*" : prefix;
197 let elements = subtree.querySelectorAll(actualPrefix); 260 let elements = scopedQuerySelectorAll(subtree, actualPrefix);
198 for (let element of elements) 261 if (elements)
199 { 262 {
200 let iter = evaluate(this._innerSelectors, 0, "", element, styles); 263 for (let element of elements)
201 for (let selector of iter) 264 {
202 { 265 let iter = evaluate(this._innerSelectors, 0, "", element, styles);
203 if (selector == null) 266 for (let selector of iter)
204 { 267 {
205 yield null; 268 if (selector == null)
206 continue; 269 yield null;
207 } 270 else if (scopedQuerySelector(element, selector))
208 if (relativeSelectorRegexp.test(selector))
209 selector = ":scope" + selector;
210 try
211 {
212 if (element.querySelector(selector))
213 yield element; 271 yield element;
214 } 272 }
215 catch (e) 273 yield null;
216 {
217 // :scope isn't supported on Edge, ignore error caused by it.
218 }
219 } 274 }
220 yield null;
221 } 275 }
222 } 276 }
223 }; 277 };
224 278
225 function ContainsSelector(textContent) 279 function ContainsSelector(textContent)
226 { 280 {
227 this._text = textContent; 281 this._text = textContent;
228 } 282 }
229 283
230 ContainsSelector.prototype = { 284 ContainsSelector.prototype = {
231 requiresHiding: true, 285 requiresHiding: true,
286 dependsOnCharacterData: true,
232 287
233 *getSelectors(prefix, subtree, stylesheet) 288 *getSelectors(prefix, subtree, stylesheet)
234 { 289 {
235 for (let element of this.getElements(prefix, subtree, stylesheet)) 290 for (let element of this.getElements(prefix, subtree, stylesheet))
236 yield [makeSelector(element, ""), subtree]; 291 yield [makeSelector(element, ""), subtree];
237 }, 292 },
238 293
239 *getElements(prefix, subtree, stylesheet) 294 *getElements(prefix, subtree, stylesheet)
240 { 295 {
241 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? 296 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ?
242 prefix + "*" : prefix; 297 prefix + "*" : prefix;
243 let elements = subtree.querySelectorAll(actualPrefix); 298
244 299 let elements = scopedQuerySelectorAll(subtree, actualPrefix);
245 for (let element of elements) 300 if (elements)
246 { 301 {
247 if (element.textContent.includes(this._text)) 302 for (let element of elements)
248 yield element; 303 {
249 else 304 if (element.textContent.includes(this._text))
250 yield null; 305 yield element;
306 else
307 yield null;
308 }
251 } 309 }
252 } 310 }
253 }; 311 };
254 312
255 function PropsSelector(propertyExpression) 313 function PropsSelector(propertyExpression)
256 { 314 {
257 let regexpString; 315 let regexpString;
258 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && 316 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" &&
259 propertyExpression[propertyExpression.length - 1] == "/") 317 propertyExpression[propertyExpression.length - 1] == "/")
260 { 318 {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 356
299 function isSelectorHidingOnlyPattern(pattern) 357 function isSelectorHidingOnlyPattern(pattern)
300 { 358 {
301 return pattern.selectors.some(s => s.preferHideWithSelector) && 359 return pattern.selectors.some(s => s.preferHideWithSelector) &&
302 !pattern.selectors.some(s => s.requiresHiding); 360 !pattern.selectors.some(s => s.requiresHiding);
303 } 361 }
304 362
305 function shouldObserveAttributes(patterns) 363 function shouldObserveAttributes(patterns)
306 { 364 {
307 // Observe changes to attributes if either there's a plain selector that 365 // Observe changes to attributes if either there's a plain selector that
308 // looks like an attribute selector in one of the patterns 366 // looks like an ID selector, class selector, or attribute selector in one of
309 // (e.g. "a[href='https://example.com/']") 367 // the patterns (e.g. "a[href='https://example.com/']")
310 // or there's a properties selector nested inside a has selector 368 // or there's a properties selector nested inside a has selector
311 // (e.g. "div:-abp-has(:-abp-properties(color: blue))") 369 // (e.g. "div:-abp-has(:-abp-properties(color: blue))")
312 return patterns.some( 370 return patterns.some(
313 pattern => pattern.selectors.some( 371 pattern => pattern.selectors.some(
314 selector => (selector instanceof PlainSelector && 372 selector => selector.maybeDependsOnAttributes ||
315 /\[.+\]/.test(selector._selector))|| 373 (selector instanceof HasSelector &&
Manish Jethani 2017/10/22 22:58:13 This check for attribute selector syntax can give
316 (pattern.selectors[0] instanceof HasSelector && 374 selector.dependsOnStyles)
Manish Jethani 2017/10/22 22:58:13 I realize that the second check is essentially pat
Manish Jethani 2017/10/23 00:12:09 Done.
317 selector instanceof PropsSelector)
318 ) 375 )
319 ); 376 );
320 } 377 }
321 378
322 function shouldObserveCharacterData(patterns) 379 function shouldObserveCharacterData(patterns)
323 { 380 {
324 // Observe changes to character data only if there's a contains selector in 381 // Observe changes to character data only if there's a contains selector in
325 // one of the patterns. 382 // one of the patterns.
326 return patterns.some( 383 return patterns.some(
327 pattern => pattern.selectors.some( 384 pattern => pattern.selectors.some(
328 selector => selector instanceof ContainsSelector 385 selector => selector.dependsOnCharacterData
329 ) 386 )
330 ); 387 );
331 } 388 }
332 389
333 function ElemHideEmulation(addSelectorsFunc, hideElemsFunc) 390 function ElemHideEmulation(addSelectorsFunc, hideElemsFunc)
334 { 391 {
335 this.document = document; 392 this.document = document;
336 this.addSelectorsFunc = addSelectorsFunc; 393 this.addSelectorsFunc = addSelectorsFunc;
337 this.hideElemsFunc = hideElemsFunc; 394 this.hideElemsFunc = hideElemsFunc;
338 this.observer = new MutationObserver(this.observe.bind(this)); 395 this.observer = new MutationObserver(this.observe.bind(this));
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 this._scheduledProcessing = {stylesheets}; 629 this._scheduledProcessing = {stylesheets};
573 setTimeout(() => 630 setTimeout(() =>
574 { 631 {
575 let newStylesheets = this._scheduledProcessing.stylesheets; 632 let newStylesheets = this._scheduledProcessing.stylesheets;
576 this._filteringInProgress = true; 633 this._filteringInProgress = true;
577 this._scheduledProcessing = null; 634 this._scheduledProcessing = null;
578 this._addSelectors(newStylesheets, completion); 635 this._addSelectors(newStylesheets, completion);
579 }, 636 },
580 MIN_INVOCATION_INTERVAL - (performance.now() - this._lastInvocation)); 637 MIN_INVOCATION_INTERVAL - (performance.now() - this._lastInvocation));
581 } 638 }
639 else if (this.document.readyState == "loading")
640 {
641 this._scheduledProcessing = {stylesheets};
642 let handler = () =>
643 {
644 document.removeEventListener("DOMContentLoaded", handler);
645 let newStylesheets = this._scheduledProcessing.stylesheets;
646 this._filteringInProgress = true;
647 this._scheduledProcessing = null;
648 this._addSelectors(newStylesheets, completion);
649 };
650 document.addEventListener("DOMContentLoaded", handler);
651 }
582 else 652 else
583 { 653 {
584 this._filteringInProgress = true; 654 this._filteringInProgress = true;
585 this._addSelectors(stylesheets, completion); 655 this._addSelectors(stylesheets, completion);
586 } 656 }
587 }, 657 },
588 658
589 onLoad(event) 659 onLoad(event)
590 { 660 {
591 let stylesheet = event.target.sheet; 661 let stylesheet = event.target.sheet;
(...skipping 27 matching lines...) Expand all
619 characterData: shouldObserveCharacterData(this.patterns), 689 characterData: shouldObserveCharacterData(this.patterns),
620 subtree: true 690 subtree: true
621 } 691 }
622 ); 692 );
623 this.document.addEventListener("load", this.onLoad.bind(this), true); 693 this.document.addEventListener("load", this.onLoad.bind(this), true);
624 } 694 }
625 } 695 }
626 }; 696 };
627 697
628 exports.ElemHideEmulation = ElemHideEmulation; 698 exports.ElemHideEmulation = ElemHideEmulation;
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