Skip to content

Commit

Permalink
fix vuejs#4055, generate style on custom component
Browse files Browse the repository at this point in the history
  • Loading branch information
HerringtonDarkholme committed Oct 31, 2016
1 parent 0d35aa5 commit b320ae3
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions src/platforms/web/server/modules/style.js
Expand Up @@ -2,24 +2,43 @@

import { hyphenate, toObject } from 'shared/util'

export default function renderStyle (node: VNodeWithData): ?string {
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
if (node.data.style || staticStyle) {
let styles = node.data.style
let res = ''
if (styles) {
if (typeof styles === 'string') {
res += styles
} else {
if (Array.isArray(styles)) {
styles = toObject(styles)
}
for (const key in styles) {
res += `${hyphenate(key)}:${styles[key]};`
}
res += staticStyle || ''
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)}`
}
}

0 comments on commit b320ae3

Please sign in to comment.