NS_IMETHODIMP nsHTMLEditor::InsertHTML(const nsString& aInputString)
NS_IMETHODIMP nsHTMLEditor::InsertHTML(const nsAString & aInString)
NS_IMETHODIMP nsHTMLEditor::InsertHTML(const nsString& aInputString){  nsAutoEditBatch beginBatching(this);  nsresult res;  nsCOMPtr<nsIDOMNode> parentNode;  PRInt32 offsetOfNewNode;  res = DeleteSelectionAndPrepareToCreateNode(parentNode, offsetOfNewNode);  nsCOMPtr<nsIDOMSelection>selection;  res = GetSelection(getter_AddRefs(selection));  if (NS_FAILED(res)) return res;  if (!selection) return NS_ERROR_NULL_POINTER;    // Get the first range in the selection, for context:  nsCOMPtr<nsIDOMRange> range;  res = selection->GetRangeAt(0, getter_AddRefs(range));  if (NS_FAILED(res))    return res;  nsCOMPtr<nsIDOMNSRange> nsrange (do_QueryInterface(range));  if (!nsrange)    return NS_ERROR_NO_INTERFACE;  nsCOMPtr<nsIDOMDocumentFragment> docfrag;  res = nsrange->CreateContextualFragment(aInputString,                                          getter_AddRefs(docfrag));  if (NS_FAILED(res))  {#ifdef DEBUG    printf("Couldn't create contextual fragment: error was %d\n", res);#endif    return res;  }#if defined(DEBUG_akkana_verbose)  printf("============ Fragment dump :===========\n");  nsCOMPtr<nsIContent> fragc (do_QueryInterface(docfrag));  if (!fragc)    printf("Couldn't get fragment is nsIContent\n");  else    fragc->List(stdout);#endif  // Insert the contents of the document fragment:  nsCOMPtr<nsIDOMNode> fragmentAsNode (do_QueryInterface(docfrag));#define INSERT_FRAGMENT_DIRECTLY 1#ifdef INSERT_FRAGMENT_DIRECTLY  // Make a collapsed range pointing to right after the current selection,  // and let range gravity keep track of where it is so that we can  // set the selection back there after the insert.  // Unfortunately this doesn't work right yet.  nsCOMPtr<nsIDOMRange> saverange;  res = selection->GetRangeAt(0, getter_AddRefs(saverange));  // Insert the node:  res = InsertNode(fragmentAsNode, parentNode, offsetOfNewNode);  // Now collapse the selection to the beginning of what we just inserted;  // would be better to set it to the end.  if (saverange)  {    nsCOMPtr<nsIDOMNode> parent;    PRInt32 offset;		// XXX: ERROR_HANDLING  error return codes are lost    if (NS_SUCCEEDED(saverange->GetEndParent(getter_AddRefs(parent))))      if (NS_SUCCEEDED(saverange->GetEndOffset(&offset)))        selection->Collapse(parent, offset);  }  else    selection->Collapse(parentNode, 0/*offsetOfNewNode*/);#else /* INSERT_FRAGMENT_DIRECTLY */  // Loop over the contents of the fragment:  nsCOMPtr<nsIDOMNode> child;  res = fragmentAsNode->GetFirstChild(getter_AddRefS(child));  if (NS_FAILED(res))  {    printf("GetFirstChild failed!\n");    return res;  }  while (child)  {    res = InsertNode(child, parentNode, offsetOfNewNode++);    if (NS_FAILED(res))      break;    nsCOMPtr<nsIDOMNode> nextSib;    if (NS_FAILED(child->GetNextSibling(getter_AddRefs(nextSib))))      /*break*/;    child = nextSib;  }  if (NS_FAILED(res))    return res;  // Now collapse the selection to the end of what we just inserted:  selection->Collapse(parentNode, offsetOfNewNode);#endif /* INSERT_FRAGMENT_DIRECTLY */  return res;}