chore: avoid affecting the client's existing rendering behavior

Currently client-side rendering supports null and undefined, which prevents styles from being inherited from the parent node.
This commit is contained in:
daiwei 2024-11-22 14:01:02 +08:00
parent 86de25311f
commit e7ce2974d7
3 changed files with 4 additions and 35 deletions

View File

@ -904,7 +904,7 @@ function toStyleMap(str: string): Map<string, string> {
let [key, value] = item.split(':')
key = key.trim()
value = value && value.trim()
if (key && isRenderableAttrValue(value)) {
if (key) {
styleMap.set(key, value)
}
}

View File

@ -465,31 +465,4 @@ describe('useCssVars', () => {
render(h(App), root)
expect(colorInOnMount).toBe(`red`)
})
test('filter non-string、non-boolean、non-number value', () => {
const state = reactive<any>({
color: 'red',
size: {},
width: false,
height: 10,
})
const root = document.createElement('div')
let style!: CSSStyleDeclaration
const App = {
setup() {
useCssVars(() => state)
onMounted(() => {
style = (root.children[0] as HTMLElement).style
})
return () => h('div')
},
}
render(h(App), root)
expect(style.getPropertyValue(`--color`)).toBe(`red`)
expect(style.getPropertyValue(`--size`)).toBe(``)
expect(style.getPropertyValue(`--width`)).toBe(`false`)
expect(style.getPropertyValue(`--height`)).toBe(`10`)
})
})

View File

@ -10,7 +10,7 @@ import {
warn,
watch,
} from '@vue/runtime-core'
import { NOOP, ShapeFlags, isRenderableAttrValue } from '@vue/shared'
import { NOOP, ShapeFlags } from '@vue/shared'
export const CSS_VAR_TEXT: unique symbol = Symbol(__DEV__ ? 'CSS_VAR_TEXT' : '')
/**
@ -99,12 +99,8 @@ function setVarsOnNode(el: Node, vars: Record<string, string>) {
const style = (el as HTMLElement).style
let cssText = ''
for (const key in vars) {
const value = vars[key]
if (isRenderableAttrValue(value)) {
const trimVal = String(value).trim()
style.setProperty(`--${key}`, trimVal)
cssText += `--${key}: ${trimVal};`
}
style.setProperty(`--${key}`, vars[key])
cssText += `--${key}: ${vars[key]};`
}
;(style as any)[CSS_VAR_TEXT] = cssText
}