Skip to content

Commit

Permalink
[feature] allow escaping of URL parameters
Browse files Browse the repository at this point in the history
This allows for literal colons in routes.
Fixes #191.
  • Loading branch information
Brian Edgerton authored and edef1c committed May 3, 2013
1 parent 3746022 commit 045fecd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/director/router.js
Expand Up @@ -103,15 +103,23 @@ function regifyString(str, params) {

str = out += str.substr(last);

var captures = str.match(/:([^\/]+)/ig),
length;

if (captures) {
length = captures.length;
for (var i = 0; i < length; i++) {
str = str.replace(captures[i], paramifyString(captures[i], params));
}
}
var captures = str.match(/:([^\/]+)/ig),
capture,
length;

if (captures) {
length = captures.length;
for (var i = 0; i < length; i++) {
capture = captures[i];
if ( capture.slice(0, 2) === "::" ) {
// This parameter was escaped and should be left in the url as a literal
// Remove the escaping : from the beginning
str = capture.slice( 1 );
} else {
str = str.replace(capture, paramifyString(capture, params));
}
}
}

return str;
}
Expand Down
14 changes: 14 additions & 0 deletions test/server/core/regifystring-test.js
Expand Up @@ -98,6 +98,20 @@ vows.describe('director/core/regifyString').addBatch({
'Should not match "/home/page"': function(result) {
assert.isFalse(result('/home/page'));
}
},
'When using "/folder/::home"': {
topic: function() {
return testRoute('/folder/::home', callback);
},
'Should match "/folder/:home"': function(result) {
assert.isTrue(result('/folder/:home'));
},
'Should not match "/folder/::home"': function(result) {
assert.isFalse(result('/folder/::home'));
},
'Should not match "/folder/abc" (the catchall regexp)': function(result) {
assert.isFalse(result('/folder/abc'));
}
}

}).export(module);

0 comments on commit 045fecd

Please sign in to comment.