create-element.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* @flow */
  2. import VNode, { emptyVNode } from './vnode'
  3. import config from '../config'
  4. import { createComponent } from './create-component'
  5. import { normalizeChildren } from './helpers'
  6. import { renderState } from '../instance/render'
  7. import { warn, resolveAsset } from '../util/index'
  8. export function renderElementWithChildren (
  9. vnode: VNode | void,
  10. children: VNodeChildren | void
  11. ): VNode | void {
  12. if (vnode) {
  13. if (vnode.componentOptions) {
  14. if (process.env.NODE_ENV !== 'production' &&
  15. children && typeof children !== 'function') {
  16. warn(
  17. 'A component\'s children should be a function that returns the ' +
  18. 'children array. This allows the component to track the children ' +
  19. 'dependencies and optimizes re-rendering.'
  20. )
  21. }
  22. vnode.componentOptions.children = children
  23. } else {
  24. vnode.setChildren(normalizeChildren(children))
  25. }
  26. }
  27. return vnode
  28. }
  29. export function renderElement (
  30. tag?: string | Class<Component> | Function | Object,
  31. data?: VNodeData,
  32. namespace?: string
  33. ): VNode | void {
  34. // make sure to expose real self instead of proxy
  35. const context: Component = this._self
  36. const parent: ?Component = renderState.activeInstance
  37. if (!parent) {
  38. process.env.NODE_ENV !== 'production' && warn(
  39. 'createElement cannot be called outside of component ' +
  40. 'render functions.'
  41. )
  42. return
  43. }
  44. if (!tag) {
  45. // in case of component :is set to falsy value
  46. return emptyVNode()
  47. }
  48. if (typeof tag === 'string') {
  49. let Ctor
  50. if (config.isReservedTag(tag)) {
  51. return new VNode(
  52. tag, data, undefined,
  53. undefined, undefined, namespace, context
  54. )
  55. } else if ((Ctor = resolveAsset(context.$options, 'components', tag))) {
  56. return createComponent(Ctor, data, parent, context, tag)
  57. } else {
  58. if (process.env.NODE_ENV !== 'production') {
  59. if (!namespace && config.isUnknownElement(tag)) {
  60. warn(
  61. 'Unknown custom element: <' + tag + '> - did you ' +
  62. 'register the component correctly? For recursive components, ' +
  63. 'make sure to provide the "name" option.'
  64. )
  65. }
  66. }
  67. return new VNode(
  68. tag, data, undefined,
  69. undefined, undefined, namespace, context
  70. )
  71. }
  72. } else {
  73. return createComponent(tag, data, parent, context)
  74. }
  75. }
  76. export function renderText (str?: string): string {
  77. return str || ''
  78. }
  79. export function renderStatic (index?: number): Object | void {
  80. return this._staticTrees[index]
  81. }