fixes for child nodes

This commit is contained in:
Marvin Kuhl 2024-10-29 10:31:17 +01:00
parent d4d370d92c
commit 793f64223e
2 changed files with 11 additions and 3 deletions

View file

@ -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": {

View file

@ -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);
});
}
}
}