Skip to content

Commit

Permalink
Fix 1568 (#1569)
Browse files Browse the repository at this point in the history
* fix: insertBefore should use correct position #1568

* chore: commit changeset
  • Loading branch information
xiaoiver authored Nov 1, 2023
1 parent a07758a commit 51b42d0
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/new-pans-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@antv/g-lite': patch
---

Use correct position when insertBefore.
58 changes: 58 additions & 0 deletions __tests__/unit/dom/element.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,64 @@ describe('DOM Element API', () => {
expect(group1.lastChild).toBe(group3);
});

it('should insertBefore correctly', () => {
const group1 = new Element();
const group2 = new Element();
const group3 = new Element();
const group4 = new Element();
const group5 = new Element();

// 2, 4, 3
group1.append(group2, group3);
group1.insertBefore(group4, group3);
expect(group1.childNodes.length).toBe(3);
expect(group1.firstChild).toBe(group2);
expect(group1.childNodes[1]).toBe(group4);
expect(group1.lastChild).toBe(group3);
group1.removeChildren();
expect(group1.childNodes.length).toBe(0);

// 4, 2, 3
group1.append(group2, group3);
group1.insertBefore(group4, group2);
expect(group1.childNodes.length).toBe(3);
expect(group1.firstChild).toBe(group4);
expect(group1.childNodes[1]).toBe(group2);
expect(group1.lastChild).toBe(group3);
group1.removeChildren();
expect(group1.childNodes.length).toBe(0);

// 2, 3, 4 -> 2, 4, 3
group1.append(group2, group3, group4);
group1.insertBefore(group4, group3);
expect(group1.childNodes.length).toBe(3);
expect(group1.firstChild).toBe(group2);
expect(group1.childNodes[1]).toBe(group4);
expect(group1.lastChild).toBe(group3);
group1.removeChildren();
expect(group1.childNodes.length).toBe(0);

// 2, 3, 4
group1.append(group2, group3);
group1.insertBefore(group4, null);
expect(group1.childNodes.length).toBe(3);
expect(group1.firstChild).toBe(group2);
expect(group1.childNodes[1]).toBe(group3);
expect(group1.lastChild).toBe(group4);
group1.removeChildren();
expect(group1.childNodes.length).toBe(0);

// 2, 3, 4
group1.append(group2, group3);
group1.insertBefore(group4, group5); // non-existed node
expect(group1.childNodes.length).toBe(3);
expect(group1.firstChild).toBe(group2);
expect(group1.childNodes[1]).toBe(group3);
expect(group1.lastChild).toBe(group4);
group1.removeChildren();
expect(group1.childNodes.length).toBe(0);
});

it('should replaceWith correctly', () => {
const group1 = new Element();
const group2 = new Element();
Expand Down
10 changes: 9 additions & 1 deletion packages/g-lite/src/dom/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,16 @@ export class Element<
if (!refChild) {
this.appendChild(newChild);
} else {
if (newChild.parentElement) {
newChild.parentElement.removeChild(newChild);
}

const index = this.childNodes.indexOf(refChild as IChildNode);
this.appendChild(newChild, index - 1);
if (index === -1) {
this.appendChild(newChild);
} else {
this.appendChild(newChild, index);
}
}
return newChild;
}
Expand Down

0 comments on commit 51b42d0

Please sign in to comment.