28 lines
1009 B
TypeScript
28 lines
1009 B
TypeScript
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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>';
|
|
})
|
|
}
|
|
}
|