You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.0 KiB
41 lines
1.0 KiB
/* @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))}`
|
|
}
|
|
}
|
|
|