url_util: Fix #256.

This commit is contained in:
Colin Caine 2017-12-26 13:56:35 +00:00
parent e4231307ce
commit df9451e83e
2 changed files with 9 additions and 1 deletions

View file

@ -66,6 +66,12 @@ function test_parent() {
["http://sub.example.com", "http://example.com/"], ["http://sub.example.com", "http://example.com/"],
// subdom with path, leave subdom // subdom with path, leave subdom
["http://sub.example.com/path", "http://sub.example.com/"], ["http://sub.example.com/path", "http://sub.example.com/"],
// trailing slash
["http://sub.example.com/path/", "http://sub.example.com/"],
// repeated slash
["http://example.com/path//", "http://example.com/"],
// repeated slash
["http://example.com//path//", "http://example.com/"],
] ]
for (let [url, exp_parent] of cases) { for (let [url, exp_parent] of cases) {

View file

@ -80,7 +80,9 @@ export function getUrlParent(url, count = 1) {
// pathname always starts '/' // pathname always starts '/'
if (parent.pathname !== '/') { if (parent.pathname !== '/') {
let path = parent.pathname.substring(1).split('/') // Split on '/' and remove empty substrings
// (handles initial and trailing slashes, repeated slashes, etc.)
let path = parent.pathname.split('/').filter(sub => sub !== "")
path.pop() path.pop()
parent.pathname = path.join('/') parent.pathname = path.join('/')
return gup(parent, count - 1) return gup(parent, count - 1)