code: Node insertChildAt(Node newChild, int index) throws DOMException {
          if (newChild instanceof DocumentFragment) {
              NodeList toAdd = newChild.getChildNodes();
              for (int i = 0; i < toAdd.getLength(); i++) {
                  insertChildAt(toAdd.item(i), index + i);
              }
              return newChild;
          }
  
          LeafNodeImpl toInsert = (LeafNodeImpl) newChild;
          if (toInsert.document != null && document != null && toInsert.document != document) {
              throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null);
          }
          if (toInsert.isParentOf(this)) {
              throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null);
          }
  
          if (toInsert.parent != null) {
              int oldIndex = toInsert.index;
              toInsert.parent.children.remove(oldIndex);
              toInsert.parent.refreshIndices(oldIndex);
          }
  
          children.add(index, toInsert);
          toInsert.parent = this;
          refreshIndices(index);
  
          return newChild;
      }
docs: Inserts {@code newChild} at {@code index}. If it is already child of
  another node, it is removed from there.