forked from vuejs/vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
style.js
44 lines (37 loc) · 1.12 KB
/
style.js
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
42
43
44
/* @flow */
import { hyphenate, toObject } from 'shared/util'
function concatStyleString (former: string, latter: string) {
if (former === '' || latter === '' || former.charAt(former.length - 1) === ';') {
return former + latter
}
return former + ';' + latter
}
function generateStyleText (node) {
const staticStyle = node.data.attrs && node.data.attrs.style
let styles = node.data.style
const parentStyle = node.parent ? generateStyleText(node.parent) : ''
if (!styles && !staticStyle) {
return parentStyle
}
let dynamicStyle = ''
if (styles) {
if (typeof styles === 'string') {
dynamicStyle += styles
} else {
if (Array.isArray(styles)) {
styles = toObject(styles)
}
for (const key in styles) {
dynamicStyle += `${hyphenate(key)}:${styles[key]};`
}
}
}
dynamicStyle = concatStyleString(parentStyle, dynamicStyle)
return concatStyleString(dynamicStyle, staticStyle || '')
}
export default function renderStyle (node: VNodeWithData): ?string {
const res = generateStyleText(node)
if (res) {
return ` style=${JSON.stringify(res)}`
}
}