From 6a797d540169ce979048e30abc619d4c6eaf7b1d Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Fri, 27 Jul 2018 14:56:15 -0700 Subject: [PATCH] refactor(ivy): `element` and `ElementStart` retuns `void` (#25173) use `loadElement` to load an element when needed in specs PR Close #25173 --- packages/core/src/render3/instructions.ts | 11 ++--- packages/core/test/render3/query_spec.ts | 50 +++++++++++++++-------- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/packages/core/src/render3/instructions.ts b/packages/core/src/render3/instructions.ts index e127854cb5..2f9a5c6c59 100644 --- a/packages/core/src/render3/instructions.ts +++ b/packages/core/src/render3/instructions.ts @@ -635,11 +635,9 @@ export function namespaceHTML() { * @param localRefs A set of local reference bindings on the element. */ export function element( - index: number, name: string, attrs?: TAttributes | null, - localRefs?: string[] | null): RElement { - const rElement = elementStart(index, name, attrs, localRefs); + index: number, name: string, attrs?: TAttributes | null, localRefs?: string[] | null): void { + elementStart(index, name, attrs, localRefs); elementEnd(); - return rElement; } /** @@ -655,8 +653,7 @@ export function element( * ['id', 'warning5', 'class', 'alert'] */ export function elementStart( - index: number, name: string, attrs?: TAttributes | null, - localRefs?: string[] | null): RElement { + index: number, name: string, attrs?: TAttributes | null, localRefs?: string[] | null): void { ngDevMode && assertEqual(viewData[BINDING_INDEX], -1, 'elements should be created before any bindings'); @@ -675,8 +672,8 @@ export function elementStart( } appendChild(getParentLNode(node), native, viewData); createDirectivesAndLocals(localRefs); - return native; } + /** * Creates a native element from a tag name, using a renderer. * @param name the tag name diff --git a/packages/core/test/render3/query_spec.ts b/packages/core/test/render3/query_spec.ts index 5cba510936..6458a64208 100644 --- a/packages/core/test/render3/query_spec.ts +++ b/packages/core/test/render3/query_spec.ts @@ -12,7 +12,7 @@ import {ElementRef, TemplateRef, ViewContainerRef} from '@angular/core'; import {EventEmitter} from '../..'; import {QUERY_READ_CONTAINER_REF, QUERY_READ_ELEMENT_REF, QUERY_READ_FROM_NODE, QUERY_READ_TEMPLATE_REF, getOrCreateNodeInjectorForNode, getOrCreateTemplateRef} from '../../src/render3/di'; import {AttributeMarker, QueryList, defineComponent, defineDirective, detectChanges, injectViewContainerRef} from '../../src/render3/index'; -import {bind, container, containerRefreshEnd, containerRefreshStart, element, elementEnd, elementProperty, elementStart, embeddedViewEnd, embeddedViewStart, load, loadDirective, loadQueryList, registerContentQuery} from '../../src/render3/instructions'; +import {bind, container, containerRefreshEnd, containerRefreshStart, element, elementEnd, elementProperty, elementStart, embeddedViewEnd, embeddedViewStart, load, loadDirective, loadElement, loadQueryList, registerContentQuery} from '../../src/render3/instructions'; import {RenderFlags} from '../../src/render3/interfaces/definition'; import {query, queryRefresh} from '../../src/render3/query'; @@ -111,7 +111,8 @@ describe('query', () => { 'cmpt', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { - elToQuery = element(1, 'div', ['child', '']); + element(1, 'div', ['child', '']); + elToQuery = loadElement(1).native; } }, [Child], [], @@ -217,7 +218,8 @@ describe('query', () => { 'cmpt', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { - elToQuery = element(1, 'div', null, ['foo', '']); + element(1, 'div', null, ['foo', '']); + elToQuery = loadElement(1).native; element(3, 'div'); } }, @@ -253,7 +255,8 @@ describe('query', () => { 'cmpt', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { - elToQuery = element(2, 'div', null, ['foo', '', 'bar', '']); + element(2, 'div', null, ['foo', '', 'bar', '']); + elToQuery = loadElement(2).native; element(5, 'div'); } }, @@ -299,9 +302,11 @@ describe('query', () => { 'cmpt', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { - el1ToQuery = element(1, 'div', null, ['foo', '']); + element(1, 'div', null, ['foo', '']); + el1ToQuery = loadElement(1).native; element(3, 'div'); - el2ToQuery = element(4, 'div', null, ['bar', '']); + element(4, 'div', null, ['bar', '']); + el2ToQuery = loadElement(4).native; } }, [], [], @@ -336,7 +341,8 @@ describe('query', () => { 'cmpt', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { - elToQuery = element(1, 'div', null, ['foo', '']); + element(1, 'div', null, ['foo', '']); + elToQuery = loadElement(1).native; element(3, 'div'); } }, @@ -739,7 +745,8 @@ describe('query', () => { 'cmpt', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { - div = element(1, 'div', ['child', ''], ['foo', 'child']); + element(1, 'div', ['child', ''], ['foo', 'child']); + div = loadElement(1).native; } }, [Child], [], @@ -773,7 +780,8 @@ describe('query', () => { 'cmpt', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { - div = element(1, 'div', ['child', ''], ['foo', '', 'bar', 'child']); + element(1, 'div', ['child', ''], ['foo', '', 'bar', 'child']); + div = loadElement(1).native; } if (rf & RenderFlags.Update) { childInstance = loadDirective(0); @@ -1239,7 +1247,8 @@ describe('query', () => { let rf1 = embeddedViewStart(1); { if (rf1 & RenderFlags.Create) { - firstEl = element(0, 'div', null, ['foo', '']); + element(0, 'div', null, ['foo', '']); + firstEl = loadElement(0).native; } } embeddedViewEnd(); @@ -1290,9 +1299,11 @@ describe('query', () => { 'cmpt', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { - firstEl = element(1, 'span', null, ['foo', '']); + element(1, 'span', null, ['foo', '']); + firstEl = loadElement(1).native; container(3); - lastEl = element(4, 'span', null, ['foo', '']); + element(4, 'span', null, ['foo', '']); + lastEl = loadElement(4).native; } if (rf & RenderFlags.Update) { containerRefreshStart(3); @@ -1301,7 +1312,8 @@ describe('query', () => { let rf1 = embeddedViewStart(1); { if (rf1 & RenderFlags.Create) { - viewEl = element(0, 'div', null, ['foo', '']); + element(0, 'div', null, ['foo', '']); + viewEl = loadElement(0).native; } } embeddedViewEnd(); @@ -1367,7 +1379,8 @@ describe('query', () => { let rf0 = embeddedViewStart(0); { if (rf0 & RenderFlags.Create) { - firstEl = element(0, 'div', null, ['foo', '']); + element(0, 'div', null, ['foo', '']); + firstEl = loadElement(0).native; } } embeddedViewEnd(); @@ -1376,7 +1389,8 @@ describe('query', () => { let rf1 = embeddedViewStart(1); { if (rf1 & RenderFlags.Create) { - lastEl = element(0, 'span', null, ['foo', '']); + element(0, 'span', null, ['foo', '']); + lastEl = loadElement(0).native; } } embeddedViewEnd(); @@ -1438,7 +1452,8 @@ describe('query', () => { let rf0 = embeddedViewStart(0); { if (rf0 & RenderFlags.Create) { - firstEl = element(0, 'div', null, ['foo', '']); + element(0, 'div', null, ['foo', '']); + firstEl = loadElement(0).native; container(2); } if (rf0 & RenderFlags.Update) { @@ -1448,7 +1463,8 @@ describe('query', () => { let rf2 = embeddedViewStart(0); { if (rf2) { - lastEl = element(0, 'span', null, ['foo', '']); + element(0, 'span', null, ['foo', '']); + lastEl = loadElement(0).native; } } embeddedViewEnd();