Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix #809 incorrect ViewNode prev and next siblings after appendChild/insertBefore #811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions __tests__/renderer/ViewNode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,27 @@ describe('ViewNode', () => {
expect(refNode.nextSibling).toBeFalsy()
})

test('insertBefore sets siblings of both siblings', () => {
let parentNode = new ViewNode()
let firstNode = new ViewNode()
let lastNode = new ViewNode()
let childNode = new ViewNode()
parentNode.childNodes = [firstNode, lastNode]
firstNode.parentNode = parentNode
lastNode.parentNode = parentNode

parentNode.insertBefore(childNode, lastNode)

expect(parentNode.childNodes.length).toBe(3)
expect(childNode.parentNode).toEqual(parentNode)
expect(firstNode.nextSibling).toEqual(childNode)
expect(lastNode.prevSibling).toEqual(childNode)
expect(childNode.prevSibling).toEqual(firstNode)
expect(childNode.nextSibling).toEqual(lastNode)
expect(firstNode.prevSibling).toBeFalsy()
expect(lastNode.nextSibling).toBeFalsy()
})

test('appendChild throws if no childNode is given', () => {
let node = new ViewNode()

Expand Down
28 changes: 12 additions & 16 deletions platform/nativescript/renderer/ViewNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,11 @@ export default class ViewNode {
throw new Error(`Can't insert child.`)
}

// in some rare cases insertBefore is called with a null referenceNode
// this makes sure that it get's appended as the last child
if (!referenceNode) {
return this.appendChild(childNode)
}

if (referenceNode.parentNode && referenceNode.parentNode !== this) {
if (
referenceNode &&
referenceNode.parentNode &&
referenceNode.parentNode !== this
) {
throw new Error(
`Can't insert child, because the reference node has a different parent.`
)
Expand All @@ -197,11 +195,18 @@ export default class ViewNode {
// throw new Error(`Can't insert child, because it is already a child.`)
}

// in some rare cases insertBefore is called with a null referenceNode
// this makes sure that it get's appended as the last child
if (!referenceNode) {
return this.appendChild(childNode)
}

let index = this.childNodes.indexOf(referenceNode)

childNode.parentNode = this
childNode.nextSibling = referenceNode
childNode.prevSibling = this.childNodes[index - 1]
if (childNode.prevSibling) childNode.prevSibling.nextSibling = childNode

referenceNode.prevSibling = childNode
this.childNodes.splice(index, 0, childNode)
Expand All @@ -220,16 +225,7 @@ export default class ViewNode {
)
}

if (childNode.parentNode === this) {
// we don't need to throw an error here, because it is a valid case
// for example when switching the order of elements in the tree
// fixes #127 - see for more details
// fixes #240
// throw new Error(`Can't append child, because it is already a child.`)
}

childNode.parentNode = this

if (this.lastChild) {
childNode.prevSibling = this.lastChild
this.lastChild.nextSibling = childNode
Expand Down