Skip to content

Commit ffb7ba7

Browse files
authored
fix(runtime-vapor): prevent passing an empty string to classList.add (#12974)
1 parent 978c47f commit ffb7ba7

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

packages/runtime-vapor/__tests__/componentAttrs.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,43 @@ describe('attribute fallthrough', () => {
322322
expect(el.getAttribute('aria-x')).toBe(parentVal.value)
323323
expect(el.getAttribute('aria-y')).toBe(parentVal.value)
324324
})
325+
326+
it('empty string should not be passed to classList.add', async () => {
327+
const t0 = template('<div>', true /* root */)
328+
const Child = defineVaporComponent({
329+
setup() {
330+
const n = t0() as Element
331+
renderEffect(() => {
332+
setClass(n, {
333+
foo: false,
334+
})
335+
})
336+
return n
337+
},
338+
})
339+
340+
const Parent = defineVaporComponent({
341+
setup() {
342+
return createComponent(
343+
Child,
344+
{
345+
class: () => ({
346+
bar: false,
347+
}),
348+
},
349+
null,
350+
true,
351+
)
352+
},
353+
})
354+
355+
const { host } = define({
356+
setup() {
357+
return createComponent(Parent)
358+
},
359+
}).render()
360+
361+
const el = host.children[0]
362+
expect(el.classList.length).toBe(0)
363+
})
325364
})

packages/runtime-vapor/src/dom/prop.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ function setClassIncremental(el: any, value: any): void {
122122
const prev = el[cacheKey]
123123
if ((value = el[cacheKey] = normalizeClass(value)) !== prev) {
124124
const nextList = value.split(/\s+/)
125-
el.classList.add(...nextList)
125+
if (value) {
126+
el.classList.add(...nextList)
127+
}
126128
if (prev) {
127129
for (const cls of prev.split(/\s+/)) {
128130
if (!nextList.includes(cls)) el.classList.remove(cls)

0 commit comments

Comments
 (0)