OLD | NEW |
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 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 try | 192 try |
193 { | 193 { |
194 return new RegExp(pattern, flags); | 194 return new RegExp(pattern, flags); |
195 } | 195 } |
196 catch (e) | 196 catch (e) |
197 { | 197 { |
198 } | 198 } |
199 return null; | 199 return null; |
200 } | 200 } |
201 | 201 |
202 function* evaluate(chain, index, prefix, subtree, styles) | 202 function* evaluate(chain, index, prefix, subtree, styles, targets) |
203 { | 203 { |
204 if (index >= chain.length) | 204 if (index >= chain.length) |
205 { | 205 { |
206 yield prefix; | 206 yield prefix; |
207 return; | 207 return; |
208 } | 208 } |
209 for (let [selector, element] of | 209 for (let [selector, element] of |
210 chain[index].getSelectors(prefix, subtree, styles)) | 210 chain[index].getSelectors(prefix, subtree, styles, targets)) |
211 { | 211 { |
212 if (selector == null) | 212 if (selector == null) |
213 yield null; | 213 yield null; |
214 else | 214 else |
215 yield* evaluate(chain, index + 1, selector, element, styles); | 215 yield* evaluate(chain, index + 1, selector, element, styles, targets); |
216 } | 216 } |
217 // Just in case the getSelectors() generator above had to run some heavy | 217 // Just in case the getSelectors() generator above had to run some heavy |
218 // document.querySelectorAll() call which didn't produce any results, make | 218 // document.querySelectorAll() call which didn't produce any results, make |
219 // sure there is at least one point where execution can pause. | 219 // sure there is at least one point where execution can pause. |
220 yield null; | 220 yield null; |
221 } | 221 } |
222 | 222 |
223 function PlainSelector(selector) | 223 function PlainSelector(selector) |
224 { | 224 { |
225 this._selector = selector; | 225 this._selector = selector; |
226 this.maybeDependsOnAttributes = /[#.]|\[.+\]/.test(selector); | 226 this.maybeDependsOnAttributes = /[#.]|\[.+\]/.test(selector); |
227 this.dependsOnDOM = this.maybeDependsOnAttributes; | 227 this.dependsOnDOM = this.maybeDependsOnAttributes; |
228 } | 228 } |
229 | 229 |
230 PlainSelector.prototype = { | 230 PlainSelector.prototype = { |
231 /** | 231 /** |
232 * Generator function returning a pair of selector | 232 * Generator function returning a pair of selector |
233 * string and subtree. | 233 * string and subtree. |
234 * @param {string} prefix the prefix for the selector. | 234 * @param {string} prefix the prefix for the selector. |
235 * @param {Node} subtree the subtree we work on. | 235 * @param {Node} subtree the subtree we work on. |
236 * @param {StringifiedStyle[]} styles the stringified style objects. | 236 * @param {StringifiedStyle[]} styles the stringified style objects. |
| 237 * @param {Node[]} [targets] the nodes we are interested in. |
237 */ | 238 */ |
238 *getSelectors(prefix, subtree, styles) | 239 *getSelectors(prefix, subtree, styles, targets) |
239 { | 240 { |
240 yield [prefix + this._selector, subtree]; | 241 yield [prefix + this._selector, subtree]; |
241 } | 242 } |
242 }; | 243 }; |
243 | 244 |
244 const incompletePrefixRegexp = /[\s>+~]$/; | 245 const incompletePrefixRegexp = /[\s>+~]$/; |
245 | 246 |
246 function HasSelector(selectors) | 247 function HasSelector(selectors) |
247 { | 248 { |
248 this._innerSelectors = selectors; | 249 this._innerSelectors = selectors; |
(...skipping 14 matching lines...) Expand all Loading... |
263 ); | 264 ); |
264 }, | 265 }, |
265 | 266 |
266 get maybeDependsOnAttributes() | 267 get maybeDependsOnAttributes() |
267 { | 268 { |
268 return this._innerSelectors.some( | 269 return this._innerSelectors.some( |
269 selector => selector.maybeDependsOnAttributes | 270 selector => selector.maybeDependsOnAttributes |
270 ); | 271 ); |
271 }, | 272 }, |
272 | 273 |
273 *getSelectors(prefix, subtree, styles) | 274 *getSelectors(prefix, subtree, styles, targets) |
274 { | 275 { |
275 for (let element of this.getElements(prefix, subtree, styles)) | 276 for (let element of this.getElements(prefix, subtree, styles, targets)) |
276 yield [makeSelector(element), element]; | 277 yield [makeSelector(element), element]; |
277 }, | 278 }, |
278 | 279 |
279 /** | 280 /** |
280 * Generator function returning selected elements. | 281 * Generator function returning selected elements. |
281 * @param {string} prefix the prefix for the selector. | 282 * @param {string} prefix the prefix for the selector. |
282 * @param {Node} subtree the subtree we work on. | 283 * @param {Node} subtree the subtree we work on. |
283 * @param {StringifiedStyle[]} styles the stringified style objects. | 284 * @param {StringifiedStyle[]} styles the stringified style objects. |
| 285 * @param {Node[]} [targets] the nodes we are interested in. |
284 */ | 286 */ |
285 *getElements(prefix, subtree, styles) | 287 *getElements(prefix, subtree, styles, targets) |
286 { | 288 { |
287 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? | 289 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? |
288 prefix + "*" : prefix; | 290 prefix + "*" : prefix; |
289 let elements = scopedQuerySelectorAll(subtree, actualPrefix); | 291 let elements = scopedQuerySelectorAll(subtree, actualPrefix); |
290 if (elements) | 292 if (elements) |
291 { | 293 { |
292 for (let element of elements) | 294 for (let element of elements) |
293 { | 295 { |
294 let iter = evaluate(this._innerSelectors, 0, "", element, styles); | 296 // If the element is neither an ancestor nor a descendant of one of the |
| 297 // targets, we can skip it. |
| 298 if (targets && !targets.some(target => element.contains(target) || |
| 299 target.contains(element))) |
| 300 { |
| 301 yield null; |
| 302 continue; |
| 303 } |
| 304 |
| 305 let iter = evaluate(this._innerSelectors, 0, "", element, styles, |
| 306 targets); |
295 for (let selector of iter) | 307 for (let selector of iter) |
296 { | 308 { |
297 if (selector == null) | 309 if (selector == null) |
298 yield null; | 310 yield null; |
299 else if (scopedQuerySelector(element, selector)) | 311 else if (scopedQuerySelector(element, selector)) |
300 yield element; | 312 yield element; |
301 } | 313 } |
302 yield null; | 314 yield null; |
303 } | 315 } |
304 } | 316 } |
305 } | 317 } |
306 }; | 318 }; |
307 | 319 |
308 function ContainsSelector(textContent) | 320 function ContainsSelector(textContent) |
309 { | 321 { |
310 this._regexp = makeRegExpParameter(textContent); | 322 this._regexp = makeRegExpParameter(textContent); |
311 } | 323 } |
312 | 324 |
313 ContainsSelector.prototype = { | 325 ContainsSelector.prototype = { |
314 dependsOnDOM: true, | 326 dependsOnDOM: true, |
315 dependsOnCharacterData: true, | 327 dependsOnCharacterData: true, |
316 | 328 |
317 *getSelectors(prefix, subtree, styles) | 329 *getSelectors(prefix, subtree, styles, targets) |
318 { | 330 { |
319 for (let element of this.getElements(prefix, subtree, styles)) | 331 for (let element of this.getElements(prefix, subtree, styles, targets)) |
320 yield [makeSelector(element), subtree]; | 332 yield [makeSelector(element), subtree]; |
321 }, | 333 }, |
322 | 334 |
323 *getElements(prefix, subtree, styles) | 335 *getElements(prefix, subtree, styles, targets) |
324 { | 336 { |
325 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? | 337 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? |
326 prefix + "*" : prefix; | 338 prefix + "*" : prefix; |
327 | 339 |
328 let elements = scopedQuerySelectorAll(subtree, actualPrefix); | 340 let elements = scopedQuerySelectorAll(subtree, actualPrefix); |
329 | 341 |
330 if (elements) | 342 if (elements) |
331 { | 343 { |
332 let lastRoot = null; | 344 let lastRoot = null; |
333 for (let element of elements) | 345 for (let element of elements) |
334 { | 346 { |
335 // For a filter like div:-abp-contains(Hello) and a subtree like | 347 // For a filter like div:-abp-contains(Hello) and a subtree like |
336 // <div id="a"><div id="b"><div id="c">Hello</div></div></div> | 348 // <div id="a"><div id="b"><div id="c">Hello</div></div></div> |
337 // we're only interested in div#a | 349 // we're only interested in div#a |
338 if (lastRoot && lastRoot.contains(element)) | 350 if (lastRoot && lastRoot.contains(element)) |
339 { | 351 { |
340 yield null; | 352 yield null; |
341 continue; | 353 continue; |
342 } | 354 } |
343 | 355 |
344 lastRoot = element; | 356 lastRoot = element; |
345 | 357 |
| 358 if (targets && !targets.some(target => element.contains(target) || |
| 359 target.contains(element))) |
| 360 { |
| 361 yield null; |
| 362 continue; |
| 363 } |
| 364 |
346 if (this._regexp && this._regexp.test(element.textContent)) | 365 if (this._regexp && this._regexp.test(element.textContent)) |
347 yield element; | 366 yield element; |
348 else | 367 else |
349 yield null; | 368 yield null; |
350 } | 369 } |
351 } | 370 } |
352 } | 371 } |
353 }; | 372 }; |
354 | 373 |
355 function PropsSelector(propertyExpression) | 374 function PropsSelector(propertyExpression) |
(...skipping 25 matching lines...) Expand all Loading... |
381 { | 400 { |
382 subSelector = subSelector.substr(1); | 401 subSelector = subSelector.substr(1); |
383 } | 402 } |
384 let idx = subSelector.lastIndexOf("::"); | 403 let idx = subSelector.lastIndexOf("::"); |
385 if (idx != -1) | 404 if (idx != -1) |
386 subSelector = subSelector.substr(0, idx); | 405 subSelector = subSelector.substr(0, idx); |
387 yield prefix + subSelector; | 406 yield prefix + subSelector; |
388 } | 407 } |
389 }, | 408 }, |
390 | 409 |
391 *getSelectors(prefix, subtree, styles) | 410 *getSelectors(prefix, subtree, styles, targets) |
392 { | 411 { |
393 for (let selector of this.findPropsSelectors(styles, prefix, this._regexp)) | 412 for (let selector of this.findPropsSelectors(styles, prefix, this._regexp)) |
394 yield [selector, subtree]; | 413 yield [selector, subtree]; |
395 } | 414 } |
396 }; | 415 }; |
397 | 416 |
398 function Pattern(selectors, text) | 417 function Pattern(selectors, text) |
399 { | 418 { |
400 this.selectors = selectors; | 419 this.selectors = selectors; |
401 this.text = text; | 420 this.text = text; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 | 506 |
488 // There are only 3 types of mutations: "attributes", "characterData", and | 507 // There are only 3 types of mutations: "attributes", "characterData", and |
489 // "childList". | 508 // "childList". |
490 if (types.size == 3) | 509 if (types.size == 3) |
491 break; | 510 break; |
492 } | 511 } |
493 | 512 |
494 return types; | 513 return types; |
495 } | 514 } |
496 | 515 |
| 516 function extractMutationTargets(mutations) |
| 517 { |
| 518 if (!mutations) |
| 519 return null; |
| 520 |
| 521 let targets = new Set(); |
| 522 |
| 523 for (let mutation of mutations) |
| 524 { |
| 525 if (mutation.type == "childList") |
| 526 { |
| 527 // When new nodes are added, we're interested in the added nodes rather |
| 528 // than the parent. |
| 529 for (let node of mutation.addedNodes) |
| 530 targets.add(node); |
| 531 |
| 532 // Ideally we would also be interested in removed nodes, but since we |
| 533 // never unhide an element once hidden we can simply ignore any removed |
| 534 // nodes. Note that this will change once we start using CSS selectors |
| 535 // for -abp-has and -abp-contains, i.e. we'll have to remove the |
| 536 // selectors for any removed nodes. |
| 537 } |
| 538 else |
| 539 { |
| 540 targets.add(mutation.target); |
| 541 } |
| 542 } |
| 543 |
| 544 return [...targets]; |
| 545 } |
| 546 |
497 function filterPatterns(patterns, {stylesheets, mutations}) | 547 function filterPatterns(patterns, {stylesheets, mutations}) |
498 { | 548 { |
499 if (!stylesheets && !mutations) | 549 if (!stylesheets && !mutations) |
500 return patterns.slice(); | 550 return patterns.slice(); |
501 | 551 |
502 let mutationTypes = mutations ? extractMutationTypes(mutations) : null; | 552 let mutationTypes = mutations ? extractMutationTypes(mutations) : null; |
503 | 553 |
504 return patterns.filter( | 554 return patterns.filter( |
505 pattern => (stylesheets && pattern.dependsOnStyles) || | 555 pattern => (stylesheets && pattern.dependsOnStyles) || |
506 (mutations && pattern.dependsOnDOM && | 556 (mutations && pattern.dependsOnDOM && |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
671 | 721 |
672 for (let rule of rules) | 722 for (let rule of rules) |
673 { | 723 { |
674 if (rule.type != rule.STYLE_RULE) | 724 if (rule.type != rule.STYLE_RULE) |
675 continue; | 725 continue; |
676 | 726 |
677 cssStyles.push(stringifyStyle(rule)); | 727 cssStyles.push(stringifyStyle(rule)); |
678 } | 728 } |
679 } | 729 } |
680 | 730 |
| 731 let targets = extractMutationTargets(mutations); |
| 732 |
681 let pattern = null; | 733 let pattern = null; |
682 let generator = null; | 734 let generator = null; |
683 | 735 |
684 let processPatterns = () => | 736 let processPatterns = () => |
685 { | 737 { |
686 let cycleStart = performance.now(); | 738 let cycleStart = performance.now(); |
687 | 739 |
688 if (!pattern) | 740 if (!pattern) |
689 { | 741 { |
690 if (!patterns.length) | 742 if (!patterns.length) |
691 { | 743 { |
692 if (selectors.length > 0) | 744 if (selectors.length > 0) |
693 this.addSelectorsFunc(selectors, selectorFilters); | 745 this.addSelectorsFunc(selectors, selectorFilters); |
694 if (elements.length > 0) | 746 if (elements.length > 0) |
695 this.hideElemsFunc(elements, elementFilters); | 747 this.hideElemsFunc(elements, elementFilters); |
696 if (typeof done == "function") | 748 if (typeof done == "function") |
697 done(); | 749 done(); |
698 return; | 750 return; |
699 } | 751 } |
700 | 752 |
701 pattern = patterns.shift(); | 753 pattern = patterns.shift(); |
702 | 754 |
703 generator = evaluate(pattern.selectors, 0, "", | 755 generator = evaluate(pattern.selectors, 0, "", |
704 this.document, cssStyles); | 756 this.document, cssStyles, targets); |
705 } | 757 } |
706 for (let selector of generator) | 758 for (let selector of generator) |
707 { | 759 { |
708 if (selector != null) | 760 if (selector != null) |
709 { | 761 { |
710 if (!this.useInlineStyles) | 762 if (!this.useInlineStyles) |
711 { | 763 { |
712 selectors.push(selector); | 764 selectors.push(selector); |
713 selectorFilters.push(pattern.text); | 765 selectorFilters.push(pattern.text); |
714 } | 766 } |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
865 characterData: shouldObserveCharacterData(this.patterns), | 917 characterData: shouldObserveCharacterData(this.patterns), |
866 subtree: true | 918 subtree: true |
867 } | 919 } |
868 ); | 920 ); |
869 this.document.addEventListener("load", this.onLoad.bind(this), true); | 921 this.document.addEventListener("load", this.onLoad.bind(this), true); |
870 } | 922 } |
871 } | 923 } |
872 }; | 924 }; |
873 | 925 |
874 exports.ElemHideEmulation = ElemHideEmulation; | 926 exports.ElemHideEmulation = ElemHideEmulation; |
OLD | NEW |