Remove commented code and improve clarity in template parsing logic
This commit is contained in:
parent
cd4d195bd4
commit
3a05316522
|
@ -28,17 +28,13 @@ export default (options: ComponentOptions) => {
|
||||||
this.shadowRoot?.appendChild(styleElement)
|
this.shadowRoot?.appendChild(styleElement)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用 text/html 解析,这更适合处理 HTML 模板
|
|
||||||
const parser = new DOMParser()
|
const parser = new DOMParser()
|
||||||
const doc = parser.parseFromString(template, 'text/html')
|
const doc = parser.parseFromString(template, 'text/html')
|
||||||
|
|
||||||
// 找到并导入主要内容元素
|
|
||||||
const mainContent = doc.body.firstElementChild
|
const mainContent = doc.body.firstElementChild
|
||||||
if (mainContent) {
|
if (mainContent) {
|
||||||
// 使用 importNode 确保所有事件和属性都被正确复制
|
|
||||||
this.shadowRoot?.appendChild(document.importNode(mainContent, true))
|
this.shadowRoot?.appendChild(document.importNode(mainContent, true))
|
||||||
} else {
|
} else {
|
||||||
// 如果没有根元素,将所有内容放入一个新的 div 中
|
|
||||||
const container = document.createElement('div')
|
const container = document.createElement('div')
|
||||||
container.innerHTML = template
|
container.innerHTML = template
|
||||||
this.shadowRoot?.appendChild(container)
|
this.shadowRoot?.appendChild(container)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user