Bug 662170 - Navigating to anchor '#' should scroll to top of the page. r=bz

This commit is contained in:
Justin Lebar 2011-06-08 14:08:29 -04:00
parent 0e6b571c88
commit b195c39d86
4 changed files with 75 additions and 8 deletions

View File

@ -8360,11 +8360,7 @@ nsDocShell::InternalLoad(nsIURI * aURI,
// sometimes we might scroll even if we don't fire a hashchange
// event! See bug 653741.
if (!aSHEntry) {
// Take the '#' off the hashes before passing them to
// ScrollToAnchor.
nsDependentCSubstring curHashName(curHash, 1);
nsDependentCSubstring newHashName(newHash, 1);
rv = ScrollToAnchor(curHashName, newHashName, aLoadType);
rv = ScrollToAnchor(curHash, newHash, aLoadType);
NS_ENSURE_SUCCESS(rv, rv);
}
@ -9159,16 +9155,20 @@ nsDocShell::ScrollToAnchor(nsACString & aCurHash, nsACString & aNewHash,
return NS_OK;
}
// Take the '#' off aNewHash to get the ref name. (aNewHash might be empty,
// but that's fine.)
nsDependentCSubstring newHashName(aNewHash, 1);
// Both the new and current URIs refer to the same page. We can now
// browse to the hash stored in the new URI.
if (!aNewHash.IsEmpty()) {
if (!newHashName.IsEmpty()) {
// anchor is there, but if it's a load from history,
// we don't have any anchor jumping to do
PRBool scroll = aLoadType != LOAD_HISTORY &&
aLoadType != LOAD_RELOAD_NORMAL;
char *str = ToNewCString(aNewHash);
char *str = ToNewCString(newHashName);
if (!str) {
return NS_ERROR_OUT_OF_MEMORY;
}
@ -9210,7 +9210,7 @@ nsDocShell::ScrollToAnchor(nsACString & aCurHash, nsACString & aNewHash,
nsXPIDLString uStr;
rv = textToSubURI->UnEscapeAndConvert(PromiseFlatCString(aCharset).get(),
PromiseFlatCString(aNewHash).get(),
PromiseFlatCString(newHashName).get(),
getter_Copies(uStr));
NS_ENSURE_SUCCESS(rv, rv);

View File

@ -109,6 +109,8 @@ _TEST_FILES = \
test_bug660404.html \
file_bug660404 \
file_bug660404^headers^ \
test_bug662170.html \
file_bug662170.html \
$(NULL)
ifeq ($(MOZ_WIDGET_TOOLKIT),cocoa)

View File

@ -0,0 +1,13 @@
<html>
<body onload='(parent || opener).childLoad()'>
<div style='height:500px; background:yellow'>
<a id='#top'>Top of the page</a>
</div>
<div id='bottom'>
<a id='#bottom'>Bottom of the page</a>
</div>
</body>
</html>

View File

@ -0,0 +1,52 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=662170
-->
<head>
<title>Test for Bug 662170</title>
<script type="application/javascript" src="/MochiKit/packed.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/WindowSnapshot.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=662170">Mozilla Bug 662170</a>
<script type="application/javascript;version=1.7">
/** Test for Bug 662170 **/
SimpleTest.waitForExplicitFinish();
function childLoad() {
// Spin the event loop so we leave the onload handler.
SimpleTest.executeSoon(childLoad2);
}
function childLoad2() {
let cw = $('iframe').contentWindow;
// When we initially load the page, we should be at the top.
is(cw.pageYOffset, 0, 'Initial Y offset should be 0.');
// Scroll the iframe to the bottom.
cw.scrollTo(0, 300);
// Did we actually scroll somewhere?
isnot(cw.pageYOffset, 0, 'Y offset should be non-zero after scrolling.');
// Now load file_bug662170.html#, which should take us to the top of the
// page.
cw.location = cw.location + '#';
is(cw.pageYOffset, 0, 'Correct Y offset after loading #.');
SimpleTest.finish();
}
</script>
<!-- When the iframe loads, it calls childLoad(). -->
<iframe height='100px' id='iframe' src='file_bug662170.html'></iframe>
</body>
</html>