fix isMutationBelongsToElement function: make it return true if the whole text node is deleted inside of some descendant of the passed element

This commit is contained in:
Kanstantsin_Vikhor 2024-02-27 19:35:02 +04:00
parent b619946e8f
commit 040760c6b3

View file

@ -7,29 +7,20 @@
export function isMutationBelongsToElement(mutationRecord: MutationRecord, element: Element): boolean {
const { type, target, addedNodes, removedNodes } = mutationRecord;
/**
* In case of removing the whole text in element, mutation type will be 'childList',
* 'removedNodes' will contain text node that is not existed anymore, so we can't check it with 'contains' method
* But Target will be the element itself, so we can detect it.
*/
if (target === element) {
if (element.contains(target)) {
return true;
}
/**
* Check typing and attributes changes
*/
if (['characterData', 'attributes'].includes(type)) {
const targetElement = target.nodeType === Node.TEXT_NODE ? target.parentNode : target;
return element.contains(targetElement);
if (type === 'childList') {
const elementAddedItself = Array.from(addedNodes).some(node => node.contains(element));
if (elementAddedItself) {
return true;
}
const elementRemovedItself = Array.from(removedNodes).some(node => node.contains(element));
if (elementRemovedItself) {
return true;
}
}
/**
* Check new/removed nodes
*/
const addedNodesBelongsToBlock = Array.from(addedNodes).some(node => element.contains(node));
const removedNodesBelongsToBlock = Array.from(removedNodes).some(node => element.contains(node));
return addedNodesBelongsToBlock || removedNodesBelongsToBlock;
return false;
}