Skip to content

Commit

Permalink
AG-19871 add probe nodes to the weakset
Browse files Browse the repository at this point in the history
Merge in ADGUARD-FILTERS/tsurlfilter from fix/AG-19871 to master

Squashed commit of the following:

commit 9f20999
Author: Maxim Topciu <mtopciu@adguard.com>
Date:   Mon Feb 20 18:53:21 2023 +0200

    AG-19871 add probe nodes to the weakset
  • Loading branch information
maximtop committed Mar 1, 2023
1 parent 20069bc commit 413989e
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/tsurlfilter/src/content-script/css-hits-counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ class CssHitsCounter {
this.observer.disconnect();
}

/**
* To avoid cases where two css hits counters try to append and remove the
* same elements one after the other, we do not append already met nodes.
*/
const probesWeakSet = new WeakSet();
let timeoutId: number | null = null;
this.observer = new MutationObserver(((mutationRecords) => {
// Collect probe elements, count them, then remove from their targets
Expand All @@ -285,11 +290,20 @@ class CssHitsCounter {

const { target } = mutationRecord;
if (!node.parentNode && target) {
// If this node has been appended to the DOM and counted once, do not add
// it again.
if (probesWeakSet.has(node)) {
return;
}
// Most likely this is a "probe" element that was added and then
// immediately removed from DOM.
// We re-add it and check if any rule matched it
probeElements.push(node);

// To ensure that this "probe" node has only been added once to the DOM,
// we add it to the weak set.
probesWeakSet.add(node);

// CSS rules could be applied to the nodes inside probe element
// that's why we get all child elements of added node
ElementUtils.appendChildren(node, childrenOfProbeElements);
Expand Down

0 comments on commit 413989e

Please sign in to comment.