style.js
1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* @flow */
import { escape, noUnitNumericStyleProps } from '../util'
import { hyphenate } from 'shared/util'
import { getStyle } from 'web/util/style'
export function genStyle (style: Object): string {
let styleText = ''
for (const key in style) {
const value = style[key]
const hyphenatedKey = hyphenate(key)
if (Array.isArray(value)) {
for (let i = 0, len = value.length; i < len; i++) {
styleText += normalizeValue(hyphenatedKey, value[i])
}
} else {
styleText += normalizeValue(hyphenatedKey, value)
}
}
return styleText
}
function normalizeValue(key: string, value: any): string {
if (
typeof value === 'string' ||
(typeof value === 'number' && noUnitNumericStyleProps[key]) ||
value === 0
) {
return `${key}:${value};`
} else {
// invalid values
return ``
}
}
export default function renderStyle (vnode: VNodeWithData): ?string {
const styleText = genStyle(getStyle(vnode, false))
if (styleText !== '') {
return ` style=${JSON.stringify(escape(styleText))}`
}
}