Skip to content

Commit

Permalink
Prevent default clicking on markdown preview links when using `openLi…
Browse files Browse the repository at this point in the history
…nksInNewTab` (#2718)

* useLinkInterception to preventDefault after opening in new tabs

* Add noopener noreferrer to window.open during link interception

* Add changeset

* Fix linting error

Co-authored-by: Josh Black <joshblack@github.com>
  • Loading branch information
marywhite and joshblack committed Jan 10, 2023
1 parent 84ea1e9 commit 9cb0119
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-pots-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Prevent default when clicking on MarkdownViewer links with openLinksInNewTab
16 changes: 1 addition & 15 deletions src/drafts/MarkdownEditor/MarkdownEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,6 @@ describe('MarkdownEditor', () => {
})

it('forces links to open in a new tab', async () => {
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})

// eslint-disable-next-line github/unescaped-html-literal
const html = '<a href="https://example.com">Link</a>'
const user = userEvent.setup()
Expand All @@ -800,19 +798,7 @@ describe('MarkdownEditor', () => {
const link = await waitFor(() => within(getPreview()).getByText('Link'))

await user.click(link)
// Note: navigation is not implemented in JSDOM and will log out an
// error when clicking the link above. The spy here captures this error
// and will assert that it is called only once, otherwise another error
// in this test has occurred
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Not implemented: navigation (except hash changes)',
}),
)
expect(windowOpenSpy).toHaveBeenCalledWith('https://example.com/', '_blank')

spy.mockRestore()
expect(windowOpenSpy).toHaveBeenCalledWith('https://example.com/', '_blank', 'noopener noreferrer')
})
})
})
Expand Down
2 changes: 1 addition & 1 deletion src/drafts/MarkdownViewer/MarkdownViewer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ text after list`
)
const link = getByRole('link') as HTMLAnchorElement
await user.click(link)
expect(windowOpenSpy).toHaveBeenCalledWith('https://example.com/', '_blank')
expect(windowOpenSpy).toHaveBeenCalledWith('https://example.com/', '_blank', 'noopener noreferrer')
})

it('calls onLinkClick on link click', async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/drafts/MarkdownViewer/_useLinkInterception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export const useLinkInterception = ({htmlContainer, onLinkClick, openLinksInNewT
onLinkClick?.(event)

if (!event.defaultPrevented && openLinksInNewTab && link.href) {
window.open(link.href, '_blank')
window.open(link.href, '_blank', 'noopener noreferrer')
event.preventDefault()
}
}

Expand Down

0 comments on commit 9cb0119

Please sign in to comment.