doc->CreateXIF(buffer,sel);
nsCOMPtr<nsIDocumentEncoder> docEncoder;
PresShell::DoCopy(){  nsCOMPtr<nsIDocument> doc;  GetDocument(getter_AddRefs(doc));  if (!doc) return NS_ERROR_FAILURE;    nsString buffer;  nsresult rv;  nsCOMPtr<nsISelection> sel;    nsCOMPtr<nsIEventStateManager> manager;  nsCOMPtr<nsIContent> content;  rv = mPresContext->GetEventStateManager(getter_AddRefs(manager));  if (NS_FAILED(rv) || !manager)    return rv?rv:NS_ERROR_FAILURE;  rv = manager->GetFocusedContent(getter_AddRefs(content));  if (NS_SUCCEEDED(rv) && content)  {    //check to see if we need to get selection from frame    //optimization that MAY need to be expanded as more things implement their own "selection"    nsCOMPtr<nsIDOMNSHTMLInputElement>    htmlInputElement(do_QueryInterface(content));    nsCOMPtr<nsIDOMNSHTMLTextAreaElement> htmlTextAreaElement(do_QueryInterface(content));    if (htmlInputElement || htmlTextAreaElement)    {      nsIFrame *htmlInputFrame;      rv = GetPrimaryFrameFor(content, &htmlInputFrame);      if (NS_FAILED(rv) || !htmlInputFrame)        return rv?rv:NS_ERROR_FAILURE;      nsCOMPtr<nsISelectionController> selCon;      rv = htmlInputFrame->GetSelectionController(mPresContext,getter_AddRefs(selCon));      if (NS_FAILED(rv) || !selCon)        return rv?rv:NS_ERROR_FAILURE;      rv = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(sel));    }  }  if (!sel) //get selection from this PresShell    rv = GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(sel));      if (NS_FAILED(rv) || !sel)    return rv?rv:NS_ERROR_FAILURE;  // Now we have the selection.  Make sure it's nonzero:  PRBool isCollapsed;  sel->GetIsCollapsed(&isCollapsed);  if (isCollapsed)    return NS_OK;  doc->CreateXIF(buffer,sel);  // Get the Clipboard  NS_WITH_SERVICE(nsIClipboard, clipboard, kCClipboardCID, &rv);  if (NS_FAILED(rv))     return rv;  if ( clipboard ) {    // Create a transferable for putting data on the Clipboard    nsCOMPtr<nsITransferable> trans;    rv = nsComponentManager::CreateInstance(kCTransferableCID, nsnull,                                             NS_GET_IID(nsITransferable),                                             getter_AddRefs(trans));    if ( trans ) {      // The data on the clipboard will be in "XIF" format      // so give the clipboard transferable a "XIFConverter" for       // converting from XIF to other formats      nsCOMPtr<nsIFormatConverter> xifConverter;      rv = nsComponentManager::CreateInstance(kCXIFConverterCID, nsnull,                                               NS_GET_IID(nsIFormatConverter),                                              getter_AddRefs(xifConverter));      if ( xifConverter ) {        // Add the XIF DataFlavor to the transferable        // this tells the transferable that it can handle receiving the XIF format        trans->AddDataFlavor(kXIFMime);        // Add the converter for going from XIF to other formats        trans->SetConverter(xifConverter);        // Now add the XIF data to the transferable, placing it into a nsISupportsWString object.        // the transferable wants the number bytes for the data and since it is double byte        // we multiply by 2.         nsCOMPtr<nsISupportsWString> dataWrapper;        rv = nsComponentManager::CreateInstance(NS_SUPPORTS_WSTRING_CONTRACTID,                                                nsnull,                                                 NS_GET_IID(nsISupportsWString),                                                getter_AddRefs(dataWrapper));        if ( dataWrapper ) {          dataWrapper->SetData ( NS_CONST_CAST(PRUnichar*,buffer.GetUnicode()) );          // 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) );          trans->SetTransferData(kXIFMime, genericDataObj, buffer.Length()*2);        }                // put the transferable on the clipboard        clipboard->SetData(trans, nsnull, nsIClipboard::kGlobalClipboard);      }    }  }    // Now that we have copied, update the Paste menu item  nsCOMPtr<nsIScriptGlobalObject> globalObject;  doc->GetScriptGlobalObject(getter_AddRefs(globalObject));    nsCOMPtr<nsIDOMWindowInternal> domWindow = do_QueryInterface(globalObject);  if (domWindow)  {    domWindow->UpdateCommands(NS_ConvertASCIItoUCS2("clipboard"));  }    return NS_OK;}