Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Improve support for URL query parameters #96

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/click_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't make sense... why are you querying by the value of the hash?

if (el != null) {
Rectangle r = el.getBoundingClientRect();
_window.scroll(0, r.top.floor());
}
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 6 additions & 3 deletions test/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down