]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix: resolve relative paths to /
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 20 May 2022 08:24:51 +0000 (10:24 +0200)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Thu, 30 Jun 2022 07:59:00 +0000 (09:59 +0200)
Fix #1410

__tests__/location.spec.ts
src/location.ts

index a770e900db86353ccd22f0f5c9d5b8e7c20dc4dc..4846f87a17c830d126a3e9b9f4d4482d5050f0d1 100644 (file)
@@ -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', () => {
index 8ba3caa6f80facc473c99e3835db906fa60e48f0..c6e6f49867f08959b74581c6f987e7f4e05c8e0b 100644 (file)
@@ -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('/')
   )