Browse Source

allow data to be undefined

dev
Evan You 9 years ago
parent
commit
9a347e02b4
  1. 9
      src/compiler/codegen/index.js
  2. 4
      src/compiler/parser/index.js
  3. 9
      src/runtime/dom/modules/class.js
  4. 2
      src/runtime/vdom/create-component.js
  5. 9
      src/runtime/vdom/create-element.js
  6. 61
      src/runtime/vdom/patch.js
  7. 3
      src/runtime/vdom/vnode.js

9
src/compiler/codegen/index.js

@ -23,7 +23,7 @@ function genElement (el) {
// if the element is potentially a component,
// wrap its children as a thunk.
const children = genChildren(el, !isReservedTag(el.tag))
return `__h__('${el.tag}', ${genData(el)}, ${children})`
return `__h__('${el.tag}', ${genData(el)}, ${children}, ${el.svg})`
}
}
@ -52,7 +52,7 @@ function genFor (el) {
function genData (el) {
if (el.plain) {
return el.svg ? '{svg:true}' : '{}'
return 'undefined'
}
let data = '{'
@ -63,11 +63,6 @@ function genData (el) {
let dirs = genDirectives(el)
if (dirs) data += dirs + ','
}
// svg
if (el.svg) {
data += 'svg:true,'
}
// pre
if (el.pre) {
data += 'pre:true,'

4
src/compiler/parser/index.js

@ -64,7 +64,6 @@ export function parse (template, options) {
tag = tag.toLowerCase()
const element = {
tag,
plain: !attrs.length,
attrsList: attrs,
attrsMap: makeAttrsMap(attrs),
parent: currentParent,
@ -91,6 +90,9 @@ export function parse (template, options) {
} else {
processFor(element)
processIf(element)
// determine whether this is a plain element after
// removing if/for attributes
element.plain = !attrs.length
processRender(element)
processSlot(element)
processComponent(element)

9
src/runtime/dom/modules/class.js

@ -10,11 +10,12 @@ function updateClass (oldVnode, vnode) {
// check if this is a component container node
// or a child component root node
if (vnode.child) {
data = mergeClassData(vnode.child._vnode.data, data)
let i
if ((i = vnode.child) && (i = i._vnode.data)) {
data = mergeClassData(i, data)
}
if (vnode.parent) {
data = mergeClassData(data, vnode.parent.data)
if ((i = vnode.parent) && (i = i.data)) {
data = mergeClassData(data, i)
}
let cls = genClass(data)

2
src/runtime/vdom/create-component.js

@ -40,6 +40,8 @@ export function createComponent (Ctor, data, parent, children) {
}
}
data = data || {}
// merge component management hooks onto the placeholder node
mergeHooks(data)

9
src/runtime/vdom/create-element.js

@ -4,19 +4,18 @@ import { flatten } from './helpers'
import { renderState } from '../instance/render'
import { warn, isReservedTag, isUnknownElement, resolveAsset } from '../util/index'
export default function createElement (tag, data, children) {
data = data || {}
export default function createElement (tag, data, children, svg) {
const context = this
const parent = renderState.activeInstance
if (typeof tag === 'string') {
let Ctor
if (isReservedTag(tag)) {
return VNode(tag, data, flatten(children))
return VNode(tag, data, flatten(children), undefined, undefined, svg)
} else if ((Ctor = resolveAsset(context.$options, 'components', tag))) {
return createComponent(Ctor, data, parent, children)
} else {
if (process.env.NODE_ENV !== 'production') {
if (!data.svg && isUnknownElement(tag)) {
if (!svg && isUnknownElement(tag)) {
warn(
'Unknown custom element: <' + tag + '> - did you ' +
'register the component correctly? For recursive components, ' +
@ -24,7 +23,7 @@ export default function createElement (tag, data, children) {
)
}
}
return VNode(tag, data, flatten(children && children()))
return VNode(tag, data, flatten(children && children()), undefined, undefined, svg)
}
} else {
return createComponent(tag, data, parent, children)

61
src/runtime/vdom/patch.js

@ -52,14 +52,18 @@ export default function createPatchFunction (backend) {
function createRmCb (childElm, listeners) {
function remove () {
if (--remove.listeners === 0) {
const parent = nodeOps.parentNode(childElm)
nodeOps.removeChild(parent, childElm)
removeElement(childElm)
}
}
remove.listeners = listeners
return remove
}
function removeElement (el) {
const parent = nodeOps.parentNode(el)
nodeOps.removeChild(parent, el)
}
function createElm (vnode, insertedVnodeQueue) {
let i, elm
const data = vnode.data
@ -77,7 +81,7 @@ export default function createPatchFunction (backend) {
const children = vnode.children
const tag = vnode.tag
if (isDef(tag)) {
elm = vnode.elm = isDef(data) && data.svg
elm = vnode.elm = vnode.svg
? nodeOps.createSVGElement(tag)
: nodeOps.createElement(tag)
if (Array.isArray(children)) {
@ -87,7 +91,9 @@ export default function createPatchFunction (backend) {
} else if (isPrimitive(vnode.text)) {
nodeOps.appendChild(elm, nodeOps.createTextNode(vnode.text))
}
invokeCreateHooks(vnode, insertedVnodeQueue)
if (isDef(data)) {
invokeCreateHooks(vnode, insertedVnodeQueue)
}
} else {
elm = vnode.elm = nodeOps.createTextNode(vnode.text)
}
@ -134,7 +140,7 @@ export default function createPatchFunction (backend) {
if (isDef(ch)) {
if (isDef(ch.tag)) {
invokeDestroyHook(ch)
invokeRemoveHook(ch)
removeAndInvokeRemoveHook(ch)
} else { // Text node
nodeOps.removeChild(parentElm, ch.elm)
}
@ -142,28 +148,31 @@ export default function createPatchFunction (backend) {
}
}
function invokeRemoveHook (vnode, rm) {
let i
let listeners = cbs.remove.length + 1
if (!rm) {
// directly removing
rm = createRmCb(vnode.elm, listeners)
} else {
// we have a recursively passed down rm callback
// increase the listeners count
rm.listeners += listeners
}
// recursively invoke hooks on child component nodes
if (isDef(i = vnode.child)) {
invokeRemoveHook(i._vnode, rm)
}
for (i = 0; i < cbs.remove.length; ++i) {
cbs.remove[i](vnode, rm)
}
if (isDef(i = vnode.data) && isDef(i = i.hook) && isDef(i = i.remove)) {
i(vnode, rm)
function removeAndInvokeRemoveHook (vnode, rm) {
if (rm || isDef(vnode.data)) {
let listeners = cbs.remove.length + 1
if (!rm) {
// directly removing
rm = createRmCb(vnode.elm, listeners)
} else {
// we have a recursively passed down rm callback
// increase the listeners count
rm.listeners += listeners
}
// recursively invoke hooks on child component root node
if (isDef(i = vnode.child) && isDef(i = i._vnode) && isDef(i.data)) {
removeAndInvokeRemoveHook(i, rm)
}
for (i = 0; i < cbs.remove.length; ++i) {
cbs.remove[i](vnode, rm)
}
if (isDef(i = vnode.data.hook) && isDef(i = i.remove)) {
i(vnode, rm)
} else {
rm()
}
} else {
rm()
removeElement(vnode.elm)
}
}

3
src/runtime/vdom/vnode.js

@ -1,10 +1,11 @@
export default function VNode (tag, data, children, text, elm) {
export default function VNode (tag, data, children, text, elm, svg) {
return {
tag,
data,
children,
text,
elm,
svg,
key: data && data.key
}
}

Loading…
Cancel
Save