diff --git a/package.json b/package.json index a6d3c79..583e72d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@digicomp/now-and-later", - "version": "1.0.2", + "version": "1.0.3", "description": "an easy API for DOM MutationObserver", "main": "src/nowAndLater.js", "scripts": { diff --git a/src/nowAndLater.js b/src/nowAndLater.js index 31909a8..9383b46 100644 --- a/src/nowAndLater.js +++ b/src/nowAndLater.js @@ -16,7 +16,11 @@ export default function nowAndLater(selector, callback) const nodes = document.querySelectorAll(selector); const id = Date.now() + '_' + lateInits.length; const wrappedCallback = (node) => { - node.dataset['nowAndLater' + id] = ''; + if (node.dataset.nowAndLater === undefined) { + node.dataset.nowAndLater = id; + } else { + node.dataset.nowAndLater += ',' + id; + } callback(node, selector); }; if (nodes.length > 0) { @@ -35,11 +39,15 @@ const observer = new MutationObserver(function (mutationsList) if (addedNode['querySelector'] === undefined) { // lets filter out text and comment nodes continue; } - const selector = lateInit.selector + ':not([data-now-and-later' + lateInit.id + '])'; + const selector = lateInit.selector + `:not([data-now-and-later~="${lateInit.id}"])`; // If the added node matches itself, it will not be found by querySelectorAll, so we have to check first if (addedNode.matches(selector)) { lateInit.callback(addedNode); } + const childNodes = addedNode.querySelectorAll(selector); + [...childNodes].forEach(node => { + lateInit.callback(node); + }); } } }