From fa006aca41858963d00e137bc30e3a52f669e001 Mon Sep 17 00:00:00 2001 From: SerageLdin Date: Tue, 24 Nov 2026 15:52:26 +0300 Subject: [PATCH] fix(runtime-vapor): preserve omitted vs empty class attribute in setClass --- .../runtime-vapor/__tests__/dom/svg.spec.ts | 125 ++++++++++++++++++ .../__tests__/hydration/interopSlots.spec.ts | 2 +- packages/runtime-vapor/src/dom/prop.ts | 78 +++++++---- packages/runtime-vapor/src/dom/template.ts | 9 +- 4 files changed, 185 insertions(+), 29 deletions(-) diff --git a/packages/runtime-vapor/__tests__/dom/svg.spec.ts b/packages/runtime-vapor/__tests__/dom/svg.spec.ts index 19812c2296a..d0e943c06de 100644 --- a/packages/runtime-vapor/__tests__/dom/svg.spec.ts +++ b/packages/runtime-vapor/__tests__/dom/svg.spec.ts @@ -225,4 +225,129 @@ describe('SVG support', () => { }, ) }) + + test('should preserve omitted vs empty class on svg root with object v-bind', async () => { + const seen: Record = { vdom: [], vapor: [] } + await renderParity( + { + App: ``, + }, + () => ref<{ attrs: Record }>({ attrs: {} }), + async (data, root, mode) => { + const svg = root.querySelector('svg')! + seen[mode].push(svg.getAttribute('class')) + + data.value.attrs = { class: '' } + await nextTick() + seen[mode].push(svg.getAttribute('class')) + + data.value.attrs = { class: 'active' } + await nextTick() + seen[mode].push(svg.getAttribute('class')) + + data.value.attrs = {} + await nextTick() + seen[mode].push(svg.getAttribute('class')) + + data.value.attrs = { class: { active: false } } + await nextTick() + seen[mode].push(svg.getAttribute('class')) + + data.value.attrs = { class: null } + await nextTick() + seen[mode].push(svg.getAttribute('class')) + }, + ) + expect(seen.vdom).toEqual([null, '', 'active', null, '', null]) + expect(seen.vapor).toEqual(seen.vdom) + }) + + test('should preserve omitted vs empty class on non-root svg with object v-bind', async () => { + const seen: Record = { vdom: [], vapor: [] } + await renderParity( + { + App: ``, + }, + () => ref<{ attrs: Record }>({ attrs: {} }), + async (data, root, mode) => { + const svg = root.querySelector('svg')! + seen[mode].push(svg.getAttribute('class')) + + data.value.attrs = { class: '' } + await nextTick() + seen[mode].push(svg.getAttribute('class')) + + data.value.attrs = { class: 'active' } + await nextTick() + seen[mode].push(svg.getAttribute('class')) + + data.value.attrs = {} + await nextTick() + seen[mode].push(svg.getAttribute('class')) + }, + ) + expect(seen.vdom).toEqual([null, '', 'active', null]) + expect(seen.vapor).toEqual(seen.vdom) + }) + + test('should preserve authored empty class and fallthrough class on svg root', async () => { + const authored: Record = { vdom: [], vapor: [] } + await renderParity( + { + App: ``, + }, + () => ref<{ attrs: Record }>({ attrs: {} }), + async (data, root, mode) => { + const svg = root.querySelector('svg')! + authored[mode].push(svg.getAttribute('class')) + + data.value.attrs = { class: 'active' } + await nextTick() + authored[mode].push(svg.getAttribute('class')) + + data.value.attrs = {} + await nextTick() + authored[mode].push(svg.getAttribute('class')) + }, + ) + expect(authored.vdom).toEqual(['', 'active', '']) + expect(authored.vapor).toEqual(authored.vdom) + + const fallthrough: Record = { + vdom: [], + vapor: [], + } + await renderParity( + { + Child: ``, + App: ``, + }, + () => + ref<{ parent: Record; child: Record }>({ + parent: { class: 'fall' }, + child: { class: 'active' }, + }), + async (data, root, mode) => { + const svg = root.querySelector('svg')! + fallthrough[mode].push(svg.getAttribute('class')) + + data.value.child = {} + await nextTick() + fallthrough[mode].push(svg.getAttribute('class')) + + data.value.parent = {} + await nextTick() + fallthrough[mode].push(svg.getAttribute('class')) + + data.value.parent = { class: '' } + await nextTick() + fallthrough[mode].push(svg.getAttribute('class')) + + data.value.parent = {} + await nextTick() + fallthrough[mode].push(svg.getAttribute('class')) + }, + ) + expect(fallthrough.vapor).toEqual(fallthrough.vdom) + }) }) diff --git a/packages/runtime-vapor/__tests__/hydration/interopSlots.spec.ts b/packages/runtime-vapor/__tests__/hydration/interopSlots.spec.ts index d878d9d701d..185c639548b 100644 --- a/packages/runtime-vapor/__tests__/hydration/interopSlots.spec.ts +++ b/packages/runtime-vapor/__tests__/hydration/interopSlots.spec.ts @@ -3826,7 +3826,7 @@ describe('VDOM interop', () => { data.value.label = 'close' data.value.attrs = {} await nextTick() - expect(button.outerHTML).toBe(``) + expect(button.outerHTML).toBe(``) }) // Reka UI in a component library: a vdom component forwards the slot to diff --git a/packages/runtime-vapor/src/dom/prop.ts b/packages/runtime-vapor/src/dom/prop.ts index 10445e7ccaa..7fd02d75059 100644 --- a/packages/runtime-vapor/src/dom/prop.ts +++ b/packages/runtime-vapor/src/dom/prop.ts @@ -83,12 +83,12 @@ import { queueSelectUpdates, registerSelectUpdate } from '../renderEffect' type TargetElement = Element & { $root?: boolean | RootMeta $html?: string - $cls?: string + $cls?: string | null $clsFlags?: number // a root's own class / style and the fallthrough one are two layers on // one element; `$sty` is the style merge last written - $clsi?: string - $clsi$?: string + $clsi?: string | null + $clsi$?: string | null $styi?: NormalizedStyle $styi$?: NormalizedStyle $sty?: NormalizedStyle | string | undefined @@ -257,27 +257,32 @@ export function setClass( if (el.$root) { setClassIncremental(el, value, isNormalized) } else { - if (!isNormalized) value = normalizeClass(value) + const normalizedValue = + value == null ? null : isNormalized ? value : normalizeClass(value) if (isHydrating && !isRecreatedNode(el)) { if (__DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__) { if (isClonedSlotContent()) { - queuePostFlushCb(() => classHasMismatch(el, value, !!el.$root)) + queuePostFlushCb(() => + classHasMismatch(el, normalizedValue || '', !!el.$root), + ) } else { - classHasMismatch(el, value, false) + classHasMismatch(el, normalizedValue || '', false) } } - el.$cls = value + el.$cls = normalizedValue return } - if (value !== el.$cls) { - el.$cls = value + if (normalizedValue !== el.$cls) { + el.$cls = normalizedValue if (isTransitionEnabled) { - patchClass(el, value, isSVG) + patchClass(el, normalizedValue, isSVG) + } else if (normalizedValue == null) { + el.removeAttribute('class') } else if (isSVG) { - el.setAttribute('class', value) + el.setAttribute('class', normalizedValue) } else { - el.className = value + el.className = normalizedValue } } } @@ -332,23 +337,24 @@ function setClassIncremental( isNormalized: boolean = false, ): void { const cacheKey = isApplyingFallthroughProps ? '$clsi$' : '$clsi' - const normalizedValue = isNormalized ? value : normalizeClass(value) + const normalizedValue = + value == null ? null : isNormalized ? value : normalizeClass(value) if (isHydrating && !isRecreatedNode(el)) { ;(__DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__) && - classHasMismatch(el, normalizedValue, true) + classHasMismatch(el, normalizedValue || '', true) el[cacheKey] = normalizedValue return } const prev = el[cacheKey] if ((value = el[cacheKey] = normalizedValue) !== prev) { - const nextList = value.split(/\s+/) + const nextList = value ? value.split(/\s+/) : EMPTY_ARR if (value) { el.classList.add(...nextList) } if (prev) { - let kept: string[] | undefined + let kept: readonly string[] | undefined for (const cls of prev.split(/\s+/)) { if ( !nextList.includes(cls) && @@ -358,14 +364,28 @@ function setClassIncremental( } } } + if (value === '') { + if (!el.hasAttribute('class')) { + el.setAttribute('class', '') + } + } else if (value === null && !el.classList.length) { + const other = isApplyingFallthroughProps ? el.$clsi : el.$clsi$ + if (other == null && el.$root.cls === undefined) { + el.removeAttribute('class') + } + } } } // tokens the other layer still holds; a root's own binding replaces its // template class -function keptClasses(el: any): string[] { +function keptClasses(el: any): readonly string[] { const other = isApplyingFallthroughProps ? el.$clsi : el.$clsi$ - return other !== undefined ? other.split(/\s+/) : el.$root.cls || EMPTY_ARR + return other != null + ? other + ? other.split(/\s+/) + : EMPTY_ARR + : el.$root.cls || EMPTY_ARR } // Defer css-var style mismatch checks until instance.block is set, so root @@ -874,17 +894,24 @@ export function adoptRoot(el: any, attrs?: Record): void { // normalized them) are taken off. const tpl: RootMeta | undefined = isHydrated ? el.$tpl : undefined const hydrated = attrs && isHydrated && !tpl ? attrs : undefined - const meta: { cls?: string[]; sty?: NormalizedStyle } = {} + const meta: { cls?: readonly string[]; sty?: NormalizedStyle } = {} // the caches default to '' on the prototype (optimizePropertyLookup) if (hasOwn(el, '$cls')) { el.$clsi = el.$cls } else if (tpl) { meta.cls = tpl.cls - } else if (el.classList.length) { - meta.cls = Array.from(el.classList) - if (hydrated && hydrated.class) { - const merged = hydrated.class.split(/\s+/) - meta.cls = meta.cls.filter(cls => !merged.includes(cls)) + } else if (el.hasAttribute('class')) { + if (el.classList.length) { + let cls = Array.from(el.classList) + if (hydrated && hydrated.class) { + const merged = hydrated.class.split(/\s+/) + cls = cls.filter(c => !merged.includes(c)) + if (cls.length) meta.cls = cls + } else { + meta.cls = cls + } + } else if (!(hydrated && 'class' in hydrated)) { + meta.cls = EMPTY_ARR } } if (hasOwn(el, '$sty')) { @@ -926,7 +953,8 @@ export function optimizePropertyLookup(): void { proto.$vei = undefined proto.$root = false proto.$clsFlags = undefined - proto.$cls = proto.$sty = '' + proto.$cls = undefined + proto.$sty = '' // same reason as $txt below: an empty string would make the first // setHtml(el, '') a no-op and leave the original children in place proto.$html = undefined diff --git a/packages/runtime-vapor/src/dom/template.ts b/packages/runtime-vapor/src/dom/template.ts index 96f6dcaa43a..5f62c9e0515 100644 --- a/packages/runtime-vapor/src/dom/template.ts +++ b/packages/runtime-vapor/src/dom/template.ts @@ -14,6 +14,7 @@ import { } from './hydration' import { insertionParent, resetInsertionState } from '../insertionState' import { + EMPTY_ARR, type Namespace, type NormalizedStyle, TemplateFlags, @@ -27,7 +28,7 @@ import { isInteropEnabled } from '../vdomInteropState' // the class and style a root's template contributes, shared by all its // instances (see the incremental setters in prop.ts) export type RootMeta = { - readonly cls?: string[] + readonly cls?: readonly string[] readonly sty?: NormalizedStyle } @@ -48,7 +49,7 @@ export function template(html: string, flags: number = 0, ns?: Namespace) { // already carries the SSR fallthrough values); most roots never need it let rootMeta: RootMeta | undefined const createRootMeta = (): RootMeta => { - let cls: string[] | undefined + let cls: readonly string[] | undefined let sty: NormalizedStyle | undefined let resolved = false const resolve = () => { @@ -56,7 +57,9 @@ export function template(html: string, flags: number = 0, ns?: Namespace) { if (html.includes(' class=') || html.includes(' style=')) { const el = (node ||= parseTemplate(html, ns)) as Element // the DOM tokenizes on ASCII whitespace only, unlike `\s` - if (el.classList.length) cls = Array.from(el.classList) + if (el.hasAttribute('class')) { + cls = el.classList.length ? Array.from(el.classList) : EMPTY_ARR + } const style = el.getAttribute('style') if (style) sty = parseStringStyle(style) }