Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions packages/runtime-vapor/__tests__/dom/svg.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, (string | null)[]> = { vdom: [], vapor: [] }
await renderParity(
{
App: `<template><svg v-bind="data.attrs" /></template>`,
},
() => ref<{ attrs: Record<string, any> }>({ 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<string, (string | null)[]> = { vdom: [], vapor: [] }
await renderParity(
{
App: `<template><div><svg v-bind="data.attrs" /></div></template>`,
},
() => ref<{ attrs: Record<string, any> }>({ 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<string, (string | null)[]> = { vdom: [], vapor: [] }
await renderParity(
{
App: `<template><svg class="" v-bind="data.attrs" /></template>`,
},
() => ref<{ attrs: Record<string, any> }>({ 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<string, (string | null)[]> = {
vdom: [],
vapor: [],
}
await renderParity(
{
Child: `<template><svg v-bind="data.child" /></template>`,
App: `<template><components.Child v-bind="data.parent" /></template>`,
},
() =>
ref<{ parent: Record<string, any>; child: Record<string, any> }>({
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)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -3826,7 +3826,7 @@ describe('VDOM interop', () => {
data.value.label = 'close'
data.value.attrs = {}
await nextTick()
expect(button.outerHTML).toBe(`<button id="x" class="">close</button>`)
expect(button.outerHTML).toBe(`<button id="x">close</button>`)
})

// Reka UI in a component library: a vdom component forwards the slot to
Expand Down
78 changes: 53 additions & 25 deletions packages/runtime-vapor/src/dom/prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}
Expand Down Expand Up @@ -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) &&
Expand All @@ -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
Expand Down Expand Up @@ -874,17 +894,24 @@ export function adoptRoot(el: any, attrs?: Record<string, any>): 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<string>(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')) {
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions packages/runtime-vapor/src/dom/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from './hydration'
import { insertionParent, resetInsertionState } from '../insertionState'
import {
EMPTY_ARR,
type Namespace,
type NormalizedStyle,
TemplateFlags,
Expand All @@ -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
}

Expand All @@ -48,15 +49,17 @@ 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 = () => {
resolved = true
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)
}
Expand Down