From d10d77288c956c3d5246de60e69f4dc7cd38bcba Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 22 May 2014 14:41:21 +0200 Subject: [PATCH] Added support for trailing slashes in routes m.route now matches a path with a trailing slash against a route without one. --- mithril.js | 4 ++-- tests/mithril-tests.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/mithril.js b/mithril.js index 708089c1..e9f9fe53 100644 --- a/mithril.js +++ b/mithril.js @@ -310,11 +310,11 @@ Mithril = m = new function app(window) { for (var route in router) { if (route == path) return !void m.module(root, router[route]) - var matcher = new RegExp("^" + route.replace(/:[^\/]+?\.{3}/g, "(.*?)").replace(/:[^\/]+/g, "([^\\/]+)") + "$") + var matcher = new RegExp("^" + route.replace(/:[^\/]+?\.{3}/g, "(.*?)").replace(/:[^\/]+/g, "([^\\/]+)") + "\/?$") if (matcher.test(path)) { return !void path.replace(matcher, function() { - var keys = route.match(/:[^\/]+/g) + var keys = route.match(/:[^\/]+/g) || [] var values = [].slice.call(arguments, 1, -2) for (var i = 0; i < keys.length; i++) routeParams[keys[i].replace(/:|\./g, "")] = decodeURIComponent(values[i]) m.module(root, router[route]) diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index 7c836372..b8f07fcf 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -573,6 +573,21 @@ function testMithril(mock) { mock.performance.$elapse(50) //teardown return root.childNodes[0].nodeValue === "foo bar" }) + test(function() { + mock.performance.$elapse(50) //setup + mock.location.search = "?" + + var root = mock.document.createElement("div") + m.route.mode = "search" + m.route(root, "/", { + "/": {controller: function() {}, view: function() {return "foo"}}, + "/test11": {controller: function() {}, view: function() {return "bar"}} + }) + mock.performance.$elapse(50) + m.route("/test11/") + mock.performance.$elapse(50) //teardown + return mock.location.search == "?/test11/" && root.childNodes[0].nodeValue === "bar" + }) //end m.route //m.prop