fix(linker): prevent pollution of empty embeddedView context (#10548)
Fixes #10045
This commit is contained in:
parent
74b57dfa7d
commit
46bbcefb36
|
@ -15,12 +15,12 @@ import {Parser} from '../expression_parser/parser';
|
|||
import {ListWrapper, SetWrapper, StringMapWrapper} from '../facade/collection';
|
||||
import {BaseException} from '../facade/exceptions';
|
||||
import {isBlank, isPresent} from '../facade/lang';
|
||||
import {Identifiers, identifierToken} from '../identifiers';
|
||||
import * as html from '../ml_parser/ast';
|
||||
import {HtmlParser, ParseTreeResult} from '../ml_parser/html_parser';
|
||||
import {expandNodes} from '../ml_parser/icu_ast_expander';
|
||||
import {InterpolationConfig} from '../ml_parser/interpolation_config';
|
||||
import {mergeNsAndName, splitNsName} from '../ml_parser/tags';
|
||||
import {Identifiers, identifierToken} from '../identifiers';
|
||||
import {ParseError, ParseErrorLevel, ParseSourceSpan} from '../parse_util';
|
||||
import {ProviderElementContext, ProviderViewContext} from '../provider_analyzer';
|
||||
import {ElementSchemaRegistry} from '../schema/element_schema_registry';
|
||||
|
@ -32,6 +32,7 @@ import {AttrAst, BoundDirectivePropertyAst, BoundElementPropertyAst, BoundEventA
|
|||
import {PreparsedElementType, preparseElement} from './template_preparser';
|
||||
|
||||
|
||||
|
||||
// Group 1 = "bind-"
|
||||
// Group 2 = "var-"
|
||||
// Group 3 = "let-"
|
||||
|
|
|
@ -6,14 +6,11 @@
|
|||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {isBlank} from '../facade/lang';
|
||||
import {AppElement} from './element';
|
||||
import {ElementRef} from './element_ref';
|
||||
import {AppView} from './view';
|
||||
import {EmbeddedViewRef} from './view_ref';
|
||||
|
||||
const EMPTY_CONTEXT = new Object();
|
||||
|
||||
/**
|
||||
* Represents an Embedded Template that can be used to instantiate Embedded Views.
|
||||
*
|
||||
|
@ -51,10 +48,7 @@ export class TemplateRef_<C> extends TemplateRef<C> {
|
|||
createEmbeddedView(context: C): EmbeddedViewRef<C> {
|
||||
var view: AppView<C> = this._viewFactory(
|
||||
this._appElement.parentView.viewUtils, this._appElement.parentInjector, this._appElement);
|
||||
if (isBlank(context)) {
|
||||
context = <any>EMPTY_CONTEXT;
|
||||
}
|
||||
view.create(context, null, null);
|
||||
view.create(context || <any>{}, null, null);
|
||||
return view.ref;
|
||||
}
|
||||
|
||||
|
|
|
@ -470,6 +470,27 @@ function declareTests({useJit}: {useJit: boolean}) {
|
|||
});
|
||||
}));
|
||||
|
||||
it('should not share empty context for template directives - issue #10045',
|
||||
inject(
|
||||
[TestComponentBuilder, AsyncTestCompleter],
|
||||
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
|
||||
tcb.overrideView(
|
||||
MyComp, new ViewMetadata({
|
||||
template:
|
||||
'<template pollutedContext let-foo="bar">{{foo}}</template><template noContext let-foo="bar">{{foo}}</template>',
|
||||
directives: [PollutedContext, NoContext]
|
||||
}))
|
||||
|
||||
.createAsync(MyComp)
|
||||
.then((fixture) => {
|
||||
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('baz');
|
||||
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should not detach views in ViewContainers when the parent view is destroyed.',
|
||||
inject(
|
||||
[TestComponentBuilder, AsyncTestCompleter],
|
||||
|
@ -2303,6 +2324,21 @@ class SomeViewport {
|
|||
}
|
||||
}
|
||||
|
||||
@Directive({selector: '[pollutedContext]'})
|
||||
class PollutedContext {
|
||||
constructor(private tplRef: TemplateRef<any>, private vcRef: ViewContainerRef) {
|
||||
const evRef = this.vcRef.createEmbeddedView(this.tplRef);
|
||||
evRef.context.bar = 'baz';
|
||||
}
|
||||
}
|
||||
|
||||
@Directive({selector: '[noContext]'})
|
||||
class NoContext {
|
||||
constructor(private tplRef: TemplateRef<any>, private vcRef: ViewContainerRef) {
|
||||
this.vcRef.createEmbeddedView(this.tplRef);
|
||||
}
|
||||
}
|
||||
|
||||
@Pipe({name: 'double'})
|
||||
class DoublePipe implements PipeTransform, OnDestroy {
|
||||
ngOnDestroy() {}
|
||||
|
|
Loading…
Reference in New Issue