feat(runtime): support rendering comment nodes

This commit is contained in:
Evan You 2019-09-24 14:37:14 -04:00
parent f5b3f580f1
commit 76a1196935
4 changed files with 19 additions and 14 deletions

View File

@ -3,7 +3,7 @@ import {
FunctionalComponent, FunctionalComponent,
Data Data
} from './component' } from './component'
import { VNode, normalizeVNode, createVNode, Empty } from './vnode' import { VNode, normalizeVNode, createVNode, Comment } from './vnode'
import { ShapeFlags } from './shapeFlags' import { ShapeFlags } from './shapeFlags'
import { handleError, ErrorCodes } from './errorHandling' import { handleError, ErrorCodes } from './errorHandling'
import { PatchFlags } from './patchFlags' import { PatchFlags } from './patchFlags'
@ -45,7 +45,7 @@ export function renderComponentRoot(
} }
} catch (err) { } catch (err) {
handleError(err, instance, ErrorCodes.RENDER_FUNCTION) handleError(err, instance, ErrorCodes.RENDER_FUNCTION)
result = createVNode(Empty) result = createVNode(Comment)
} }
currentRenderingInstance = null currentRenderingInstance = null
return result return result

View File

@ -1,7 +1,7 @@
import { import {
Text, Text,
Fragment, Fragment,
Empty, Comment,
Portal, Portal,
normalizeVNode, normalizeVNode,
VNode, VNode,
@ -191,8 +191,8 @@ export function createRenderer<
case Text: case Text:
processText(n1, n2, container, anchor) processText(n1, n2, container, anchor)
break break
case Empty: case Comment:
processEmptyNode(n1, n2, container, anchor) processCommentNode(n1, n2, container, anchor)
break break
case Fragment: case Fragment:
processFragment( processFragment(
@ -283,15 +283,20 @@ export function createRenderer<
} }
} }
function processEmptyNode( function processCommentNode(
n1: HostVNode | null, n1: HostVNode | null,
n2: HostVNode, n2: HostVNode,
container: HostElement, container: HostElement,
anchor: HostNode | null anchor: HostNode | null
) { ) {
if (n1 == null) { if (n1 == null) {
hostInsert((n2.el = hostCreateComment('')), container, anchor) hostInsert(
(n2.el = hostCreateComment((n2.children as string) || '')),
container,
anchor
)
} else { } else {
// there's no support for dynamic comments
n2.el = n1.el n2.el = n1.el
} }
} }
@ -677,7 +682,7 @@ export function createRenderer<
} }
} }
// insert an empty node as the placeholder for the portal // insert an empty node as the placeholder for the portal
processEmptyNode(n1, n2, container, anchor) processCommentNode(n1, n2, container, anchor)
} }
function processSuspense( function processSuspense(
@ -1057,8 +1062,8 @@ export function createRenderer<
}) })
// give it a placeholder // give it a placeholder
const placeholder = (instance.subTree = createVNode(Empty)) const placeholder = (instance.subTree = createVNode(Comment))
processEmptyNode(null, placeholder, container, anchor) processCommentNode(null, placeholder, container, anchor)
initialVNode.el = placeholder.el initialVNode.el = placeholder.el
return return
} }

View File

@ -19,7 +19,7 @@ export {
createBlock createBlock
} from './vnode' } from './vnode'
// VNode type symbols // VNode type symbols
export { Text, Empty, Fragment, Portal, Suspense } from './vnode' export { Text, Comment, Fragment, Portal, Suspense } from './vnode'
// VNode flags // VNode flags
export { PublicPatchFlags as PatchFlags } from './patchFlags' export { PublicPatchFlags as PatchFlags } from './patchFlags'
export { PublicShapeFlags as ShapeFlags } from './shapeFlags' export { PublicShapeFlags as ShapeFlags } from './shapeFlags'

View File

@ -16,7 +16,7 @@ import { SuspenseBoundary } from './suspense'
export const Fragment = __DEV__ ? Symbol('Fragment') : Symbol() export const Fragment = __DEV__ ? Symbol('Fragment') : Symbol()
export const Text = __DEV__ ? Symbol('Text') : Symbol() export const Text = __DEV__ ? Symbol('Text') : Symbol()
export const Empty = __DEV__ ? Symbol('Empty') : Symbol() export const Comment = __DEV__ ? Symbol('Empty') : Symbol()
export const Portal = __DEV__ ? Symbol('Portal') : Symbol() export const Portal = __DEV__ ? Symbol('Portal') : Symbol()
export const Suspense = __DEV__ ? Symbol('Suspense') : Symbol() export const Suspense = __DEV__ ? Symbol('Suspense') : Symbol()
@ -27,7 +27,7 @@ export type VNodeTypes =
| typeof Fragment | typeof Fragment
| typeof Portal | typeof Portal
| typeof Text | typeof Text
| typeof Empty | typeof Comment
| typeof Suspense | typeof Suspense
type VNodeChildAtom<HostNode, HostElement> = type VNodeChildAtom<HostNode, HostElement> =
@ -245,7 +245,7 @@ export function cloneVNode(vnode: VNode): VNode {
export function normalizeVNode(child: VNodeChild): VNode { export function normalizeVNode(child: VNodeChild): VNode {
if (child == null) { if (child == null) {
// empty placeholder // empty placeholder
return createVNode(Empty) return createVNode(Comment)
} else if (isArray(child)) { } else if (isArray(child)) {
// fragment // fragment
return createVNode(Fragment, null, child) return createVNode(Fragment, null, child)