node->QueryInterface(NS_GET_IID(nsIContent),getter_AddRefs(content));
content = do_QueryInterface(node);
PresShell::GoToAnchor(const nsAString& aAnchorName, PRBool aScroll){  nsCOMPtr<nsIEventStateManager> esm;  mPresContext->GetEventStateManager(getter_AddRefs(esm));  if (aAnchorName.IsEmpty()) {    NS_ASSERTION(!aScroll, "can't scroll to empty anchor name");    if (esm) {      esm->SetContentState(nsnull, NS_EVENT_STATE_URLTARGET);    }    return NS_OK;  }  nsCOMPtr<nsIDOMDocument> doc = do_QueryInterface(mDocument);  nsCOMPtr<nsIDOMHTMLDocument> htmlDoc = do_QueryInterface(mDocument);  nsresult rv = NS_OK;  nsCOMPtr<nsIContent> content;  // Search for an element with a matching "id" attribute  if (doc) {        nsCOMPtr<nsIDOMElement> element;    rv = doc->GetElementById(aAnchorName, getter_AddRefs(element));    if (NS_SUCCEEDED(rv) && element) {      // Get the nsIContent interface, because that's what we need to      // get the primary frame      content = do_QueryInterface(element);    }  }  // Search for an anchor element with a matching "name" attribute  if (!content && htmlDoc) {    nsCOMPtr<nsIDOMNodeList> list;    // Find a matching list of named nodes    rv = htmlDoc->GetElementsByName(aAnchorName, getter_AddRefs(list));    if (NS_SUCCEEDED(rv) && list) {      PRUint32 i;      // Loop through the named nodes looking for the first anchor      for (i = 0; PR_TRUE; i++) {        nsCOMPtr<nsIDOMNode> node;        rv = list->Item(i, getter_AddRefs(node));        if (!node) {  // End of list          break;        }        // Ensure it's an anchor element        content = do_QueryInterface(node);        if (content) {          nsCOMPtr<nsIAtom> tag;          content->GetTag(*getter_AddRefs(tag));          if (tag == nsHTMLAtoms::a) {            break;          }          content = nsnull;        }      }    }  }  // Search for anchor in the HTML namespace with a matching name  if (!content && !htmlDoc)  {    nsCOMPtr<nsIDOMNodeList> list;    NS_NAMED_LITERAL_STRING(nameSpace, "http://www.w3.org/1999/xhtml");    // Get the list of anchor elements    rv = doc->GetElementsByTagNameNS(nameSpace, NS_LITERAL_STRING("a"), getter_AddRefs(list));    if (NS_SUCCEEDED(rv) && list) {      PRUint32 i;      // Loop through the named nodes looking for the first anchor      for (i = 0; PR_TRUE; i++) {        nsCOMPtr<nsIDOMNode> node;        rv = list->Item(i, getter_AddRefs(node));        if (!node) { // End of list          break;        }        // Compare the name attribute        nsCOMPtr<nsIDOMElement> element = do_QueryInterface(node);        nsAutoString value;        if (element && NS_SUCCEEDED(element->GetAttribute(NS_LITERAL_STRING("name"), value))) {          if (value.Equals(aAnchorName)) {            content = do_QueryInterface(element);            break;          }        }      }    }  }  // Finally try FIXptr  nsCOMPtr<nsIDOMRange> jumpToRange;  if (!content) {    nsCOMPtr<nsIDOMXMLDocument> xmldoc = do_QueryInterface(mDocument);    if (xmldoc) {      xmldoc->EvaluateFIXptr(aAnchorName,getter_AddRefs(jumpToRange));      if (jumpToRange) {        nsCOMPtr<nsIDOMNode> node;        jumpToRange->GetStartContainer(getter_AddRefs(node));        if (node) {          node->QueryInterface(NS_GET_IID(nsIContent),getter_AddRefs(content));        }      }    }  }   if (esm) {    esm->SetContentState(content, NS_EVENT_STATE_URLTARGET);  }  if (content) {    // Get the primary frame    nsIFrame* frame = nsnull;    if (aScroll &&        NS_SUCCEEDED(GetPrimaryFrameFor(content, &frame)) &&        frame) {      rv = ScrollFrameIntoView(frame, NS_PRESSHELL_SCROLL_TOP,                               NS_PRESSHELL_SCROLL_ANYWHERE);      if (NS_SUCCEEDED(rv)) {        // Should we select the target?        // This action is controlled by a preference: the default is to not select.        PRBool selectAnchor = PR_FALSE;        nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID));        if (prefBranch) {          prefBranch->GetBoolPref("layout.selectanchor", &selectAnchor);        }        // Even if select anchor pref is false, we must still move the caret there.        // That way tabbing will start from the new location        if (!jumpToRange) {          jumpToRange = do_CreateInstance(kRangeCID);          nsCOMPtr<nsIDOMNode> node(do_QueryInterface(content));          if (jumpToRange && node)             jumpToRange->SelectNode(node);        }        if (jumpToRange) {          if (!selectAnchor)            jumpToRange->Collapse(PR_TRUE);          SelectRange(jumpToRange);        }                if (esm) {          PRBool isSelectionWithFocus;          esm->MoveFocusToCaret(PR_TRUE, &isSelectionWithFocus);        }      }    }  } else {    rv = NS_ERROR_FAILURE; //changed to NS_OK in quirks mode if ScrollTo is called        // Scroll to the top/left if the anchor can not be    // found and it is labelled top (quirks mode only). @see bug 80784    nsCompatibility compatMode;    mPresContext->GetCompatibilityMode(&compatMode);       if ((NS_LossyConvertUCS2toASCII(aAnchorName).EqualsIgnoreCase("top")) &&        (compatMode == eCompatibility_NavQuirks)) {      rv = NS_OK;      // Check |aScroll| after setting |rv| so we set |rv| to the same      // thing whether or not |aScroll| is true.      if (aScroll && mViewManager) {        // Get the viewport scroller        nsIScrollableView* scrollingView;        mViewManager->GetRootScrollableView(&scrollingView);        if (scrollingView) {          // Scroll to the top of the page          scrollingView->ScrollTo(0, 0, NS_VMREFRESH_IMMEDIATE);        }      }    }  }  return rv;}