Skip to content

Commit

Permalink
Fix unterminated RegExp groups.
Browse files Browse the repository at this point in the history
Bugfix.
  • Loading branch information
Southern committed Sep 20, 2012
1 parent 559ca26 commit d683eae
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/director/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,25 @@ function regifyString(str, params) {
//
// ### Fix unterminated RegExp groups in routes.
//
function terminator(routes, delimiter) {
function terminator(routes, delimiter, start, stop) {
var last = 0,
left = 0,
right = 0,
start = (start || '(').toString(),
stop = (stop || ')').toString(),
i;

for (i = 0; i < routes.length; i++) {
var chunk = routes[i];
if ((chunk.indexOf('(', last) > chunk.indexOf(')', last)) ||
(~chunk.indexOf('(', last) && !~chunk.indexOf(')', last)) ||
(!~chunk.indexOf('(', last) && ~chunk.indexOf(')', last))) {

left = chunk.indexOf('(', last);
right = chunk.indexOf(')', last);
if ((chunk.indexOf(start, last) > chunk.indexOf(stop, last)) ||
(~chunk.indexOf(start, last) && !~chunk.indexOf(stop, last)) ||
(!~chunk.indexOf(start, last) && ~chunk.indexOf(stop, last))) {

if (!~right) {
left = chunk.indexOf(start, last);
right = chunk.indexOf(stop, last);

if ((~left && !~right) || (!~left && ~right)) {
var tmp = routes.slice(0, (i || 1) + 1).join(delimiter);
routes = [tmp].concat(routes.slice((i || 1) + 1));
}
Expand Down Expand Up @@ -771,6 +774,7 @@ Router.prototype.mount = function(routes, path) {

if (isRoute) {
local = local.concat(rename.split(self.delimiter));
local = terminator(local, self.delimiter);
}

self.insert(event, local, routes[route]);
Expand Down

0 comments on commit d683eae

Please sign in to comment.