nsCOMPtr<nsIDOMNode> anchorNode; res = selection->GetAnchorNode(getter_AddRefs(anchorNode)); if (NS_FAILED(res)) return res; PRInt32 anchorOffset = -1; if (anchorNode) selection->GetAnchorOffset(&anchorOffset);  nsCOMPtr<nsIDOMNode> focusNode; res = selection->GetFocusNode(getter_AddRefs(focusNode)); if (NS_FAILED(res)) return res; PRInt32 focusOffset = -1; if (focusNode) selection->GetFocusOffset(&focusOffset);   if (NS_SUCCEEDED(res) && anchorNode)
nsCOMPtr<nsIDOMNode> selectedNode = GetChildAt(startParent, startOffset); if (NS_FAILED(res)) return NS_OK; if (selectedNode)
nsHTMLEditor::GetSelectedElement(const nsString& aTagName, nsIDOMElement** aReturn){  if (!aReturn )    return NS_ERROR_NULL_POINTER;    // default is null - no element found  *aReturn = nsnull;    nsAutoString TagName = aTagName;  TagName.ToLowerCase();  // Empty string indicates we should match any element tag  PRBool anyTag = (TagName == "");    //Note that this doesn't need to go through the transaction system  nsresult res=NS_ERROR_NOT_INITIALIZED;  //PRBool first=PR_TRUE;  nsCOMPtr<nsIDOMSelection>selection;  res = GetSelection(getter_AddRefs(selection));  if (NS_FAILED(res)) return res;  if (!selection) return NS_ERROR_NULL_POINTER;    PRBool isCollapsed;  selection->GetIsCollapsed(&isCollapsed);  nsCOMPtr<nsIDOMElement> selectedElement;  PRBool bNodeFound = PR_FALSE;  if (IsLink(TagName))  {    // Link tag is a special case - we return the anchor node    //  found for any selection that is totally within a link,    //  included a collapsed selection (just a caret in a link)    nsCOMPtr<nsIDOMNode> anchorNode;    res = selection->GetAnchorNode(getter_AddRefs(anchorNode));    if (NS_FAILED(res)) return res;    PRInt32 anchorOffset = -1;    if (anchorNode)      selection->GetAnchorOffset(&anchorOffset);        nsCOMPtr<nsIDOMNode> focusNode;    res = selection->GetFocusNode(getter_AddRefs(focusNode));    if (NS_FAILED(res)) return res;    PRInt32 focusOffset = -1;    if (focusNode)      selection->GetFocusOffset(&focusOffset);    // Link node must be the same for both ends of selection    if (NS_SUCCEEDED(res) && anchorNode)    {#ifdef DEBUG_cmanske      {      nsAutoString name;      anchorNode->GetNodeName(name);      printf("GetSelectedElement: Anchor node of selection: ");      wprintf(name.GetUnicode());      printf(" Offset: %d\n", anchorOffset);      focusNode->GetNodeName(name);      printf("Focus node of selection: ");      wprintf(name.GetUnicode());      printf(" Offset: %d\n", focusOffset);      }#endif      nsCOMPtr<nsIDOMElement> parentLinkOfAnchor;      res = GetElementOrParentByTagName("href", anchorNode, getter_AddRefs(parentLinkOfAnchor));      // XXX: ERROR_HANDLING  can parentLinkOfAnchor be null?      if (NS_SUCCEEDED(res) && parentLinkOfAnchor)      {        if (isCollapsed)        {          // We have just a caret in the link          bNodeFound = PR_TRUE;        } else if(focusNode)         {  // Link node must be the same for both ends of selection          nsCOMPtr<nsIDOMElement> parentLinkOfFocus;          res = GetElementOrParentByTagName("href", focusNode, getter_AddRefs(parentLinkOfFocus));          if (NS_SUCCEEDED(res) && parentLinkOfFocus == parentLinkOfAnchor)            bNodeFound = PR_TRUE;        }              // We found a link node parent        if (bNodeFound) {          // GetElementOrParentByTagName addref'd this, so we don't need to do it here          *aReturn = parentLinkOfAnchor;          return NS_OK;        }      }      else if (anchorOffset >= 0)  // Check if link node is the only thing selected      {        nsCOMPtr<nsIDOMNode> anchorChild;        anchorChild = GetChildAt(anchorNode,anchorOffset);        if (anchorChild && IsLinkNode(anchorChild) &&             (anchorNode == focusNode) && focusOffset == (anchorOffset+1))        {          selectedElement = do_QueryInterface(anchorChild);          bNodeFound = PR_TRUE;        }      }    }  }   if (!bNodeFound && !isCollapsed)   // Don't bother to examine selection if it is collapsed  {    nsCOMPtr<nsIEnumerator> enumerator;    res = selection->GetEnumerator(getter_AddRefs(enumerator));    if (NS_SUCCEEDED(res))    {      if(!enumerator)        return NS_ERROR_NULL_POINTER;      enumerator->First();       nsCOMPtr<nsISupports> currentItem;      res = enumerator->CurrentItem(getter_AddRefs(currentItem));      if ((NS_SUCCEEDED(res)) && currentItem)      {        nsCOMPtr<nsIDOMRange> range( do_QueryInterface(currentItem) );        nsCOMPtr<nsIContentIterator> iter;        res = nsComponentManager::CreateInstance(kCContentIteratorCID, nsnull,                                                    NS_GET_IID(nsIContentIterator),                                                     getter_AddRefs(iter));        // XXX: ERROR_HANDLING  XPCOM usage        if ((NS_SUCCEEDED(res)) && iter)        {          iter->Init(range);          // loop through the content iterator for each content node          nsCOMPtr<nsIContent> content;          while (NS_ENUMERATOR_FALSE == iter->IsDone())          {            res = iter->CurrentNode(getter_AddRefs(content));            // Note likely!            if (NS_FAILED(res))              return NS_ERROR_FAILURE;             // Query interface to cast nsIContent to nsIDOMNode             //  then get tagType to compare to  aTagName             // Clone node of each desired type and append it to the aDomFrag            selectedElement = do_QueryInterface(content);            if (selectedElement)            {              // If we already found a node, then we have another element,              //  thus there's not just one element selected              if (bNodeFound)              {                bNodeFound = PR_FALSE;                break;              }              nsAutoString domTagName;              selectedElement->GetNodeName(domTagName);              domTagName.ToLowerCase();              if (anyTag)              {                // Get name of first selected element                selectedElement->GetTagName(TagName);                TagName.ToLowerCase();                anyTag = PR_FALSE;              }              // The "A" tag is a pain,              //  used for both link(href is set) and "Named Anchor"              nsCOMPtr<nsIDOMNode> selectedNode = do_QueryInterface(selectedElement);              if ( (IsLink(TagName) && IsLinkNode(selectedNode)) ||                   (IsNamedAnchor(TagName) && IsNamedAnchorNode(selectedNode)) )              {                bNodeFound = PR_TRUE;              } else if (TagName == domTagName) { // All other tag names are handled here                bNodeFound = PR_TRUE;              }              if (!bNodeFound)              {                // Check if node we have is really part of the selection???                break;              }            }            iter->Next();          }        }      } else {        // Should never get here?        isCollapsed = PR_TRUE;        printf("isCollapsed was FALSE, but no elements found in selection\n");      }    } else {      printf("Could not create enumerator for GetSelectionProperties\n");    }  }  if (bNodeFound)  {        *aReturn = selectedElement;    if (selectedElement)    {        // Getters must addref      NS_ADDREF(*aReturn);    }  }   else res = NS_EDITOR_ELEMENT_NOT_FOUND;  return res;}