diff --git a/src/compiler/optimizer.js b/src/compiler/optimizer.js index dc70181ec81..60c61d9096e 100644 --- a/src/compiler/optimizer.js +++ b/src/compiler/optimizer.js @@ -77,14 +77,12 @@ function markStaticRoots (node: ASTNode, isInFor: boolean) { } if (node.children) { for (let i = 0, l = node.children.length; i < l; i++) { - const child = node.children[i] - isInFor = isInFor || !!node.for - markStaticRoots(child, isInFor) - if (child.type === 1 && child.elseBlock) { - markStaticRoots(child.elseBlock, isInFor) - } + markStaticRoots(node.children[i], isInFor || !!node.for) } } + if (node.elseBlock) { + markStaticRoots(node.elseBlock, isInFor) + } } } diff --git a/test/unit/features/directives/once.spec.js b/test/unit/features/directives/once.spec.js index 50791edcdbe..21da307980b 100644 --- a/test/unit/features/directives/once.spec.js +++ b/test/unit/features/directives/once.spec.js @@ -213,6 +213,35 @@ describe('Directive v-once', () => { }).then(done) }) + it('should work inside v-for with nested v-else', done => { + const vm = new Vue({ + data: { + list: [{ id: 0, text: 'a', tester: true, truthy: 'y' }] + }, + template: ` +
+
+
+ {{ i.truthy }} + {{ i.text }} +
+
+ ` + }).$mount() + + expectTextContent(vm, 'y') + vm.list[0].truthy = 'yy' + waitForUpdate(() => { + expectTextContent(vm, 'y') + vm.list[0].tester = false + }).then(() => { + expectTextContent(vm, 'a') + vm.list[0].text = 'nn' + }).then(() => { + expectTextContent(vm, 'a') + }).then(done) + }) + it('should warn inside non-keyed v-for', () => { const vm = new Vue({ data: { diff --git a/test/unit/modules/compiler/optimizer.spec.js b/test/unit/modules/compiler/optimizer.spec.js index 412ada62556..f7b3e08c21b 100644 --- a/test/unit/modules/compiler/optimizer.spec.js +++ b/test/unit/modules/compiler/optimizer.spec.js @@ -209,4 +209,18 @@ describe('optimizer', () => { expect(ast.children[0].children[0].staticRoot).toBe(true) expect(ast.children[0].children[0].staticInFor).toBe(true) }) + + it('mark static trees inside v-for with nested v-else and v-once', () => { + const ast = parse(` +
+
+
+
{{ i }}
+
{{ i }}
+
+
`, baseOptions) + optimize(ast, baseOptions) + expect(ast.elseBlock.children[0].children[0].elseBlock.staticRoot).toBe(false) + expect(ast.elseBlock.children[0].children[0].elseBlock.staticInFor).toBe(true) + }) })