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

path is broken in Context constructor #526

Closed
onury opened this issue Dec 2, 2018 · 0 comments
Closed

path is broken in Context constructor #526

onury opened this issue Dec 2, 2018 · 0 comments

Comments

@onury
Copy link
Contributor

onury commented Dec 2, 2018

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, '') || '/';
onury added a commit to onury/page.js that referenced this issue Dec 2, 2018
- Added escapeRegExp() function
- Updated Context constructor
This was referenced Dec 2, 2018
matthewp added a commit that referenced this issue Jan 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant