2a60c2759a424ef854aa5c7245be5c1e9bb2eacc52d673859fa837216e20c9a5ce4fbba3bbdf0f86a91db8008f8a822eff8b0e4323524a2f7403a796e1c0a3 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* @flow */
  2. function getVNodeType (vnode: VNode): string {
  3. if (!vnode.tag) {
  4. return ''
  5. }
  6. return vnode.tag.replace(/vue\-component\-(\d+\-)?/, '')
  7. }
  8. function isSimpleSpan (vnode: VNode): boolean {
  9. return vnode.children &&
  10. vnode.children.length === 1 &&
  11. !vnode.children[0].tag
  12. }
  13. function parseStyle (vnode: VNode): Object | void {
  14. if (!vnode || !vnode.data) {
  15. return
  16. }
  17. const { staticStyle, staticClass } = vnode.data
  18. if (vnode.data.style || vnode.data.class || staticStyle || staticClass) {
  19. const styles = Object.assign({}, staticStyle, vnode.data.style)
  20. const cssMap = vnode.context.$options.style || {}
  21. const classList = [].concat(staticClass, vnode.data.class)
  22. classList.forEach(name => {
  23. if (name && cssMap[name]) {
  24. Object.assign(styles, cssMap[name])
  25. }
  26. })
  27. return styles
  28. }
  29. }
  30. function convertVNodeChildren (children: Array<VNode>): Array<VNode> | void {
  31. if (!children.length) {
  32. return
  33. }
  34. return children.map(vnode => {
  35. const type: string = getVNodeType(vnode)
  36. const props: Object = { type }
  37. // convert raw text node
  38. if (!type) {
  39. props.type = 'span'
  40. props.attr = {
  41. value: (vnode.text || '').trim()
  42. }
  43. } else {
  44. props.style = parseStyle(vnode)
  45. if (vnode.data) {
  46. props.attr = vnode.data.attrs
  47. if (vnode.data.on) {
  48. props.events = vnode.data.on
  49. }
  50. }
  51. if (type === 'span' && isSimpleSpan(vnode)) {
  52. props.attr = props.attr || {}
  53. props.attr.value = vnode.children[0].text.trim()
  54. return props
  55. }
  56. }
  57. if (vnode.children && vnode.children.length) {
  58. props.children = convertVNodeChildren(vnode.children)
  59. }
  60. return props
  61. })
  62. }
  63. export default {
  64. name: 'richtext',
  65. render (h: Function) {
  66. return h('weex:richtext', {
  67. on: this._events,
  68. attrs: {
  69. value: convertVNodeChildren(this.$options._renderChildren || [])
  70. }
  71. })
  72. }
  73. }