nsresult rv;  nsCOMPtr<nsIDOMEventTarget> eventTarget; rv = aDragEvent->GetTarget(getter_AddRefs(eventTarget)); if (NS_FAILED(rv)) return rv; nsCOMPtr<nsIDOMNode> domnode = do_QueryInterface(eventTarget);   nsCOMPtr<nsISelection> selection; rv = GetSelection(getter_AddRefs(selection)); if (NS_FAILED(rv)) return rv;   nsCOMPtr<nsISupportsArray> transferableArray; NS_NewISupportsArray(getter_AddRefs(transferableArray)); if (transferableArray == nsnull) return NS_ERROR_OUT_OF_MEMORY;   nsCOMPtr<nsIDragService> dragService = do_GetService("@mozilla.org/widget/dragservice;1", &rv); if (NS_FAILED(rv)) return rv;   nsCOMPtr<nsITransferable> trans = do_CreateInstance(kCTransferableCID); NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);  nsCOMPtr<nsIDOMDocument> domdoc; rv = GetDocument(getter_AddRefs(domdoc)); if (NS_FAILED(rv)) return rv;  nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc); if (doc) {  PRUint32 editorFlags = 0; rv = GetFlags(&editorFlags); if (NS_FAILED(rv)) return rv;  PRBool bIsPlainTextControl = ((editorFlags & eEditorPlaintextMask) != 0);   nsAutoString mimeType; PRUint32 docEncoderFlags = 0; if (bIsPlainTextControl) { docEncoderFlags |= nsIDocumentEncoder::OutputBodyOnly | nsIDocumentEncoder::OutputPreformatted; mimeType = NS_LITERAL_STRING(kUnicodeMime); } else mimeType = NS_LITERAL_STRING(kHTMLMime);   nsCOMPtr<nsIDocumentEncoder> docEncoder = do_CreateInstance(NS_HTMLCOPY_ENCODER_CONTRACTID); NS_ENSURE_TRUE(docEncoder, NS_ERROR_FAILURE);  rv = docEncoder->Init(doc, mimeType, docEncoderFlags); if (NS_FAILED(rv)) return rv;  rv = docEncoder->SetSelection(selection); if (NS_FAILED(rv)) return rv;   nsAutoString buffer, parents, info;  if (!bIsPlainTextControl) {  rv = docEncoder->EncodeToStringWithContext(buffer, parents, info); if (NS_FAILED(rv)) return rv; } else {  rv = docEncoder->EncodeToString(buffer); if (NS_FAILED(rv)) return rv; }   if ( !buffer.IsEmpty() ) { nsCOMPtr<nsISupportsWString> dataWrapper, contextWrapper, infoWrapper;  dataWrapper = do_CreateInstance(NS_SUPPORTS_WSTRING_CONTRACTID); NS_ENSURE_TRUE(dataWrapper, NS_ERROR_FAILURE); rv = dataWrapper->SetData( NS_CONST_CAST(PRUnichar*, buffer.get()) ); if (NS_FAILED(rv)) return rv;  if (bIsPlainTextControl) {  rv = trans->AddDataFlavor(kUnicodeMime); if (NS_FAILED(rv)) return rv;    nsCOMPtr<nsISupports> genericDataObj ( do_QueryInterface(dataWrapper) ); rv = trans->SetTransferData(kUnicodeMime, genericDataObj, buffer.Length() * 2); if (NS_FAILED(rv)) return rv; } else { contextWrapper = do_CreateInstance(NS_SUPPORTS_WSTRING_CONTRACTID); NS_ENSURE_TRUE(contextWrapper, NS_ERROR_FAILURE); infoWrapper = do_CreateInstance(NS_SUPPORTS_WSTRING_CONTRACTID); NS_ENSURE_TRUE(infoWrapper, NS_ERROR_FAILURE);  contextWrapper->SetData ( NS_CONST_CAST(PRUnichar*,parents.get()) ); infoWrapper->SetData ( NS_CONST_CAST(PRUnichar*,info.get()) );  rv = trans->AddDataFlavor(kHTMLMime); if (NS_FAILED(rv)) return rv;  nsCOMPtr<nsIFormatConverter> htmlConverter = do_CreateInstance(kCHTMLFormatConverterCID); NS_ENSURE_TRUE(htmlConverter, NS_ERROR_FAILURE);  rv = trans->SetConverter(htmlConverter); if (NS_FAILED(rv)) return rv;  nsCOMPtr<nsISupports> genericDataObj ( do_QueryInterface(dataWrapper) ); rv = trans->SetTransferData(kHTMLMime, genericDataObj, buffer.Length() * 2); if (NS_FAILED(rv)) return rv;  if (parents.Length()) {  trans->AddDataFlavor(kHTMLContext); genericDataObj = do_QueryInterface(contextWrapper); trans->SetTransferData(kHTMLContext, genericDataObj, parents.Length()*2); } if (info.Length()) {  trans->AddDataFlavor(kHTMLInfo); genericDataObj = do_QueryInterface(infoWrapper); trans->SetTransferData(kHTMLInfo, genericDataObj, info.Length()*2); } }   rv = transferableArray->AppendElement(trans); if (NS_FAILED(rv)) return rv;   unsigned int flags;  flags = nsIDragService::DRAGDROP_ACTION_COPY + nsIDragService::DRAGDROP_ACTION_MOVE;  rv = dragService->InvokeDragSession( domnode, transferableArray, nsnull, flags); if (NS_FAILED(rv)) return rv;  nsCOMPtr<nsIDOMNSEvent> nsevent(do_QueryInterface(aDragEvent));  if (nsevent) { nsevent->PreventBubble(); } } }  return rv;
return nsPlaintextEditor::DoDrag(aDragEvent);
NS_IMETHODIMP nsHTMLEditor::DoDrag(nsIDOMEvent *aDragEvent){  nsresult rv;  nsCOMPtr<nsIDOMEventTarget> eventTarget;  rv = aDragEvent->GetTarget(getter_AddRefs(eventTarget));  if (NS_FAILED(rv)) return rv;  nsCOMPtr<nsIDOMNode> domnode = do_QueryInterface(eventTarget);  /* get the selection to be dragged */  nsCOMPtr<nsISelection> selection;  rv = GetSelection(getter_AddRefs(selection));  if (NS_FAILED(rv)) return rv;  /* create an array of transferables */  nsCOMPtr<nsISupportsArray> transferableArray;  NS_NewISupportsArray(getter_AddRefs(transferableArray));  if (transferableArray == nsnull)    return NS_ERROR_OUT_OF_MEMORY;  /* get the drag service */  nsCOMPtr<nsIDragService> dragService =            do_GetService("@mozilla.org/widget/dragservice;1", &rv);  if (NS_FAILED(rv)) return rv;  /* create html flavor transferable */  nsCOMPtr<nsITransferable> trans = do_CreateInstance(kCTransferableCID);  NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);  nsCOMPtr<nsIDOMDocument> domdoc;  rv = GetDocument(getter_AddRefs(domdoc));  if (NS_FAILED(rv)) return rv;	  nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc);  if (doc)  {    // find out if we're a plaintext control or not    PRUint32 editorFlags = 0;    rv = GetFlags(&editorFlags);    if (NS_FAILED(rv)) return rv;    PRBool bIsPlainTextControl = ((editorFlags & eEditorPlaintextMask) != 0);        // get correct mimeType and document encoder flags set    nsAutoString mimeType;    PRUint32 docEncoderFlags = 0;    if (bIsPlainTextControl)    {      docEncoderFlags |= nsIDocumentEncoder::OutputBodyOnly | nsIDocumentEncoder::OutputPreformatted;      mimeType = NS_LITERAL_STRING(kUnicodeMime);    }    else      mimeType = NS_LITERAL_STRING(kHTMLMime);        // set up docEncoder    nsCOMPtr<nsIDocumentEncoder> docEncoder = do_CreateInstance(NS_HTMLCOPY_ENCODER_CONTRACTID);    NS_ENSURE_TRUE(docEncoder, NS_ERROR_FAILURE);    rv = docEncoder->Init(doc, mimeType, docEncoderFlags);    if (NS_FAILED(rv)) return rv;        rv = docEncoder->SetSelection(selection);    if (NS_FAILED(rv)) return rv;    // grab a string    nsAutoString buffer, parents, info;    if (!bIsPlainTextControl)    {      // encode the selection as html with contextual info      rv = docEncoder->EncodeToStringWithContext(buffer, parents, info);      if (NS_FAILED(rv)) return rv;    }    else    {      // encode the selection      rv = docEncoder->EncodeToString(buffer);      if (NS_FAILED(rv)) return rv;    }    // if we have an empty string, we're done; otherwise continue    if ( !buffer.IsEmpty() )    {      nsCOMPtr<nsISupportsWString> dataWrapper, contextWrapper, infoWrapper;      dataWrapper = do_CreateInstance(NS_SUPPORTS_WSTRING_CONTRACTID);      NS_ENSURE_TRUE(dataWrapper, NS_ERROR_FAILURE);      rv = dataWrapper->SetData( NS_CONST_CAST(PRUnichar*, buffer.get()) );      if (NS_FAILED(rv)) return rv;      if (bIsPlainTextControl)      {         // Add the unicode flavor to the transferable        rv = trans->AddDataFlavor(kUnicodeMime);        if (NS_FAILED(rv)) return rv;        // QI the data object an |nsISupports| so that when the transferable holds        // onto it, it will addref the correct interface.        nsCOMPtr<nsISupports> genericDataObj ( do_QueryInterface(dataWrapper) );        rv = trans->SetTransferData(kUnicodeMime, genericDataObj, buffer.Length() * 2);        if (NS_FAILED(rv)) return rv;      }      else      {        contextWrapper = do_CreateInstance(NS_SUPPORTS_WSTRING_CONTRACTID);        NS_ENSURE_TRUE(contextWrapper, NS_ERROR_FAILURE);        infoWrapper = do_CreateInstance(NS_SUPPORTS_WSTRING_CONTRACTID);        NS_ENSURE_TRUE(infoWrapper, NS_ERROR_FAILURE);        contextWrapper->SetData ( NS_CONST_CAST(PRUnichar*,parents.get()) );        infoWrapper->SetData ( NS_CONST_CAST(PRUnichar*,info.get()) );        rv = trans->AddDataFlavor(kHTMLMime);        if (NS_FAILED(rv)) return rv;        nsCOMPtr<nsIFormatConverter> htmlConverter = do_CreateInstance(kCHTMLFormatConverterCID);        NS_ENSURE_TRUE(htmlConverter, NS_ERROR_FAILURE);        rv = trans->SetConverter(htmlConverter);        if (NS_FAILED(rv)) return rv;        nsCOMPtr<nsISupports> genericDataObj ( do_QueryInterface(dataWrapper) );        rv = trans->SetTransferData(kHTMLMime, genericDataObj, buffer.Length() * 2);        if (NS_FAILED(rv)) return rv;        if (parents.Length())        {          // Add the htmlcontext DataFlavor to the transferable          trans->AddDataFlavor(kHTMLContext);          genericDataObj = do_QueryInterface(contextWrapper);          trans->SetTransferData(kHTMLContext, genericDataObj, parents.Length()*2);        }        if (info.Length())        {          // Add the htmlinfo DataFlavor to the transferable          trans->AddDataFlavor(kHTMLInfo);          genericDataObj = do_QueryInterface(infoWrapper);          trans->SetTransferData(kHTMLInfo, genericDataObj, info.Length()*2);        }      }      /* add the transferable to the array */      rv = transferableArray->AppendElement(trans);      if (NS_FAILED(rv)) return rv;      /* invoke drag */      unsigned int flags;      // in some cases we'll want to cut rather than copy... hmmmmm...      flags = nsIDragService::DRAGDROP_ACTION_COPY + nsIDragService::DRAGDROP_ACTION_MOVE;            rv = dragService->InvokeDragSession( domnode, transferableArray, nsnull, flags);      if (NS_FAILED(rv)) return rv;      nsCOMPtr<nsIDOMNSEvent> nsevent(do_QueryInterface(aDragEvent));      if (nsevent) {        nsevent->PreventBubble();      }    }  }  return rv;}