/** * @license * Copyright Google Inc. All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import {SelectorFlags} from '@angular/core/src/render3/interfaces/projection'; import {AttributeMarker, detectChanges} from '../../src/render3/index'; import {bind, container, containerRefreshEnd, containerRefreshStart, element, elementContainerEnd, elementContainerStart, elementEnd, elementProperty, elementStart, embeddedViewEnd, embeddedViewStart, projection, projectionDef, template, text} from '../../src/render3/instructions'; import {RenderFlags} from '../../src/render3/interfaces/definition'; import {NgIf} from './common_with_def'; import {ComponentFixture, createComponent, getDirectiveOnNode, renderComponent, toHtml} from './render_util'; describe('content projection', () => { it('should project content', () => { /** *
*/ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'div'); { projection(1); } elementEnd(); } }, 2); /** * content */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { text(1, 'content'); } elementEnd(); } }, 2, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)).toEqual('
content
'); }); it('should project content when root.', () => { /** */ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); projection(0); } }, 1); /** content */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { text(1, 'content'); } elementEnd(); } }, 2, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)).toEqual('content'); }); it('should project content with siblings', () => { /** */ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); projection(0); } }, 1); /** * * before *
content
* after *
*/ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { text(1, 'before'); elementStart(2, 'div'); { text(3, 'content'); } elementEnd(); text(4, 'after'); } elementEnd(); } }, 5, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)).toEqual('before
content
after
'); }); it('should re-project content when root.', () => { /**
*/ const GrandChild = createComponent('grand-child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'div'); { projection(1); } elementEnd(); } }, 2); /** */ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'grand-child'); { projection(1); } elementEnd(); } }, 2, 0, [GrandChild]); /** HelloWorld! */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { elementStart(1, 'b'); text(2, 'Hello'); elementEnd(); text(3, 'World!'); } elementEnd(); } }, 4, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)) .toEqual('
HelloWorld!
'); }); it('should project components', () => { /**
*/ const Child = createComponent('child', (rf: RenderFlags, ctx: any) => { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'div'); { projection(1); } elementEnd(); } }, 2); const ProjectedComp = createComponent('projected-comp', (rf: RenderFlags, ctx: any) => { if (rf & RenderFlags.Create) { text(0, 'content'); } }, 1); /** * * * */ const Parent = createComponent('parent', (rf: RenderFlags, ctx: any) => { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { element(1, 'projected-comp'); } elementEnd(); } }, 2, 0, [Child, ProjectedComp]); const parent = renderComponent(Parent); expect(toHtml(parent)) .toEqual('
content
'); }); it('should project components that have their own projection', () => { /**
*/ const Child = createComponent('child', (rf: RenderFlags, ctx: any) => { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'div'); { projection(1); } elementEnd(); } }, 2); /**

*/ const ProjectedComp = createComponent('projected-comp', (rf: RenderFlags, ctx: any) => { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'p'); projection(1); elementEnd(); } }, 2); /** * * *
Some content
* Other content *
*
*/ const Parent = createComponent('parent', (rf: RenderFlags, ctx: any) => { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { elementStart(1, 'projected-comp'); { elementStart(2, 'div'); text(3, 'Some content'); elementEnd(); text(4, 'Other content'); } elementEnd(); } elementEnd(); } }, 5, 0, [Child, ProjectedComp]); const parent = renderComponent(Parent); expect(toHtml(parent)) .toEqual( '

Some content
Other content

'); }); it('should project containers', () => { /**
*/ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'div'); { projection(1); } elementEnd(); } }, 2); /** * * ( * % if (value) { * content * % } * ) * */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: {value: any}) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { text(1, '('); container(2); text(3, ')'); } elementEnd(); } if (rf & RenderFlags.Update) { containerRefreshStart(2); { if (ctx.value) { let rf0 = embeddedViewStart(0, 1, 0); if (rf0 & RenderFlags.Create) { text(0, 'content'); } embeddedViewEnd(); } } containerRefreshEnd(); } }, 4, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)).toEqual('
()
'); parent.value = true; detectChanges(parent); expect(toHtml(parent)).toEqual('
(content)
'); parent.value = false; detectChanges(parent); expect(toHtml(parent)).toEqual('
()
'); }); it('should project containers into root', () => { /** */ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); projection(0); } }, 1); /** * * % if (value) { * content * % } * */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: {value: any}) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { container(1); } elementEnd(); } if (rf & RenderFlags.Update) { containerRefreshStart(1); { if (ctx.value) { let rf0 = embeddedViewStart(0, 1, 0); if (rf0 & RenderFlags.Create) { text(0, 'content'); } embeddedViewEnd(); } } containerRefreshEnd(); } }, 2, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)).toEqual(''); parent.value = true; detectChanges(parent); expect(toHtml(parent)).toEqual('content'); parent.value = false; detectChanges(parent); expect(toHtml(parent)).toEqual(''); }); it('should project containers with if-else.', () => { /**
*/ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'div'); { projection(1); } elementEnd(); } }, 2); /** * * ( * % if (value) { * content * % } else { * else * % } * ) * */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: {value: any}) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { text(1, '('); container(2); text(3, ')'); } elementEnd(); } if (rf & RenderFlags.Update) { containerRefreshStart(2); { if (ctx.value) { let rf0 = embeddedViewStart(0, 1, 0); if (rf0 & RenderFlags.Create) { text(0, 'content'); } embeddedViewEnd(); } else { if (embeddedViewStart(1, 1, 0)) { text(0, 'else'); } embeddedViewEnd(); } } containerRefreshEnd(); } }, 4, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)).toEqual('
(else)
'); parent.value = true; detectChanges(parent); expect(toHtml(parent)).toEqual('
(content)
'); parent.value = false; detectChanges(parent); expect(toHtml(parent)).toEqual('
(else)
'); }); it('should support projection into embedded views', () => { let childCmptInstance: any; /** *
* % if (!skipContent) { * * * * % } *
*/ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'div'); { container(1); } elementEnd(); } if (rf & RenderFlags.Update) { containerRefreshStart(1); { if (!ctx.skipContent) { let rf0 = embeddedViewStart(0, 2, 0); if (rf0 & RenderFlags.Create) { elementStart(0, 'span'); projection(1); elementEnd(); } embeddedViewEnd(); } } containerRefreshEnd(); } }, 2, 0); /** * *
text
* content *
*/ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { elementStart(1, 'div'); { text(2, 'text'); } elementEnd(); text(3, 'content'); } elementEnd(); // testing childCmptInstance = getDirectiveOnNode(0); } }, 4, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)).toEqual('
text
content
'); childCmptInstance.skipContent = true; detectChanges(parent); expect(toHtml(parent)).toEqual('
'); }); it('should support projection into embedded views when no projected nodes', () => { let childCmptInstance: any; /** *
* % if (!skipContent) { * * text * % } *
*/ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'div'); { container(1); } elementEnd(); } if (rf & RenderFlags.Update) { containerRefreshStart(1); { if (!ctx.skipContent) { let rf0 = embeddedViewStart(0, 2, 0); if (rf0 & RenderFlags.Create) { projection(0); text(1, 'text'); } embeddedViewEnd(); } } containerRefreshEnd(); } }, 2); /** */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { element(0, 'child'); // testing childCmptInstance = getDirectiveOnNode(0); } }, 1, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)).toEqual('
text
'); childCmptInstance.skipContent = true; detectChanges(parent); expect(toHtml(parent)).toEqual('
'); }); it('should support projection into embedded views when ng-content is a root node of an embedded view', () => { let childCmptInstance: any; /** *
* % if (!skipContent) { * * % } *
*/ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'div'); { container(1); } elementEnd(); } if (rf & RenderFlags.Update) { containerRefreshStart(1); { if (!ctx.skipContent) { let rf0 = embeddedViewStart(0, 1, 0); if (rf0 & RenderFlags.Create) { projection(0); } embeddedViewEnd(); } } containerRefreshEnd(); } }, 2); /** * content */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { childCmptInstance = getDirectiveOnNode(0); text(1, 'content'); } elementEnd(); } }, 2, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)).toEqual('
content
'); childCmptInstance.skipContent = true; detectChanges(parent); expect(toHtml(parent)).toEqual('
'); }); it('should project containers into containers', () => { /** *
* Before (inside) * % if (!skipContent) { * * % } * After (inside) *
*/ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'div'); { text(1, 'Before (inside)-'); container(2); text(3, '-After (inside)'); } elementEnd(); } if (rf & RenderFlags.Update) { containerRefreshStart(2); { if (!ctx.skipContent) { let rf0 = embeddedViewStart(0, 1, 0); if (rf0 & RenderFlags.Create) { projection(0); } embeddedViewEnd(); } } containerRefreshEnd(); } }, 4); /** * * Before text- * % if (!skipContent) { * content * % } * -After text * */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { text(1, 'Before text-'); container(2); text(3, '-After text'); } elementEnd(); } if (rf & RenderFlags.Update) { containerRefreshStart(2); { if (!ctx.skipContent) { let rf0 = embeddedViewStart(0, 1, 0); if (rf0 & RenderFlags.Create) { text(0, 'content'); } embeddedViewEnd(); } } containerRefreshEnd(); } }, 4, 0, [Child]); const fixture = new ComponentFixture(Parent); expect(fixture.html) .toEqual( '
Before (inside)-Before text-content-After text-After (inside)
'); fixture.component.skipContent = true; fixture.update(); expect(fixture.html) .toEqual( '
Before (inside)-Before text--After text-After (inside)
'); }); it('should re-project containers into containers', () => { /** *
* % if (!skipContent) { * * % } *
*/ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'div'); { container(1); } elementEnd(); } if (rf & RenderFlags.Update) { containerRefreshStart(1); { if (!ctx.skipContent) { let rf0 = embeddedViewStart(0, 1, 0); if (rf0 & RenderFlags.Create) { projection(0); } embeddedViewEnd(); } } containerRefreshEnd(); } }, 2); /** * * Before text * % if (!skipContent) { * * % } * -After text * */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'child'); { text(1, 'Before text'); container(2); text(3, '-After text'); } elementEnd(); } if (rf & RenderFlags.Update) { containerRefreshStart(2); { if (!ctx.skipContent) { let rf0 = embeddedViewStart(0, 1, 0); if (rf0 & RenderFlags.Create) { projection(0); } embeddedViewEnd(); } } containerRefreshEnd(); } }, 4, 0, [Child]); let parent: any; /**

text

*/ const App = createComponent('app', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'parent'); { elementStart(1, 'p'); { text(2, 'text'); } elementEnd(); } elementEnd(); // testing parent = getDirectiveOnNode(0); } }, 3, 0, [Parent]); const fixture = new ComponentFixture(App); expect(fixture.html) .toEqual('
Before text

text

-After text
'); parent.skipContent = true; fixture.update(); expect(fixture.html) .toEqual('
Before text-After text
'); }); it('should support projection into embedded views when ng-content is a root node of an embedded view, with other nodes after', () => { let childCmptInstance: any; /** *
* % if (!skipContent) { * before--after * % } *
*/ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'div'); { container(1); } elementEnd(); } if (rf & RenderFlags.Update) { containerRefreshStart(1); { if (!ctx.skipContent) { let rf0 = embeddedViewStart(0, 3, 0); if (rf0 & RenderFlags.Create) { text(0, 'before-'); projection(1); text(2, '-after'); } embeddedViewEnd(); } } containerRefreshEnd(); } }, 2); /** * content */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { childCmptInstance = getDirectiveOnNode(0); text(1, 'content'); } elementEnd(); } }, 2, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)).toEqual('
before-content-after
'); childCmptInstance.skipContent = true; detectChanges(parent); expect(toHtml(parent)).toEqual('
'); }); it('should project into dynamic views (with createEmbeddedView)', () => { /** * Before- * * * * -After */ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); text(0, 'Before-'); template(1, IfTemplate, 1, 0, '', [AttributeMarker.SelectOnly, 'ngIf']); text(2, '-After'); } if (rf & RenderFlags.Update) { elementProperty(1, 'ngIf', bind(ctx.showing)); } }, 3, 1, [NgIf]); function IfTemplate(rf1: RenderFlags, ctx: any) { if (rf1 & RenderFlags.Create) { projectionDef(); projection(0); } } let child: {showing: boolean}; /** * *
A
* Some text *
*/ const App = createComponent('app', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { elementStart(1, 'div'); { text(2, 'A'); } elementEnd(); text(3, 'Some text'); } elementEnd(); // testing child = getDirectiveOnNode(0); } }, 4, 0, [Child]); const fixture = new ComponentFixture(App); child !.showing = true; fixture.update(); expect(fixture.html).toEqual('Before-
A
Some text-After
'); child !.showing = false; fixture.update(); expect(fixture.html).toEqual('Before--After'); child !.showing = true; fixture.update(); expect(fixture.html).toEqual('Before-
A
Some text-After
'); }); it('should project into dynamic views (with insertion)', () => { /** * Before- * * * * -After */ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); text(0, 'Before-'); template(1, IfTemplate, 1, 0, '', [AttributeMarker.SelectOnly, 'ngIf']); text(2, '-After'); } if (rf & RenderFlags.Update) { elementProperty(1, 'ngIf', bind(ctx.showing)); } }, 3, 1, [NgIf]); function IfTemplate(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); projection(0); } } let child: {showing: boolean}; /** * *
A
* Some text *
*/ const App = createComponent('app', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { elementStart(1, 'div'); { text(2, 'A'); } elementEnd(); text(3, 'Some text'); } elementEnd(); // testing child = getDirectiveOnNode(0); } }, 4, 0, [Child]); const fixture = new ComponentFixture(App); child !.showing = true; fixture.update(); expect(fixture.html).toEqual('Before-
A
Some text-After
'); child !.showing = false; fixture.update(); expect(fixture.html).toEqual('Before--After'); child !.showing = true; fixture.update(); expect(fixture.html).toEqual('Before-
A
Some text-After
'); }); it('should project nodes into the last ng-content', () => { /** *
* */ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'div'); { projection(1); } elementEnd(); elementStart(2, 'span'); { projection(3); } elementEnd(); } }, 4); /** * content */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { text(1, 'content'); } elementEnd(); } }, 2, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)).toEqual('
content
'); }); /** * Warning: this test is _not_ in-line with what Angular does atm. * Moreover the current implementation logic will result in DOM nodes * being re-assigned from one parent to another. Proposal: have compiler * to remove all but the latest occurrence of so we generate * only one P(n, m, 0) instruction. It would make it consistent with the * current Angular behaviour: * http://plnkr.co/edit/OAYkNawTDPkYBFTqovTP?p=preview */ it('should project nodes into the last available ng-content', () => { let childCmptInstance: any; /** * *
* % if (show) { * * % } *
*/ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); projection(0); elementStart(1, 'div'); { container(2); } elementEnd(); } if (rf & RenderFlags.Update) { containerRefreshStart(2); { if (ctx.show) { let rf0 = embeddedViewStart(0, 1, 0); if (rf0 & RenderFlags.Create) { projection(0); } embeddedViewEnd(); } } containerRefreshEnd(); } }, 3); /** * content */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { childCmptInstance = getDirectiveOnNode(0); text(1, 'content'); } elementEnd(); } }, 2, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)).toEqual('content
'); childCmptInstance.show = true; detectChanges(parent); expect(toHtml(parent)).toEqual('
content
'); }); it('should project with multiple instances of a component with projection', () => { const ProjectionComp = createComponent('projection-comp', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); text(0, 'Before'); projection(1); text(2, 'After'); } }, 3); /** * *
A
*

123

*
* *
B
*

456

*
*/ const AppComp = createComponent('app-comp', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'projection-comp'); { elementStart(1, 'div'); { text(2, 'A'); } elementEnd(); elementStart(3, 'p'); { text(4, '123'); } elementEnd(); } elementEnd(); elementStart(5, 'projection-comp'); { elementStart(6, 'div'); { text(7, 'B'); } elementEnd(); elementStart(8, 'p'); { text(9, '456'); } elementEnd(); } elementEnd(); } }, 10, 0, [ProjectionComp]); const fixture = new ComponentFixture(AppComp); fixture.update(); expect(fixture.html) .toEqual( 'Before
A

123

After
' + 'Before
B

456

After
'); }); it('should re-project with multiple instances of a component with projection', () => { /** * Before * * After */ const ProjectionComp = createComponent('projection-comp', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); text(0, 'Before'); projection(1); text(2, 'After'); } }, 3); /** * *
A
* *

123

*
* *
B
*

456

*
*/ const ProjectionParent = createComponent('parent-comp', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'projection-comp'); { elementStart(1, 'div'); { text(2, 'A'); } elementEnd(); projection(3, 0); elementStart(4, 'p'); { text(5, '123'); } elementEnd(); } elementEnd(); elementStart(6, 'projection-comp'); { elementStart(7, 'div'); { text(8, 'B'); } elementEnd(); elementStart(9, 'p'); { text(10, '456'); } elementEnd(); } elementEnd(); } }, 11, 0, [ProjectionComp]); /** * * **ABC** * * * **DEF** * */ const AppComp = createComponent('app-comp', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'parent-comp'); { text(1, '**ABC**'); } elementEnd(); elementStart(2, 'parent-comp'); { text(3, '**DEF**'); } elementEnd(); } }, 4, 0, [ProjectionParent]); const fixture = new ComponentFixture(AppComp); fixture.update(); expect(fixture.html) .toEqual( '' + 'Before
A
**ABC**

123

After
' + 'Before
B

456

After
' + '' + 'Before
A
**DEF**

123

After
' + 'Before
B

456

After
'); }); it('should project ng-container at the content root', () => { ``; const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); projection(0); } }, 1); ` content `; const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { elementContainerStart(1); { elementContainerStart(2); { text(3, 'content'); } elementContainerEnd(); } elementContainerEnd(); } elementEnd(); } }, 4, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)).toEqual('content'); }); it('should re-project ng-container at the content root', () => { ``; const GrandChild = createComponent('grand-child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); projection(0); } }, 1); ` `; const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'grand-child'); { projection(1); } elementEnd(); } }, 2, 0, [GrandChild]); ` content `; const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { elementContainerStart(1); { elementContainerStart(2); { text(3, 'content'); } elementContainerEnd(); } elementContainerEnd(); } elementEnd(); } }, 4, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)).toEqual('content'); }); describe('with selectors', () => { it('should project nodes using attribute selectors', () => { /** *
*
*/ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef( [[['span', 'title', 'toFirst']], [['span', 'title', 'toSecond']]], ['span[title=toFirst]', 'span[title=toSecond]']); elementStart(0, 'div', ['id', 'first']); { projection(1, 1); } elementEnd(); elementStart(2, 'div', ['id', 'second']); { projection(3, 2); } elementEnd(); } }, 4); /** * * 1 * 2 * */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { elementStart(1, 'span', ['title', 'toFirst']); { text(2, '1'); } elementEnd(); elementStart(3, 'span', ['title', 'toSecond']); { text(4, '2'); } elementEnd(); } elementEnd(); } }, 5, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)) .toEqual( '
1
2
'); }); // https://stackblitz.com/edit/angular-psokum?file=src%2Fapp%2Fapp.module.ts it('should project nodes where attribute selector matches a binding', () => { /** * */ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef([[['', 'title', '']]], ['[title]']); { projection(0, 1); } } }, 1); /** * * Has title * */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { elementStart(1, 'span', [AttributeMarker.SelectOnly, 'title']); { text(2, 'Has title'); } elementEnd(); } elementEnd(); } if (rf & RenderFlags.Update) { elementProperty(1, 'title', bind('Some title')); } }, 3, 1, [Child]); const fixture = new ComponentFixture(Parent); expect(fixture.html).toEqual('Has title'); }); it('should project nodes using class selectors', () => { /** *
*
*/ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef( [ [['span', SelectorFlags.CLASS, 'toFirst']], [['span', SelectorFlags.CLASS, 'toSecond']] ], ['span.toFirst', 'span.toSecond']); elementStart(0, 'div', ['id', 'first']); { projection(1, 1); } elementEnd(); elementStart(2, 'div', ['id', 'second']); { projection(3, 2); } elementEnd(); } }, 4); /** * * 1 * 2 * */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { elementStart(1, 'span', ['class', 'toFirst']); { text(2, '1'); } elementEnd(); elementStart(3, 'span', ['class', 'toSecond']); { text(4, '2'); } elementEnd(); } elementEnd(); } }, 5, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)) .toEqual( '
1
2
'); }); it('should project nodes using class selectors when element has multiple classes', () => { /** *
*
*/ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef( [ [['span', SelectorFlags.CLASS, 'toFirst']], [['span', SelectorFlags.CLASS, 'toSecond']] ], ['span.toFirst', 'span.toSecond']); elementStart(0, 'div', ['id', 'first']); { projection(1, 1); } elementEnd(); elementStart(2, 'div', ['id', 'second']); { projection(3, 2); } elementEnd(); } }, 4); /** * * 1 * 2 * */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { elementStart(1, 'span', ['class', 'other toFirst']); { text(2, '1'); } elementEnd(); elementStart(3, 'span', ['class', 'toSecond noise']); { text(4, '2'); } elementEnd(); } elementEnd(); } }, 5, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)) .toEqual( '
1
2
'); }); it('should project nodes into the first matching selector', () => { /** *
*
*/ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef( [[['span']], [['span', SelectorFlags.CLASS, 'toSecond']]], ['span', 'span.toSecond']); elementStart(0, 'div', ['id', 'first']); { projection(1, 1); } elementEnd(); elementStart(2, 'div', ['id', 'second']); { projection(3, 2); } elementEnd(); } }, 4); /** * * 1 * 2 * */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { elementStart(1, 'span', ['class', 'toFirst']); { text(2, '1'); } elementEnd(); elementStart(3, 'span', ['class', 'toSecond']); { text(4, '2'); } elementEnd(); } elementEnd(); } }, 5, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)) .toEqual( '
12
'); }); it('should allow mixing ng-content with and without selectors', () => { /** *
*
*/ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef([[['span', SelectorFlags.CLASS, 'toFirst']]], ['span.toFirst']); elementStart(0, 'div', ['id', 'first']); { projection(1, 1); } elementEnd(); elementStart(2, 'div', ['id', 'second']); { projection(3); } elementEnd(); } }, 4); /** * * 1 * 2 * */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { elementStart(1, 'span', ['class', 'toFirst']); { text(2, '1'); } elementEnd(); elementStart(3, 'span'); { text(4, 'remaining'); } elementEnd(); text(5, 'more remaining'); } elementEnd(); } }, 6, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)) .toEqual( '
1
remainingmore remaining
'); }); it('should allow mixing ng-content with and without selectors - ng-content first', () => { /** *
*
*/ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef([[['span', SelectorFlags.CLASS, 'toSecond']]], ['span.toSecond']); elementStart(0, 'div', ['id', 'first']); { projection(1); } elementEnd(); elementStart(2, 'div', ['id', 'second']); { projection(3, 1); } elementEnd(); } }, 4); /** * * 1 * 2 * remaining * */ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { elementStart(1, 'span'); { text(2, '1'); } elementEnd(); elementStart(3, 'span', ['class', 'toSecond']); { text(4, '2'); } elementEnd(); text(5, 'remaining'); } elementEnd(); } }, 6, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)) .toEqual( '
1remaining
2
'); }); /** * Descending into projected content for selector-matching purposes is not supported * today: http://plnkr.co/edit/MYQcNfHSTKp9KvbzJWVQ?p=preview */ it('should not descend into re-projected content', () => { /** * *
* */ const GrandChild = createComponent('grand-child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef([[['span']]], ['span']); projection(0, 1); element(1, 'hr'); projection(2); } }, 3); /** * * * in child template * */ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'grand-child'); { projection(1); elementStart(2, 'span'); { text(3, 'in child template'); } elementEnd(); } elementEnd(); } }, 4, 0, [GrandChild]); /** * *
* parent content *
*
*/ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { elementStart(1, 'span'); { text(2, 'parent content'); } elementEnd(); } elementEnd(); } }, 3, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)) .toEqual( 'in child template
parent content
'); }); it('should match selectors on ng-content nodes with attributes', () => { /** * *
* */ const Card = createComponent('card', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef( [[['', 'card-title', '']], [['', 'card-content', '']]], ['[card-title]', '[card-content]']); projection(0, 1); element(1, 'hr'); projection(2, 2); } }, 3); /** * *

Title

* *
*/ const CardWithTitle = createComponent('card-with-title', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'card'); { elementStart(1, 'h1', ['card-title', '']); { text(2, 'Title'); } elementEnd(); projection(3, 0, ['card-content', '']); } elementEnd(); } }, 4, 0, [Card]); /** * * content * */ const App = createComponent('app', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'card-with-title'); { text(1, 'content'); } elementEnd(); } }, 2, 0, [CardWithTitle]); const app = renderComponent(App); expect(toHtml(app)) .toEqual( '

Title


content
'); }); it('should support ngProjectAs on elements (including )', () => { /** * *
* */ const Card = createComponent('card', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef( [[['', 'card-title', '']], [['', 'card-content', '']]], ['[card-title]', '[card-content]']); projection(0, 1); element(1, 'hr'); projection(2, 2); } }, 3); /** * *

* */ const CardWithTitle = createComponent('card-with-title', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef(); elementStart(0, 'card'); { elementStart(1, 'h1', ['ngProjectAs', '[card-title]']); { text(2, 'Title'); } elementEnd(); projection(3, 0, ['ngProjectAs', '[card-content]']); } elementEnd(); } }, 4, 0, [Card]); /** * * content * */ const App = createComponent('app', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'card-with-title'); { text(1, 'content'); } elementEnd(); } }, 2, 0, [CardWithTitle]); const app = renderComponent(App); expect(toHtml(app)) .toEqual('

Title


content
'); }); it('should not match selectors against node having ngProjectAs attribute', function() { /** * */ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef([[['div']]], ['div']); projection(0, 1); } }, 1); /** * *
should not project
*
should project
*
*/ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { elementStart(1, 'div', ['ngProjectAs', 'span']); { text(2, 'should not project'); } elementEnd(); elementStart(3, 'div'); { text(4, 'should project'); } elementEnd(); } elementEnd(); } }, 5, 0, [Child]); const parent = renderComponent(Parent); expect(toHtml(parent)).toEqual('
should project
'); }); it('should match selectors against projected containers', () => { /** * * * */ const Child = createComponent('child', function(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { projectionDef([[['div']]], ['div']); elementStart(0, 'span'); { projection(1, 1); } elementEnd(); } }, 2); function IfTemplate(rf: RenderFlags, ctx: any) { if (rf & RenderFlags.Create) { elementStart(0, 'div'); { text(1, 'content'); } elementEnd(); } } /** * *
content
*
*/ const Parent = createComponent('parent', function(rf: RenderFlags, ctx: {value: any}) { if (rf & RenderFlags.Create) { elementStart(0, 'child'); { template(1, IfTemplate, 2, 0, 'div', [AttributeMarker.SelectOnly, 'ngIf']); } elementEnd(); } if (rf & RenderFlags.Update) { elementProperty(1, 'ngIf', bind(ctx.value)); } }, 2, 1, [Child, NgIf]); const fixture = new ComponentFixture(Parent); fixture.component.value = true; fixture.update(); expect(fixture.html).toEqual('
content
'); }); }); });