Remove commented code and improve clarity in template parsing logic

This commit is contained in:
Astrian Zheng 2025-05-14 15:25:30 +10:00
parent cd4d195bd4
commit 3a05316522
Signed by: Astrian
SSH Key Fingerprint: SHA256:rVnhx3DAKjujCwWE13aDl7uV6+9U1MvydLkNRXJrBiA

View File

@ -28,17 +28,13 @@ export default (options: ComponentOptions) => {
this.shadowRoot?.appendChild(styleElement)
}
// 使用 text/html 解析,这更适合处理 HTML 模板
const parser = new DOMParser()
const doc = parser.parseFromString(template, 'text/html')
// 找到并导入主要内容元素
const mainContent = doc.body.firstElementChild
if (mainContent) {
// 使用 importNode 确保所有事件和属性都被正确复制
this.shadowRoot?.appendChild(document.importNode(mainContent, true))
} else {
// 如果没有根元素,将所有内容放入一个新的 div 中
const container = document.createElement('div')
container.innerHTML = template
this.shadowRoot?.appendChild(container)