if (SameCOMIdentity(win, focusedWin)) {
if (SameCOMIdentity(win->GetOuterWindow(), focusedWin)) {
PresShell::GoToAnchor(const nsAString& aAnchorName, PRBool aScroll){  if (!mDocument) {    return NS_ERROR_FAILURE;  }    // Hold a reference to the ESM in case event dispatch tears us down.  nsCOMPtr<nsIEventStateManager> esm = mPresContext->EventStateManager();  if (aAnchorName.IsEmpty()) {    NS_ASSERTION(!aScroll, "can't scroll to empty anchor name");    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) {          if (content->Tag() == nsHTMLAtoms::a &&              content->IsContentOfType(nsIContent::eHTML)) {            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;          }        }      }    }  }  nsCOMPtr<nsIDOMRange> jumpToRange;  nsCOMPtr<nsIXPointerResult> xpointerResult;  if (!content) {    nsCOMPtr<nsIDOMXMLDocument> xmldoc = do_QueryInterface(mDocument);    if (xmldoc) {      // Try XPointer      xmldoc->EvaluateXPointer(aAnchorName, getter_AddRefs(xpointerResult));      if (xpointerResult) {        xpointerResult->Item(0, getter_AddRefs(jumpToRange));        if (!jumpToRange) {          // We know it was an XPointer, so there is no point in          // trying any other pointer types, let's just return          // an error.          return NS_ERROR_FAILURE;        }      }      // Finally try FIXptr      if (!jumpToRange) {        xmldoc->EvaluateFIXptr(aAnchorName,getter_AddRefs(jumpToRange));      }      if (jumpToRange) {        nsCOMPtr<nsIDOMNode> node;        jumpToRange->GetStartContainer(getter_AddRefs(node));        if (node) {          PRUint16 nodeType;          node->GetNodeType(&nodeType);          PRInt32 offset = -1;          jumpToRange->GetStartOffset(&offset);          switch (nodeType) {            case nsIDOMNode::ATTRIBUTE_NODE:            {              // XXX Assuming jumping to the ownerElement is the sanest action.              nsCOMPtr<nsIAttribute> attr = do_QueryInterface(node);              content = attr->GetContent();              break;            }            case nsIDOMNode::DOCUMENT_NODE:            {              if (offset >= 0) {                nsCOMPtr<nsIDocument> document = do_QueryInterface(node);                content = document->GetChildAt(offset);              }              break;            }            case nsIDOMNode::DOCUMENT_FRAGMENT_NODE:            case nsIDOMNode::ELEMENT_NODE:            case nsIDOMNode::ENTITY_REFERENCE_NODE:            {              if (offset >= 0) {                nsCOMPtr<nsIContent> parent = do_QueryInterface(node);                content = parent->GetChildAt(offset);              }              break;            }            case nsIDOMNode::CDATA_SECTION_NODE:            case nsIDOMNode::COMMENT_NODE:            case nsIDOMNode::TEXT_NODE:            case nsIDOMNode::PROCESSING_INSTRUCTION_NODE:            {              // XXX This should scroll to a specific position in the text.              content = do_QueryInterface(node);              break;            }          }        }      }    }  }  esm->SetContentState(content, NS_EVENT_STATE_URLTARGET);  if (content) {    // Flush notifications so we scroll to the right place    if (aScroll) {      mDocument->FlushPendingNotifications(Flush_Layout);         // Get the primary frame      nsIFrame* frame = nsnull;      GetPrimaryFrameFor(content, &frame);      NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);      rv = ScrollFrameIntoView(frame, NS_PRESSHELL_SCROLL_TOP,                               NS_PRESSHELL_SCROLL_ANYWHERE);      NS_ENSURE_SUCCESS(rv, rv);    }    // Should we select the target? This action is controlled by a    // preference: the default is to not select.    PRBool selectAnchor = nsContentUtils::GetBoolPref("layout.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);      if (jumpToRange) {        while (content && content->GetChildCount() > 0) {          content = content->GetChildAt(0);        }        nsCOMPtr<nsIDOMNode> node(do_QueryInterface(content));        NS_ASSERTION(node, "No nsIDOMNode for descendent of anchor");        jumpToRange->SelectNodeContents(node);      }    }    if (jumpToRange) {      // Select the anchor      nsCOMPtr<nsISelection> sel;      if (NS_SUCCEEDED(          GetSelection(nsISelectionController::SELECTION_NORMAL,                        getter_AddRefs(sel))) &&          sel) {        sel->RemoveAllRanges();        sel->AddRange(jumpToRange);        if (!selectAnchor) {          // Use a caret (collapsed selection) at the start of the anchor          sel->CollapseToStart();        }      }        if (selectAnchor && xpointerResult) {        // Select the rest (if any) of the ranges in XPointerResult        PRUint32 count, i;        xpointerResult->GetLength(&count);        for (i = 1; i < count; i++) { // jumpToRange is i = 0          nsCOMPtr<nsIDOMRange> range;          xpointerResult->Item(i, getter_AddRefs(range));          sel->AddRange(range);        }      }      // Selection is at anchor.      // Now focus the document itself if focus is on an element within it.      nsCOMPtr<nsPIDOMWindow> win =        do_QueryInterface(mDocument->GetScriptGlobalObject());      if (win) {        nsCOMPtr<nsIFocusController> focusController = win->GetRootFocusController();        if (focusController) {          nsCOMPtr<nsIDOMWindowInternal> focusedWin;          focusController->GetFocusedWindow(getter_AddRefs(focusedWin));          if (SameCOMIdentity(win, focusedWin)) {            esm->ChangeFocusWith(nsnull, nsIEventStateManager::eEventFocusedByApplication);          }        }      }    }  } 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    if ((NS_LossyConvertUCS2toASCII(aAnchorName).LowerCaseEqualsLiteral("top")) &&        (mPresContext->CompatibilityMode() == 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;}