Skip to content

Commit

Permalink
Use getElementById to find hovercard destination (#50049)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe authored Apr 8, 2024
1 parent 2a6acbf commit adf1317
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/links/components/LinkPreviewPopover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,12 @@ function popoverWrap(element: HTMLLinkElement, filledCallback?: (popover: HTMLDi
element.href.startsWith(`${window.location.href.split('#')[0]}#`)
) {
const domID = element.href.split('#')[1]
const domElement = document.querySelector(`#${domID}`)
// The reason we're using `getElementById(...)` instead of
// `querySelector(#...)` is because `getElementById(...)` will not
// throw a DOMException if the ID starts with a number.
// For example, `document.getElementById('123-thing')` will work, but
// `document.querySelector('#123-thing')` will throw a DOMException.
const domElement = document.getElementById(domID)
if (domElement && domElement.textContent) {
anchor = domElement.textContent
// Headings will have the `#` character to the right which is to
Expand Down

0 comments on commit adf1317

Please sign in to comment.