insertBefore method

  1. @mustCallSuper
Node insertBefore(
  1. Node child,
  2. Node referenceNode
)

Implementation

@mustCallSuper
Node insertBefore(Node child, Node referenceNode) {
  child._ensureOrphan();
  int referenceIndex = childNodes.indexOf(referenceNode);
  if (referenceIndex == -1) {
    return appendChild(child);
  } else {
    child.parentNode = this;
    childNodes.insert(referenceIndex, child);

    // To insert a node into a parent before a child, run step 9 from the spec:
    // 9. Run the children changed steps for parent when inserting a node into a parent.
    // https://dom.spec.whatwg.org/#concept-node-insert
    childrenChanged();

    if (child.isConnected) {
      child.connectedCallback();
    }
    return child;
  }
}