From 5fe6068b4ab38a3cdf26a5f0a0d4f516e9029828 Mon Sep 17 00:00:00 2001 From: Colin Caine Date: Wed, 27 Dec 2017 14:37:01 +0000 Subject: [PATCH] url_util: Keep trailing slashes And don't remove repeated slashes until they're at the end. --- src/url_util.test.ts | 8 +++++--- src/url_util.ts | 9 +++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/url_util.test.ts b/src/url_util.test.ts index c887bd21..521234e5 100644 --- a/src/url_util.test.ts +++ b/src/url_util.test.ts @@ -61,17 +61,19 @@ function test_parent() { // single level path ["http://example.com/path", "http://example.com/"], // multi-level path - ["http://example.com/path1/path2", "http://example.com/path1"], + ["http://example.com/path1/path2", "http://example.com/path1/"], + ["http://example.com/path1/path2/path3", "http://example.com/path1/path2/"], // subdomains ["http://sub.example.com", "http://example.com/"], // subdom with path, leave subdom ["http://sub.example.com/path", "http://sub.example.com/"], // trailing slash ["http://sub.example.com/path/", "http://sub.example.com/"], + ["http://sub.example.com/path/to/", "http://sub.example.com/path/"], // repeated slash ["http://example.com/path//", "http://example.com/"], - // repeated slash - ["http://example.com//path//", "http://example.com/"], + ["http://example.com//path//", "http://example.com//"], + ["http://example.com//path//", "http://example.com//"], ] for (let [url, exp_parent] of cases) { diff --git a/src/url_util.ts b/src/url_util.ts index 541d0f19..56cd99e8 100644 --- a/src/url_util.ts +++ b/src/url_util.ts @@ -78,13 +78,10 @@ export function getUrlParent(url, count = 1) { return gup(parent, count - 1) } - // pathname always starts '/' + // empty path is '/' if (parent.pathname !== '/') { - // Split on '/' and remove empty substrings - // (handles initial and trailing slashes, repeated slashes, etc.) - let path = parent.pathname.split('/').filter(sub => sub !== "") - path.pop() - parent.pathname = path.join('/') + // Remove trailing slashes and everything to the next slash: + parent.pathname = parent.pathname.replace(/\/[^\/]*?\/*$/, '/') return gup(parent, count - 1) }