From f18a129c4bbf7d881c386e253f09f25db0ddeafb Mon Sep 17 00:00:00 2001 From: Nikita Gazarov Date: Mon, 30 Dec 2019 00:32:20 -0800 Subject: [PATCH] Fix: ParentNode.appendChild can now move the child from a different parent --- .../com/raquo/laminar/nodes/ParentNode.scala | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/scala/com/raquo/laminar/nodes/ParentNode.scala b/src/main/scala/com/raquo/laminar/nodes/ParentNode.scala index 64838559..bc3e1193 100644 --- a/src/main/scala/com/raquo/laminar/nodes/ParentNode.scala +++ b/src/main/scala/com/raquo/laminar/nodes/ParentNode.scala @@ -11,7 +11,10 @@ trait ParentNode[+Ref <: dom.Element] extends ReactiveNode[Ref] { @inline def maybeChildren: Option[mutable.Buffer[ChildNode.Base]] = _maybeChildren - /** @return Whether child was successfully appended */ + /** Note: can also be used to move children, even within the same parent + * + * @return Whether child was successfully appended + */ def appendChild(child: ChildNode.Base): Boolean = { val nextParent = Some(this) child.willSetParent(nextParent) @@ -20,7 +23,12 @@ trait ParentNode[+Ref <: dom.Element] extends ReactiveNode[Ref] { val appended = DomApi.appendChild(parent = this, child = child) if (appended) { - // 2. Update this node + // 2A. Update child's current parent node + child.maybeParent.foreach { childParent => + childParent._maybeChildren.foreach(childParentChildren => childParentChildren -= child) + } + + // 2B. Update this node if (_maybeChildren.isEmpty) { _maybeChildren = Some(mutable.Buffer(child)) } else { @@ -60,7 +68,8 @@ trait ParentNode[+Ref <: dom.Element] extends ReactiveNode[Ref] { /** Note: can also be used to move children, even within the same parent * - * @return Whether child was successfully inserted */ + * @return Whether child was successfully inserted + */ def insertChild( child: ChildNode.Base, atIndex: Int