Skip to content

Commit d293d6d

Browse files
authored
feat: expose internal _parseURL and _stringifyURL (#2912)
* feat: expose internal _parseURL and _stringifyURL * feat: expose experimental too * test: nah * test: more
1 parent 7d465db commit d293d6d

6 files changed

Lines changed: 101 additions & 15 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ describe('parseURL', () => {
8686
})
8787
})
8888

89+
it('works with relative query and hash and current location', () => {
90+
expect(parseURL('?f=foo#hash', '/parent/bar')).toEqual({
91+
fullPath: '/parent/bar?f=foo#hash',
92+
path: '/parent/bar',
93+
hash: '#hash',
94+
query: { f: 'foo' },
95+
})
96+
})
97+
8998
it('works with relative hash and current location', () => {
9099
expect(parseURL('#hash', '/parent/bar')).toEqual({
91100
fullPath: '/parent/bar#hash',
@@ -259,6 +268,17 @@ describe('parseURL', () => {
259268
expect(parseQuery).toHaveBeenCalledTimes(1)
260269
expect(parseQuery).toHaveBeenCalledWith('é=é&é=a')
261270
})
271+
272+
it('uses the parseQuery result as query', () => {
273+
const query = { custom: 'value' }
274+
expect(originalParseURL(() => query, '/foo?x').query).toBe(query)
275+
})
276+
277+
it('does not call parseQuery for a ? in the hash', () => {
278+
const parseQuery = vi.fn()
279+
expect(originalParseURL(parseQuery, '/foo#a?b').query).toEqual({})
280+
expect(parseQuery).not.toHaveBeenCalled()
281+
})
262282
})
263283

264284
describe('stringifyURL', () => {

‎packages/router/src/experimental/index.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,10 @@ export {
186186
type RoutePropsForFilePath as _RoutePropsForFilePath,
187187
} from './runtime'
188188
export { NavigationResult as _NavigationResult } from './data-loaders/entries/index'
189+
/**
190+
* For official external integrations like Nuxt. Does not follow semver.
191+
*/
192+
export {
193+
experimental_parseURL as _parseURL,
194+
experimental_stringifyURL as _stringifyURL,
195+
} from './location'

‎packages/router/src/experimental/location.spec.ts‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { experimental_parseQuery as parseQuery } from './query'
2-
import { experimental_parseURL as originalParseURL } from './location'
2+
import {
3+
experimental_parseURL as originalParseURL,
4+
experimental_stringifyURL as stringifyURL,
5+
} from './location'
6+
import { stringifyQuery } from '../query'
37
import { vi, describe, expect, it } from 'vitest'
48

59
describe('parseURL', () => {
@@ -77,6 +81,15 @@ describe('parseURL', () => {
7781
})
7882
})
7983

84+
it('works with relative query and hash and current location', () => {
85+
expect(parseURL('?f=foo#hash', '/parent/bar')).toEqual({
86+
fullPath: '/parent/bar?f=foo#hash',
87+
path: '/parent/bar',
88+
hash: '#hash',
89+
query: { f: ['foo'] },
90+
})
91+
})
92+
8093
it('works with relative hash and current location', () => {
8194
expect(parseURL('#hash', '/parent/bar')).toEqual({
8295
fullPath: '/parent/bar#hash',
@@ -242,6 +255,17 @@ describe('parseURL', () => {
242255
expect(parseQuery).toHaveBeenCalledWith('é=é&é=a')
243256
})
244257

258+
it('uses the parseQuery result as query', () => {
259+
const query = { custom: ['value'] }
260+
expect(originalParseURL(() => query, '/foo?x').query).toBe(query)
261+
})
262+
263+
it('does not call parseQuery for a ? in the hash', () => {
264+
const parseQuery = vi.fn()
265+
expect(originalParseURL(parseQuery, '/foo#a?b').query).toEqual({})
266+
expect(parseQuery).not.toHaveBeenCalled()
267+
})
268+
245269
describe('hash', () => {
246270
it('keeps the hash as it appears in the URL, like location.hash', () => {
247271
expect(parseURL('/foo#%22%20%25')).toMatchObject({
@@ -268,3 +292,22 @@ describe('parseURL', () => {
268292
})
269293
})
270294
})
295+
296+
describe('stringifyURL', () => {
297+
it('joins path, query and hash', () => {
298+
expect(stringifyURL(stringifyQuery, '/foo', { a: ['1', '2'] }, '#h')).toBe(
299+
'/foo?a=1&a=2#h'
300+
)
301+
})
302+
303+
it('omits the ? for an empty query', () => {
304+
expect(stringifyURL(stringifyQuery, '/foo', {})).toBe('/foo')
305+
expect(stringifyURL(stringifyQuery, '/foo')).toBe('/foo')
306+
})
307+
308+
it('appends the hash as is', () => {
309+
expect(stringifyURL(stringifyQuery, '/foo', {}, '#%20 a')).toBe(
310+
'/foo#%20 a'
311+
)
312+
})
313+
})

‎packages/router/src/experimental/location.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import type { LocationQuery, LocationQueryRaw } from './query'
66
/**
77
* Transforms a URI into a normalized history location. Same behavior as the
88
* legacy `parseURL` but uses `Object.create(null)` for the query object so it
9-
* cannot be exploited via prototype pollution.
9+
* cannot be exploited via prototype pollution. Unlike the legacy one, `hash`
10+
* is not decoded.
1011
*
11-
* @param parseQuery
12+
* @internal no semver guarantee
13+
* @param parseQuery - receives the search without the leading `?`
1214
* @param location - URI to normalize
1315
* @param currentLocation - current absolute location. Allows resolving relative
1416
* paths. Must start with `/`. Defaults to `/`
@@ -48,18 +50,15 @@ export function experimental_parseURL(
4850
}
4951

5052
if (hashPos >= 0) {
51-
// TODO(major): path ||=
52-
path = path || location.slice(0, hashPos)
53+
// keep an empty path (`?a#h`) so it resolves to currentLocation
54+
path ??= location.slice(0, hashPos)
5355
// keep the # character
5456
hash = location.slice(hashPos, location.length)
5557
}
5658

5759
path = resolveRelativePath(
58-
// TODO(major): path ?? location
59-
path != null
60-
? path
61-
: // empty path means a relative query or hash `?foo=f`, `#thing`
62-
location,
60+
// empty path means a relative query or hash `?foo=f`, `#thing`
61+
path ?? location,
6362
currentLocation
6463
)
6564

@@ -77,6 +76,7 @@ export function experimental_parseURL(
7776
* Stringifies a URL from its parts. The `hash` must already be encoded (as
7877
* returned by `encodeHash` or read from a URL), it is appended as is.
7978
*
79+
* @internal no semver guarantee
8080
* @param stringifyQuery - function to stringify the query object
8181
* @param path - path section of the URL
8282
* @param query - normalized query object

‎packages/router/src/index.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ export {
4545
} from './injectionSymbols'
4646

4747
export { START_LOCATION_NORMALIZED as START_LOCATION } from './location'
48+
/**
49+
* Internal helpers for integrations (e.g. Nuxt). No semver guarantee.
50+
*/
51+
export {
52+
parseURL as _parseURL,
53+
stringifyURL as _stringifyURL,
54+
} from './location'
4855
export type {
4956
_RouteLocationBase,
5057
_RouteRecordBase,

‎packages/router/src/location.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,15 @@ export const removeTrailingSlash = (path: string) =>
3737
path.replace(TRAILING_SLASH_RE, '')
3838

3939
/**
40-
* Transforms a URI into a normalized history location
40+
* Transforms a URI into a normalized history location.
4141
*
42-
* @param parseQuery
42+
* Only handles paths: a protocol and host (`https://a.com/x`) are not parsed
43+
* and end up in `path`. `path` is not decoded, `hash` is decoded. `fullPath` is
44+
* the resolved `path` plus the raw query and hash, so it does not keep a
45+
* relative input (`../a` gives `/a`).
46+
*
47+
* @internal no semver guarantee
48+
* @param parseQuery - receives the search without the leading `?`
4349
* @param location - URI to normalize
4450
* @param currentLocation - current absolute location. Allows resolving relative
4551
* paths. Must start with `/`. Defaults to `/`
@@ -80,8 +86,9 @@ export function parseURL(
8086
}
8187

8288
if (hashPos >= 0) {
83-
// TODO(major): path ||=
84-
path = path || location.slice(0, hashPos)
89+
// keep an empty path (`?a#h`) so it resolves to currentLocation
90+
// TODO(major): path ??=
91+
path = path != null ? path : location.slice(0, hashPos)
8592
// keep the # character
8693
hash = location.slice(hashPos, location.length)
8794
}
@@ -105,8 +112,10 @@ export function parseURL(
105112
}
106113

107114
/**
108-
* Stringifies a URL object
115+
* Stringifies a URL object. `path` and `hash` are used as is (no encoding),
116+
* `hash` must start with `#`.
109117
*
118+
* @internal no semver guarantee
110119
* @param stringifyQuery
111120
* @param location
112121
*/

0 commit comments

Comments
 (0)