Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsplatform/nativescript/renderer/ViewNode.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp