0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-11-22 05:01:20 +01:00

createElementFromHTML<T extends HTMLElement>

This commit is contained in:
wxiaoguang 2024-11-21 20:51:06 +08:00
parent cd01c71926
commit 4eaf28d5cc

View file

@ -301,13 +301,13 @@ export function replaceTextareaSelection(textarea: HTMLTextAreaElement, text: st
}
// Warning: Do not enter any unsanitized variables here
export function createElementFromHTML<T = HTMLElement>(htmlString: string): T {
export function createElementFromHTML<T extends HTMLElement>(htmlString: string): T {
htmlString = htmlString.trim();
// some tags like "tr" are special, it must use a correct parent container to create
if (htmlString.startsWith('<tr')) {
const container = document.createElement('table');
container.innerHTML = htmlString;
return container.querySelector('tr') as T;
return container.querySelector<T>('tr');
}
const div = document.createElement('div');
div.innerHTML = htmlString;