mirror of https://github.com/vuejs/core.git
chore: update setVarsOnNode
This commit is contained in:
parent
7d5a67a35c
commit
86de25311f
|
@ -465,4 +465,31 @@ describe('useCssVars', () => {
|
||||||
render(h(App), root)
|
render(h(App), root)
|
||||||
expect(colorInOnMount).toBe(`red`)
|
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`)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -10,7 +10,7 @@ import {
|
||||||
warn,
|
warn,
|
||||||
watch,
|
watch,
|
||||||
} from '@vue/runtime-core'
|
} from '@vue/runtime-core'
|
||||||
import { NOOP, ShapeFlags } from '@vue/shared'
|
import { NOOP, ShapeFlags, isRenderableAttrValue } from '@vue/shared'
|
||||||
|
|
||||||
export const CSS_VAR_TEXT: unique symbol = Symbol(__DEV__ ? 'CSS_VAR_TEXT' : '')
|
export const CSS_VAR_TEXT: unique symbol = Symbol(__DEV__ ? 'CSS_VAR_TEXT' : '')
|
||||||
/**
|
/**
|
||||||
|
@ -99,8 +99,12 @@ function setVarsOnNode(el: Node, vars: Record<string, string>) {
|
||||||
const style = (el as HTMLElement).style
|
const style = (el as HTMLElement).style
|
||||||
let cssText = ''
|
let cssText = ''
|
||||||
for (const key in vars) {
|
for (const key in vars) {
|
||||||
style.setProperty(`--${key}`, vars[key])
|
const value = vars[key]
|
||||||
cssText += `--${key}: ${vars[key]};`
|
if (isRenderableAttrValue(value)) {
|
||||||
|
const trimVal = String(value).trim()
|
||||||
|
style.setProperty(`--${key}`, trimVal)
|
||||||
|
cssText += `--${key}: ${trimVal};`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
;(style as any)[CSS_VAR_TEXT] = cssText
|
;(style as any)[CSS_VAR_TEXT] = cssText
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue