diff --git a/lib/click_handler.dart b/lib/click_handler.dart index c8c5a89..ae3a786 100644 --- a/lib/click_handler.dart +++ b/lib/click_handler.dart @@ -48,10 +48,20 @@ class DefaultWindowClickHandler { if (!_linkMatcher.matches(anchor)) { return; } + if (anchor.host == _window.location.host) { e.preventDefault(); - _router.gotoUrl( - _useFragment ? _normalizer(anchor.hash) : '${anchor.pathname}'); + if (_useFragment) { + _router.gotoUrl(_normalizer(anchor.hash)); + } else if (anchor.hash == '') { + _router.gotoUrl('${anchor.pathname}${anchor.search}'); + } else { + Element el = document.querySelector(anchor.hash); + if (el != null) { + Rectangle r = el.getBoundingClientRect(); + _window.scroll(0, r.top.floor()); + } + } } } } diff --git a/lib/client.dart b/lib/client.dart index 6809310..3b00c21 100644 --- a/lib/client.dart +++ b/lib/client.dart @@ -810,7 +810,8 @@ class Router { route(_normalizeHash(_window.location.hash)); } else { String getPath() => - '${_window.location.pathname}${_window.location.hash}'; + '${_window.location.pathname}${_window.location.search}' + '${_window.location.hash}'; _window.onPopState.listen((_) { route(getPath()).then((allowed) { diff --git a/test/client_test.dart b/test/client_test.dart index 76f79d1..2af0e30 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -1691,12 +1691,15 @@ main() { testInit(mockWindow, [count = 1]) { mockWindow.location.when(callsTo('get hash')).alwaysReturn(''); - mockWindow.location.when(callsTo('get pathname')).alwaysReturn('/foo'); + mockWindow.location.when(callsTo('get pathname')).alwaysReturn('/hello'); + mockWindow.location.when(callsTo('get search')).alwaysReturn('?foo=bar&baz=bat'); var router = new Router(useFragment: false, windowImpl: mockWindow); - router.root.addRoute(name: 'foo', path: '/foo'); + router.root.addRoute(name: 'hello', path: '/hello'); router.onRouteStart.listen(expectAsync((RouteStartEvent start) { start.completed.then(expectAsync((_) { - expect(router.findRoute('foo').isActive, isTrue); + expect(router.findRoute('hello').isActive, isTrue); + expect(router.findRoute('hello').queryParameters['baz'], 'bat'); + expect(router.findRoute('hello').queryParameters['foo'], 'bar'); })); }, count: count)); router.listen(ignoreClick: true);