Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors in path matching in paths with repeated substrings #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 28 additions & 9 deletions path.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,32 @@ var Path = {
}
},
'match': function (path, parameterize) {
function matchPathToRoute(path, route) {
var tokens = route.split('/');
var re = /\/?([^\/]+)/;
var isMatch = true;
var params = {};
var match, routePart;

for(;
isMatch &&
tokens.length &&
path.length &&
(match = re.exec(path));
path = path.substring(match[0].length)) {
var routePart = tokens.shift();
if(routePart.charAt(0) == ':') {
// keep this param
params[routePart.substring(1)] = match[1];
} else if(routePart !== match[1]) {
// non-parameterized part, they must match
isMatch = false;
} // else just continue
}
return(isMatch && (0 === tokens.length) && (0 === path.length)) ?
params : false;
}

var params = {}, route = null, possible_routes, slice, i, j, compare;
for (route in Path.routes.defined) {
if (route !== null && route !== undefined) {
Expand All @@ -61,15 +87,8 @@ var Path = {
for (j = 0; j < possible_routes.length; j++) {
slice = possible_routes[j];
compare = path;
if (slice.search(/:/) > 0) {
for (i = 0; i < slice.split("/").length; i++) {
if ((i < compare.split("/").length) && (slice.split("/")[i].charAt(0) === ":")) {
params[slice.split('/')[i].replace(/:/, '')] = compare.split("/")[i];
compare = compare.replace(compare.split("/")[i], slice.split("/")[i]);
}
}
}
if (slice === compare) {
params = matchPathToRoute(compare, slice);
if (params) {
if (parameterize) {
route.params = params;
}
Expand Down
28 changes: 18 additions & 10 deletions tests/path.js.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
"#E/params/3/check",
"#F",
"#G",
"#H",
"#H/10",
"#H/10/20"
"#H",
"#H/10",
"#H/10/20",
"#I/foran/I/anda/tooth/fora/tooth"
];
var index = 0;
var timer = null;
Expand Down Expand Up @@ -115,11 +116,17 @@
update("G[action - NOT HIT]");
});

Path.map("#H(/:id_one)(/:id_two)").to(function(){
var id_one = this.params["id_one"] || "N/A";
var id_two = this.params["id_two"] || "N/A";
update("H(one=" + id_one + ", two=" + id_two + ")");
});
Path.map("#H(/:id_one)(/:id_two)").to(function(){
var id_one = this.params["id_one"] || "N/A";
var id_two = this.params["id_two"] || "N/A";
update("H(one=" + id_one + ", two=" + id_two + ")");
});

Path.map("#I/foran/:one/anda/tooth/fora/:two").to(function(){
var param_one = this.params["one"];
var param_two = this.params["two"];
update("I(one=" + param_one + ", two=" + param_two + ")");
});

Path.rescue(function(){
update("RESCUE");
Expand Down Expand Up @@ -176,12 +183,13 @@ <h2>Test Suite</h2>
<tr><td>H(one=N/A, two=N/A)</td> <td>Optional parameters with only the require part submitted</td></tr>
<tr><td>H(one=10, two=N/A)</td> <td>Optional parameters with one optional part submitted</td></tr>
<tr><td>H(one=10, two=20)</td> <td>Optional parameters two levels deep</td></tr>
<tr><td>H(one=10, two=N/A)</td> <td>Testing "back" functionality</td></tr>
<tr><td>H(one=10, two=N/A)</td> <td>Testing "back" functionality</td></tr>
<tr><td>I(one=I, two=tooth)</td> <td>Matching with repeated tokens in a path</td></tr>
</table>
</div><br /><br />
<div id="console">
<h3>Expected</h3>
<div id="expected">F[enter]::F[action]::A[enter]::A[action]::A[exit]::B[enter]::B[action]::C[action]::C[exit]::RESCUE::RESCUE::E[enter](parse id=1)::E[action](parse id=1)::E[enter](parse id=2)::E[action](parse id=2)::E[action](check id=3)::E[exit](check id=3)::F[enter]::F[action]::G[enter 1]::G[enter 2]::G[enter 3]::G[enter 4]::H(one=N/A, two=N/A)::H(one=10, two=N/A)::H(one=10, two=20)::H(one=10, two=N/A)</div>
<div id="expected">F[enter]::F[action]::A[enter]::A[action]::A[exit]::B[enter]::B[action]::C[action]::C[exit]::RESCUE::RESCUE::E[enter](parse id=1)::E[action](parse id=1)::E[enter](parse id=2)::E[action](parse id=2)::E[action](check id=3)::E[exit](check id=3)::F[enter]::F[action]::G[enter 1]::G[enter 2]::G[enter 3]::G[enter 4]::H(one=N/A, two=N/A)::H(one=10, two=N/A)::H(one=10, two=20)::I(one=I, two=tooth)::H(one=10, two=20)</div>
<h3>Actual</h3>
<div id="actual"></div>
<h3>Grade</h3>
Expand Down