From b432eb1caeac3ad435ec6376d767f7649cb48871 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Tue, 8 Jan 2019 16:13:16 +0100 Subject: [PATCH] refactor(ivy): simplify signature of appendChild / removeChild (#27987) Previously the appendChild / removeChild could take null as an argument for a child to be added / removed. This is difficult to understand since the mentioned methods are noop if a child is null. This commit clarifies the appendChild / removeChild signature to systematically require a child node to be added removed. It turns out that null could be passed only for a very specific i18n cases so now we guard a call to removeChild with an explicit check on the i18n side. PR Close #27987 --- packages/core/src/render3/i18n.ts | 7 +++++-- packages/core/src/render3/node_manipulation.ts | 9 ++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/core/src/render3/i18n.ts b/packages/core/src/render3/i18n.ts index cff91d2278..7455ab11df 100644 --- a/packages/core/src/render3/i18n.ts +++ b/packages/core/src/render3/i18n.ts @@ -782,7 +782,10 @@ function readUpdateOpCodes( function removeNode(index: number, viewData: LView) { const removedPhTNode = getTNode(index, viewData); const removedPhRNode = getNativeByIndex(index, viewData); - removeChild(removedPhTNode, removedPhRNode || null, viewData); + if (removedPhRNode) { + removeChild(removedPhTNode, removedPhRNode, viewData); + } + removedPhTNode.detached = true; ngDevMode && ngDevMode.rendererRemoveNode++; @@ -790,7 +793,7 @@ function removeNode(index: number, viewData: LView) { if (isLContainer(slotValue)) { const lContainer = slotValue as LContainer; if (removedPhTNode.type !== TNodeType.Container) { - removeChild(removedPhTNode, lContainer[NATIVE] || null, viewData); + removeChild(removedPhTNode, lContainer[NATIVE], viewData); } lContainer[RENDER_PARENT] = null; } diff --git a/packages/core/src/render3/node_manipulation.ts b/packages/core/src/render3/node_manipulation.ts index 3d39d92bb9..d07248768e 100644 --- a/packages/core/src/render3/node_manipulation.ts +++ b/packages/core/src/render3/node_manipulation.ts @@ -631,9 +631,8 @@ export function nativeNextSibling(renderer: Renderer3, node: RNode): RNode|null * @param currentView The current LView * @returns Whether or not the child was appended */ -export function appendChild( - childEl: RNode | null = null, childTNode: TNode, currentView: LView): boolean { - if (childEl !== null && canInsertNativeNode(childTNode, currentView)) { +export function appendChild(childEl: RNode, childTNode: TNode, currentView: LView): boolean { + if (canInsertNativeNode(childTNode, currentView)) { const renderer = currentView[RENDERER]; const renderParent = getRenderParent(childTNode, currentView) !; const parentTNode: TNode = childTNode.parent || currentView[HOST_NODE] !; @@ -690,9 +689,9 @@ export function getBeforeNodeForView(index: number, views: LView[], containerNat * @param currentView The current LView * @returns Whether or not the child was removed */ -export function removeChild(childTNode: TNode, childEl: RNode | null, currentView: LView): boolean { +export function removeChild(childTNode: TNode, childEl: RNode, currentView: LView): boolean { // We only remove the element if not in View or not projected. - if (childEl !== null && canInsertNativeNode(childTNode, currentView)) { + if (canInsertNativeNode(childTNode, currentView)) { const parentNative = getRenderParent(childTNode, currentView) !; nativeRemoveChild(currentView[RENDERER], parentNative, childEl); return true;