23e84399ecefbcc9c580af100c4d01054e0050920c4abf4cd59aaa7d3ea8d1025d3d266f695773840a88e3a805875889b487ebfaace14e688b33161af9bfb5 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* @flow */
  2. import { cached, extend, toObject } from 'shared/util'
  3. export const parseStyleText = cached(function (cssText) {
  4. const res = {}
  5. const listDelimiter = /;(?![^(]*\))/g
  6. const propertyDelimiter = /:(.+)/
  7. cssText.split(listDelimiter).forEach(function (item) {
  8. if (item) {
  9. const tmp = item.split(propertyDelimiter)
  10. tmp.length > 1 && (res[tmp[0].trim()] = tmp[1].trim())
  11. }
  12. })
  13. return res
  14. })
  15. // merge static and dynamic style data on the same vnode
  16. function normalizeStyleData (data: VNodeData): ?Object {
  17. const style = normalizeStyleBinding(data.style)
  18. // static style is pre-processed into an object during compilation
  19. // and is always a fresh object, so it's safe to merge into it
  20. return data.staticStyle
  21. ? extend(data.staticStyle, style)
  22. : style
  23. }
  24. // normalize possible array / string values into Object
  25. export function normalizeStyleBinding (bindingStyle: any): ?Object {
  26. if (Array.isArray(bindingStyle)) {
  27. return toObject(bindingStyle)
  28. }
  29. if (typeof bindingStyle === 'string') {
  30. return parseStyleText(bindingStyle)
  31. }
  32. return bindingStyle
  33. }
  34. /**
  35. * parent component style should be after child's
  36. * so that parent component's style could override it
  37. */
  38. export function getStyle (vnode: VNodeWithData, checkChild: boolean): Object {
  39. const res = {}
  40. let styleData
  41. if (checkChild) {
  42. let childNode = vnode
  43. while (childNode.componentInstance) {
  44. childNode = childNode.componentInstance._vnode
  45. if (
  46. childNode && childNode.data &&
  47. (styleData = normalizeStyleData(childNode.data))
  48. ) {
  49. extend(res, styleData)
  50. }
  51. }
  52. }
  53. if ((styleData = normalizeStyleData(vnode.data))) {
  54. extend(res, styleData)
  55. }
  56. let parentNode = vnode
  57. while ((parentNode = parentNode.parent)) {
  58. if (parentNode.data && (styleData = normalizeStyleData(parentNode.data))) {
  59. extend(res, styleData)
  60. }
  61. }
  62. return res
  63. }