diff --git a/app-route.html b/app-route.html index 2165f84..8fc4d6e 100644 --- a/app-route.html +++ b/app-route.html @@ -243,19 +243,14 @@ if (!this.route) { return; } - - var path = this.route.path; + + var path = this.route.path || ""; var pattern = this.pattern; if (!pattern) { return; } - if (!path) { - this.__resetProperties(); - return; - } - var remainingPieces = path.split('/'); var patternPieces = pattern.split('/'); @@ -268,17 +263,25 @@ break; } var pathPiece = remainingPieces.shift(); - - // We don't match this path. - if (!pathPiece && pathPiece !== '') { + var patternPieceOptional = false; + + if (patternPiece.slice(-1) == '?') { + // Optional parameter. + patternPiece = patternPiece.slice(0, -1); + patternPieceOptional = true; + } else if (!pathPiece && pathPiece !== '') { + // No optional parameter and we don't match this path. this.__resetProperties(); return; } - matched.push(pathPiece); + + if (pathPiece !== undefined) { + matched.push(pathPiece); + } if (patternPiece.charAt(0) == ':') { namedMatches[patternPiece.slice(1)] = pathPiece; - } else if (patternPiece !== pathPiece) { + } else if (patternPiece !== pathPiece && !patternPieceOptional) { this.__resetProperties(); return; } diff --git a/test/app-route-optional-params.html b/test/app-route-optional-params.html new file mode 100644 index 0000000..1348dbd --- /dev/null +++ b/test/app-route-optional-params.html @@ -0,0 +1,214 @@ + + + + + app-route-optional-params + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/index.html b/test/index.html index 73039c8..d410212 100644 --- a/test/index.html +++ b/test/index.html @@ -26,7 +26,8 @@ 'app-route.html?wc-ce=true&wc-shadydom=true', 'app-location.html?wc-ce=true&wc-shadydom=true', 'test-observer-app.html?wc-ce=true&wc-shadydom=true', - 'test-app-example-1.html?wc-ce=true&wc-shadydom=true' + 'test-app-example-1.html?wc-ce=true&wc-shadydom=true', + 'app-route-optional-params.html?wc-ce=true&wc-shadydom=true' ]);