diff --git a/platform/nativescript/renderer/ViewNode.js b/platform/nativescript/renderer/ViewNode.js index c5eddb98..571fe523 100644 --- a/platform/nativescript/renderer/ViewNode.js +++ b/platform/nativescript/renderer/ViewNode.js @@ -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.` ) @@ -197,9 +195,17 @@ 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 + if (childNode.prevSibling) childNode.prevSibling.nextSibling = childNode + childNode.nextSibling = referenceNode childNode.prevSibling = this.childNodes[index - 1] @@ -220,16 +226,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