export class HtmlUtils { public static getElementByIdOrFail(id: string): T { const elem = document.getElementById(id); if (HtmlUtils.isHtmlElement(elem)) { return elem; } throw new Error("Cannot find HTML element with id '"+id+"'"); } public static querySelectorOrFail(selector: string): T { const elem = document.querySelector(selector); if (HtmlUtils.isHtmlElement(elem)) { return elem; } throw new Error("Cannot find HTML element with selector '"+selector+"'"); } public static removeElementByIdOrFail(id: string): T { const elem = document.getElementById(id); if (HtmlUtils.isHtmlElement(elem)) { elem.remove(); return elem; } throw new Error("Cannot find HTML element with id '"+id+"'"); } public static urlify(text: string): HTMLSpanElement { const textReturn : HTMLSpanElement = document.createElement('span'); textReturn.innerText = text; const urlRegex = /(https?:\/\/[^\s]+)/g; text.replace(urlRegex, (url: string) => { let link : HTMLAnchorElement = document.createElement('a'); link.innerText = ` ${url}`; link.href = url; link.target = '_blank'; textReturn.append(link); return url; }); return textReturn; } private static isHtmlElement(elem: HTMLElement | null): elem is T { return elem !== null; } }