From 23e25e5b55592790805b98e21836bd4e57eb037c Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 20 May 2022 10:24:51 +0200 Subject: [PATCH] fix: resolve relative paths to / Fix #1410 --- __tests__/location.spec.ts | 3 +++ src/location.ts | 15 +++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) 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('/') ) -- 2.39.5