fix(compiler): update compiler to flatten nested template fns (#24943)

PR Close #24943
This commit is contained in:
Kara Erickson
2018-07-30 15:54:11 -07:00
committed by Igor Minar
parent 87419097da
commit fe14f180a6
19 changed files with 863 additions and 557 deletions
@@ -541,6 +541,17 @@ describe('compiler compliance', () => {
const MyComponentDefinition = `
const $c1$ = ["foo", ""];
const $c2$ = ["if", ""];
function MyComponent_li_Template_2(rf, ctx0, ctx) {
if (rf & 1) {
$r3$.ɵE(0, "li");
$r3$.ɵT(1);
$r3$.ɵe();
}
if (rf & 2) {
const $foo$ = $r3$.ɵr(1, 1);
$r3$.ɵt(1, $r3$.ɵi2("", ctx.salutation, " ", $foo$, ""));
}
}
…
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: MyComponent,
@@ -552,17 +563,6 @@ describe('compiler compliance', () => {
$r3$.ɵC(2, MyComponent_li_Template_2, null, $c2$);
$r3$.ɵe();
}
const $foo$ = $r3$.ɵld(1);
function MyComponent_li_Template_2(rf, ctx0) {
if (rf & 1) {
$r3$.ɵE(0, "li");
$r3$.ɵT(1);
$r3$.ɵe();
}
if (rf & 2) {
$r3$.ɵt(1, $r3$.ɵi2("", ctx.salutation, " ", $foo$, ""));
}
}
},
directives:[IfDirective]
});`;
@@ -1173,8 +1173,8 @@ describe('compiler compliance', () => {
$r3$.ɵEe(0, "input", null, $c1$);
$r3$.ɵT(2);
}
const $user$ = $r3$.ɵld(1);
if (rf & 2) {
const $user$ = $r3$.ɵld(1);
$r3$.ɵt(2, $r3$.ɵi1("Hello ", $user$.value, "!"));
}
}
@@ -1187,6 +1187,97 @@ describe('compiler compliance', () => {
expectEmit(source, MyComponentDefinition, 'Incorrect MyComponent.ngComponentDef');
});
it('local references in nested views', () => {
const files = {
app: {
'spec.ts': `
import {Component, Directive, NgModule, TemplateRef} from '@angular/core';
@Directive({selector: '[if]'})
export class IfDirective {
constructor(template: TemplateRef<any>) { }
}
@Component({
selector: 'my-component',
template: \`
<div #foo></div>
{{foo}}
<div *if>
{{foo}}-{{bar}}
<span *if>{{foo}}-{{bar}}-{{baz}}</span>
<span #bar></span>
</div>
<div #baz></div>
\`
})
export class MyComponent {}
@NgModule({declarations: [IfDirective, MyComponent]})
export class MyModule {}
`
}
};
const MyComponentDefinition = `
const $c1$ = ["foo", ""];
const $c2$ = ["if", ""];
const $c3$ = ["baz", ""];
const $c4$ = ["bar", ""];
function MyComponent_div_span_Template_2(rf, ctx1, ctx0, ctx) {
if (rf & 1) {
$r3$.ɵE(0, "span");
$r3$.ɵT(1);
$r3$.ɵe();
}
if (rf & 2) {
const $foo$ = $r3$.ɵr(2, 1);
const $bar$ = $r3$.ɵr(1, 4);
const $baz$ = $r3$.ɵr(2, 5);
$r3$.ɵt(1, $r3$.ɵi3("", $foo$, "-", $bar$, "-", $baz$, ""));
}
}
function MyComponent_div_Template_3(rf, ctx0, ctx) {
if (rf & 1) {
$r3$.ɵE(0, "div");
$r3$.ɵT(1);
$r3$.ɵC(2, MyComponent_div_span_Template_2, null, $c2$);
$r3$.ɵEe(3, "span", null, $c4$);
$r3$.ɵe();
}
if (rf & 2) {
const $foo$ = $r3$.ɵr(1, 1);
const $bar$ = $r3$.ɵld(4);
$r3$.ɵt(1, $r3$.ɵi2(" ", $foo$, "-", $bar$, " "));
}
}
…
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: MyComponent,
selectors: [["my-component"]],
factory: function MyComponent_Factory() { return new MyComponent(); },
template: function MyComponent_Template(rf, ctx) {
if (rf & 1) {
$r3$.ɵEe(0, "div", null, $c1$);
$r3$.ɵT(2);
$r3$.ɵC(3, MyComponent_div_Template_3, null, $c2$);
$r3$.ɵEe(4, "div", null, $c3$);
}
if (rf & 2) {
const $foo$ = $r3$.ɵld(1);
$r3$.ɵt(2, $r3$.ɵi1(" ", $foo$, " "));
}
},
directives:[IfDirective]
});`;
const result = compile(files, angularFiles);
const source = result.source;
expectEmit(source, MyComponentDefinition, 'Incorrect MyComponent.ngComponentDef');
});
describe('lifecycle hooks', () => {
const files = {
app: {
@@ -1361,6 +1452,14 @@ describe('compiler compliance', () => {
const MyComponentDefinition = `
const $_c0$ = ["for","","forOf",""];
function MyComponent__svg_g_Template_1(rf, ctx0, ctx) {
if (rf & 1) {
$r3$.ɵNS();
$r3$.ɵE(0,"g");
$r3$.ɵEe(1,"circle");
$r3$.ɵe();
}
}
…
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: MyComponent,
@@ -1374,14 +1473,6 @@ describe('compiler compliance', () => {
$r3$.ɵe();
}
if (rf & 2) { $r3$.ɵp(1,"forOf",$r3$.ɵb(ctx.items)); }
function MyComponent__svg_g_Template_1(rf, ctx0) {
if (rf & 1) {
$r3$.ɵNS();
$r3$.ɵE(0,"g");
$r3$.ɵEe(1,"circle");
$r3$.ɵe();
}
}
},
directives: [ForOfDirective]
});
@@ -1434,6 +1525,17 @@ describe('compiler compliance', () => {
const MyComponentDefinition = `
const $_c0$ = ["for","","forOf",""];
function MyComponent_li_Template_1(rf, ctx0, ctx) {
if (rf & 1) {
$r3$.ɵE(0, "li");
$r3$.ɵT(1);
$r3$.ɵe();
}
if (rf & 2) {
const $item$ = ctx0.$implicit;
$r3$.ɵt(1, $r3$.ɵi1("", $item$.name, ""));
}
}
…
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: MyComponent,
@@ -1448,18 +1550,6 @@ describe('compiler compliance', () => {
if (rf & 2) {
$r3$.ɵp(1, "forOf", $r3$.ɵb(ctx.items));
}
function MyComponent_li_Template_1(rf, ctx0) {
if (rf & 1) {
$r3$.ɵE(0, "li");
$r3$.ɵT(1);
$r3$.ɵe();
}
if (rf & 2) {
const $item$ = ctx0.$implicit;
$r3$.ɵt(1, $r3$.ɵi1("", $item$.name, ""));
}
}
},
directives: [ForOfDirective]
});
@@ -1512,6 +1602,37 @@ describe('compiler compliance', () => {
const MyComponentDefinition = `
const $c1$ = ["for", "", "forOf", ""];
function MyComponent_li_li_Template_4(rf, ctx1, ctx0, ctx) {
if (rf & 1) {
$r3$.ɵE(0, "li");
$r3$.ɵT(1);
$r3$.ɵe();
}
if (rf & 2) {
const $item$ = ctx0.$implicit;
const $info$ = ctx1.$implicit;
$r3$.ɵt(1, $r3$.ɵi2(" ", $item$.name, ": ", $info$.description, " "));
}
}
function MyComponent_li_Template_1(rf, ctx0, ctx) {
if (rf & 1) {
$r3$.ɵE(0, "li");
$r3$.ɵE(1, "div");
$r3$.ɵT(2);
$r3$.ɵe();
$r3$.ɵE(3, "ul");
$r3$.ɵC(4, MyComponent_li_li_Template_4, null, $c1$);
$r3$.ɵe();
$r3$.ɵe();
}
if (rf & 2) {
const $item$ = ctx0.$implicit;
$r3$.ɵt(2, $r3$.ɵi1("", IDENT.name, ""));
$r3$.ɵp(4, "forOf", $r3$.ɵb(IDENT.infos));
}
}
…
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: MyComponent,
@@ -1526,37 +1647,6 @@ describe('compiler compliance', () => {
if (rf & 2) {
$r3$.ɵp(1, "forOf", $r3$.ɵb(ctx.items));
}
function MyComponent_li_Template_1(rf, ctx0) {
if (rf & 1) {
$r3$.ɵE(0, "li");
$r3$.ɵE(1, "div");
$r3$.ɵT(2);
$r3$.ɵe();
$r3$.ɵE(3, "ul");
$r3$.ɵC(4, MyComponent_li_li_Template_4, null, $c1$);
$r3$.ɵe();
$r3$.ɵe();
}
if (rf & 2) {
const $item$ = ctx0.$implicit;
$r3$.ɵt(2, $r3$.ɵi1("", IDENT.name, ""));
$r3$.ɵp(4, "forOf", $r3$.ɵb(IDENT.infos));
}
function MyComponent_li_li_Template_4(rf, ctx1) {
if (rf & 1) {
$r3$.ɵE(0, "li");
$r3$.ɵT(1);
$r3$.ɵe();
}
if (rf & 2) {
const $item$ = ctx0.$implicit;
const $info$ = ctx1.$implicit;
$r3$.ɵt(1, $r3$.ɵi2(" ", $item$.name, ": ", $info$.description, " "));
}
}
}
},
directives: [ForOfDirective]
});`;
@@ -52,6 +52,49 @@ describe('compiler compliance: template', () => {
// The template should look like this (where IDENT is a wild card for an identifier):
const template = `
const $c0$ = ["ngFor","","ngForOf",""];
function MyComponent_ul_li_div_Template_1(rf, $ctx2$, $ctx1$, $ctx0$, $ctx$) {
if (rf & 1) {
$i0$.ɵE(0, "div");
$i0$.ɵL("click", function MyComponent_ul_li_div_Template_1_div_click_listener($event){
const $outer$ = $ctx0$.$implicit;
const $middle$ = $ctx1$.$implicit;
const $inner$ = $ctx2$.$implicit;
return ctx.onClick($outer$, $middle$, $inner$);
});
$i0$.ɵT(1);
$i0$.ɵe();
}
if (rf & 2) {
const $outer$ = $ctx0$.$implicit;
const $middle$ = $ctx1$.$implicit;
const $inner$ = $ctx2$.$implicit;
$i0$.ɵp(0, "title", $i0$.ɵb(ctx.format($outer$, $middle$, $inner$, $ctx$.component)));
$i0$.ɵt(1, $i0$.ɵi1(" ", ctx.format($outer$, $middle$, $inner$, $ctx$.component), " "));
}
}
function MyComponent_ul_li_Template_1(rf, $ctx1$, $ctx0$, $ctx$) {
if (rf & 1) {
$i0$.ɵE(0, "li");
$i0$.ɵC(1, MyComponent_ul_li_div_Template_1, null, _c0);
$i0$.ɵe();
}
if (rf & 2) {
$i0$.ɵp(1, "ngForOf", $i0$.ɵb($ctx$.items));
}
}
function MyComponent_ul_Template_0(rf, $ctx0$, $ctx$) {
if (rf & 1) {
$i0$.ɵE(0, "ul");
$i0$.ɵC(1, MyComponent_ul_li_Template_1, null, _c0);
$i0$.ɵe();
}
if (rf & 2) {
const $outer$ = $ctx0$.$implicit;
$i0$.ɵp(1, "ngForOf", $i0$.ɵb($outer$.items));
}
}
// ...
template:function MyComponent_Template(rf, $ctx$){
if (rf & 1) {
@@ -60,48 +103,6 @@ describe('compiler compliance: template', () => {
if (rf & 2) {
$i0$.ɵp(0, "ngForOf", $i0$.ɵb($ctx$.items));
}
function MyComponent_ul_Template_0(rf, $ctx0$) {
if (rf & 1) {
$i0$.ɵE(0, "ul");
$i0$.ɵC(1, MyComponent_ul_li_Template_1, null, _c0);
$i0$.ɵe();
}
if (rf & 2) {
const $outer$ = $ctx0$.$implicit;
$i0$.ɵp(1, "ngForOf", $i0$.ɵb($outer$.items));
}
function MyComponent_ul_li_Template_1(rf, $ctx1$) {
if (rf & 1) {
$i0$.ɵE(0, "li");
$i0$.ɵC(1, MyComponent_ul_li_div_Template_1, null, _c0);
$i0$.ɵe();
}
if (rf & 2) {
$i0$.ɵp(1, "ngForOf", $i0$.ɵb($ctx$.items));
}
function MyComponent_ul_li_div_Template_1(rf, $ctx2$) {
if (rf & 1) {
$i0$.ɵE(0, "div");
$i0$.ɵL("click", function MyComponent_ul_li_div_Template_1_div_click_listener($event){
const $outer$ = $ctx0$.$implicit;
const $middle$ = $ctx1$.$implicit;
const $inner$ = $ctx2$.$implicit;
return ctx.onClick($outer$, $middle$, $inner$);
});
$i0$.ɵT(1);
$i0$.ɵe();
}
if (rf & 2) {
const $outer$ = $ctx0$.$implicit;
const $middle$ = $ctx1$.$implicit;
const $inner$ = $ctx2$.$implicit;
$i0$.ɵp(0, "title", $i0$.ɵb(ctx.format($outer$, $middle$, $inner$, $ctx$.component)));
$i0$.ɵt(1, $i0$.ɵi1(" ", ctx.format($outer$, $middle$, $inner$, $ctx$.component), " "));
}
}
}
}
}`;
const result = compile(files, angularFiles);
@@ -87,6 +87,8 @@ export class Identifiers {
static projection: o.ExternalReference = {name: 'ɵP', moduleName: CORE};
static projectionDef: o.ExternalReference = {name: 'ɵpD', moduleName: CORE};
static reference: o.ExternalReference = {name: 'ɵr', moduleName: CORE};
static inject: o.ExternalReference = {name: 'inject', moduleName: CORE};
static injectAttribute: o.ExternalReference = {name: 'ɵinjectAttribute', moduleName: CORE};
File diff suppressed because it is too large Load Diff
@@ -76,6 +76,7 @@ export {
e as ɵe,
p as ɵp,
pD as ɵpD,
r as ɵr,
rS as ɵrS,
a as ɵa,
s as ɵs,
+1 -1
View File
@@ -24,7 +24,7 @@ import {LInjector} from './interfaces/injector';
import {AttributeMarker, LContainerNode, LElementNode, LNode, LViewNode, TContainerNode, TElementNode, TNodeFlags, TNodeType} from './interfaces/node';
import {LQueries, QueryReadType} from './interfaces/query';
import {Renderer3} from './interfaces/renderer';
import {DECLARATION_PARENT, DIRECTIVES, HOST_NODE, INJECTOR, LViewData, QUERIES, RENDERER, TVIEW, TView} from './interfaces/view';
import {DECLARATION_VIEW, DIRECTIVES, HOST_NODE, INJECTOR, LViewData, QUERIES, RENDERER, TVIEW, TView} from './interfaces/view';
import {assertNodeOfPossibleTypes, assertNodeType} from './node_assert';
import {addRemoveViewFromContainer, appendChild, detachView, getChildLNode, getParentLNode, insertView, removeView} from './node_manipulation';
import {ViewRef} from './view_ref';
+2
View File
@@ -76,6 +76,8 @@ export {
text as T,
textBinding as t,
reference as r,
reserveSlots as rS,
embeddedViewStart as V,
+86 -55
View File
@@ -16,13 +16,13 @@ import {assertDefined, assertEqual, assertLessThan, assertNotDefined, assertNotE
import {throwCyclicDependencyError, throwErrorIfNoChangesMode, throwMultipleComponentError} from './errors';
import {executeHooks, executeInitHooks, queueInitHooks, queueLifecycleHooks} from './hooks';
import {ACTIVE_INDEX, LContainer, RENDER_PARENT, VIEWS} from './interfaces/container';
import {ComponentDefInternal, ComponentQuery, ComponentTemplate, DirectiveDefInternal, DirectiveDefListOrFactory, InitialStylingFlags, PipeDefListOrFactory, RenderFlags} from './interfaces/definition';
import {ComponentDefInternal, ComponentQuery, ComponentTemplate, DirectiveDefInternal, DirectiveDefListOrFactory, EmbeddedTemplate, InitialStylingFlags, PipeDefListOrFactory, RenderFlags} from './interfaces/definition';
import {LInjector} from './interfaces/injector';
import {AttributeMarker, InitialInputData, InitialInputs, LContainerNode, LElementNode, LNode, LProjectionNode, LTextNode, LViewNode, PropertyAliasValue, PropertyAliases, TAttributes, TContainerNode, TElementNode, TNode, TNodeFlags, TNodeType} from './interfaces/node';
import {CssSelectorList, NG_PROJECT_AS_ATTR_NAME} from './interfaces/projection';
import {LQueries} from './interfaces/query';
import {ProceduralRenderer3, RComment, RElement, RText, Renderer3, RendererFactory3, RendererStyleFlags3, isProceduralRenderer} from './interfaces/renderer';
import {BINDING_INDEX, CLEANUP, CONTAINER_INDEX, CONTENT_QUERIES, CONTEXT, CurrentMatchesList, DECLARATION_PARENT, DIRECTIVES, FLAGS, HEADER_OFFSET, HOST_NODE, INJECTOR, LViewData, LViewFlags, NEXT, PARENT, QUERIES, RENDERER, RootContext, SANITIZER, TAIL, TData, TVIEW, TView} from './interfaces/view';
import {BINDING_INDEX, CLEANUP, CONTAINER_INDEX, CONTENT_QUERIES, CONTEXT, CurrentMatchesList, DECLARATION_VIEW, DIRECTIVES, FLAGS, HEADER_OFFSET, HOST_NODE, INJECTOR, LViewData, LViewFlags, NEXT, PARENT, QUERIES, RENDERER, RootContext, SANITIZER, TAIL, TData, TVIEW, TView} from './interfaces/view';
import {assertNodeOfPossibleTypes, assertNodeType} from './node_assert';
import {appendChild, appendProjectedNode, canInsertNativeNode, createTextNode, findComponentHost, getChildLNode, getLViewChild, getNextLNode, getParentLNode, insertView, removeView} from './node_manipulation';
import {isNodeMatchingSelectorList, matchingSelectorIndex} from './node_selector_matcher';
@@ -334,7 +334,7 @@ export function createLViewData<T>(
null, // tail
-1, // containerIndex
null, // contentQueries
null // declarationParent
null // declarationView
];
}
@@ -500,7 +500,7 @@ export function renderTemplate<T>(
* Such lViewNode will then be renderer with renderEmbeddedTemplate() (see below).
*/
export function createEmbeddedViewNode<T>(
tView: TView, context: T, declarationParent: LViewData, renderer: Renderer3,
tView: TView, context: T, declarationView: LViewData, renderer: Renderer3,
queries?: LQueries | null): LViewNode {
const _isParent = isParent;
const _previousOrParentNode = previousOrParentNode;
@@ -509,7 +509,7 @@ export function createEmbeddedViewNode<T>(
const lView =
createLViewData(renderer, tView, context, LViewFlags.CheckAlways, getCurrentSanitizer());
lView[DECLARATION_PARENT] = declarationParent;
lView[DECLARATION_VIEW] = declarationView;
if (queries) {
lView[QUERIES] = queries.createView();
@@ -547,7 +547,7 @@ export function renderEmbeddedTemplate<T>(
oldView = enterView(viewNode.data !, viewNode);
namespaceHTML();
callTemplateWithContexts(rf, context, tView.template !, viewNode.data ![DECLARATION_PARENT] !);
callTemplateWithContexts(rf, context, tView.template !, viewNode.data ![DECLARATION_VIEW] !);
if (rf & RenderFlags.Update) {
refreshDescendantViews();
} else {
@@ -577,11 +577,14 @@ export function renderEmbeddedTemplate<T>(
* <li *ngFor="let item of list"> {{ item }} </li>
* </ul>
*
* function AppComponentTemplate(rf, ctx) {
* // instructions
* function ulTemplate(rf, ulCtx, appCtx) {...}
* function liTemplate(rf, liCtx, ulCtx, appCtx) {...}
* }
* function ulTemplate(rf, ulCtx, appCtx) {...}
* function liTemplate(rf, liCtx, ulCtx, appCtx) {...}
*
* class AppComponent {...}
* AppComponent.ngComponentDef = defineComponent({
* template: function AppComponentTemplate(rf, ctx) {...}
* });
*
*
* The ul view's template must be called with its own context and its declaration
* parent, AppComponent. The li view's template must be called with its own context, its
@@ -591,71 +594,85 @@ export function renderEmbeddedTemplate<T>(
* can be declared in different views than they are used.
*
* @param rf The RenderFlags for this template invocation
* @param context The context for this template
* @param currentContext The context for this template
* @param template The template function to call
* @param parent1 The declaration parent of the dynamic view
* @param parentView The declaration view of the dynamic view
*/
function callTemplateWithContexts(
rf: RenderFlags, context: any, template: ComponentTemplate<any>, parent1: LViewData): void {
const parent2 = parent1[DECLARATION_PARENT];
function callTemplateWithContexts<T>(
rf: RenderFlags, currentContext: T, template: EmbeddedTemplate<T>,
parentView: LViewData): void {
const parentContext = parentView[CONTEXT];
const parentView2 = parentView[DECLARATION_VIEW];
// Calling a function with extra arguments has a VM cost, so only call with necessary args
if (!parent2) return template(rf, context, parent1[CONTEXT]);
const parent3 = parent2[DECLARATION_PARENT];
if (!parent3) return template(rf, context, parent1[CONTEXT], parent2[CONTEXT]);
const parent4 = parent3[DECLARATION_PARENT];
if (!parent4) {
return template(rf, context, parent1[CONTEXT], parent2[CONTEXT], parent3[CONTEXT]);
if (parentView2 === null) {
return template(rf, currentContext, parentContext);
}
const parent5 = parent4[DECLARATION_PARENT];
if (!parent5) {
return template(
rf, context, parent1[CONTEXT], parent2[CONTEXT], parent3[CONTEXT], parent4[CONTEXT]);
const parentContext2 = parentView2[CONTEXT];
const parentView3 = parentView2[DECLARATION_VIEW];
if (parentView3 === null) {
return template(rf, currentContext, parentContext, parentContext2);
}
const parent6 = parent5[DECLARATION_PARENT];
if (!parent6) {
return template(
rf, context, parent1[CONTEXT], parent2[CONTEXT], parent3[CONTEXT], parent4[CONTEXT],
parent5[CONTEXT]);
const parentContext3 = parentView3[CONTEXT];
const parentView4 = parentView3[DECLARATION_VIEW];
if (parentView4 === null) {
return template(rf, currentContext, parentContext, parentContext2, parentContext3);
}
const parent7 = parent6[DECLARATION_PARENT];
if (!parent7) {
const parentContext4 = parentView4[CONTEXT];
const parentView5 = parentView4[DECLARATION_VIEW];
if (parentView5 === null) {
return template(
rf, context, parent1[CONTEXT], parent2[CONTEXT], parent3[CONTEXT], parent4[CONTEXT],
parent5[CONTEXT], parent6[CONTEXT]);
rf, currentContext, parentContext, parentContext2, parentContext3, parentContext4);
}
const parent8 = parent7[DECLARATION_PARENT];
if (!parent8) {
const parentContext5 = parentView5[CONTEXT];
const parentView6 = parentView5[DECLARATION_VIEW];
if (parentView6 === null) {
return template(
rf, context, parent1[CONTEXT], parent2[CONTEXT], parent3[CONTEXT], parent4[CONTEXT],
parent5[CONTEXT], parent6[CONTEXT], parent7[CONTEXT]);
rf, currentContext, parentContext, parentContext2, parentContext3, parentContext4,
parentContext5);
}
const parent9 = parent8[DECLARATION_PARENT];
if (!parent9) {
const parentContext6 = parentView6[CONTEXT];
const parentView7 = parentView6[DECLARATION_VIEW];
if (parentView7 === null) {
return template(
rf, context, parent1[CONTEXT], parent2[CONTEXT], parent3[CONTEXT], parent4[CONTEXT],
parent5[CONTEXT], parent6[CONTEXT], parent7[CONTEXT], parent8[CONTEXT]);
rf, currentContext, parentContext, parentContext2, parentContext3, parentContext4,
parentContext5, parentContext6);
}
const parentContext7 = parentView7[CONTEXT];
const parentView8 = parentView7[DECLARATION_VIEW];
if (parentView8 === null) {
return template(
rf, currentContext, parentContext, parentContext2, parentContext3, parentContext4,
parentContext5, parentContext6, parentContext7);
}
const parentContext8 = parentView8[CONTEXT];
const parentView9 = parentView8[DECLARATION_VIEW];
if (parentView9 === null) {
return template(
rf, currentContext, parentContext, parentContext2, parentContext3, parentContext4,
parentContext5, parentContext6, parentContext7, parentContext8);
}
// We support up to 8 nesting levels in embedded views before we give up and call apply()
const contexts = [
parent1[CONTEXT], parent2[CONTEXT], parent3[CONTEXT], parent4[CONTEXT], parent5[CONTEXT],
parent6[CONTEXT], parent7[CONTEXT], parent8[CONTEXT], parent9[CONTEXT]
const templateArgs = [
rf, currentContext, parentContext, parentContext2, parentContext3, parentContext4,
parentContext5, parentContext6, parentContext7, parentContext8, parentView9[CONTEXT]
];
let currentView: LViewData = parent9;
while (currentView[DECLARATION_PARENT]) {
contexts.push(currentView[DECLARATION_PARENT] ![CONTEXT]);
currentView = currentView[DECLARATION_PARENT] !;
let currentDeclarationView: LViewData|null = parentView9[DECLARATION_VIEW];
while (currentDeclarationView) {
templateArgs.push(currentDeclarationView[CONTEXT]);
currentDeclarationView = currentDeclarationView[DECLARATION_VIEW] !;
}
tView.template !(rf, context, ...contexts);
template.apply(null, templateArgs);
}
export function renderComponentOrTemplate<T>(
@@ -990,7 +1007,7 @@ function getOrCreateTView(
* @param pipes Registry of pipes for this view
*/
export function createTView(
viewIndex: number, template: ComponentTemplate<any>| null,
viewIndex: number, template: ComponentTemplate<any>| EmbeddedTemplate<any>| null,
directives: DirectiveDefListOrFactory | null, pipes: PipeDefListOrFactory | null,
viewQuery: ComponentQuery<any>| null): TView {
ngDevMode && ngDevMode.tView++;
@@ -1811,7 +1828,7 @@ export function createLContainer(
* @param localRefs A set of local reference bindings on the element.
*/
export function container(
index: number, template?: ComponentTemplate<any>, tagName?: string | null, attrs?: TAttributes,
index: number, template?: EmbeddedTemplate<any>, tagName?: string | null, attrs?: TAttributes,
localRefs?: string[] | null): void {
ngDevMode &&
assertEqual(
@@ -2636,6 +2653,20 @@ export function store<T>(index: number, value: T): void {
viewData[adjustedIndex] = value;
}
/** Retrieves a value from an LViewData at the given nesting level. */
export function reference<T>(nestingLevel: number, index: number) {
let currentView = viewData;
while (nestingLevel > 0) {
ngDevMode && assertDefined(
currentView[DECLARATION_VIEW],
'Declaration view should be defined if nesting level is greater than 0.');
currentView = currentView[DECLARATION_VIEW] !;
nestingLevel--;
}
return loadInternal<T>(index, currentView);
}
/** Retrieves a value from the `directives` array. */
export function loadDirective<T>(index: number): T {
ngDevMode && assertDefined(directives, 'Directives array should be defined if reading a dir.');
@@ -11,11 +11,19 @@ import {RendererType2} from '../../render/api';
import {Type} from '../../type';
import {CssSelectorList} from './projection';
/**
* Definition of what a template rendering function should look like.
* Definition of what a template rendering function should look like for a component.
*/
export type ComponentTemplate<T> = {
(rf: RenderFlags, ctx: T, ...parentCtx: ({} | null)[]): void; ngPrivateData?: never;
(rf: RenderFlags, ctx: T): void; ngPrivateData?: never;
};
/**
* Definition of what a template rendering function should look like for an embedded view.
*/
export type EmbeddedTemplate<T> = {
(rf: RenderFlags, ctx: T, ...parentCtx: any[]): void;
};
/**
+11 -11
View File
@@ -38,7 +38,7 @@ export const SANITIZER = 12;
export const TAIL = 13;
export const CONTAINER_INDEX = 14;
export const CONTENT_QUERIES = 15;
export const DECLARATION_PARENT = 16;
export const DECLARATION_VIEW = 16;
/**
* `LViewData` stores all of the information needed to process the instructions as
@@ -63,7 +63,7 @@ export interface LViewData extends Array<any> {
* `LViewData`. Without this, the render method would have to keep a stack of
* views as it is recursively rendering templates.
*
* This is also the "insertion" parent for embedded views. This allows us to properly
* This is the "insertion" view for embedded views. This allows us to properly
* destroy embedded views.
*/
[PARENT]: LViewData|null;
@@ -167,16 +167,16 @@ export interface LViewData extends Array<any> {
[CONTENT_QUERIES]: QueryList<any>[]|null;
/**
* Parent view where this view's template was declared.
* View where this view's template was declared.
*
* Only applicable for dynamically created views. Will be null for inline/component views.
*
* The template for a dynamically created view may be declared in a different view than
* it is inserted. We already track the "insertion parent" (view where the template was
* inserted) in LViewData[PARENT], but we also need access to the "declaration parent"
* it is inserted. We already track the "insertion view" (view where the template was
* inserted) in LViewData[PARENT], but we also need access to the "declaration view"
* (view where the template was declared). Otherwise, we wouldn't be able to call the
* view's template function with the proper contexts. Context should be inherited from
* the declaration parent tree, not the insertion parent tree.
* the declaration view tree, not the insertion view tree.
*
* Example (AppComponent template):
*
@@ -184,13 +184,13 @@ export interface LViewData extends Array<any> {
* <some-comp [tpl]="foo"></some-comp> <-- inserted inside this component -->
*
* The <ng-template> above is declared in the AppComponent template, but it will be passed into
* SomeComp and inserted there. In this case, the declaration parent would be the AppComponent,
* but the insertion parent would be SomeComp. When we are removing views, we would want to
* traverse through the insertion parent to clean up listeners. When we are calling the
* template function during change detection, we need the declaration parent to get inherited
* SomeComp and inserted there. In this case, the declaration view would be the AppComponent,
* but the insertion view would be SomeComp. When we are removing views, we would want to
* traverse through the insertion view to clean up listeners. When we are calling the
* template function during change detection, we need the declaration view to get inherited
* context.
*/
[DECLARATION_PARENT]: LViewData|null;
[DECLARATION_VIEW]: LViewData|null;
}
/** Flags associated with an LView (saved in LViewData[FLAGS]) */
@@ -81,6 +81,7 @@ export const angularCoreEnv: {[name: string]: Function} = {
'ɵqR': r3.qR,
'ɵQr': r3.Qr,
'ɵrS': r3.rS,
'ɵr': r3.r,
'ɵs': r3.s,
'ɵsm': r3.sm,
'ɵsp': r3.sp,
+28
View File
@@ -12,6 +12,7 @@ import {ViewContainerRef as viewEngine_ViewContainerRef} from '../linker/view_co
import {EmbeddedViewRef as viewEngine_EmbeddedViewRef, InternalViewRef as viewEngine_InternalViewRef} from '../linker/view_ref';
import {checkNoChanges, detectChanges, markViewDirty, storeCleanupFn, viewAttached} from './instructions';
import {EmbeddedTemplate} from './interfaces/definition';
import {LViewNode} from './interfaces/node';
import {FLAGS, LViewData, LViewFlags} from './interfaces/view';
import {destroyLView} from './node_manipulation';
@@ -243,3 +244,30 @@ export class ViewRef<T> implements viewEngine_EmbeddedViewRef<T>, viewEngine_Int
attachToAppRef(appRef: ApplicationRef) { this._appRef = appRef; }
}
<<<<<<< HEAD
=======
export class EmbeddedViewRef<T> extends ViewRef<T> {
/**
* @internal
*/
_lViewNode: LViewNode;
private _viewContainerRef: viewEngine_ViewContainerRef|null = null;
constructor(viewNode: LViewNode, template: EmbeddedTemplate<T>, context: T) {
super(viewNode.data, context);
this._lViewNode = viewNode;
}
destroy(): void {
if (this._viewContainerRef && viewAttached(this._view)) {
this._viewContainerRef.detach(this._viewContainerRef.indexOf(this));
this._viewContainerRef = null;
}
super.destroy();
}
attachToViewContainerRef(vcRef: viewEngine_ViewContainerRef) { this._viewContainerRef = vcRef; }
}
>>>>>>> fixup! fix(ivy): flatten template fns for nested views
@@ -17,6 +17,9 @@
{
"name": "ChangeDetectionStrategy"
},
{
"name": "DECLARATION_VIEW"
},
{
"name": "DIRECTIVES"
},
@@ -98,6 +101,9 @@
{
"name": "callHooks"
},
{
"name": "callTemplateWithContexts"
},
{
"name": "canInsertNativeNode"
},
@@ -210,10 +216,10 @@
"name": "refreshContentQueries"
},
{
"name": "refreshDynamicEmbeddedViews"
"name": "refreshDescendantViews"
},
{
"name": "refreshView"
"name": "refreshDynamicEmbeddedViews"
},
{
"name": "renderComponent"
@@ -26,6 +26,9 @@
{
"name": "ChangeDetectionStrategy"
},
{
"name": "DECLARATION_VIEW"
},
{
"name": "DIRECTIVES"
},
@@ -137,6 +140,24 @@
{
"name": "TemplateRef$1"
},
{
"name": "ToDoAppComponent_footer_Template_6"
},
{
"name": "ToDoAppComponent_footer_button_Template_5"
},
{
"name": "ToDoAppComponent_section_Template_5"
},
{
"name": "ToDoAppComponent_section_input_Template_1"
},
{
"name": "ToDoAppComponent_section_li_Template_3"
},
{
"name": "ToDoAppComponent_section_li_input_Template_6"
},
{
"name": "Todo"
},
@@ -305,6 +326,9 @@
{
"name": "callHooks"
},
{
"name": "callTemplateWithContexts"
},
{
"name": "canInsertNativeNode"
},
@@ -701,6 +725,9 @@
{
"name": "readElementValue"
},
{
"name": "reference"
},
{
"name": "refreshChildComponents"
},
@@ -708,10 +735,10 @@
"name": "refreshContentQueries"
},
{
"name": "refreshDynamicEmbeddedViews"
"name": "refreshDescendantViews"
},
{
"name": "refreshView"
"name": "refreshDynamicEmbeddedViews"
},
{
"name": "removeListeners"
File diff suppressed because it is too large Load Diff
+21 -18
View File
@@ -389,28 +389,31 @@ describe('recursive components', () => {
elementProperty(2, 'ngIf', bind(ctx.data.right));
}
function IfTemplate(rf1: RenderFlags, left: any, parent: NgIfTree) {
if (rf1 & RenderFlags.Create) {
elementStart(0, 'ng-if-tree');
elementEnd();
}
if (rf1 & RenderFlags.Update) {
elementProperty(0, 'data', bind(parent.data.left));
}
}
function IfTemplate2(rf1: RenderFlags, right: any, parent: NgIfTree) {
if (rf1 & RenderFlags.Create) {
elementStart(0, 'ng-if-tree');
elementEnd();
}
if (rf1 & RenderFlags.Update) {
elementProperty(0, 'data', bind(parent.data.right));
}
}
},
inputs: {data: 'data'},
});
}
function IfTemplate(rf1: RenderFlags, left: any, parent: NgIfTree) {
if (rf1 & RenderFlags.Create) {
elementStart(0, 'ng-if-tree');
elementEnd();
}
if (rf1 & RenderFlags.Update) {
elementProperty(0, 'data', bind(parent.data.left));
}
}
function IfTemplate2(rf1: RenderFlags, right: any, parent: NgIfTree) {
if (rf1 & RenderFlags.Create) {
elementStart(0, 'ng-if-tree');
elementEnd();
}
if (rf1 & RenderFlags.Update) {
elementProperty(0, 'data', bind(parent.data.right));
}
}
(NgIfTree.ngComponentDef as ComponentDefInternal<NgIfTree>).directiveDefs =
() => [NgIfTree.ngComponentDef, NgIf.ngDirectiveDef];
+14 -12
View File
@@ -823,14 +823,15 @@ describe('content projection', () => {
elementProperty(1, 'ngIf', bind(ctx.showing));
}
function IfTemplate(rf1: RenderFlags, ctx1: any, child: any) {
if (rf1 & RenderFlags.Create) {
projectionDef();
projection(0);
}
}
}, [NgIf]);
function IfTemplate(rf1: RenderFlags, ctx: any, child: any) {
if (rf1 & RenderFlags.Create) {
projectionDef();
projection(0);
}
}
let child: {showing: boolean};
/**
* <child>
@@ -887,14 +888,15 @@ describe('content projection', () => {
elementProperty(1, 'ngIf', bind(ctx.showing));
}
function IfTemplate(rf1: RenderFlags, ctx1: any, child: any) {
if (rf1 & RenderFlags.Create) {
projectionDef();
projection(0);
}
}
}, [NgIf]);
function IfTemplate(rf: RenderFlags, ctx: any, child: any) {
if (rf & RenderFlags.Create) {
projectionDef();
projection(0);
}
}
let child: {showing: boolean};
/**
* <child>
+81 -22
View File
@@ -6,10 +6,11 @@
* found in the LICENSE file at https://angular.io/license
*/
import {defineComponent, defineDirective} from '../../src/render3/index';
import {bind, container, containerRefreshEnd, containerRefreshStart, element, elementAttribute, elementClassProp, elementEnd, elementProperty, elementStart, elementStyling, elementStylingApply, embeddedViewEnd, embeddedViewStart, load, text, textBinding} from '../../src/render3/instructions';
import {AttributeMarker, defineComponent, defineDirective} from '../../src/render3/index';
import {bind, container, containerRefreshEnd, containerRefreshStart, element, elementAttribute, elementClassProp, elementEnd, elementProperty, elementStart, elementStyling, elementStylingApply, embeddedViewEnd, embeddedViewStart, interpolation2, load, reference, text, textBinding} from '../../src/render3/instructions';
import {InitialStylingFlags, RenderFlags} from '../../src/render3/interfaces/definition';
import {NgIf} from './common_with_def';
import {ComponentFixture, createComponent, renderToHtml} from './render_util';
describe('exports', () => {
@@ -21,9 +22,8 @@ describe('exports', () => {
element(0, 'input', ['value', 'one'], ['myInput', '']);
text(2);
}
let tmp: any;
if (rf & RenderFlags.Update) {
tmp = load(1);
const tmp = load(1) as any;
textBinding(2, tmp.value);
}
}
@@ -39,9 +39,8 @@ describe('exports', () => {
element(0, 'comp', null, ['myComp', '']);
text(2);
}
let tmp: any;
if (rf & RenderFlags.Update) {
tmp = load(1);
const tmp = load(1) as any;
textBinding(2, tmp.name);
}
}
@@ -94,9 +93,8 @@ describe('exports', () => {
element(0, 'comp', null, ['myComp', '']);
element(2, 'div', ['myDir', '']);
}
let tmp: any;
if (rf & RenderFlags.Update) {
tmp = load(1);
const tmp = load(1) as any;
elementProperty(2, 'myDir', bind(tmp));
}
}
@@ -113,9 +111,8 @@ describe('exports', () => {
element(0, 'div', ['someDir', ''], ['myDir', 'someDir']);
text(2);
}
let tmp: any;
if (rf & RenderFlags.Update) {
tmp = load(1);
const tmp = load(1) as any;
textBinding(2, tmp.name);
}
}
@@ -155,8 +152,8 @@ describe('exports', () => {
text(0);
element(1, 'input', ['value', 'one'], ['myInput', '']);
}
const tmp = load(2) as any;
if (rf & RenderFlags.Update) {
const tmp = load(2) as any;
textBinding(0, bind(tmp.value));
}
}
@@ -172,8 +169,8 @@ describe('exports', () => {
element(0, 'div');
element(1, 'input', ['value', 'one'], ['myInput', '']);
}
const tmp = load(2) as any;
if (rf & RenderFlags.Update) {
const tmp = load(2) as any;
elementProperty(0, 'title', bind(tmp.value));
}
}
@@ -188,8 +185,8 @@ describe('exports', () => {
element(0, 'div');
element(1, 'input', ['value', 'one'], ['myInput', '']);
}
const tmp = load(2) as any;
if (rf & RenderFlags.Update) {
const tmp = load(2) as any;
elementAttribute(0, 'aria-label', bind(tmp.value));
}
}
@@ -206,8 +203,8 @@ describe('exports', () => {
elementEnd();
element(1, 'input', ['type', 'checkbox', 'checked', 'true'], ['myInput', '']);
}
const tmp = load(2) as any;
if (rf & RenderFlags.Update) {
const tmp = load(2) as any;
elementClassProp(0, 0, tmp.checked);
elementStylingApply(0);
}
@@ -253,9 +250,8 @@ describe('exports', () => {
element(0, 'div', ['myDir', '']);
element(1, 'comp', null, ['myComp', '']);
}
let tmp: any;
if (rf & RenderFlags.Update) {
tmp = load(2) as any;
const tmp = load(2) as any;
elementProperty(0, 'myDir', bind(tmp));
}
}
@@ -274,11 +270,9 @@ describe('exports', () => {
element(2, 'comp', null, ['myComp', '']);
element(4, 'input', ['value', 'one'], ['myInput', '']);
}
let tmp1: any;
let tmp2: any;
if (rf & RenderFlags.Update) {
tmp1 = load(3) as any;
tmp2 = load(5) as any;
const tmp1 = load(3) as any;
const tmp2 = load(5) as any;
textBinding(0, bind(tmp2.value));
textBinding(1, bind(tmp1.name));
}
@@ -315,13 +309,12 @@ describe('exports', () => {
if (ctx.condition) {
let rf1 = embeddedViewStart(1);
{
let tmp: any;
if (rf1 & RenderFlags.Create) {
text(0);
element(1, 'input', ['value', 'one'], ['myInput', '']);
}
if (rf1 & RenderFlags.Update) {
tmp = load(2);
const tmp = load(2) as any;
textBinding(0, bind(tmp.value));
}
}
@@ -337,5 +330,71 @@ describe('exports', () => {
})).toEqual('<div>one<input value="one"></div>');
expect(renderToHtml(Template, {condition: false})).toEqual('<div></div>');
});
it('should support local refs in nested dynamic views', () => {
/**
* <input value="one" #outerInput>
* <div *ngIf="outer">
* {{ outerInput.value }}
*
* <input value = "two" #innerInput>
*
* <div *ngIf="inner">
* {{ outerInput.value }} - {{ innerInput.value}}
* </div>
* </div>
*/
const App = createComponent('app', function(rf: RenderFlags, app: any) {
if (rf & RenderFlags.Create) {
elementStart(0, 'input', ['value', 'one'], ['outerInput', '']);
elementEnd();
container(2, outerTemplate, '', [AttributeMarker.SelectOnly, 'ngIf']);
}
if (rf & RenderFlags.Update) {
elementProperty(2, 'ngIf', bind(app.outer));
}
}, [NgIf]);
function outerTemplate(rf: RenderFlags, outer: any, app: any) {
if (rf & RenderFlags.Create) {
elementStart(0, 'div');
{
text(1);
elementStart(2, 'input', ['value', 'two'], ['innerInput', '']);
elementEnd();
container(4, innerTemplate, '', [AttributeMarker.SelectOnly, 'ngIf']);
}
elementEnd();
}
const outerInput = reference(1, 1) as any;
if (rf & RenderFlags.Update) {
textBinding(1, bind(outerInput.value));
elementProperty(4, 'ngIf', bind(app.inner));
}
}
function innerTemplate(rf: RenderFlags, inner: any, outer: any, app: any) {
if (rf & RenderFlags.Create) {
elementStart(0, 'div');
{ text(1); }
elementEnd();
}
const outerInput = reference(2, 1) as any;
const innerInput = reference(1, 3) as any;
if (rf & RenderFlags.Update) {
textBinding(1, interpolation2('', outerInput.value, ' - ', innerInput.value, ''));
}
}
const fixture = new ComponentFixture(App);
fixture.component.outer = true;
fixture.component.inner = true;
fixture.update();
expect(fixture.html)
.toEqual(`<input value="one"><div>one<input value="two"><div>one - two</div></div>`);
});
});
});
@@ -506,9 +506,9 @@ describe('ViewContainerRef', () => {
*
* <child [tpl]="foo"></child> <-- template insertion inside
*/
const Parent = createComponent('parent', function(rf: RenderFlags, ctx: any) {
const Parent = createComponent('parent', function(rf: RenderFlags, parent: any) {
if (rf & RenderFlags.Create) {
container(0, template);
container(0, fooTemplate);
elementStart(1, 'child');
elementEnd();
}
@@ -519,19 +519,20 @@ describe('ViewContainerRef', () => {
elementProperty(1, 'tpl', bind(tplRef));
}
function template(rf1: RenderFlags, ctx1: any, parent: any) {
if (rf1 & RenderFlags.Create) {
elementStart(0, 'div');
{ text(1); }
elementEnd();
}
if (rf1 & RenderFlags.Update) {
textBinding(1, bind(parent.name));
}
}
}, [Child]);
function fooTemplate(rf1: RenderFlags, ctx: any, parent: any) {
if (rf1 & RenderFlags.Create) {
elementStart(0, 'div');
{ text(1); }
elementEnd();
}
if (rf1 & RenderFlags.Update) {
textBinding(1, bind(parent.name));
}
}
const fixture = new ComponentFixture(Parent);
fixture.component.name = 'Parent';
fixture.update();
@@ -609,37 +610,37 @@ describe('ViewContainerRef', () => {
elementProperty(1, 'rows', bind(parent.rows));
}
function rowTemplate(rf1: RenderFlags, row: any, parent: any) {
if (rf1 & RenderFlags.Create) {
container(0, cellTemplate);
elementStart(1, 'loop-comp');
elementEnd();
}
if (rf1 & RenderFlags.Update) {
// Hack until we have local refs for templates
const cellTemplateRef =
getOrCreateTemplateRef(getOrCreateNodeInjectorForNode(load(0)));
elementProperty(1, 'tpl', bind(cellTemplateRef));
elementProperty(1, 'rows', bind(row.$implicit.data));
}
}
function cellTemplate(rf1: RenderFlags, cell: any, row: any, parent: any) {
if (rf1 & RenderFlags.Create) {
elementStart(0, 'div');
{ text(1); }
elementEnd();
}
if (rf1 & RenderFlags.Update) {
textBinding(
1, interpolation3(
'', cell.$implicit, ' - ', row.$implicit.value, ' - ', parent.name, ''));
}
}
}, [LoopComp]);
function rowTemplate(rf1: RenderFlags, row: any, parent: any) {
if (rf1 & RenderFlags.Create) {
container(0, cellTemplate);
elementStart(1, 'loop-comp');
elementEnd();
}
if (rf1 & RenderFlags.Update) {
// Hack until we have local refs for templates
const cellTemplateRef = getOrCreateTemplateRef(getOrCreateNodeInjectorForNode(load(0)));
elementProperty(1, 'tpl', bind(cellTemplateRef));
elementProperty(1, 'rows', bind(row.$implicit.data));
}
}
function cellTemplate(rf1: RenderFlags, cell: any, row: any, parent: any) {
if (rf1 & RenderFlags.Create) {
elementStart(0, 'div');
{ text(1); }
elementEnd();
}
if (rf1 & RenderFlags.Update) {
textBinding(
1, interpolation3(
'', cell.$implicit, ' - ', row.$implicit.value, ' - ', parent.name, ''));
}
}
const fixture = new ComponentFixture(Parent);
fixture.component.name = 'Parent';
fixture.component.rows =