fix(runtime-vapor): prevent passing an empty string to classList.add (#12974)

This commit is contained in:
zhiyuanzmj 2025-06-18 08:44:50 +08:00 committed by GitHub
parent 978c47f751
commit ffb7ba77c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 1 deletions

View File

@ -322,4 +322,43 @@ describe('attribute fallthrough', () => {
expect(el.getAttribute('aria-x')).toBe(parentVal.value) expect(el.getAttribute('aria-x')).toBe(parentVal.value)
expect(el.getAttribute('aria-y')).toBe(parentVal.value) expect(el.getAttribute('aria-y')).toBe(parentVal.value)
}) })
it('empty string should not be passed to classList.add', async () => {
const t0 = template('<div>', true /* root */)
const Child = defineVaporComponent({
setup() {
const n = t0() as Element
renderEffect(() => {
setClass(n, {
foo: false,
})
})
return n
},
})
const Parent = defineVaporComponent({
setup() {
return createComponent(
Child,
{
class: () => ({
bar: false,
}),
},
null,
true,
)
},
})
const { host } = define({
setup() {
return createComponent(Parent)
},
}).render()
const el = host.children[0]
expect(el.classList.length).toBe(0)
})
}) })

View File

@ -122,7 +122,9 @@ function setClassIncremental(el: any, value: any): void {
const prev = el[cacheKey] const prev = el[cacheKey]
if ((value = el[cacheKey] = normalizeClass(value)) !== prev) { if ((value = el[cacheKey] = normalizeClass(value)) !== prev) {
const nextList = value.split(/\s+/) const nextList = value.split(/\s+/)
el.classList.add(...nextList) if (value) {
el.classList.add(...nextList)
}
if (prev) { if (prev) {
for (const cls of prev.split(/\s+/)) { for (const cls of prev.split(/\s+/)) {
if (!nextList.includes(cls)) el.classList.remove(cls) if (!nextList.includes(cls)) el.classList.remove(cls)