All files / roosterjs-content-model-core/lib/coreApi/restoreUndoSnapshot restoreSnapshotHTML.ts

97.73% Statements 43/44
78.57% Branches 33/42
100% Functions 5/5
97.62% Lines 41/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 1101x                         1x       72x 36x   36x   36x 134x 134x   134x 52x   82x   82x 82x   82x 2x   2x 2x       1x   1x 1x     2x         134x     36x 27x   27x 27x               136x   136x 136x 31x   31x 105x 26x       136x             26x 26x 26x 26x   26x                         57x   57x    
import {
    getAllEntityWrappers,
    isBlockEntityContainer,
    isEntityElement,
    isNodeOfType,
    parseEntityFormat,
    reuseCachedElement,
} from 'roosterjs-content-model-dom';
import type { Snapshot, EditorCore, KnownEntityItem } from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export function restoreSnapshotHTML(core: EditorCore, snapshot: Snapshot) {
    const {
        physicalRoot,
        entity: { entityMap },
    } = core;
    let refNode: Node | null = physicalRoot.firstChild;
 
    const body = core.domCreator.htmlToDOM(snapshot.html).body;
 
    for (let currentNode = body.firstChild; currentNode; ) {
        const next = currentNode.nextSibling;
        const originalEntityElement = tryGetEntityElement(entityMap, currentNode);
 
        if (originalEntityElement) {
            refNode = reuseCachedElement(physicalRoot, originalEntityElement, refNode);
        } else {
            physicalRoot.insertBefore(currentNode, refNode);
 
            Eif (isNodeOfType(currentNode, 'ELEMENT_NODE')) {
                const childEntities = getAllEntityWrappers(currentNode);
 
                childEntities.forEach(element => {
                    const wrapper = tryGetEntityElement(entityMap, element);
 
                    Eif (wrapper) {
                        if (wrapper == refNode) {
                            // In case the node we are moving is just the ref node,
                            // We create a temporary clone and insert it before the refNode, and use this cloned node as refNode
                            // After replaceChild(), the original refNode will be moved away
                            const markerNode = wrapper.cloneNode();
 
                            physicalRoot.insertBefore(markerNode, refNode);
                            refNode = markerNode;
                        }
 
                        element.parentNode?.replaceChild(wrapper, element);
                    }
                });
            }
        }
        currentNode = next;
    }
 
    while (refNode) {
        const next = refNode.nextSibling;
 
        refNode.parentNode?.removeChild(refNode);
        refNode = next;
    }
}
 
function tryGetEntityElement(
    entityMap: Record<string, KnownEntityItem>,
    node: Node
): HTMLElement | null {
    let result: HTMLElement | null = null;
 
    Eif (isNodeOfType(node, 'ELEMENT_NODE')) {
        if (isEntityElement(node)) {
            const format = parseEntityFormat(node);
 
            result = getEntityWrapperForReuse(entityMap, format.id);
        } else if (isBlockEntityContainer(node)) {
            result = tryGetEntityFromContainer(node, entityMap);
        }
    }
 
    return result;
}
 
function tryGetEntityFromContainer(
    element: HTMLElement,
    entityMap: Record<string, KnownEntityItem>
): HTMLElement | null {
    for (let node = element.firstChild; node; node = node.nextSibling) {
        Eif (isEntityElement(node) && isNodeOfType(node, 'ELEMENT_NODE')) {
            const format = parseEntityFormat(node);
            const parent = getEntityWrapperForReuse(entityMap, format.id)?.parentElement;
 
            return isNodeOfType(parent, 'ELEMENT_NODE') && isBlockEntityContainer(parent)
                ? parent
                : null;
        }
    }
 
    return null;
}
 
function getEntityWrapperForReuse(
    entityMap: Record<string, KnownEntityItem>,
    entityId: string | undefined
): HTMLElement | null {
    const entry = entityId ? entityMap[entityId] : undefined;
 
    return entry?.canPersist ? entry.element : null;
}