attachChild method

void attachChild(
  1. DomRenderObject child,
  2. DomRenderObject? after, {
  3. Node? startNode,
})
inherited

Implementation

void attachChild(DomRenderObject child, DomRenderObject? after, {web.Node? startNode}) {
  child.parent = this;

  final targetNode = attachTargetNode;

  // Link the child on every path below, including when its nodes are already in place,
  // so that the links follow the order of the children after a move. This happens before
  // resolving the node to attach after: a sibling whose slot did not change is not
  // attached again, so its links can still point at the child's old position until the
  // child is linked.
  _linkChild(child, after);

  final afterNode = getRealNodeOf(after) ?? startNode;

  if (child case DomRenderFragment(isAttached: true)) {
    child.move(this, targetNode, afterNode);
    return;
  }

  try {
    final childNode = child.node;
    if (childNode.previousSibling == afterNode && childNode.parentNode == targetNode) {
      return;
    }

    if (kVerboseMode) {
      print('Attach node $childNode to $targetNode after $afterNode ($after)');
    }

    if (afterNode == null) {
      targetNode.insertBefore(childNode, targetNode.childNodes.item(0));
    } else {
      targetNode.insertBefore(childNode, afterNode.nextSibling);
    }

    if (child is! DomRenderFragment) {
      assert(
        childNode.previousSibling == afterNode,
        'Child node should have been placed after the specified node.',
      );
    } else if (child.firstChildNode != null) {
      assert(
        child.firstChildNode?.previousSibling == afterNode,
        'Fragment first child should have been placed after the specified node.',
      );
    }
  } finally {
    child.finalize();
  }
}