From 39712bcdb2c20a543f0a7f735cc99320dcdbc991 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Thu, 7 Nov 2019 12:23:17 +0100 Subject: [PATCH] test(ivy): get ViewRef.rootNodes should get all root nodes from projectable nodes (#33647) PR Close #33647 --- packages/core/src/render3/view_ref.ts | 15 +++++++-------- .../core/test/acceptance/template_ref_spec.ts | 10 ++++++---- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/core/src/render3/view_ref.ts b/packages/core/src/render3/view_ref.ts index a88f43d354..0a3b5e82e0 100644 --- a/packages/core/src/render3/view_ref.ts +++ b/packages/core/src/render3/view_ref.ts @@ -19,7 +19,7 @@ import {CONTEXT, FLAGS, HOST, LView, LViewFlags, TVIEW, T_HOST} from './interfac import {assertNodeOfPossibleTypes} from './node_assert'; import {destroyLView, renderDetachView} from './node_manipulation'; import {findComponentView, getLViewParent} from './util/view_traversal_utils'; -import {getNativeByTNode, unwrapRNode} from './util/view_utils'; +import {unwrapRNode} from './util/view_utils'; @@ -302,7 +302,8 @@ export class RootViewRef extends ViewRef { get context(): T { return null !; } } -function collectNativeNodes(lView: LView, tNode: TNode | null, result: any[]): any[] { +function collectNativeNodes( + lView: LView, tNode: TNode | null, result: any[], isProjection: boolean = false): any[] { while (tNode !== null) { ngDevMode && assertNodeOfPossibleTypes( tNode, TNodeType.Element, TNodeType.Container, TNodeType.Projection, @@ -333,15 +334,13 @@ function collectNativeNodes(lView: LView, tNode: TNode | null, result: any[]): a const componentView = findComponentView(lView); const componentHost = componentView[T_HOST] as TElementNode; const parentView = getLViewParent(componentView); - let currentProjectedNode: TNode|null = + let firstProjectedNode: TNode|null = (componentHost.projection as(TNode | null)[])[tNode.projection as number]; - - while (currentProjectedNode !== null && parentView !== null) { - result.push(getNativeByTNode(currentProjectedNode, parentView)); - currentProjectedNode = currentProjectedNode.next; + if (firstProjectedNode !== null && parentView !== null) { + collectNativeNodes(parentView, firstProjectedNode, result, true); } } - tNode = tNode.next; + tNode = isProjection ? tNode.projectionNext : tNode.next; } return result; diff --git a/packages/core/test/acceptance/template_ref_spec.ts b/packages/core/test/acceptance/template_ref_spec.ts index 9fe0d00668..b55018e8ec 100644 --- a/packages/core/test/acceptance/template_ref_spec.ts +++ b/packages/core/test/acceptance/template_ref_spec.ts @@ -55,7 +55,7 @@ describe('TemplateRef', () => { expect(rootNodes.length).toBe(0); }); - it('should include projected nodes', () => { + it('should include projected nodes and their children', () => { @Component({ selector: 'menu-content', template: ` @@ -75,6 +75,7 @@ describe('TemplateRef', () => { + ` }) @@ -90,10 +91,11 @@ describe('TemplateRef', () => { const instance = fixture.componentInstance; const viewRef = instance.viewContainerRef.createEmbeddedView(instance.content.template); - const rootNodeTextContent = viewRef.rootNodes.map(node => node && node.textContent.trim()) - .filter(text => text !== ''); + const rootNodeTextContent = + viewRef.rootNodes.map(node => node && node.textContent.trim()) + .filter(text => text !== '' && text.indexOf('ng-reflect-ng-if') === -1); - expect(rootNodeTextContent).toEqual(['Header', 'Item one', 'Item two']); + expect(rootNodeTextContent).toEqual(['Header', 'Item one', 'Item two', 'Item three']); }); it('should descend into view containers on ng-template', () => {