createElementFromHTML<T extends HTMLElement>

This commit is contained in:
wxiaoguang 2024-11-21 20:51:06 +08:00
parent cd01c71926
commit 4eaf28d5cc
1 changed files with 2 additions and 2 deletions

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;