From: Eduardo San Martin Morote Date: Fri, 20 May 2022 08:24:51 +0000 (+0200) Subject: fix: resolve relative paths to / X-Git-Tag: v4.1.0~90 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=23e25e5b55592790805b98e21836bd4e57eb037c;p=thirdparty%2Fvuejs%2Frouter.git fix: resolve relative paths to / Fix #1410 --- diff --git a/__tests__/location.spec.ts b/__tests__/location.spec.ts index a770e900..4846f87a 100644 --- a/__tests__/location.spec.ts +++ b/__tests__/location.spec.ts @@ -329,6 +329,9 @@ describe('resolveRelativePath', () => { expect(resolveRelativePath('./../add', '/')).toBe('/add') expect(resolveRelativePath('../../add', '/')).toBe('/add') expect(resolveRelativePath('../../../add', '/')).toBe('/add') + expect(resolveRelativePath('a/add', '/')).toBe('/a/add') + expect(resolveRelativePath('./a/add', '/')).toBe('/a/add') + expect(resolveRelativePath('../a/add', '/')).toBe('/a/add') }) it('ignores it location is absolute', () => { diff --git a/src/location.ts b/src/location.ts index 8ba3caa6..c6e6f498 100644 --- a/src/location.ts +++ b/src/location.ts @@ -215,10 +215,16 @@ export function resolveRelativePath(to: string, from: string): string { for (toPosition = 0; toPosition < toSegments.length; toPosition++) { segment = toSegments[toPosition] - // can't go below zero - if (position === 1 || segment === '.') continue - if (segment === '..') position-- - // found something that is not relative path + + // we stay on the same position + if (segment === '.') continue + // go up in the from array + if (segment === '..') { + // we can't go below zero but we still need to increment toPosition + if (position > 1) position-- + // continue + } + // we reached a non relative path, we stop here else break } @@ -226,6 +232,7 @@ export function resolveRelativePath(to: string, from: string): string { fromSegments.slice(0, position).join('/') + '/' + toSegments + // ensure we use at least the last element in the toSegments .slice(toPosition - (toPosition === toSegments.length ? 1 : 0)) .join('/') )