Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions integration/_payload-limits.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"master": {
"uncompressed": {
"runtime-es2015": 1485,
"main-es2015": 14786,
"main-es2015": 15040,
"polyfills-es2015": 36808
}
}
Expand All @@ -39,7 +39,7 @@
"master": {
"uncompressed": {
"runtime-es2015": 2289,
"main-es2015": 268331,
"main-es2015": 268404,
"polyfills-es2015": 36808,
"5-es2015": 751
}
Expand All @@ -64,4 +64,4 @@
}
}
}
}
}
62 changes: 46 additions & 16 deletions packages/core/src/render3/node_manipulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import {ProceduralRenderer3, RElement, RNode, RText, Renderer3, isProceduralRend
import {isLContainer, isLView, isRootView} from './interfaces/type_checks';
import {CHILD_HEAD, CLEANUP, DECLARATION_LCONTAINER, FLAGS, HOST, HookData, LView, LViewFlags, NEXT, PARENT, QUERIES, RENDERER, TVIEW, T_HOST, unusedValueExportToPlacateAjd as unused5} from './interfaces/view';
import {assertNodeOfPossibleTypes, assertNodeType} from './node_assert';
import {findComponentView} from './util/view_traversal_utils';
import {getNativeByTNode, getNativeByTNodeOrNull, unwrapRNode} from './util/view_utils';
import {findComponentView, getLViewParent} from './util/view_traversal_utils';
import {getNativeByTNode, unwrapRNode} from './util/view_utils';

const unusedValueToPlacateAjd = unused1 + unused2 + unused3 + unused4 + unused5;

Expand Down Expand Up @@ -652,25 +652,55 @@ export function appendChild(childEl: RNode | RNode[], childTNode: TNode, current
}
}

/**
* Returns the first native node for a given LView, starting from the provided TNode.
*
* Native nodes are returned in the order in which those appear in the native tree (DOM).
*/
function getFirstNativeNode(lView: LView, tNode: TNode | null): RNode|null {
if (tNode !== null) {
ngDevMode && assertNodeOfPossibleTypes(
tNode, TNodeType.Element, TNodeType.Container, TNodeType.ElementContainer,
TNodeType.IcuContainer, TNodeType.Projection);

const tNodeType = tNode.type;

if (tNodeType === TNodeType.Element) {
return getNativeByTNode(tNode, lView);
} else if (tNodeType === TNodeType.Container) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is one more use case. You can have an LContainer but the TNode type is a element. This is because any element can be upgraded into a container. I think we need a test case for that.

@pkozlowski-opensource pkozlowski-opensource Nov 6, 2019 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can add a test but since views inserted on a VCRef asked on an element are inserted after the element, it shouldn't matter for finding the first node of a view.

In fact I wanted to have a deeper look at the case of a directive asking for a ViwContainerRef on a <ng-container> in a follow-up PR as I'm not sure what will happen for a directive that asks for a ViewContainerRef on a <ng-container>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, Thank you.

const lContainer = lView[tNode.index];
if (lContainer.length > CONTAINER_HEADER_OFFSET) {
const firstView = lContainer[CONTAINER_HEADER_OFFSET];
return getFirstNativeNode(firstView, firstView[TVIEW].firstChild);
} else {
return lContainer[NATIVE];
}
} else if (tNodeType === TNodeType.ElementContainer || tNodeType === TNodeType.IcuContainer) {
return getFirstNativeNode(lView, tNode.child);
} else {
const componentView = findComponentView(lView);
const componentHost = componentView[T_HOST] as TElementNode;
const parentView = getLViewParent(componentView);
const firstProjectedTNode: TNode|null =
(componentHost.projection as(TNode | null)[])[tNode.projection as number];

if (firstProjectedTNode != null) {
return getFirstNativeNode(parentView !, firstProjectedTNode);
} else {
return getFirstNativeNode(lView, tNode.next);
}
}
}

return null;
}

export function getBeforeNodeForView(viewIndexInContainer: number, lContainer: LContainer): RNode|
null {
const nextViewIndex = CONTAINER_HEADER_OFFSET + viewIndexInContainer + 1;
if (nextViewIndex < lContainer.length) {
const lView = lContainer[nextViewIndex] as LView;
ngDevMode && assertDefined(lView[T_HOST], 'Missing Host TNode');
let tViewNodeChild = (lView[T_HOST] as TViewNode).child;
if (tViewNodeChild !== null) {
if (tViewNodeChild.type === TNodeType.ElementContainer ||
tViewNodeChild.type === TNodeType.IcuContainer) {
let currentChild = tViewNodeChild.child;
while (currentChild && (currentChild.type === TNodeType.ElementContainer ||
currentChild.type === TNodeType.IcuContainer)) {
currentChild = currentChild.child;
}
tViewNodeChild = currentChild || tViewNodeChild;
}
return getNativeByTNodeOrNull(tViewNodeChild, lView);
}
return getFirstNativeNode(lView, lView[TVIEW].firstChild);
}

return lContainer[NATIVE];
Expand Down
265 changes: 264 additions & 1 deletion packages/core/test/acceptance/view_insertion_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
*/

import {CommonModule} from '@angular/common';
import {ChangeDetectorRef, Component, EmbeddedViewRef, TemplateRef, ViewChild, ViewContainerRef} from '@angular/core';
import {ChangeDetectorRef, Component, ComponentFactoryResolver, Directive, EmbeddedViewRef, Injector, NgModule, TemplateRef, ViewChild, ViewContainerRef, ViewRef} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
import {onlyInIvy} from '@angular/private/testing';

describe('view insertion', () => {
describe('of a simple template', () => {
Expand Down Expand Up @@ -249,4 +250,266 @@ describe('view insertion', () => {
expect(fixture.debugElement.queryAll(By.css('div.dynamic')).length).toBe(4);
});
});

describe('before another view', () => {
@Directive({selector: '[viewInserting]', exportAs: 'vi'})
class ViewInsertingDir {
constructor(private _vcRef: ViewContainerRef) {}

insert(beforeView: ViewRef, insertTpl: TemplateRef<{}>) {
this._vcRef.insert(beforeView, 0);
this._vcRef.createEmbeddedView(insertTpl, {}, 0);
}
}

describe('before embedded view', () => {
@Component({
selector: 'test-cmpt',
template: `
<ng-template #insert>insert</ng-template>
<ng-template #before>|before</ng-template>

<div><ng-template #vi="vi" viewInserting></ng-template></div>
`
})
class TestCmpt {
@ViewChild('before', {static: true}) beforeTpl !: TemplateRef<{}>;
@ViewChild('insert', {static: true}) insertTpl !: TemplateRef<{}>;
@ViewChild('vi', {static: true}) viewInsertingDir !: ViewInsertingDir;

minutes = 10;

insert() {
const beforeView = this.beforeTpl.createEmbeddedView({});
// change-detect the "before view" to create all child views
beforeView.detectChanges();
this.viewInsertingDir.insert(beforeView, this.insertTpl);
}
}

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TestCmpt, ViewInsertingDir],
imports: [CommonModule],
});
});

function createAndInsertViews(beforeTpl: string): any {
TestBed.overrideTemplate(TestCmpt, `
<ng-template #insert>insert</ng-template>
<ng-template #before>${beforeTpl}</ng-template>

<div><ng-template #vi="vi" viewInserting></ng-template></div>
`);
const fixture = TestBed.createComponent(TestCmpt);
fixture.detectChanges();

fixture.componentInstance.insert();
fixture.detectChanges();

return fixture.nativeElement;
}


it('should insert before a view with the text node as the first root node',
() => { expect(createAndInsertViews('|before').textContent).toBe('insert|before'); });

it('should insert before a view with the element as the first root node', () => {
expect(createAndInsertViews('<span>|before</span>').textContent).toBe('insert|before');
});

it('should insert before a view with the ng-container as the first root node', () => {
expect(createAndInsertViews(`
<ng-container>
<ng-container>|before</ng-container>
</ng-container>
`).textContent)
.toBe('insert|before');
});

it('should insert before a view with ICU container inside a ng-container as the first root node',
() => {
expect(
createAndInsertViews(
`<ng-container i18n>{minutes, plural, =0 {just now} =1 {one minute ago} other {|before}}</ng-container>`)
.textContent)
.toBe('insert|before');
});

it('should insert before a view with a container as the first root node', () => {
expect(createAndInsertViews(`<ng-template [ngIf]="true">|before</ng-template>`).textContent)
.toBe('insert|before');

});

it('should insert before a view with an empty container as the first root node', () => {
expect(createAndInsertViews(`<ng-template [ngIf]="true"></ng-template>`).textContent)
.toBe('insert');

});

onlyInIvy('VE incorrectly inserts views before ng-container content')
.it('should insert before a view with a ng-container where ViewContainerRef is injected',
() => {
expect(createAndInsertViews(`
<ng-container [ngTemplateOutlet]="after">|before</ng-container>
<ng-template #after>|after</ng-template>
`).textContent)
.toBe('insert|before|after');

});

it('should insert before a view with an element where ViewContainerRef is injected', () => {
expect(createAndInsertViews(`
<div [ngTemplateOutlet]="after">|before</div>
<ng-template #after>|after</ng-template>
`).textContent)
.toBe('insert|before|after');

});

it('should insert before a view with an empty projection as the first root node', () => {
expect(createAndInsertViews(`<ng-content></ng-content>|before`).textContent)
.toBe('insert|before');
});

it('should insert before a view with complex node structure', () => {
expect(createAndInsertViews(`
<ng-template [ngIf]="true">
<ng-container>
<ng-container>
<ng-template [ngIf]="true">|before</ng-template>
</ng-container>
</ng-container>
</ng-template>
`).textContent)
.toBe('insert|before');
});

});

describe('before embedded view with projection', () => {

@Component({
selector: 'with-content',
template: `
<ng-template #insert>insert</ng-template>
<ng-template #before><ng-content></ng-content></ng-template>
<div><ng-template #vi="vi" viewInserting></ng-template></div>
`
})
class WithContentCmpt {
@ViewChild('insert', {static: true}) insertTpl !: TemplateRef<{}>;
@ViewChild('before', {static: true}) beforeTpl !: TemplateRef<{}>;
@ViewChild('vi', {static: true}) viewInsertingDir !: ViewInsertingDir;

insert() {
const beforeView = this.beforeTpl.createEmbeddedView({});
// change-detect the "before view" to create all child views
beforeView.detectChanges();
this.viewInsertingDir.insert(beforeView, this.insertTpl);
}
}

@Component({selector: 'test-cmpt', template: ''})
class TestCmpt {
@ViewChild('wc', {static: true}) withContentCmpt !: WithContentCmpt;
}

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ViewInsertingDir, WithContentCmpt, TestCmpt],
imports: [CommonModule],
});
});

it('should insert before a view with projected text nodes', () => {
TestBed.overrideTemplate(TestCmpt, `<with-content #wc>|before</with-content>`);
const fixture = TestBed.createComponent(TestCmpt);
fixture.detectChanges();

fixture.componentInstance.withContentCmpt.insert();
fixture.detectChanges();

expect(fixture.nativeElement.textContent).toBe('insert|before');
});

it('should insert before a view with projected container', () => {
TestBed.overrideTemplate(
TestCmpt,
`<with-content #wc><ng-template [ngIf]="true">|before</ng-template></with-content>`);

const fixture = TestBed.createComponent(TestCmpt);
fixture.detectChanges();

fixture.componentInstance.withContentCmpt.insert();
fixture.detectChanges();

expect(fixture.nativeElement.textContent).toBe('insert|before');
});

});

describe('before component view', () => {
@Directive({selector: '[viewInserting]', exportAs: 'vi'})
class ViewInsertingDir {
constructor(private _vcRef: ViewContainerRef) {}

insert(beforeView: ViewRef, insertTpl: TemplateRef<{}>) {
this._vcRef.insert(beforeView, 0);
this._vcRef.createEmbeddedView(insertTpl, {}, 0);
}
}

@Component({selector: 'dynamic-cmpt', template: '|before'})
class DynamicComponent {
}

it('should insert in front a dynamic component view', () => {
@Component({
selector: 'test-cmpt',
template: `
<ng-template #insert>insert</ng-template>
<div><ng-template #vi="vi" viewInserting></ng-template></div>
`
})
class TestCmpt {
@ViewChild('insert', {static: true}) insertTpl !: TemplateRef<{}>;
@ViewChild('vi', {static: true}) viewInsertingDir !: ViewInsertingDir;

constructor(private _cfr: ComponentFactoryResolver, private _injector: Injector) {}

insert() {
// create a dynamic component view to act as an "insert before" view
const componentFactory = this._cfr.resolveComponentFactory(DynamicComponent);
const beforeView = componentFactory.create(this._injector).hostView;
// change-detect the "before view" to create all child views
beforeView.detectChanges();
this.viewInsertingDir.insert(beforeView, this.insertTpl);
}
}


@NgModule({
declarations: [TestCmpt, ViewInsertingDir, DynamicComponent],
imports: [CommonModule],
entryComponents: [DynamicComponent]
})
class TestModule {
}

TestBed.configureTestingModule({imports: [TestModule]});

const fixture = TestBed.createComponent(TestCmpt);
fixture.detectChanges();

fixture.componentInstance.insert();
fixture.detectChanges();

expect(fixture.nativeElement.textContent).toBe('insert|before');
});

});
});

});
Loading