Skip to content

Commit

Permalink
fix(compiler): project using the right directive as component.
Browse files Browse the repository at this point in the history
Closes #8344
  • Loading branch information
tbosch committed Apr 30, 2016
1 parent 351f24e commit 0f774df
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 19 deletions.
13 changes: 0 additions & 13 deletions modules/angular2/src/compiler/template_ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,6 @@ export class ElementAst implements TemplateAst {
visit(visitor: TemplateAstVisitor, context: any): any {
return visitor.visitElement(this, context);
}

/**
* Get the component associated with this element, if any.
*/
getComponent(): CompileDirectiveMetadata {
for (var i = 0; i < this.directives.length; i++) {
var dirAst = this.directives[i];
if (dirAst.directive.isComponent) {
return dirAst.directive;
}
}
return null;
}
}

/**
Expand Down
5 changes: 3 additions & 2 deletions modules/angular2/src/compiler/template_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,9 @@ class ElementContext {
providerContext: ProviderElementContext): ElementContext {
var matcher = new SelectorMatcher();
var wildcardNgContentIndex = null;
if (directives.length > 0 && directives[0].directive.isComponent) {
var ngContentSelectors = directives[0].directive.template.ngContentSelectors;
var component = directives.find(directive => directive.directive.isComponent);
if (isPresent(component)) {
var ngContentSelectors = component.directive.template.ngContentSelectors;
for (var i = 0; i < ngContentSelectors.length; i++) {
var selector = ngContentSelectors[i];
if (StringWrapper.equals(selector, '*')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ class ViewBuilderVisitor implements TemplateAstVisitor {

var renderNode = o.THIS_EXPR.prop(fieldName);

var component = ast.getComponent();
var directives = ast.directives.map(directiveAst => directiveAst.directive);
var component = directives.find(directive => directive.isComponent);
var htmlAttrs = _readHtmlAttrs(ast.attrs);
var attrNameAndValues = _mergeHtmlAndDirectiveAttrs(htmlAttrs, directives);
for (var i = 0; i < attrNameAndValues.length; i++) {
Expand Down
4 changes: 2 additions & 2 deletions modules/angular2/src/core/linker/view_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ export function castByValue<T>(input: any, value: T): T {
return <T>input;
}

export const EMPTY_ARRAY = CONST_EXPR([]);
export const EMPTY_MAP = CONST_EXPR({});
export const EMPTY_ARRAY = /*@ts2dart_const*/[];
export const EMPTY_MAP = /*@ts2dart_const*/ {};

export function pureProxy1<P0, R>(fn: (p0: P0) => R): (p0: P0) => R {
var result: R;
Expand Down
14 changes: 14 additions & 0 deletions modules/angular2/test/compiler/template_parser_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,14 @@ There is no directive with "exportAs" set to "dirA" ("<div [ERROR ->]#a="dirA"><
})
}

function createDir(selector: string): CompileDirectiveMetadata {
return CompileDirectiveMetadata.create({
selector: selector,
type:
new CompileTypeMetadata({moduleUrl: someModuleUrl, name: `SomeDir${compCounter++}`})
})
}

describe('project text nodes', () => {
it('should project text nodes with wildcard selector', () => {
expect(humanizeContentProjection(parse('<div>hello</div>', [createComp('div', ['*'])])))
Expand Down Expand Up @@ -1140,6 +1148,12 @@ There is no directive with "exportAs" set to "dirA" ("<div [ERROR ->]#a="dirA"><
.toEqual([['div', null], ['template', 1], ['a', null]]);
});
});

it('should support other directives before the component', () => {
expect(humanizeContentProjection(
parse('<div>hello</div>', [createDir('div'), createComp('div', ['*'])])))
.toEqual([['div', null], ['#text(hello)', 0]]);
});
});

describe('splitClasses', () => {
Expand Down
21 changes: 21 additions & 0 deletions modules/angular2/test/core/linker/regression_integration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
OpaqueToken,
Injector
} from 'angular2/core';
import {NgIf, NgClass} from 'angular2/common';
import {CompilerConfig} from 'angular2/compiler';

export function main() {
Expand Down Expand Up @@ -170,6 +171,22 @@ function declareTests(isJit: boolean) {
});
}));

it('should support ngClass before a component and content projection inside of an ngIf',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
tcb.overrideView(
MyComp, new ViewMetadata({
template: `A<cmp-content *ngIf="true" [ngClass]="'red'">B</cmp-content>C`,
directives: [NgClass, NgIf, CmpWithNgContent]
}))
.createAsync(MyComp)
.then((fixture) => {
fixture.detectChanges();
expect(fixture.nativeElement).toHaveText('ABC');
async.done();
});
}));


});
}

Expand All @@ -187,3 +204,7 @@ class PlatformPipe implements PipeTransform {
class CustomPipe implements PipeTransform {
transform(value: any): any { return 'someCustomPipe'; }
}

@Component({selector: 'cmp-content', template: `<ng-content></ng-content>`})
class CmpWithNgContent {
}
1 change: 0 additions & 1 deletion tools/public_api_guard/public_api_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,6 @@ const COMPILER = [
'DirectiveAst.visit(visitor:TemplateAstVisitor, context:any):any',
'ElementAst',
'ElementAst.constructor(name:string, attrs:AttrAst[], inputs:BoundElementPropertyAst[], outputs:BoundEventAst[], references:ReferenceAst[], directives:DirectiveAst[], providers:ProviderAst[], hasViewContainer:boolean, children:TemplateAst[], ngContentIndex:number, sourceSpan:ParseSourceSpan)',
'ElementAst.getComponent():CompileDirectiveMetadata',
'ElementAst.visit(visitor:TemplateAstVisitor, context:any):any',
'EmbeddedTemplateAst',
'EmbeddedTemplateAst.constructor(attrs:AttrAst[], outputs:BoundEventAst[], references:ReferenceAst[], variables:VariableAst[], directives:DirectiveAst[], providers:ProviderAst[], hasViewContainer:boolean, children:TemplateAst[], ngContentIndex:number, sourceSpan:ParseSourceSpan)',
Expand Down

0 comments on commit 0f774df

Please sign in to comment.