Skip to content

Commit

Permalink
chore: avoid affecting the client's existing rendering behavior
Browse files Browse the repository at this point in the history
Currently client-side rendering supports null and undefined, which prevents styles from being inherited from the parent node.
  • Loading branch information
edison1105 committed Nov 22, 2024
1 parent 86de253 commit e7ce297
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 35 deletions.
2 changes: 1 addition & 1 deletion packages/runtime-core/src/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
27 changes: 0 additions & 27 deletions packages/runtime-dom/__tests__/helpers/useCssVars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
})
})
10 changes: 3 additions & 7 deletions packages/runtime-dom/src/helpers/useCssVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' : '')
/**
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit e7ce297

Please sign in to comment.