Buggy code:
public abstract class ParentNode extends ChildNode { static final long serialVersionUID = 2815829867152120872L ; protected CoreDocumentImpl ownerDocument ; protected ChildNode firstChild = null ; protected transient NodeListCache fNodeListCache = null ; protected ParentNode ( CoreDocumentImpl ownerDocument ) { } public ParentNode ( ) { } public Node cloneNode ( boolean deep ) { } public Document getOwnerDocument ( ) { } CoreDocumentImpl ownerDocument ( ) { } void setOwnerDocument ( CoreDocumentImpl doc ) { } public boolean hasChildNodes ( ) { } public NodeList getChildNodes ( ) { } public Node getFirstChild ( ) { } public Node getLastChild ( ) { } final ChildNode lastChild ( ) { } final void lastChild ( ChildNode node ) { } public Node insertBefore ( Node newChild , Node refChild ) throws DOMException { } Node internalInsertBefore ( Node newChild , Node refChild , boolean replace ) throws DOMException { boolean errorChecking = ownerDocument . errorChecking ; if ( ( newChild . getNodeType ( ) ) == ( Node . DOCUMENT_FRAGMENT_NODE ) ) { if ( errorChecking ) { for ( Node kid = newChild . getFirstChild ( ) ; kid != null ; kid = kid . getNextSibling ( ) ) { if ( ! ( ownerDocument . isKidOK ( this , kid ) ) ) { throw new DOMException ( DOMException . HIERARCHY_REQUEST_ERR , DOMMessageFormatter . formatMessage ( DOM_DOMAIN , "HIERARCHY_REQUEST_ERR" , null ) ) ; } } } while ( newChild . hasChildNodes ( ) ) { insertBefore ( newChild . getFirstChild ( ) , refChild ) ; } return newChild ; } if ( newChild == refChild ) { refChild = refChild . getNextSibling ( ) ; removeChild ( newChild ) ; insertBefore ( newChild , refChild ) ; return newChild ; } if ( needsSyncChildren ( ) ) { synchronizeChildren ( ) ; } if ( errorChecking ) { if ( isReadOnly ( ) ) { throw new DOMException ( DOMException . NO_MODIFICATION_ALLOWED_ERR , DOMMessageFormatter . formatMessage ( DOM_DOMAIN , "NO_MODIFICATION_ALLOWED_ERR" , null ) ) ; } <START_BUG> if ( ( newChild . getOwnerDocument ( ) ) != ( ownerDocument ) ) { <END_BUG> throw new DOMException ( DOMException . WRONG_DOCUMENT_ERR , DOMMessageFormatter . formatMessage ( DOM_DOMAIN , "WRONG_DOCUMENT_ERR" , null ) ) ; } if ( ! ( ownerDocument . isKidOK ( this , newChild ) ) ) { throw new DOMException ( DOMException . HIERARCHY_REQUEST_ERR , DOMMessageFormatter . formatMessage ( DOM_DOMAIN , "HIERARCHY_REQUEST_ERR" , null ) ) ; } if ( ( refChild != null ) && ( ( refChild . getParentNode ( ) ) != ( this ) ) ) { throw new DOMException ( DOMException . NOT_FOUND_ERR , DOMMessageFormatter . formatMessage ( DOM_DOMAIN , "NOT_FOUND_ERR" , null ) ) ; } boolean treeSafe = true ; for ( NodeImpl a = this ; treeSafe && ( a != null ) ; a = a . parentNode ( ) ) { treeSafe = newChild != a ; } if ( ! treeSafe ) { throw new DOMException ( DOMException . HIERARCHY_REQUEST_ERR , DOMMessageFormatter . formatMessage ( DOM_DOMAIN , "HIERARCHY_REQUEST_ERR" , null ) ) ; } } ownerDocument . insertingNode ( this , replace ) ; ChildNode newInternal = ( ( ChildNode ) ( newChild ) ) ; Node oldparent = newInternal . parentNode ( ) ; if ( oldparent != null ) { oldparent . removeChild ( newInternal ) ; } ChildNode refInternal = ( ( ChildNode ) ( refChild ) ) ; newInternal . ownerNode = this ; newInternal . isOwned ( true ) ; if ( ( firstChild ) == null ) { firstChild = newInternal ; newInternal . isFirstChild ( true ) ; newInternal . previousSibling = newInternal ; } else { if ( refInternal == null ) { ChildNode lastChild = firstChild . previousSibling ; lastChild . nextSibling = newInternal ; newInternal . previousSibling = lastChild ; firstChild . previousSibling = newInternal ; } else { if ( refChild == ( firstChild ) ) { firstChild . isFirstChild ( false ) ; newInternal . nextSibling = firstChild ; newInternal . previousSibling = firstChild . previousSibling ; firstChild . previousSibling = newInternal ; firstChild = newInternal ; newInternal . isFirstChild ( true ) ; } else { ChildNode prev = refInternal . previousSibling ; newInternal . nextSibling = refInternal ; prev . nextSibling = newInternal ; refInternal . previousSibling = newInternal ; newInternal . previousSibling = prev ; } } } changed ( ) ; if ( ( fNodeListCache ) != null ) { if ( ( fNodeListCache . fLength ) != ( - 1 ) ) { ( fNodeListCache . fLength ) ++ ; } if ( ( fNodeListCache . fChildIndex ) != ( - 1 ) ) { if ( ( fNodeListCache . fChild ) == refInternal ) { fNodeListCache . fChild = newInternal ; } else { fNodeListCache . fChildIndex = - 1 ; } } } ownerDocument . insertedNode ( this , newInternal , replace ) ; checkNormalizationAfterInsert ( newInternal ) ; return newChild ; } public Node removeChild ( Node oldChild ) throws DOMException { } Node internalRemoveChild ( Node oldChild , boolean replace ) throws DOMException { } public Node replaceChild ( Node newChild , Node oldChild ) throws DOMException { } public String getTextContent ( ) throws DOMException { } void getTextContent ( StringBuffer buf ) throws DOMException { } final boolean hasTextContent ( Node child ) { } public void setTextContent ( String textContent ) throws DOMException { } private int nodeListGetLength ( ) { } public int getLength ( ) { } private Node nodeListItem ( int index ) { } public Node item ( int index ) { } protected final NodeList getChildNodesUnoptimized ( ) { } public void normalize ( ) { } public boolean isEqualNode ( Node arg ) { } public void setReadOnly ( boolean readOnly , boolean deep ) { } protected void synchronizeChildren ( ) { } void checkNormalizationAfterInsert ( ChildNode insertedChild ) { }

Fix:
if ( ( ( newChild . getOwnerDocument ( ) ) != ( ownerDocument ) ) && ( newChild != ( ownerDocument ) ) ) {