Fix hash link reliability (fix #434)

This commit is contained in:
Benjamin GROENEVELD
2018-10-13 15:09:39 +02:00
parent abcac75826
commit a68c62ce91
3 changed files with 23 additions and 0 deletions

View File

@@ -83,6 +83,10 @@ function handle_click(event: MouseEvent) {
const svg = typeof a.href === 'object' && a.href.constructor.name === 'SVGAnimatedString';
const href = String(svg ? (<SVGAElement>a).href.baseVal : a.href);
if (location.hash && href === location.href) {
return;
}
if (href === location.href) {
event.preventDefault();
return;

View File

@@ -1,3 +1,4 @@
<a href="tall-page#foo">scroll to foo</a>
<div style="height: 9999px"></div>
<div id="foo">

View File

@@ -33,6 +33,24 @@ describe('scroll', function() {
assert.ok(scrollY > 0, scrollY);
});
it('scrolls to any deeplink if it was already active', async () => {
await page.goto(`${base}/tall-page#foo`);
await start();
let scrollY = await page.evaluate(() => window.scrollY);
assert.ok(scrollY > 0, scrollY);
scrollY = await page.evaluate(() => {
window.scrollTo(0, 0)
return window.scrollY
});
assert.ok(scrollY === 0, scrollY);
await page.click('[href="tall-page#foo"]');
scrollY = await page.evaluate(() => window.scrollY);
assert.ok(scrollY > 0, scrollY);
});
it('resets scroll when a link is clicked', async () => {
await page.goto(`${base}/tall-page#foo`);
await start();