Commit 311da648 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Migrate disable-opener-for-external-links module to TS

parent b3ef3587
function isHTMLAnchorElement(
element: HTMLElement
): element is HTMLAnchorElement {
return element.tagName === 'A';
}
/**
* Prevent windows or tabs opened via links under `root` from accessing their
* opening `Window`.
......@@ -12,17 +18,14 @@
* [1] https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types#noopener
* [2] https://mathiasbynens.github.io/rel-noopener/
* [3] https://bugs.chromium.org/p/chromium/issues/detail?id=753314
*
* @param {Element} root - Root element
*/
export function disableOpenerForExternalLinks(root) {
export function disableOpenerForExternalLinks(root: Element) {
root.addEventListener('click', event => {
const target = /** @type {HTMLElement} */ (event.target);
const target = event.target as HTMLElement;
if (target.tagName === 'A') {
const linkEl = /** @type {HTMLAnchorElement} */ (target);
if (linkEl.target === '_blank') {
linkEl.rel = 'noopener';
if (isHTMLAnchorElement(target)) {
if (target.target === '_blank') {
target.rel = 'noopener';
}
}
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment