bb3a7fe38a63423d159daf6fac75a87652cf0448b8e505e626b1b8ba656e2685f7cbf33f5e9eadde71518dd09bb4fdb02299766682b36846e7df94313c5e60 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* @flow */
  2. import { isDef, isObject } from 'shared/util'
  3. export function genClassForVnode (vnode: VNodeWithData): string {
  4. let data = vnode.data
  5. let parentNode = vnode
  6. let childNode = vnode
  7. while (isDef(childNode.componentInstance)) {
  8. childNode = childNode.componentInstance._vnode
  9. if (childNode && childNode.data) {
  10. data = mergeClassData(childNode.data, data)
  11. }
  12. }
  13. while (isDef(parentNode = parentNode.parent)) {
  14. if (parentNode && parentNode.data) {
  15. data = mergeClassData(data, parentNode.data)
  16. }
  17. }
  18. return renderClass(data.staticClass, data.class)
  19. }
  20. function mergeClassData (child: VNodeData, parent: VNodeData): {
  21. staticClass: string,
  22. class: any
  23. } {
  24. return {
  25. staticClass: concat(child.staticClass, parent.staticClass),
  26. class: isDef(child.class)
  27. ? [child.class, parent.class]
  28. : parent.class
  29. }
  30. }
  31. export function renderClass (
  32. staticClass: ?string,
  33. dynamicClass: any
  34. ): string {
  35. if (isDef(staticClass) || isDef(dynamicClass)) {
  36. return concat(staticClass, stringifyClass(dynamicClass))
  37. }
  38. /* istanbul ignore next */
  39. return ''
  40. }
  41. export function concat (a: ?string, b: ?string): string {
  42. return a ? b ? (a + ' ' + b) : a : (b || '')
  43. }
  44. export function stringifyClass (value: any): string {
  45. if (Array.isArray(value)) {
  46. return stringifyArray(value)
  47. }
  48. if (isObject(value)) {
  49. return stringifyObject(value)
  50. }
  51. if (typeof value === 'string') {
  52. return value
  53. }
  54. /* istanbul ignore next */
  55. return ''
  56. }
  57. function stringifyArray (value: Array<any>): string {
  58. let res = ''
  59. let stringified
  60. for (let i = 0, l = value.length; i < l; i++) {
  61. if (isDef(stringified = stringifyClass(value[i])) && stringified !== '') {
  62. if (res) res += ' '
  63. res += stringified
  64. }
  65. }
  66. return res
  67. }
  68. function stringifyObject (value: Object): string {
  69. let res = ''
  70. for (const key in value) {
  71. if (value[key]) {
  72. if (res) res += ' '
  73. res += key
  74. }
  75. }
  76. return res
  77. }