blob: b25de2bcec6c085d6b017f913031d2ef49352b3a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import { query } from '../utils.js';
/**
* Find the anchor element for a given hash.
*
* @param hash Hash with or without leading '#'
* @returns The element, if found, or null.
*
* @see https://html.spec.whatwg.org/#find-a-potential-indicated-element
*/
export const getAnchorElement = (hash?: string): Element | null => {
if (hash && hash.charAt(0) === '#') {
hash = hash.substring(1);
}
if (!hash) {
return null;
}
const decoded = decodeURIComponent(hash);
let element =
document.getElementById(hash) ||
document.getElementById(decoded) ||
query(`a[name='${CSS.escape(hash)}']`) ||
query(`a[name='${CSS.escape(decoded)}']`);
if (!element && hash === 'top') {
element = document.body;
}
return element;
};
|