visionmedia/page.js

path is broken in Context constructor

onury opened this issue · 0 comments

onury commented

Problem

This line:

this.path = path.replace(pageBase, '') || '/'

changes the path incorrectly by replacing (removing) the pageBase - first occurrence.
It should remove the most left only, if there is any.

For example,

var pageBase = "/";
var path = "my/route/";
path.replace(pageBase, '');    // ——» produces "myroute/"

Solution

We need to escape regexp characters within pageBase:

function escapeRegExp(s) {
    return s.replace(/([.+*?=^!:${}()[\]|/\\])/g, '\\$1')
}

Then change the buggy line with:

// we'll ensure it only matches the start of the string.
var re = new RegExp('^' + escapeRegExp(pageBase));
this.path = path.replace(re, '') || '/';