Open History to see the last commit
1/** @jsxImportSource solid-js */
2import { renderToString } from 'solid-js/web'
3import { expect, test, vi } from 'vitest'
4import {
5 Link,
6 RouterContextProvider,
7 createLink,
8 createMemoryHistory,
9 createRootRoute,
10 createRouter,
11} from '../../src'
12import type { JSX } from 'solid-js'
13β
14test.each([
15 { to: '/', href: '/' },
16 { to: '/internal', href: '/internal' },
17 { to: 'https://example.com/', href: 'https://example.com/' },
18 { to: '/external', href: 'https://example.com/rewritten' },
19 { to: 'javascript:blocked()', href: undefined },
20])('SSR custom links retain caller props for $to', ({ to, href }) => {
21 let received: JSX.AnchorHTMLAttributes<HTMLAnchorElement> | undefined
22 const CustomLink = createLink(
23 (props: JSX.AnchorHTMLAttributes<HTMLAnchorElement>) => {
24 received = { ...props }
25 return <a />
26 },
27 )
28 const router = createRouter({
29 routeTree: createRootRoute(),
30 history: createMemoryHistory(),
31 isServer: true,
32 rewrite: {
33 output: ({ url }) =>
34 url.pathname === '/external'
35 ? new URL('https://example.com/rewritten')
36 : url,
37 },
38 })
39 const handlers = {
40 onClick: vi.fn(),
41 onBlur: vi.fn(),
42 onFocus: vi.fn(),
43 onMouseEnter: vi.fn(),
44 onMouseLeave: vi.fn(),
45 onMouseOut: vi.fn(),
46 onMouseOver: vi.fn(),
47 onTouchStart: vi.fn(),
48 }
49 const ref = vi.fn()
50 const callerProps = { ref, ...handlers }
51 const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
52 try {
53 renderToString(() => (
54 <RouterContextProvider router={router}>
55 {() => (
56 <CustomLink
57 to={to}
58 href={to.startsWith('/') ? undefined : 'javascript:spoofed()'}
59 target="_blank"
60 title="custom link"
61 {...callerProps}
62 />
63 )}
64 </RouterContextProvider>
65 ))
66 expect(received).toMatchObject({
67 href,
68 target: '_blank',
69 title: 'custom link',
70 })
71 const receivedRef = received?.ref as (element: HTMLAnchorElement) => void
72 expect(receivedRef).toBeTypeOf('function')
73 const element = {} as HTMLAnchorElement
74 receivedRef(element)
75 expect(ref).toHaveBeenCalledExactlyOnceWith(element)
76 for (const name of Object.keys(handlers) as Array<keyof typeof handlers>) {
77 const handler = received?.[name] as (event: Event) => void
78 expect(handler).toBeTypeOf('function')
79 const event = new Event('test', { cancelable: true })
80 event.preventDefault()
81 handler(event)
82 expect(handlers[name]).toHaveBeenCalledExactlyOnceWith(event)
83 }
84 if (!href) {
85 expect(received).toMatchObject({ role: 'link', 'aria-disabled': true })
86 }
87 } finally {
88 warn.mockRestore()
89 }
90})
91β
92test.each([
93 { to: 'https://example.com/', href: 'https://example.com/' },
94 { to: 'myapp:open', href: 'myapp:open' },
95 { to: 'javascript:alert(1)', href: undefined },
96 { to: 'mailto:person@example.com', href: undefined },
97 { to: '/external', href: 'https://example.com/rewritten' },
98 { to: '/blocked', href: undefined },
99])('keeps the final SSR href safe and inactive for $to', ({ to, href }) => {
100 const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
101 try {
102 const router = createRouter({
103 routeTree: createRootRoute(),
104 history: createMemoryHistory(),
105 isServer: true,
106 protocolAllowlist: ['http:', 'https:', 'myapp:'],
107 rewrite: {
108 output: ({ url }) =>
109 url.pathname === '/external'
110 ? new URL('https://example.com/rewritten')
111 : url.pathname === '/blocked'
112 ? new URL('javascript:alert(1)')
113 : url,
114 },
115 })
116 for (const disabled of [false, true]) {
117 for (const customStyles of [false, true]) {
118 const inactiveProps = customStyles
119 ? {
120 class: 'inactive',
121 style: { color: 'red' },
122 href: 'javascript:inactive()',
123 }
124 : undefined
125 const html = renderToString(() => (
126 <RouterContextProvider router={router}>
127 {() => (
128 <Link
129 to={to}
130 disabled={disabled}
131 href={to.startsWith('/') ? undefined : 'javascript:spoofed()'}
132 class={customStyles ? 'base' : undefined}
133 inactiveProps={inactiveProps}
134 >
135 {({ isActive }) => String(isActive)}
136 </Link>
137 )}
138 </RouterContextProvider>
139 ))
140 // Direct schemes retain their href when disabled; built locations do not.
141 const expectedHref = disabled && to.startsWith('/') ? undefined : href
142 if (expectedHref) {
143 expect(html).toContain(`href="${expectedHref}"`)
144 } else {
145 expect(html).not.toContain('href=')
146 }
147 expect(html).not.toContain('javascript:')
148 expect(html).not.toContain('aria-current')
149 expect(html).toContain('false')
150 if (disabled || !href) {
151 expect(html).toContain('aria-disabled="true"')
152 }
153 if (customStyles) {
154 expect(html).toContain('base inactive')
155 expect(html).toContain('color:red')
156 }
157 }
158 }
159 } finally {
160 warn.mockRestore()
161 }
162})