2020-08-11 22:32:55 +02:00
|
|
|
export class HtmlUtils {
|
|
|
|
public static getElementByIdOrFail<T extends HTMLElement>(id: string): T {
|
|
|
|
const elem = document.getElementById(id);
|
|
|
|
if (elem === null) {
|
|
|
|
throw new Error("Cannot find HTML element with id '"+id+"'");
|
|
|
|
}
|
|
|
|
// FIXME: does not check the type of the returned type
|
|
|
|
return elem as T;
|
|
|
|
}
|
2020-09-16 18:38:50 +02:00
|
|
|
|
|
|
|
public static removeElementByIdOrFail<T extends HTMLElement>(id: string): T {
|
|
|
|
const elem = document.getElementById(id);
|
|
|
|
if (elem === null) {
|
|
|
|
throw new Error("Cannot find HTML element with id '"+id+"'");
|
|
|
|
}
|
|
|
|
// FIXME: does not check the type of the returned type
|
|
|
|
elem.remove();
|
|
|
|
return elem as T;
|
|
|
|
}
|
2021-01-22 15:01:10 +01:00
|
|
|
|
|
|
|
public static urlify(text: string): string {
|
|
|
|
const urlRegex = /(https?:\/\/[^\s]+)/g;
|
|
|
|
return text.replace(urlRegex, (url: string) => {
|
|
|
|
return '<a href="' + url + '" target="_blank" style=":visited {color: white}">' + url + '</a>';
|
|
|
|
})
|
|
|
|
}
|
2020-08-11 22:32:55 +02:00
|
|
|
}
|