Skip to content

Commit

Permalink
fix: fix tree-adapter append replace and remove unexistent child
Browse files Browse the repository at this point in the history
  • Loading branch information
Masquerade-Circus committed Sep 27, 2023
1 parent 88a1cbc commit 992fbd6
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions lib/node/utils/tree-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,30 @@ export class Node implements Node {
constructor() {}

appendChild<T extends Node>(node: T): T {
node.parentNode && node.parentNode.removeChild(node as Node);
this.childNodes.push(node);
node.parentNode = this;
if (node) {
node.parentNode && node.parentNode.removeChild(node as Node);
this.childNodes.push(node);
node.parentNode = this;
}
return node;
}

insertBefore<T extends Node>(node: T, child: Node | null): T {
node.parentNode && node.parentNode.removeChild(node as Node);
node.parentNode = this;
if (child) {
let idx = this.childNodes.indexOf(child);
this.childNodes.splice(idx, 0, node);
} else {
this.childNodes.push(node);
if (node) {
node.parentNode && node.parentNode.removeChild(node as Node);
node.parentNode = this;
if (child) {
let idx = this.childNodes.indexOf(child);
this.childNodes.splice(idx, 0, node);
} else {
this.childNodes.push(node);
}
}
return node;
}

replaceChild<T extends Node>(node: Node, child: T): T {
if (child && child.parentNode === this) {
if (node && child && child.parentNode === this) {
this.insertBefore(node, child);
child.parentNode && child.parentNode.removeChild(child);
}
Expand Down

0 comments on commit 992fbd6

Please sign in to comment.