Skip to content

Commit 7e90d43

Browse files
authored
always pass the target attribute regardless of whether the event is intercepted (#907)
1 parent a7481f7 commit 7e90d43

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/routing/Link.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface LinkProperties extends VNodeProperties {
1717
const factory = create({ injector }).properties<LinkProperties>();
1818

1919
export const Link = factory(function Link({ middleware: { injector }, properties, children }) {
20-
let { routerKey = 'router', to, isOutlet = true, target, params = {}, onClick, ...props } = properties();
20+
let { routerKey = 'router', to, isOutlet = true, params = {}, onClick, ...props } = properties();
2121
const router = injector.get<Router>(routerKey);
2222
let href: string | undefined = to;
2323

@@ -29,7 +29,13 @@ export const Link = factory(function Link({ middleware: { injector }, properties
2929
const onclick = (event: MouseEvent) => {
3030
onClick && onClick(event);
3131

32-
if (!event.defaultPrevented && event.button === 0 && !event.metaKey && !event.ctrlKey && !target) {
32+
if (
33+
!event.defaultPrevented &&
34+
event.button === 0 &&
35+
!event.metaKey &&
36+
!event.ctrlKey &&
37+
!linkProps.target
38+
) {
3339
if (!has('build-serve') || !has('build-time-rendered')) {
3440
event.preventDefault();
3541
href !== undefined && router.setPath(href);

tests/routing/unit/Link.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ describe('Link', () => {
129129
const r = renderer(() => w(Link, { to: 'foo', target: '_blank' }), {
130130
middleware: [[getRegistry, mockGetRegistry]]
131131
});
132-
const template = assertion(() => v(WrappedAnchor.tag, { href: 'foo', onclick: noop }));
132+
const template = assertion(() => v(WrappedAnchor.tag, { href: 'foo', onclick: noop, target: '_blank' }));
133133
r.expect(template);
134134
r.property(WrappedAnchor, 'onclick', createMockEvent());
135135
r.expect(template);
@@ -190,4 +190,15 @@ describe('Link', () => {
190190
// nothing to see here
191191
}
192192
});
193+
194+
it('does pass target through to element', () => {
195+
const WrappedAnchor = wrap('a');
196+
const r = renderer(() => w(Link, { to: '#foo/static', isOutlet: false, target: '_blank' }), {
197+
middleware: [[getRegistry, mockGetRegistry]]
198+
});
199+
const template = assertion(() =>
200+
v(WrappedAnchor.tag, { href: '#foo/static', onclick: noop, target: '_blank' })
201+
);
202+
r.expect(template);
203+
});
193204
});

0 commit comments

Comments
 (0)