419dbed276d7d40c3a602330de68da268ec7d8ec4d1e04e1f1c8e046b568ace93d1a7f592ca78c7bcfa8adabebd5da6d2f4350da5f60c3eb945fe6b5b7544b 1.0 KB

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