diff --git a/lib/src/route.dart b/lib/src/route.dart index 3b620d57..4103f0b9 100644 --- a/lib/src/route.dart +++ b/lib/src/route.dart @@ -170,36 +170,41 @@ class Route { Iterable handlers: const [], method: "GET", String name: null}) { - final segments = path - .toString() - .split('/') - .where((str) => str.isNotEmpty) - .toList(growable: false); Route result; - if (segments.isEmpty) { - return new Route('/', - children: children, - debug: debug, - handlers: handlers, - method: method, - name: name); - } + if (path is RegExp) { + result = new Route(path, debug: debug); + } else { + final segments = path + .toString() + .split('/') + .where((str) => str.isNotEmpty) + .toList(growable: false); - for (int i = 0; i < segments.length; i++) { - final segment = segments[i]; + if (segments.isEmpty) { + return new Route('/', + children: children, + debug: debug, + handlers: handlers, + method: method, + name: name); + } - if (i == segments.length - 1) { - if (result == null) { - result = new Route(segment, debug: debug); + for (int i = 0; i < segments.length; i++) { + final segment = segments[i]; + + if (i == segments.length - 1) { + if (result == null) { + result = new Route(segment, debug: debug); + } else { + result = result.child(segment, debug: debug); + } } else { - result = result.child(segment, debug: debug); - } - } else { - if (result == null) { - result = new Route(segment, debug: debug, method: "*"); - } else { - result = result.child(segment, debug: debug, method: "*"); + if (result == null) { + result = new Route(segment, debug: debug, method: "*"); + } else { + result = result.child(segment, debug: debug, method: "*"); + } } } } @@ -354,6 +359,8 @@ class Route { /// /// Can be used to navigate a route hierarchy like a file system. Route resolve(String path, {bool filter(Route route), String fullPath}) { + _printDebug( + 'Path to resolve: "/${path.replaceAll(_straySlashes, '')}", our matcher: ${matcher.pattern}'); bool _filter(route) { if (filter == null) { _printDebug('No filter provided, returning true for $route'); @@ -484,15 +491,15 @@ class Route { 'Trying to match full $_fullPath for ${route.path} on ${this.path}'); if ((route.match(_fullPath) != null || route._resolver.firstMatch(_fullPath) != null) && - _filter(route)) + _filter(route)) { + _printDebug('Matched full path!'); return route.resolve(''); - else if ((route.match(_fullPath) != null || - route._resolver.firstMatch(_fullPath) != null) && - _filter(route)) - return route.resolve(''); - else if ((route.match('/$_fullPath') != null || + } else if ((route.match('/$_fullPath') != null || route._resolver.firstMatch('/$_fullPath') != null) && - _filter(route)) return route.resolve(''); + _filter(route)) { + _printDebug('Matched full path (with a leading slash!)'); + return route.resolve(''); + } } // Lastly, check to see if we have an index route to resolve with diff --git a/lib/src/router.dart b/lib/src/router.dart index 274ebf58..2dec5367 100644 --- a/lib/src/router.dart +++ b/lib/src/router.dart @@ -55,7 +55,8 @@ class Router extends Extensible { return new Route('/', debug: debug, handlers: handlers, method: method) ..debug = debug; } else { - result = resolve(segments[0], (route) => route.method == method || route.method == '*'); + result = resolve(segments[0], + (route) => route.method == method || route.method == '*'); if (result != null) { if (segments.length > 1) { @@ -65,7 +66,9 @@ class Router extends Extensible { Route existing; do { - existing = result.resolve(segments[0], filter: (route) => route.method == method || route.method == '*'); + existing = result.resolve(segments[0], + filter: (route) => + route.method == method || route.method == '*'); if (existing != null) { result = existing; @@ -106,29 +109,31 @@ class Router extends Extensible { var tabs = 0; final buf = new StringBuffer(); - void dumpRoute(Route route, {String replace: null}) { + void dumpRoute(Route route, {Pattern replace: null}) { for (var i = 0; i < tabs; i++) buf.write(tab); if (route == root) - buf.write('(root) ${route.method} '); - else + buf.write('(root)'); + else { buf.write('- ${route.method} '); - final p = - replace != null ? route.path.replaceAll(replace, '') : route.path; + final p = + replace != null ? route.path.replaceAll(replace, '') : route.path; - if (p.isEmpty) - buf.write("'/'"); - else - buf.write("'${p.replaceAll(_straySlashes, '')}'"); + if (p.isEmpty) + buf.write("'/'"); + else + buf.write("'${p.replaceAll(_straySlashes, '')}'"); - if (route.handlers.isNotEmpty) - buf.writeln(' => ${route.handlers.length} handler(s)'); - else - buf.writeln(); + if (route.handlers.isNotEmpty) + buf.writeln(' => ${route.handlers.length} handler(s)'); + else + buf.writeln(); + } tabs++; - route.children.forEach((r) => dumpRoute(r, replace: route.path)); + route.children + .forEach((r) => dumpRoute(r, replace: new RegExp("^${route.path}"))); tabs--; } diff --git a/test/server/packages b/test/server/packages new file mode 120000 index 00000000..4b727bf6 --- /dev/null +++ b/test/server/packages @@ -0,0 +1 @@ +../../packages \ No newline at end of file diff --git a/web/hash/packages b/web/hash/packages new file mode 120000 index 00000000..4b727bf6 --- /dev/null +++ b/web/hash/packages @@ -0,0 +1 @@ +../../packages \ No newline at end of file diff --git a/web/packages b/web/packages new file mode 120000 index 00000000..a16c4050 --- /dev/null +++ b/web/packages @@ -0,0 +1 @@ +../packages \ No newline at end of file diff --git a/web/push_state/packages b/web/push_state/packages new file mode 120000 index 00000000..4b727bf6 --- /dev/null +++ b/web/push_state/packages @@ -0,0 +1 @@ +../../packages \ No newline at end of file diff --git a/web/shared/packages b/web/shared/packages new file mode 120000 index 00000000..4b727bf6 --- /dev/null +++ b/web/shared/packages @@ -0,0 +1 @@ +../../packages \ No newline at end of file