From 9faffb9ffcdb9b673a8a993fb2386a094dcfcbdc Mon Sep 17 00:00:00 2001 From: "aaronleventhal%moonset.net" Date: Fri, 12 Nov 2004 21:58:12 +0000 Subject: [PATCH] Bug 258514. Clicked anchor with location hash set to element id gives first child anchor focus rectangle & selection not always updated correctly. r+sr=bryner git-svn-id: svn://10.0.0.236/trunk@165323 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/layout/base/nsPresShell.cpp | 97 ++++++++++---------- mozilla/layout/html/base/src/nsPresShell.cpp | 97 ++++++++++---------- 2 files changed, 98 insertions(+), 96 deletions(-) diff --git a/mozilla/layout/base/nsPresShell.cpp b/mozilla/layout/base/nsPresShell.cpp index 21abf1c2674..9dac6662748 100644 --- a/mozilla/layout/base/nsPresShell.cpp +++ b/mozilla/layout/base/nsPresShell.cpp @@ -3969,61 +3969,62 @@ PresShell::GoToAnchor(const nsAString& aAnchorName, PRBool aScroll) if (content) { // Flush notifications so we scroll to the right place if (aScroll) { - mDocument->FlushPendingNotifications(Flush_Layout); - } - - // Get the primary frame - nsIFrame* frame = nsnull; - if (aScroll && - NS_SUCCEEDED(GetPrimaryFrameFor(content, &frame)) && - frame) { + 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); + } - if (NS_SUCCEEDED(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"); + // 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); - nsCOMPtr node(do_QueryInterface(content)); - if (jumpToRange && node) - jumpToRange->SelectNode(node); + // 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); } - if (jumpToRange) { - if (!selectAnchor) - jumpToRange->Collapse(PR_TRUE); - - nsCOMPtr sel; - if (NS_SUCCEEDED( - GetSelection(nsISelectionController::SELECTION_NORMAL, - getter_AddRefs(sel))) && - sel) { - sel->RemoveAllRanges(); - sel->AddRange(jumpToRange); - } - - 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 range; - xpointerResult->Item(i, getter_AddRefs(range)); - sel->AddRange(range); - } - } - } - - PRBool isSelectionWithFocus; - esm->ChangeFocusWith(nsnull, nsIEventStateManager::eEventFocusedByApplication); + nsCOMPtr node(do_QueryInterface(content)); + NS_ASSERTION(node, "No nsIDOMNode for descendent of anchor"); + jumpToRange->SelectNodeContents(node); } } + if (jumpToRange) { + // Select the anchor + nsCOMPtr 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 range; + xpointerResult->Item(i, getter_AddRefs(range)); + sel->AddRange(range); + } + } + // Selection is at anchor, but put focus on the document + esm->ChangeFocusWith(nsnull, nsIEventStateManager::eEventFocusedByApplication); + } } else { rv = NS_ERROR_FAILURE; //changed to NS_OK in quirks mode if ScrollTo is called diff --git a/mozilla/layout/html/base/src/nsPresShell.cpp b/mozilla/layout/html/base/src/nsPresShell.cpp index 21abf1c2674..9dac6662748 100644 --- a/mozilla/layout/html/base/src/nsPresShell.cpp +++ b/mozilla/layout/html/base/src/nsPresShell.cpp @@ -3969,61 +3969,62 @@ PresShell::GoToAnchor(const nsAString& aAnchorName, PRBool aScroll) if (content) { // Flush notifications so we scroll to the right place if (aScroll) { - mDocument->FlushPendingNotifications(Flush_Layout); - } - - // Get the primary frame - nsIFrame* frame = nsnull; - if (aScroll && - NS_SUCCEEDED(GetPrimaryFrameFor(content, &frame)) && - frame) { + 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); + } - if (NS_SUCCEEDED(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"); + // 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); - nsCOMPtr node(do_QueryInterface(content)); - if (jumpToRange && node) - jumpToRange->SelectNode(node); + // 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); } - if (jumpToRange) { - if (!selectAnchor) - jumpToRange->Collapse(PR_TRUE); - - nsCOMPtr sel; - if (NS_SUCCEEDED( - GetSelection(nsISelectionController::SELECTION_NORMAL, - getter_AddRefs(sel))) && - sel) { - sel->RemoveAllRanges(); - sel->AddRange(jumpToRange); - } - - 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 range; - xpointerResult->Item(i, getter_AddRefs(range)); - sel->AddRange(range); - } - } - } - - PRBool isSelectionWithFocus; - esm->ChangeFocusWith(nsnull, nsIEventStateManager::eEventFocusedByApplication); + nsCOMPtr node(do_QueryInterface(content)); + NS_ASSERTION(node, "No nsIDOMNode for descendent of anchor"); + jumpToRange->SelectNodeContents(node); } } + if (jumpToRange) { + // Select the anchor + nsCOMPtr 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 range; + xpointerResult->Item(i, getter_AddRefs(range)); + sel->AddRange(range); + } + } + // Selection is at anchor, but put focus on the document + esm->ChangeFocusWith(nsnull, nsIEventStateManager::eEventFocusedByApplication); + } } else { rv = NS_ERROR_FAILURE; //changed to NS_OK in quirks mode if ScrollTo is called