fix(ivy): @Host should behave as in View Engine (#27646)

PR Close #27646
This commit is contained in:
Marc Laval
2018-12-13 11:14:33 +01:00
committed by Matias Niemelä
parent e8f7241366
commit 8f8572fd3e
12 changed files with 337 additions and 83 deletions

View File

@ -13,7 +13,7 @@ import {ACTIVE_INDEX, LContainer} from './interfaces/container';
import {LContext, MONKEY_PATCH_KEY_NAME} from './interfaces/context';
import {ComponentDef, DirectiveDef} from './interfaces/definition';
import {NO_PARENT_INJECTOR, RelativeInjectorLocation, RelativeInjectorLocationFlags} from './interfaces/injector';
import {TContainerNode, TElementNode, TNode, TNodeFlags} from './interfaces/node';
import {TContainerNode, TElementNode, TNode, TNodeFlags, TNodeType} from './interfaces/node';
import {RComment, RElement, RText} from './interfaces/renderer';
import {StylingContext} from './interfaces/styling';
import {CONTEXT, DECLARATION_VIEW, FLAGS, HEADER_OFFSET, HOST, HOST_NODE, LView, LViewFlags, PARENT, RootContext, TData, TVIEW, TView} from './interfaces/view';
@ -260,3 +260,33 @@ export function addAllToArray(items: any[], arr: any[]) {
arr.push(items[i]);
}
}
/**
* Given a current view, finds the nearest component's host (LElement).
*
* @param lView LView for which we want a host element node
* @param declarationMode indicates whether DECLARATION_VIEW or PARENT should be used to climb the
* tree.
* @returns The host node
*/
export function findComponentView(lView: LView, declarationMode?: boolean): LView {
let rootTNode = lView[HOST_NODE];
while (rootTNode && rootTNode.type === TNodeType.View) {
ngDevMode && assertDefined(
lView[declarationMode ? DECLARATION_VIEW : PARENT],
declarationMode ? 'lView.declarationView' : 'lView.parent');
lView = lView[declarationMode ? DECLARATION_VIEW : PARENT] !;
rootTNode = lView[HOST_NODE];
}
return lView;
}
/**
* Return the host TElementNode of the starting LView
* @param lView the starting LView.
*/
export function getHostTElementNode(lView: LView): TElementNode|null {
return findComponentView(lView, true)[HOST_NODE] as TElementNode;
}