src/core/vdom/create-element.ts TYPESCRIPT 173 lines View on github.com → Search inside
1import config from '../config'2import VNode, { createEmptyVNode } from './vnode'3import { createComponent } from './create-component'4import { traverse } from '../observer/traverse'56import {7  warn,8  isDef,9  isUndef,10  isArray,11  isTrue,12  isObject,13  isPrimitive,14  resolveAsset,15  isFunction16} from '../util/index'1718import { normalizeChildren, simpleNormalizeChildren } from './helpers/index'19import type { Component } from 'types/component'20import type { VNodeData } from 'types/vnode'2122const SIMPLE_NORMALIZE = 123const ALWAYS_NORMALIZE = 22425// wrapper function for providing a more flexible interface26// without getting yelled at by flow27export function createElement(28  context: Component,29  tag: any,30  data: any,31  children: any,32  normalizationType: any,33  alwaysNormalize: boolean34): VNode | Array<VNode> {35  if (isArray(data) || isPrimitive(data)) {36    normalizationType = children37    children = data38    data = undefined39  }40  if (isTrue(alwaysNormalize)) {41    normalizationType = ALWAYS_NORMALIZE42  }43  return _createElement(context, tag, data, children, normalizationType)44}4546export function _createElement(47  context: Component,48  tag?: string | Component | Function | Object,49  data?: VNodeData,50  children?: any,51  normalizationType?: number52): VNode | Array<VNode> {53  if (isDef(data) && isDef((data as any).__ob__)) {54    __DEV__ &&55      warn(56        `Avoid using observed data object as vnode data: ${JSON.stringify(57          data58        )}\n` + 'Always create fresh vnode data objects in each render!',59        context60      )61    return createEmptyVNode()62  }63  // object syntax in v-bind64  if (isDef(data) && isDef(data.is)) {65    tag = data.is66  }67  if (!tag) {68    // in case of component :is set to falsy value69    return createEmptyVNode()70  }71  // warn against non-primitive key72  if (__DEV__ && isDef(data) && isDef(data.key) && !isPrimitive(data.key)) {73    warn(74      'Avoid using non-primitive value as key, ' +75        'use string/number value instead.',76      context77    )78  }79  // support single function children as default scoped slot80  if (isArray(children) && isFunction(children[0])) {81    data = data || {}82    data.scopedSlots = { default: children[0] }83    children.length = 084  }85  if (normalizationType === ALWAYS_NORMALIZE) {86    children = normalizeChildren(children)87  } else if (normalizationType === SIMPLE_NORMALIZE) {88    children = simpleNormalizeChildren(children)89  }90  let vnode, ns91  if (typeof tag === 'string') {92    let Ctor93    ns = (context.$vnode && context.$vnode.ns) || config.getTagNamespace(tag)94    if (config.isReservedTag(tag)) {95      // platform built-in elements96      if (97        __DEV__ &&98        isDef(data) &&99        isDef(data.nativeOn) &&100        data.tag !== 'component'101      ) {102        warn(103          `The .native modifier for v-on is only valid on components but it was used on <${tag}>.`,104          context105        )106      }107      vnode = new VNode(108        config.parsePlatformTagName(tag),109        data,110        children,111        undefined,112        undefined,113        context114      )115    } else if (116      (!data || !data.pre) &&117      isDef((Ctor = resolveAsset(context.$options, 'components', tag)))118    ) {119      // component120      vnode = createComponent(Ctor, data, context, children, tag)121    } else {122      // unknown or unlisted namespaced elements123      // check at runtime because it may get assigned a namespace when its124      // parent normalizes children125      vnode = new VNode(tag, data, children, undefined, undefined, context)126    }127  } else {128    // direct component options / constructor129    vnode = createComponent(tag as any, data, context, children)130  }131  if (isArray(vnode)) {132    return vnode133  } else if (isDef(vnode)) {134    if (isDef(ns)) applyNS(vnode, ns)135    if (isDef(data)) registerDeepBindings(data)136    return vnode137  } else {138    return createEmptyVNode()139  }140}141142function applyNS(vnode, ns, force?: boolean) {143  vnode.ns = ns144  if (vnode.tag === 'foreignObject') {145    // use default namespace inside foreignObject146    ns = undefined147    force = true148  }149  if (isDef(vnode.children)) {150    for (let i = 0, l = vnode.children.length; i < l; i++) {151      const child = vnode.children[i]152      if (153        isDef(child.tag) &&154        (isUndef(child.ns) || (isTrue(force) && child.tag !== 'svg'))155      ) {156        applyNS(child, ns, force)157      }158    }159  }160}161162// ref #5318163// necessary to ensure parent re-render when deep bindings like :style and164// :class are used on slot nodes165function registerDeepBindings(data) {166  if (isObject(data.style)) {167    traverse(data.style)168  }169  if (isObject(data.class)) {170    traverse(data.class)171  }172}

Findings

✓ No findings reported for this file.

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.