Skip to content

There is wrong wtih replaceElement(elem: Node, replacement: Node): void  #966

@lamianbu

Description

@lamianbu
export function replaceElement(elem: Node, replacement: Node): void {
    const prev = (replacement.prev = elem.prev);
    if (prev) {
        prev.next = replacement;
    }

    const next = (replacement.next = elem.next);
    if (next) {
        next.prev = replacement;
    }

    const parent = (replacement.parent = elem.parent);
    if (parent) {
        const childs = parent.children;
        childs[childs.lastIndexOf(elem)] = replacement;
    }
}

forgetting to write elem.parent = null

it is very importfant!

if next step is appendChild's innner removeElement

export function removeElement(elem: Node): void {
    if (elem.prev) elem.prev.next = elem.next;
    if (elem.next) elem.next.prev = elem.prev;

    if (elem.parent) {
        const childs = elem.parent.children;
        childs.splice(childs.lastIndexOf(elem), 1);
    }
}

childs.lastIndexOf(elem) is -1, it will delete some element else

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions