From 847e493768371cec4718969e02bdb7f8463f4e03 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 11 Dec 2018 17:47:59 -0500 Subject: [PATCH] fix: fix single v-for child optimization --- .../instance/render-helpers/render-list.js | 5 +++-- src/core/vdom/helpers/normalize-children.js | 3 --- test/unit/features/directives/for.spec.js | 19 ++++++++++++++++++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/core/instance/render-helpers/render-list.js b/src/core/instance/render-helpers/render-list.js index abe99db4..040391c0 100644 --- a/src/core/instance/render-helpers/render-list.js +++ b/src/core/instance/render-helpers/render-list.js @@ -32,8 +32,9 @@ export function renderList ( ret[i] = render(val[key], key, i) } } - if (isDef(ret)) { - (ret: any)._isVList = true + if (!isDef(ret)) { + ret = [] } + (ret: any)._isVList = true return ret } diff --git a/src/core/vdom/helpers/normalize-children.js b/src/core/vdom/helpers/normalize-children.js index 4348e5df..a4622ef4 100644 --- a/src/core/vdom/helpers/normalize-children.js +++ b/src/core/vdom/helpers/normalize-children.js @@ -16,9 +16,6 @@ import { isFalse, isTrue, isDef, isUndef, isPrimitive } from 'shared/util' // thing with Array.prototype.concat. It is guaranteed to be only 1-level deep // because functional components already normalize their own children. export function simpleNormalizeChildren (children: any) { - if (!Array.isArray(children)) { - return - } for (let i = 0; i < children.length; i++) { if (Array.isArray(children[i])) { return Array.prototype.concat.apply([], children) diff --git a/test/unit/features/directives/for.spec.js b/test/unit/features/directives/for.spec.js index db0470b5..71b4b8ba 100644 --- a/test/unit/features/directives/for.spec.js +++ b/test/unit/features/directives/for.spec.js @@ -489,7 +489,7 @@ describe('Directive v-for', () => { const vm = new Vue({ template: '
' + - '{{ item }}' + + '{{ item }}' + '
', data: { list: undefined @@ -507,6 +507,23 @@ describe('Directive v-for', () => { }).then(done) }) + it('elements with v-for and empty list', done => { + const vm = new Vue({ + template: + '
' + + '
{{ item }}
' + + '
', + data: { + list: undefined + } + }).$mount() + expect(vm.$el.innerHTML).toBe('') + vm.list = [1, 2, 3] + waitForUpdate(() => { + expect(vm.$el.innerHTML).toBe('
1
2
3
') + }).then(done) + }) + const supportsDestructuring = (() => { try { new Function('var { foo } = bar')