]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(location): correctly parse url ending with .
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 28 Nov 2022 08:18:57 +0000 (09:18 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 28 Nov 2022 08:18:57 +0000 (09:18 +0100)
Fix #1620

packages/router/__tests__/location.spec.ts
packages/router/src/location.ts

index b848ebbbedfe6e754f2fbce03e31f2ac9689616f..076faa1b086a12173d33d37479458a6a8cbab7be 100644 (file)
@@ -353,6 +353,18 @@ describe('resolveRelativePath', () => {
     expect(resolveRelativePath('/add', '/users/posva')).toBe('/add')
   })
 
+  it('works without anything after the .', () => {
+    expect(resolveRelativePath('./', '/users/posva')).toBe('/users/')
+    expect(resolveRelativePath('.', '/users/posva')).toBe('/users/')
+  })
+
+  it('works without anything after the ..', () => {
+    expect(resolveRelativePath('../', '/users/posva/new')).toBe('/users/')
+    expect(resolveRelativePath('../../', '/users/posva/a/b')).toBe('/users/')
+    expect(resolveRelativePath('..', '/users/posva/new')).toBe('/users/')
+    expect(resolveRelativePath('../..', '/users/posva/a/b')).toBe('/users/')
+  })
+
   it('resolves empty path', () => {
     expect(resolveRelativePath('', '/users/posva')).toBe('/users/posva')
     expect(resolveRelativePath('', '/users')).toBe('/users')
index 24fa764a944cfeed124c781f4f94575ca9ce74af..0f981b47df7679b6ee972c3252d81f7f962d2c56 100644 (file)
@@ -212,6 +212,13 @@ export function resolveRelativePath(to: string, from: string): string {
 
   const fromSegments = from.split('/')
   const toSegments = to.split('/')
+  const lastToSegment = toSegments[toSegments.length - 1]
+
+  // make . and ./ the same (../ === .., ../../ === ../..)
+  // this is the same behavior as new URL()
+  if (lastToSegment === '..' || lastToSegment === '.') {
+    toSegments.push('')
+  }
 
   let position = fromSegments.length - 1
   let toPosition: number