Skip to content

Commit f219344

Browse files
committed
fix(router-core): update URL path segment encoding logic in tests and parsing function
Signed-off-by: leesb971204 <[email protected]>
1 parent 146c0ac commit f219344

2 files changed

Lines changed: 26 additions & 31 deletions

File tree

‎packages/react-router/tests/router.test.tsx‎

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -723,72 +723,74 @@ describe('encoding: URL path segment', () => {
723723
it.each([
724724
{
725725
input: '/path-segment/%C3%A9',
726-
output: '/path-segment/é',
726+
output: '/path-segment/%25C3%25A9',
727727
type: 'encoded',
728728
},
729729
{
730730
input: '/path-segment/é',
731-
output: '/path-segment/é',
732-
type: 'not encoded',
731+
output: '/path-segment/%C3%A9',
732+
type: 'encoded',
733733
},
734734
{
735-
input: '/path-segment/100%25', // `%25` = `%`
736-
output: '/path-segment/100%25',
737-
type: 'not encoded',
735+
input: '/path-segment/100%25',
736+
output: '/path-segment/100%2525',
737+
type: 'encoded',
738738
},
739739
{
740740
input: '/path-segment/100%25%25',
741-
output: '/path-segment/100%25%25',
742-
type: 'not encoded',
741+
output: '/path-segment/100%2525%2525',
742+
type: 'encoded',
743743
},
744744
{
745-
input: '/path-segment/100%26', // `%26` = `&`
746-
output: '/path-segment/100%26',
747-
type: 'not encoded',
745+
input: '/path-segment/100%26',
746+
output: '/path-segment/100%2526',
747+
type: 'encoded',
748748
},
749749
{
750750
input: '/path-segment/%F0%9F%9A%80',
751-
output: '/path-segment/🚀',
751+
output: '/path-segment/%25F0%259F%259A%2580',
752752
type: 'encoded',
753753
},
754754
{
755755
input: '/path-segment/%F0%9F%9A%80to%2Fthe%2Fmoon',
756-
output: '/path-segment/🚀to%2Fthe%2Fmoon',
756+
output: '/path-segment/%25F0%259F%259A%2580to%252Fthe%252Fmoon',
757757
type: 'encoded',
758758
},
759759
{
760760
input: '/path-segment/%25%F0%9F%9A%80to%2Fthe%2Fmoon',
761-
output: '/path-segment/%25🚀to%2Fthe%2Fmoon',
761+
output: '/path-segment/%2525%25F0%259F%259A%2580to%252Fthe%252Fmoon',
762762
type: 'encoded',
763763
},
764764
{
765765
input: '/path-segment/%F0%9F%9A%80to%2Fthe%2Fmoon%25',
766-
output: '/path-segment/🚀to%2Fthe%2Fmoon%25',
766+
output: '/path-segment/%25F0%259F%259A%2580to%252Fthe%252Fmoon%2525',
767767
type: 'encoded',
768768
},
769769
{
770770
input:
771771
'/path-segment/%F0%9F%9A%80to%2Fthe%2Fmoon%25%F0%9F%9A%80to%2Fthe%2Fmoon',
772-
output: '/path-segment/🚀to%2Fthe%2Fmoon%25🚀to%2Fthe%2Fmoon',
772+
output:
773+
'/path-segment/%25F0%259F%259A%2580to%252Fthe%252Fmoon%2525%25F0%259F%259A%2580to%252Fthe%252Fmoon',
773774
type: 'encoded',
774775
},
775776
{
776777
input: '/path-segment/🚀',
777-
output: '/path-segment/🚀',
778-
type: 'not encoded',
778+
output: '/path-segment/%F0%9F%9A%80',
779+
type: 'encoded',
779780
},
780781
{
781782
input: '/path-segment/🚀to%2Fthe%2Fmoon',
782-
output: '/path-segment/🚀to%2Fthe%2Fmoon',
783-
type: 'not encoded',
783+
output: '/path-segment/%F0%9F%9A%80to%252Fthe%252Fmoon',
784+
type: 'encoded',
784785
},
785786
])(
786787
'should resolve $input to $output when the path segment is $type',
787788
async ({ input, output }) => {
788789
const { router } = createTestRouter({
789-
history: createMemoryHistory({ initialEntries: [input] }),
790+
history: createMemoryHistory({
791+
initialEntries: [input.split('/').map(encodeURIComponent).join('/')], // In browser, the path is encoded by default
792+
}),
790793
})
791-
792794
render(<RouterProvider router={router} />)
793795
await act(() => router.load())
794796

‎packages/router-core/src/path.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { hasUriEncodedChars, last } from './utils'
1+
import { last } from './utils'
22
import type { MatchLocation } from './RouterProvider'
33
import type { AnyPathParams } from './route'
44

@@ -183,14 +183,7 @@ export function parsePathname(pathname?: string): Array<Segment> {
183183

184184
return {
185185
type: 'pathname',
186-
value: part.includes('%25')
187-
? part
188-
.split('%25')
189-
.map((segment) => decodeURI(segment))
190-
.join('%25')
191-
: hasUriEncodedChars(part)
192-
? part
193-
: decodeURI(part),
186+
value: part,
194187
}
195188
}),
196189
)

0 commit comments

Comments
 (0)