move method

void move(
  1. DomRenderObject parent,
  2. Node targetNode,
  3. Node? afterNode
)

Implementation

void move(DomRenderObject parent, web.Node targetNode, web.Node? afterNode) {
  assert(parent == this.parent, 'Cannot move fragment to a different parent.');
  assert(isAttached, 'Cannot move fragment that is not attached to a parent.');

  if (kVerboseMode) {
    print('Move fragment to $targetNode after $afterNode');
  }

  final originalFirstChildNode = firstChildNode;
  if (originalFirstChildNode == null) {
    // If fragment is empty, nothing to move.
    return;
  }

  assert(lastChildNode != null, 'Non-empty attached fragments must have a valid last child reference.');

  if (originalFirstChildNode.previousSibling == afterNode && originalFirstChildNode.parentNode == targetNode) {
    return;
  }

  web.Node? currentNode = lastChildNode;
  web.Node? beforeNode = afterNode == null ? targetNode.childNodes.item(0) : afterNode.nextSibling;

  // Move nodes in reverse order for efficient insertion.
  while (currentNode != null) {
    final prevNode = currentNode != firstChildNode ? currentNode.previousSibling : null;

    // Attach to new parent.
    targetNode.insertBefore(currentNode, beforeNode);

    beforeNode = currentNode;
    currentNode = prevNode;
  }

  assert(
    firstChildNode!.previousSibling == afterNode,
    'First child node should have been placed after the specified node.',
  );
}