MutationObserver how to get a snapshot of DOM for specific mutation of stack array

386 Views Asked by At

Having some problems with MutationObserver. While observing the document, which contains contenteditable elements, MO catches all the document.execCommand
invocations. Everything is good, but sometimes, dom endures some complicated mutations, so, observer-function receives a very big array of step-by-step mutations:

enter image description here

The problem is if I will choose, for example, one of the first mutations (like mutation.type=="attributes") of the stack array, and it's mutation.target dom-element was de facto moved through the dom-tree to other parent OR it was actually removed from the DOM (move/remove event may be below at the same stack array), when I am trying to get, for example, mutation.target.parent or mutation.target.previousSiblingElement properties of the former chosen mutation (these properties are unlikely not passed inside the MutationRecord object), I am receiving the result of final document dom-tree (when the mutation.target is already moved/removed from the DOM), because all the MutationRecord's in a mutations array are already happened, so, all the properties of (HTMLElement)mutation.target excepting the latest, are already likely may be not relevant, due to the probability that current mutations array contains events of the same (HTMLElement)mutation.target affection.

I am writing an undo/redo dispatcher, so, sometimes, I need to write down all the changes from the mutations stack array with an affecting element's parent to the memory by "forEaching" the mutations array, so, the problem which is described above makes things much times complicated to solve the problem!

0

There are 0 best solutions below