Merge pull request #672 from Amirault/tam_use_type_guard_on_html_element
Use type guard for html element subtype
This commit is contained in:
commit
b7b2934106
@ -1,21 +1,19 @@
|
|||||||
export class HtmlUtils {
|
export class HtmlUtils {
|
||||||
public static getElementByIdOrFail<T extends HTMLElement>(id: string): T {
|
public static getElementByIdOrFail<T extends HTMLElement>(id: string): T {
|
||||||
const elem = document.getElementById(id);
|
const elem = document.getElementById(id);
|
||||||
if (elem === null) {
|
if (HtmlUtils.isHtmlElement<T>(elem)) {
|
||||||
throw new Error("Cannot find HTML element with id '"+id+"'");
|
return elem;
|
||||||
}
|
}
|
||||||
// FIXME: does not check the type of the returned type
|
throw new Error("Cannot find HTML element with id '"+id+"'");
|
||||||
return elem as T;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static removeElementByIdOrFail<T extends HTMLElement>(id: string): T {
|
public static removeElementByIdOrFail<T extends HTMLElement>(id: string): T {
|
||||||
const elem = document.getElementById(id);
|
const elem = document.getElementById(id);
|
||||||
if (elem === null) {
|
if (HtmlUtils.isHtmlElement<T>(elem)) {
|
||||||
throw new Error("Cannot find HTML element with id '"+id+"'");
|
elem.remove();
|
||||||
|
return elem;
|
||||||
}
|
}
|
||||||
// FIXME: does not check the type of the returned type
|
throw new Error("Cannot find HTML element with id '"+id+"'");
|
||||||
elem.remove();
|
|
||||||
return elem as T;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static urlify(text: string): string {
|
public static urlify(text: string): string {
|
||||||
@ -24,4 +22,8 @@ export class HtmlUtils {
|
|||||||
return '<a href="' + url + '" target="_blank" style=":visited {color: white}">' + url + '</a>';
|
return '<a href="' + url + '" target="_blank" style=":visited {color: white}">' + url + '</a>';
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static isHtmlElement<T extends HTMLElement>(elem: HTMLElement | null): elem is T {
|
||||||
|
return elem !== null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user