Skip to content

Commit 2d138b9

Browse files
committed
chore: wip encoding
1 parent 4d2c23b commit 2d138b9

File tree

3 files changed

+62
-26
lines changed

3 files changed

+62
-26
lines changed

packages/router/src/location.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { RouteParamValue, RouteParamsGeneric } from './types'
33
import { RouteRecord } from './matcher/types'
44
import { warn } from './warning'
55
import { isArray } from './utils'
6-
import { decode } from './encoding'
6+
import { decode, encodeHash } from './encoding'
77
import { RouteLocation, RouteLocationNormalizedLoaded } from './typed-routes'
88

99
/**
@@ -94,6 +94,25 @@ export function parseURL(
9494
}
9595
}
9696

97+
/**
98+
* Creates a `fullPath` property from the `path`, `query` and `hash` properties
99+
*
100+
* @param stringifyQuery - custom function to stringify the query object. It should handle encoding values
101+
* @param path - An encdoded path
102+
* @param query - A decoded query object
103+
* @param hash - A decoded hash
104+
* @returns a valid `fullPath`
105+
*/
106+
export function NEW_stringifyURL(
107+
stringifyQuery: (query?: LocationQueryRaw) => string,
108+
path: LocationPartial['path'],
109+
query?: LocationPartial['query'],
110+
hash: LocationPartial['hash'] = ''
111+
): string {
112+
const searchText = stringifyQuery(query)
113+
return path + (searchText && '?') + searchText + encodeHash(hash)
114+
}
115+
97116
/**
98117
* Stringifies a URL object
99118
*

packages/router/src/new-route-resolver/matcher.spec.ts

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -314,33 +314,50 @@ describe('RouterMatcher', () => {
314314
})
315315

316316
describe('encoding', () => {
317-
it('handles encoded string path', () => {
318-
const matcher = createCompiledMatcher([ANY_PATH_ROUTE])
319-
console.log(matcher.resolve('/%23%2F%3F'))
320-
expect(matcher.resolve('/%23%2F%3F')).toMatchObject({
321-
fullPath: '/%23%2F%3F',
322-
path: '/%23%2F%3F',
323-
query: {},
324-
params: {},
325-
hash: '',
317+
const matcher = createCompiledMatcher([ANY_PATH_ROUTE])
318+
describe('decodes', () => {
319+
it('handles encoded string path', () => {
320+
expect(matcher.resolve('/%23%2F%3F')).toMatchObject({
321+
fullPath: '/%23%2F%3F',
322+
path: '/%23%2F%3F',
323+
query: {},
324+
params: {},
325+
hash: '',
326+
})
326327
})
327-
})
328328

329-
it('decodes query from a string', () => {
330-
const matcher = createCompiledMatcher([ANY_PATH_ROUTE])
331-
expect(matcher.resolve('/foo?foo=%23%2F%3F')).toMatchObject({
332-
path: '/foo',
333-
fullPath: '/foo?foo=%23%2F%3F',
334-
query: { foo: '#/?' },
329+
it('decodes query from a string', () => {
330+
expect(matcher.resolve('/foo?foo=%23%2F%3F')).toMatchObject({
331+
path: '/foo',
332+
fullPath: '/foo?foo=%23%2F%3F',
333+
query: { foo: '#/?' },
334+
})
335+
})
336+
337+
it('decodes hash from a string', () => {
338+
expect(matcher.resolve('/foo#%22')).toMatchObject({
339+
path: '/foo',
340+
fullPath: '/foo#%22',
341+
hash: '#"',
342+
})
335343
})
336344
})
337345

338-
it('decodes hash from a string', () => {
339-
const matcher = createCompiledMatcher([ANY_PATH_ROUTE])
340-
expect(matcher.resolve('/foo#h-%23%2F%3F')).toMatchObject({
341-
path: '/foo',
342-
fullPath: '/foo#h-%23%2F%3F',
343-
hash: '#h-#/?',
346+
describe('encodes', () => {
347+
it('encodes the query', () => {
348+
expect(
349+
matcher.resolve({ path: '/foo', query: { foo: '"' } })
350+
).toMatchObject({
351+
fullPath: '/foo?foo=%22',
352+
query: { foo: '"' },
353+
})
354+
})
355+
356+
it('encodes the hash', () => {
357+
expect(matcher.resolve({ path: '/foo', hash: '#"' })).toMatchObject({
358+
fullPath: '/foo#%22',
359+
hash: '#"',
360+
})
344361
})
345362
})
346363
})

packages/router/src/new-route-resolver/matcher.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
} from './matcher-pattern'
1212
import { warn } from '../warning'
1313
import { encodeQueryValue as _encodeQueryValue, encodeParam } from '../encoding'
14-
import { parseURL, stringifyURL } from '../location'
14+
import { parseURL, NEW_stringifyURL } from '../location'
1515
import type {
1616
MatcherLocationAsNamed,
1717
MatcherLocationAsPathAbsolute,
@@ -432,7 +432,7 @@ export function createCompiledMatcher(
432432
const path = location.path ?? '/'
433433
return {
434434
...NO_MATCH_LOCATION,
435-
fullPath: stringifyURL(stringifyQuery, { path, query, hash }),
435+
fullPath: NEW_stringifyURL(stringifyQuery, path, query, hash),
436436
path,
437437
query,
438438
hash,
@@ -465,7 +465,7 @@ export function createCompiledMatcher(
465465

466466
return {
467467
name,
468-
fullPath: stringifyURL(stringifyQuery, { path, query, hash }),
468+
fullPath: NEW_stringifyURL(stringifyQuery, path, query, hash),
469469
path,
470470
query,
471471
hash,

0 commit comments

Comments
 (0)