Browse Source

optimize whitespace vnode

Evan You 10 years ago
parent
commit
08f813f06e
1 changed files with 9 additions and 2 deletions
  1. 9 2
      src/core/vdom/helpers.js

+ 9 - 2
src/core/vdom/helpers.js

@@ -1,6 +1,8 @@
 import { isArray, isPrimitive } from '../util/index'
 import VNode from './vnode'
 
+const whitespace = VNode(undefined, undefined, undefined, ' ')
+
 export function flatten (children) {
   if (typeof children === 'string') {
     return [VNode(undefined, undefined, undefined, children)]
@@ -13,8 +15,13 @@ export function flatten (children) {
       if (isArray(c)) {
         res.push.apply(res, flatten(c))
       } else if (isPrimitive(c)) {
-        // convert primitive to vnode
-        res.push(VNode(undefined, undefined, undefined, c))
+        // optimize whitespace
+        if (c === ' ') {
+          res.push(whitespace)
+        } else {
+          // convert primitive to vnode
+          res.push(VNode(undefined, undefined, undefined, c))
+        }
       } else if (c) {
         res.push(c)
       }