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 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 yield null; | 232 yield null; |
233 else | 233 else |
234 yield* evaluate(chain, index + 1, selector, element, styles, targets); | 234 yield* evaluate(chain, index + 1, selector, element, styles, targets); |
235 } | 235 } |
236 // Just in case the getSelectors() generator above had to run some heavy | 236 // Just in case the getSelectors() generator above had to run some heavy |
237 // document.querySelectorAll() call which didn't produce any results, make | 237 // document.querySelectorAll() call which didn't produce any results, make |
238 // sure there is at least one point where execution can pause. | 238 // sure there is at least one point where execution can pause. |
239 yield null; | 239 yield null; |
240 } | 240 } |
241 | 241 |
242 function PlainSelector(selector) | 242 class PlainSelector |
243 { | 243 { |
244 this._selector = selector; | 244 constructor(selector) |
245 this.maybeDependsOnAttributes = /[#.]|\[.+\]/.test(selector); | 245 { |
246 this.dependsOnDOM = this.maybeDependsOnAttributes; | 246 this._selector = selector; |
247 this.maybeContainsSiblingCombinators = /[~+]/.test(selector); | 247 this.maybeDependsOnAttributes = /[#.]|\[.+\]/.test(selector); |
248 } | 248 this.dependsOnDOM = this.maybeDependsOnAttributes; |
| 249 this.maybeContainsSiblingCombinators = /[~+]/.test(selector); |
| 250 } |
249 | 251 |
250 PlainSelector.prototype = { | |
251 /** | 252 /** |
252 * Generator function returning a pair of selector | 253 * Generator function returning a pair of selector |
253 * string and subtree. | 254 * string and subtree. |
254 * @param {string} prefix the prefix for the selector. | 255 * @param {string} prefix the prefix for the selector. |
255 * @param {Node} subtree the subtree we work on. | 256 * @param {Node} subtree the subtree we work on. |
256 * @param {StringifiedStyle[]} styles the stringified style objects. | 257 * @param {StringifiedStyle[]} styles the stringified style objects. |
257 * @param {Node[]} [targets] the nodes we are interested in. | 258 * @param {Node[]} [targets] the nodes we are interested in. |
258 */ | 259 */ |
259 *getSelectors(prefix, subtree, styles, targets) | 260 *getSelectors(prefix, subtree, styles, targets) |
260 { | 261 { |
261 yield [prefix + this._selector, subtree]; | 262 yield [prefix + this._selector, subtree]; |
262 } | 263 } |
263 }; | 264 } |
264 | 265 |
265 const incompletePrefixRegexp = /[\s>+~]$/; | 266 const incompletePrefixRegexp = /[\s>+~]$/; |
266 | 267 |
267 function HasSelector(selectors) | 268 class HasSelector |
268 { | 269 { |
269 this._innerSelectors = selectors; | 270 constructor(selectors) |
270 } | 271 { |
| 272 this.dependsOnDOM = true; |
271 | 273 |
272 HasSelector.prototype = { | 274 this._innerSelectors = selectors; |
273 dependsOnDOM: true, | 275 } |
274 | 276 |
275 get dependsOnStyles() | 277 get dependsOnStyles() |
276 { | 278 { |
277 return this._innerSelectors.some(selector => selector.dependsOnStyles); | 279 return this._innerSelectors.some(selector => selector.dependsOnStyles); |
278 }, | 280 } |
279 | 281 |
280 get dependsOnCharacterData() | 282 get dependsOnCharacterData() |
281 { | 283 { |
282 return this._innerSelectors.some( | 284 return this._innerSelectors.some( |
283 selector => selector.dependsOnCharacterData | 285 selector => selector.dependsOnCharacterData |
284 ); | 286 ); |
285 }, | 287 } |
286 | 288 |
287 get maybeDependsOnAttributes() | 289 get maybeDependsOnAttributes() |
288 { | 290 { |
289 return this._innerSelectors.some( | 291 return this._innerSelectors.some( |
290 selector => selector.maybeDependsOnAttributes | 292 selector => selector.maybeDependsOnAttributes |
291 ); | 293 ); |
292 }, | 294 } |
293 | 295 |
294 *getSelectors(prefix, subtree, styles, targets) | 296 *getSelectors(prefix, subtree, styles, targets) |
295 { | 297 { |
296 for (let element of this.getElements(prefix, subtree, styles, targets)) | 298 for (let element of this.getElements(prefix, subtree, styles, targets)) |
297 yield [makeSelector(element), element]; | 299 yield [makeSelector(element), element]; |
298 }, | 300 } |
299 | 301 |
300 /** | 302 /** |
301 * Generator function returning selected elements. | 303 * Generator function returning selected elements. |
302 * @param {string} prefix the prefix for the selector. | 304 * @param {string} prefix the prefix for the selector. |
303 * @param {Node} subtree the subtree we work on. | 305 * @param {Node} subtree the subtree we work on. |
304 * @param {StringifiedStyle[]} styles the stringified style objects. | 306 * @param {StringifiedStyle[]} styles the stringified style objects. |
305 * @param {Node[]} [targets] the nodes we are interested in. | 307 * @param {Node[]} [targets] the nodes we are interested in. |
306 */ | 308 */ |
307 *getElements(prefix, subtree, styles, targets) | 309 *getElements(prefix, subtree, styles, targets) |
308 { | 310 { |
(...skipping 22 matching lines...) Expand all Loading... |
331 else if (scopedQuerySelector(element, selector)) | 333 else if (scopedQuerySelector(element, selector)) |
332 yield element; | 334 yield element; |
333 } | 335 } |
334 yield null; | 336 yield null; |
335 | 337 |
336 if (testInfo) | 338 if (testInfo) |
337 testInfo.lastProcessedElements.add(element); | 339 testInfo.lastProcessedElements.add(element); |
338 } | 340 } |
339 } | 341 } |
340 } | 342 } |
341 }; | |
342 | |
343 function ContainsSelector(textContent) | |
344 { | |
345 this._regexp = makeRegExpParameter(textContent); | |
346 } | 343 } |
347 | 344 |
348 ContainsSelector.prototype = { | 345 class ContainsSelector |
349 dependsOnDOM: true, | 346 { |
350 dependsOnCharacterData: true, | 347 constructor(textContent) |
| 348 { |
| 349 this.dependsOnDOM = true; |
| 350 this.dependsOnCharacterData = true; |
| 351 |
| 352 this._regexp = makeRegExpParameter(textContent); |
| 353 } |
351 | 354 |
352 *getSelectors(prefix, subtree, styles, targets) | 355 *getSelectors(prefix, subtree, styles, targets) |
353 { | 356 { |
354 for (let element of this.getElements(prefix, subtree, styles, targets)) | 357 for (let element of this.getElements(prefix, subtree, styles, targets)) |
355 yield [makeSelector(element), subtree]; | 358 yield [makeSelector(element), subtree]; |
356 }, | 359 } |
357 | 360 |
358 *getElements(prefix, subtree, styles, targets) | 361 *getElements(prefix, subtree, styles, targets) |
359 { | 362 { |
360 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? | 363 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? |
361 prefix + "*" : prefix; | 364 prefix + "*" : prefix; |
362 | 365 |
363 let elements = scopedQuerySelectorAll(subtree, actualPrefix); | 366 let elements = scopedQuerySelectorAll(subtree, actualPrefix); |
364 | 367 |
365 if (elements) | 368 if (elements) |
366 { | 369 { |
(...skipping 21 matching lines...) Expand all Loading... |
388 if (this._regexp && this._regexp.test(element.textContent)) | 391 if (this._regexp && this._regexp.test(element.textContent)) |
389 yield element; | 392 yield element; |
390 else | 393 else |
391 yield null; | 394 yield null; |
392 | 395 |
393 if (testInfo) | 396 if (testInfo) |
394 testInfo.lastProcessedElements.add(element); | 397 testInfo.lastProcessedElements.add(element); |
395 } | 398 } |
396 } | 399 } |
397 } | 400 } |
398 }; | |
399 | |
400 function PropsSelector(propertyExpression) | |
401 { | |
402 let regexpString; | |
403 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && | |
404 propertyExpression[propertyExpression.length - 1] == "/") | |
405 { | |
406 regexpString = propertyExpression.slice(1, -1) | |
407 .replace("\\7B ", "{").replace("\\7D ", "}"); | |
408 } | |
409 else | |
410 regexpString = filterToRegExp(propertyExpression); | |
411 | |
412 this._regexp = new RegExp(regexpString, "i"); | |
413 } | 401 } |
414 | 402 |
415 PropsSelector.prototype = { | 403 class PropsSelector |
416 dependsOnStyles: true, | 404 { |
417 dependsOnDOM: true, | 405 constructor(propertyExpression) |
| 406 { |
| 407 this.dependsOnStyles = true; |
| 408 this.dependsOnDOM = true; |
| 409 |
| 410 let regexpString; |
| 411 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && |
| 412 propertyExpression[propertyExpression.length - 1] == "/") |
| 413 { |
| 414 regexpString = propertyExpression.slice(1, -1) |
| 415 .replace("\\7B ", "{").replace("\\7D ", "}"); |
| 416 } |
| 417 else |
| 418 regexpString = filterToRegExp(propertyExpression); |
| 419 |
| 420 this._regexp = new RegExp(regexpString, "i"); |
| 421 } |
418 | 422 |
419 *findPropsSelectors(styles, prefix, regexp) | 423 *findPropsSelectors(styles, prefix, regexp) |
420 { | 424 { |
421 for (let style of styles) | 425 for (let style of styles) |
422 if (regexp.test(style.style)) | 426 if (regexp.test(style.style)) |
423 for (let subSelector of style.subSelectors) | 427 for (let subSelector of style.subSelectors) |
424 { | 428 { |
425 if (subSelector.startsWith("*") && | 429 if (subSelector.startsWith("*") && |
426 !incompletePrefixRegexp.test(prefix)) | 430 !incompletePrefixRegexp.test(prefix)) |
427 { | 431 { |
428 subSelector = subSelector.substr(1); | 432 subSelector = subSelector.substr(1); |
429 } | 433 } |
430 let idx = subSelector.lastIndexOf("::"); | 434 let idx = subSelector.lastIndexOf("::"); |
431 if (idx != -1) | 435 if (idx != -1) |
432 subSelector = subSelector.substr(0, idx); | 436 subSelector = subSelector.substr(0, idx); |
433 yield qualifySelector(subSelector, prefix); | 437 yield qualifySelector(subSelector, prefix); |
434 } | 438 } |
435 }, | 439 } |
436 | 440 |
437 *getSelectors(prefix, subtree, styles, targets) | 441 *getSelectors(prefix, subtree, styles, targets) |
438 { | 442 { |
439 for (let selector of this.findPropsSelectors(styles, prefix, this._regexp)) | 443 for (let selector of this.findPropsSelectors(styles, prefix, this._regexp)) |
440 yield [selector, subtree]; | 444 yield [selector, subtree]; |
441 } | 445 } |
442 }; | |
443 | |
444 function Pattern(selectors, text) | |
445 { | |
446 this.selectors = selectors; | |
447 this.text = text; | |
448 } | 446 } |
449 | 447 |
450 Pattern.prototype = { | 448 class Pattern |
| 449 { |
| 450 constructor(selectors, text) |
| 451 { |
| 452 this.selectors = selectors; |
| 453 this.text = text; |
| 454 } |
| 455 |
451 get dependsOnStyles() | 456 get dependsOnStyles() |
452 { | 457 { |
453 return getCachedPropertyValue( | 458 return getCachedPropertyValue( |
454 this, "_dependsOnStyles", | 459 this, "_dependsOnStyles", |
455 () => this.selectors.some(selector => selector.dependsOnStyles) | 460 () => this.selectors.some(selector => selector.dependsOnStyles) |
456 ); | 461 ); |
457 }, | 462 } |
458 | 463 |
459 get dependsOnDOM() | 464 get dependsOnDOM() |
460 { | 465 { |
461 return getCachedPropertyValue( | 466 return getCachedPropertyValue( |
462 this, "_dependsOnDOM", | 467 this, "_dependsOnDOM", |
463 () => this.selectors.some(selector => selector.dependsOnDOM) | 468 () => this.selectors.some(selector => selector.dependsOnDOM) |
464 ); | 469 ); |
465 }, | 470 } |
466 | 471 |
467 get dependsOnStylesAndDOM() | 472 get dependsOnStylesAndDOM() |
468 { | 473 { |
469 return getCachedPropertyValue( | 474 return getCachedPropertyValue( |
470 this, "_dependsOnStylesAndDOM", | 475 this, "_dependsOnStylesAndDOM", |
471 () => this.selectors.some(selector => selector.dependsOnStyles && | 476 () => this.selectors.some(selector => selector.dependsOnStyles && |
472 selector.dependsOnDOM) | 477 selector.dependsOnDOM) |
473 ); | 478 ); |
474 }, | 479 } |
475 | 480 |
476 get maybeDependsOnAttributes() | 481 get maybeDependsOnAttributes() |
477 { | 482 { |
478 // Observe changes to attributes if either there's a plain selector that | 483 // Observe changes to attributes if either there's a plain selector that |
479 // looks like an ID selector, class selector, or attribute selector in one | 484 // looks like an ID selector, class selector, or attribute selector in one |
480 // of the patterns (e.g. "a[href='https://example.com/']") | 485 // of the patterns (e.g. "a[href='https://example.com/']") |
481 // or there's a properties selector nested inside a has selector | 486 // or there's a properties selector nested inside a has selector |
482 // (e.g. "div:-abp-has(:-abp-properties(color: blue))") | 487 // (e.g. "div:-abp-has(:-abp-properties(color: blue))") |
483 return getCachedPropertyValue( | 488 return getCachedPropertyValue( |
484 this, "_maybeDependsOnAttributes", | 489 this, "_maybeDependsOnAttributes", |
485 () => this.selectors.some( | 490 () => this.selectors.some( |
486 selector => selector.maybeDependsOnAttributes || | 491 selector => selector.maybeDependsOnAttributes || |
487 (selector instanceof HasSelector && | 492 (selector instanceof HasSelector && |
488 selector.dependsOnStyles) | 493 selector.dependsOnStyles) |
489 ) | 494 ) |
490 ); | 495 ); |
491 }, | 496 } |
492 | 497 |
493 get dependsOnCharacterData() | 498 get dependsOnCharacterData() |
494 { | 499 { |
495 // Observe changes to character data only if there's a contains selector in | 500 // Observe changes to character data only if there's a contains selector in |
496 // one of the patterns. | 501 // one of the patterns. |
497 return getCachedPropertyValue( | 502 return getCachedPropertyValue( |
498 this, "_dependsOnCharacterData", | 503 this, "_dependsOnCharacterData", |
499 () => this.selectors.some(selector => selector.dependsOnCharacterData) | 504 () => this.selectors.some(selector => selector.dependsOnCharacterData) |
500 ); | 505 ); |
501 }, | 506 } |
502 | 507 |
503 get maybeContainsSiblingCombinators() | 508 get maybeContainsSiblingCombinators() |
504 { | 509 { |
505 return getCachedPropertyValue( | 510 return getCachedPropertyValue( |
506 this, "_maybeContainsSiblingCombinators", | 511 this, "_maybeContainsSiblingCombinators", |
507 () => this.selectors.some(selector => | 512 () => this.selectors.some(selector => |
508 selector.maybeContainsSiblingCombinators) | 513 selector.maybeContainsSiblingCombinators) |
509 ); | 514 ); |
510 }, | 515 } |
511 | 516 |
512 matchesMutationTypes(mutationTypes) | 517 matchesMutationTypes(mutationTypes) |
513 { | 518 { |
514 let mutationTypeMatchMap = getCachedPropertyValue( | 519 let mutationTypeMatchMap = getCachedPropertyValue( |
515 this, "_mutationTypeMatchMap", | 520 this, "_mutationTypeMatchMap", |
516 () => new Map([ | 521 () => new Map([ |
517 // All types of DOM-dependent patterns are affected by mutations of | 522 // All types of DOM-dependent patterns are affected by mutations of |
518 // type "childList". | 523 // type "childList". |
519 ["childList", true], | 524 ["childList", true], |
520 ["attributes", this.maybeDependsOnAttributes], | 525 ["attributes", this.maybeDependsOnAttributes], |
521 ["characterData", this.dependsOnCharacterData] | 526 ["characterData", this.dependsOnCharacterData] |
522 ]) | 527 ]) |
523 ); | 528 ); |
524 | 529 |
525 for (let mutationType of mutationTypes) | 530 for (let mutationType of mutationTypes) |
526 { | 531 { |
527 if (mutationTypeMatchMap.get(mutationType)) | 532 if (mutationTypeMatchMap.get(mutationType)) |
528 return true; | 533 return true; |
529 } | 534 } |
530 | 535 |
531 return false; | 536 return false; |
532 } | 537 } |
533 }; | 538 } |
534 | 539 |
535 function extractMutationTypes(mutations) | 540 function extractMutationTypes(mutations) |
536 { | 541 { |
537 let types = new Set(); | 542 let types = new Set(); |
538 | 543 |
539 for (let mutation of mutations) | 544 for (let mutation of mutations) |
540 { | 545 { |
541 types.add(mutation.type); | 546 types.add(mutation.type); |
542 | 547 |
543 // There are only 3 types of mutations: "attributes", "characterData", and | 548 // There are only 3 types of mutations: "attributes", "characterData", and |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
591 function shouldObserveAttributes(patterns) | 596 function shouldObserveAttributes(patterns) |
592 { | 597 { |
593 return patterns.some(pattern => pattern.maybeDependsOnAttributes); | 598 return patterns.some(pattern => pattern.maybeDependsOnAttributes); |
594 } | 599 } |
595 | 600 |
596 function shouldObserveCharacterData(patterns) | 601 function shouldObserveCharacterData(patterns) |
597 { | 602 { |
598 return patterns.some(pattern => pattern.dependsOnCharacterData); | 603 return patterns.some(pattern => pattern.dependsOnCharacterData); |
599 } | 604 } |
600 | 605 |
601 function ElemHideEmulation(addSelectorsFunc, hideElemsFunc) | 606 class ElemHideEmulation |
602 { | 607 { |
603 this.document = document; | 608 constructor(addSelectorsFunc, hideElemsFunc) |
604 this.addSelectorsFunc = addSelectorsFunc; | 609 { |
605 this.hideElemsFunc = hideElemsFunc; | 610 this._filteringInProgress = false; |
606 this.observer = new MutationObserver(this.observe.bind(this)); | 611 this._lastInvocation = -MIN_INVOCATION_INTERVAL; |
607 } | 612 this._scheduledProcessing = null; |
608 | 613 |
609 ElemHideEmulation.prototype = { | 614 this.document = document; |
| 615 this.addSelectorsFunc = addSelectorsFunc; |
| 616 this.hideElemsFunc = hideElemsFunc; |
| 617 this.observer = new MutationObserver(this.observe.bind(this)); |
| 618 } |
| 619 |
610 isSameOrigin(stylesheet) | 620 isSameOrigin(stylesheet) |
611 { | 621 { |
612 try | 622 try |
613 { | 623 { |
614 return new URL(stylesheet.href).origin == this.document.location.origin; | 624 return new URL(stylesheet.href).origin == this.document.location.origin; |
615 } | 625 } |
616 catch (e) | 626 catch (e) |
617 { | 627 { |
618 // Invalid URL, assume that it is first-party. | 628 // Invalid URL, assume that it is first-party. |
619 return true; | 629 return true; |
620 } | 630 } |
621 }, | 631 } |
622 | 632 |
623 /** Parse the selector | 633 /** Parse the selector |
624 * @param {string} selector the selector to parse | 634 * @param {string} selector the selector to parse |
625 * @return {Array} selectors is an array of objects, | 635 * @return {Array} selectors is an array of objects, |
626 * or null in case of errors. | 636 * or null in case of errors. |
627 */ | 637 */ |
628 parseSelector(selector) | 638 parseSelector(selector) |
629 { | 639 { |
630 if (selector.length == 0) | 640 if (selector.length == 0) |
631 return []; | 641 return []; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 selectors.push(...suffix); | 684 selectors.push(...suffix); |
675 | 685 |
676 if (selectors.length == 1 && selectors[0] instanceof ContainsSelector) | 686 if (selectors.length == 1 && selectors[0] instanceof ContainsSelector) |
677 { | 687 { |
678 console.error(new SyntaxError("Failed to parse Adblock Plus " + | 688 console.error(new SyntaxError("Failed to parse Adblock Plus " + |
679 `selector ${selector}, can't ` + | 689 `selector ${selector}, can't ` + |
680 "have a lonely :-abp-contains().")); | 690 "have a lonely :-abp-contains().")); |
681 return null; | 691 return null; |
682 } | 692 } |
683 return selectors; | 693 return selectors; |
684 }, | 694 } |
685 | 695 |
686 /** | 696 /** |
687 * Processes the current document and applies all rules to it. | 697 * Processes the current document and applies all rules to it. |
688 * @param {CSSStyleSheet[]} [stylesheets] | 698 * @param {CSSStyleSheet[]} [stylesheets] |
689 * The list of new stylesheets that have been added to the document and | 699 * The list of new stylesheets that have been added to the document and |
690 * made reprocessing necessary. This parameter shouldn't be passed in for | 700 * made reprocessing necessary. This parameter shouldn't be passed in for |
691 * the initial processing, all of document's stylesheets will be considered | 701 * the initial processing, all of document's stylesheets will be considered |
692 * then and all rules, including the ones not dependent on styles. | 702 * then and all rules, including the ones not dependent on styles. |
693 * @param {MutationRecord[]} [mutations] | 703 * @param {MutationRecord[]} [mutations] |
694 * The list of DOM mutations that have been applied to the document and | 704 * The list of DOM mutations that have been applied to the document and |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
810 { | 820 { |
811 setTimeout(processPatterns, 0); | 821 setTimeout(processPatterns, 0); |
812 return; | 822 return; |
813 } | 823 } |
814 } | 824 } |
815 pattern = null; | 825 pattern = null; |
816 return processPatterns(); | 826 return processPatterns(); |
817 }; | 827 }; |
818 | 828 |
819 processPatterns(); | 829 processPatterns(); |
820 }, | 830 } |
821 | 831 |
822 // This property is only used in the tests | 832 // This property is only used in the tests |
823 // to shorten the invocation interval | 833 // to shorten the invocation interval |
824 get MIN_INVOCATION_INTERVAL() | 834 get MIN_INVOCATION_INTERVAL() |
825 { | 835 { |
826 return MIN_INVOCATION_INTERVAL; | 836 return MIN_INVOCATION_INTERVAL; |
827 }, | 837 } |
828 | 838 |
829 set MIN_INVOCATION_INTERVAL(interval) | 839 set MIN_INVOCATION_INTERVAL(interval) |
830 { | 840 { |
831 MIN_INVOCATION_INTERVAL = interval; | 841 MIN_INVOCATION_INTERVAL = interval; |
832 }, | 842 } |
833 | |
834 _filteringInProgress: false, | |
835 _lastInvocation: -MIN_INVOCATION_INTERVAL, | |
836 _scheduledProcessing: null, | |
837 | 843 |
838 /** | 844 /** |
839 * Re-run filtering either immediately or queued. | 845 * Re-run filtering either immediately or queued. |
840 * @param {CSSStyleSheet[]} [stylesheets] | 846 * @param {CSSStyleSheet[]} [stylesheets] |
841 * new stylesheets to be processed. This parameter should be omitted | 847 * new stylesheets to be processed. This parameter should be omitted |
842 * for full reprocessing. | 848 * for full reprocessing. |
843 * @param {MutationRecord[]} [mutations] | 849 * @param {MutationRecord[]} [mutations] |
844 * new DOM mutations to be processed. This parameter should be omitted | 850 * new DOM mutations to be processed. This parameter should be omitted |
845 * for full reprocessing. | 851 * for full reprocessing. |
846 */ | 852 */ |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
908 this._scheduledProcessing = null; | 914 this._scheduledProcessing = null; |
909 this._addSelectors(params.stylesheets, params.mutations, completion); | 915 this._addSelectors(params.stylesheets, params.mutations, completion); |
910 }; | 916 }; |
911 this.document.addEventListener("DOMContentLoaded", handler); | 917 this.document.addEventListener("DOMContentLoaded", handler); |
912 } | 918 } |
913 else | 919 else |
914 { | 920 { |
915 this._filteringInProgress = true; | 921 this._filteringInProgress = true; |
916 this._addSelectors(stylesheets, mutations, completion); | 922 this._addSelectors(stylesheets, mutations, completion); |
917 } | 923 } |
918 }, | 924 } |
919 | 925 |
920 onLoad(event) | 926 onLoad(event) |
921 { | 927 { |
922 let stylesheet = event.target.sheet; | 928 let stylesheet = event.target.sheet; |
923 if (stylesheet) | 929 if (stylesheet) |
924 this.queueFiltering([stylesheet]); | 930 this.queueFiltering([stylesheet]); |
925 }, | 931 } |
926 | 932 |
927 observe(mutations) | 933 observe(mutations) |
928 { | 934 { |
929 if (testInfo) | 935 if (testInfo) |
930 { | 936 { |
931 // In test mode, filter out any mutations likely done by us | 937 // In test mode, filter out any mutations likely done by us |
932 // (i.e. style="display: none !important"). This makes it easier to | 938 // (i.e. style="display: none !important"). This makes it easier to |
933 // observe how the code responds to DOM mutations. | 939 // observe how the code responds to DOM mutations. |
934 mutations = mutations.filter( | 940 mutations = mutations.filter( |
935 ({type, attributeName, target: {style: newValue}, oldValue}) => | 941 ({type, attributeName, target: {style: newValue}, oldValue}) => |
936 !(type == "attributes" && attributeName == "style" && | 942 !(type == "attributes" && attributeName == "style" && |
937 newValue.display == "none" && oldValue.display != "none") | 943 newValue.display == "none" && oldValue.display != "none") |
938 ); | 944 ); |
939 | 945 |
940 if (mutations.length == 0) | 946 if (mutations.length == 0) |
941 return; | 947 return; |
942 } | 948 } |
943 | 949 |
944 this.queueFiltering(null, mutations); | 950 this.queueFiltering(null, mutations); |
945 }, | 951 } |
946 | 952 |
947 apply(patterns) | 953 apply(patterns) |
948 { | 954 { |
949 this.patterns = []; | 955 this.patterns = []; |
950 for (let pattern of patterns) | 956 for (let pattern of patterns) |
951 { | 957 { |
952 let selectors = this.parseSelector(pattern.selector); | 958 let selectors = this.parseSelector(pattern.selector); |
953 if (selectors != null && selectors.length > 0) | 959 if (selectors != null && selectors.length > 0) |
954 this.patterns.push(new Pattern(selectors, pattern.text)); | 960 this.patterns.push(new Pattern(selectors, pattern.text)); |
955 } | 961 } |
956 | 962 |
957 if (this.patterns.length > 0) | 963 if (this.patterns.length > 0) |
958 { | 964 { |
959 this.queueFiltering(); | 965 this.queueFiltering(); |
960 this.observer.observe( | 966 this.observer.observe( |
961 this.document, | 967 this.document, |
962 { | 968 { |
963 childList: true, | 969 childList: true, |
964 attributes: shouldObserveAttributes(this.patterns), | 970 attributes: shouldObserveAttributes(this.patterns), |
965 characterData: shouldObserveCharacterData(this.patterns), | 971 characterData: shouldObserveCharacterData(this.patterns), |
966 subtree: true | 972 subtree: true |
967 } | 973 } |
968 ); | 974 ); |
969 this.document.addEventListener("load", this.onLoad.bind(this), true); | 975 this.document.addEventListener("load", this.onLoad.bind(this), true); |
970 } | 976 } |
971 } | 977 } |
972 }; | 978 } |
973 | 979 |
974 exports.ElemHideEmulation = ElemHideEmulation; | 980 exports.ElemHideEmulation = ElemHideEmulation; |
OLD | NEW |