2018-08-20 09:20:12 -04:00
|
|
|
/**
|
|
|
|
* @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 {AttributeMarker} from '@angular/compiler/src/core';
|
|
|
|
import {setup} from '@angular/compiler/test/aot/test_util';
|
|
|
|
import {compile, expectEmit} from './mock_compile';
|
|
|
|
|
|
|
|
describe('compiler compliance: directives', () => {
|
|
|
|
|
|
|
|
const angularFiles = setup({
|
|
|
|
compileAngular: false,
|
|
|
|
compileAnimations: false,
|
|
|
|
compileFakeCore: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('matching', () => {
|
|
|
|
|
|
|
|
it('should not match directives on i18n attribute', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
2018-12-12 18:23:12 -05:00
|
|
|
import {Component, Directive, NgModule} from '@angular/core';
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
@Directive({selector: '[i18n]'})
|
|
|
|
export class I18nDirective {}
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
@Component({selector: 'my-component', template: '<div i18n></div>'})
|
|
|
|
export class MyComponent {}
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
@NgModule({declarations: [I18nDirective, MyComponent]})
|
|
|
|
export class MyModule{}`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// MyComponent definition should be:
|
|
|
|
const MyComponentDefinition = `
|
|
|
|
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
|
2018-12-19 15:17:38 -05:00
|
|
|
type: MyComponent,
|
|
|
|
selectors: [["my-component"]],
|
|
|
|
factory: function MyComponent_Factory(t) { return new (t || MyComponent)(); },
|
|
|
|
consts: 1,
|
|
|
|
vars: 0,
|
2018-08-20 09:20:12 -04:00
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
|
|
|
$r3$.ɵelement(0, "div");
|
|
|
|
}
|
2018-11-19 12:48:14 -05:00
|
|
|
},
|
|
|
|
encapsulation: 2
|
2018-08-20 09:20:12 -04:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, MyComponentDefinition, 'Incorrect ChildComponent.ngComponentDef');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not match directives on i18n-prefixed attributes', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
2018-12-12 18:23:12 -05:00
|
|
|
import {Component, Directive, NgModule} from '@angular/core';
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
@Directive({selector: '[i18n]'})
|
|
|
|
export class I18nDirective {}
|
|
|
|
|
|
|
|
@Directive({selector: '[i18n-foo]'})
|
|
|
|
export class I18nFooDirective {}
|
|
|
|
|
|
|
|
@Directive({selector: '[foo]'})
|
|
|
|
export class FooDirective {}
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
@Component({selector: 'my-component', template: '<div i18n-foo></div>'})
|
|
|
|
export class MyComponent {}
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
@NgModule({declarations: [I18nDirective, I18nFooDirective, FooDirective, MyComponent]})
|
|
|
|
export class MyModule{}`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// MyComponent definition should be:
|
|
|
|
const MyComponentDefinition = `
|
|
|
|
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
|
2018-12-19 15:17:38 -05:00
|
|
|
type: MyComponent,
|
|
|
|
selectors: [["my-component"]],
|
|
|
|
factory: function MyComponent_Factory(t) { return new (t || MyComponent)(); },
|
|
|
|
consts: 1,
|
|
|
|
vars: 0,
|
2018-08-20 09:20:12 -04:00
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
|
|
|
$r3$.ɵelement(0, "div");
|
|
|
|
}
|
2018-11-19 12:48:14 -05:00
|
|
|
},
|
|
|
|
encapsulation: 2
|
2018-08-20 09:20:12 -04:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, MyComponentDefinition, 'Incorrect ChildComponent.ngComponentDef');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should match directives on element bindings', () => {
|
|
|
|
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, Directive, Input, NgModule} from '@angular/core';
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
@Directive({selector: '[someDirective]'})
|
|
|
|
export class SomeDirective {
|
|
|
|
@Input() someDirective;
|
|
|
|
}
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
@Component({selector: 'my-component', template: '<div [someDirective]="true"></div>'})
|
|
|
|
export class MyComponent {}
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
@NgModule({declarations: [SomeDirective, MyComponent]})
|
|
|
|
export class MyModule{}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// MyComponent definition should be:
|
|
|
|
const MyComponentDefinition = `
|
2018-12-19 15:17:38 -05:00
|
|
|
…
|
2019-03-07 03:31:31 -05:00
|
|
|
const _c0 = [${AttributeMarker.Bindings}, "someDirective"];
|
2018-08-20 09:20:12 -04:00
|
|
|
…
|
|
|
|
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
|
|
|
|
…
|
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
|
|
|
$r3$.ɵelement(0, "div", _c0);
|
|
|
|
}
|
|
|
|
if (rf & 2) {
|
|
|
|
$r3$.ɵelementProperty(0, "someDirective", $r3$.ɵbind(true));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
…
|
2018-11-19 12:48:14 -05:00
|
|
|
directives: [SomeDirective],
|
|
|
|
encapsulation: 2
|
2018-08-20 09:20:12 -04:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, MyComponentDefinition, 'Incorrect ChildComponent.ngComponentDef');
|
|
|
|
});
|
|
|
|
|
2018-12-12 18:23:12 -05:00
|
|
|
it('should match directives on ng-templates', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, Directive, NgModule, TemplateRef} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: 'ng-template[directiveA]'
|
|
|
|
})
|
|
|
|
export class DirectiveA {
|
|
|
|
constructor(public templateRef: TemplateRef<any>) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`
|
|
|
|
<ng-template directiveA>Some content</ng-template>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class MyComponent {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [DirectiveA, MyComponent]})
|
|
|
|
export class MyModule{}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyComponentDefinition = `
|
|
|
|
…
|
|
|
|
const $_c0$ = ["directiveA", ""];
|
2018-12-19 15:17:38 -05:00
|
|
|
function MyComponent_ng_template_0_Template(rf, ctx) {
|
2018-12-12 18:23:12 -05:00
|
|
|
if (rf & 1) {
|
|
|
|
$r3$.ɵtext(0, "Some content");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
…
|
|
|
|
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
|
|
|
|
…
|
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2018-12-19 15:17:38 -05:00
|
|
|
$r3$.ɵtemplate(0, MyComponent_ng_template_0_Template, 1, 0, "ng-template", $_c0$);
|
2018-12-12 18:23:12 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
…
|
|
|
|
directives: [DirectiveA],
|
|
|
|
…
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, MyComponentDefinition, 'Incorrect ChildComponent.ngComponentDef');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should match directives on ng-container', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, Directive, NgModule, TemplateRef} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: 'ng-container[directiveA]'
|
|
|
|
})
|
|
|
|
export class DirectiveA {
|
|
|
|
constructor(public templateRef: TemplateRef<any>) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`
|
|
|
|
<ng-container *ngIf="showing" directiveA>Some content</ng-container>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class MyComponent {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [DirectiveA, MyComponent]})
|
|
|
|
export class MyModule{}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyComponentDefinition = `
|
|
|
|
…
|
2019-03-07 03:31:31 -05:00
|
|
|
const $_c0$ = [${AttributeMarker.Bindings}, "ngIf"];
|
2018-12-12 18:23:12 -05:00
|
|
|
const $_c1$ = ["directiveA", ""];
|
2018-12-19 15:17:38 -05:00
|
|
|
function MyComponent_ng_container_0_Template(rf, ctx) {
|
2018-12-12 18:23:12 -05:00
|
|
|
if (rf & 1) {
|
|
|
|
$r3$.ɵelementContainerStart(0, $_c1$);
|
|
|
|
$r3$.ɵtext(1, "Some content");
|
|
|
|
$r3$.ɵelementContainerEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
…
|
|
|
|
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
|
|
|
|
…
|
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2018-12-19 15:17:38 -05:00
|
|
|
$r3$.ɵtemplate(0, MyComponent_ng_container_0_Template, 2, 0, "ng-container", $_c0$);
|
2018-12-12 18:23:12 -05:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
|
|
|
$r3$.ɵelementProperty(0, "ngIf", $r3$.ɵbind(ctx.showing));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
…
|
|
|
|
directives: [DirectiveA],
|
|
|
|
…
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, MyComponentDefinition, 'Incorrect ChildComponent.ngComponentDef');
|
|
|
|
});
|
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
it('should match directives on ng-template bindings', () => {
|
|
|
|
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, Directive, Input, NgModule} from '@angular/core';
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
@Directive({selector: '[someDirective]'})
|
|
|
|
export class SomeDirective {
|
|
|
|
@Input() someDirective;
|
|
|
|
}
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
@Component({selector: 'my-component', template: '<ng-template [someDirective]="true"></ng-template>'})
|
|
|
|
export class MyComponent {}
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
@NgModule({declarations: [SomeDirective, MyComponent]})
|
|
|
|
export class MyModule{}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// MyComponent definition should be:
|
|
|
|
const MyComponentDefinition = `
|
2018-12-19 15:17:38 -05:00
|
|
|
…
|
2019-03-07 03:31:31 -05:00
|
|
|
const $c0_a0$ = [${AttributeMarker.Bindings}, "someDirective"];
|
2018-08-20 09:20:12 -04:00
|
|
|
…
|
|
|
|
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
|
|
|
|
…
|
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2018-12-19 15:17:38 -05:00
|
|
|
$r3$.ɵtemplate(0, MyComponent_ng_template_0_Template, 0, 0, "ng-template", $c0_a0$);
|
2018-08-20 09:20:12 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
|
|
|
$r3$.ɵelementProperty(0, "someDirective", $r3$.ɵbind(true));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
…
|
2018-11-19 12:48:14 -05:00
|
|
|
directives: [SomeDirective],
|
|
|
|
encapsulation: 2
|
2018-08-20 09:20:12 -04:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, MyComponentDefinition, 'Incorrect ChildComponent.ngComponentDef');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should match structural directives', () => {
|
|
|
|
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, Directive, Input, NgModule} from '@angular/core';
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
@Directive({selector: '[someDirective]'})
|
|
|
|
export class SomeDirective {
|
|
|
|
@Input() someDirective;
|
|
|
|
}
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
@Component({selector: 'my-component', template: '<div *someDirective></div>'})
|
|
|
|
export class MyComponent {}
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
@NgModule({declarations: [SomeDirective, MyComponent]})
|
|
|
|
export class MyModule{}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// MyComponent definition should be:
|
|
|
|
const MyComponentDefinition = `
|
2018-12-19 15:17:38 -05:00
|
|
|
…
|
2018-08-20 09:20:12 -04:00
|
|
|
const $c0_a0$ = ["someDirective", ""];
|
|
|
|
…
|
|
|
|
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
|
|
|
|
…
|
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2018-12-19 15:17:38 -05:00
|
|
|
$r3$.ɵtemplate(0, MyComponent_div_0_Template, 1, 0, "div", $c0_a0$);
|
2018-08-20 09:20:12 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
…
|
2018-11-19 12:48:14 -05:00
|
|
|
directives: [SomeDirective],
|
|
|
|
encapsulation: 2
|
2018-08-20 09:20:12 -04:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, MyComponentDefinition, 'Incorrect ChildComponent.ngComponentDef');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should match directives on element outputs', () => {
|
|
|
|
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, Directive, Output, EventEmitter, NgModule} from '@angular/core';
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
@Directive({selector: '[someDirective]'})
|
|
|
|
export class SomeDirective {
|
|
|
|
@Output() someDirective = new EventEmitter();
|
|
|
|
}
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
@Component({selector: 'my-component', template: '<div (someDirective)="noop()"></div>'})
|
|
|
|
export class MyComponent {
|
|
|
|
noop() {}
|
|
|
|
}
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-08-20 09:20:12 -04:00
|
|
|
@NgModule({declarations: [SomeDirective, MyComponent]})
|
|
|
|
export class MyModule{}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// MyComponent definition should be:
|
|
|
|
const MyComponentDefinition = `
|
2018-12-19 15:17:38 -05:00
|
|
|
…
|
2019-03-07 03:31:31 -05:00
|
|
|
const $c0_a0$ = [${AttributeMarker.Bindings}, "someDirective"];
|
2018-08-20 09:20:12 -04:00
|
|
|
…
|
|
|
|
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
|
|
|
|
…
|
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
|
|
|
$r3$.ɵelementStart(0, "div", $c0_a0$);
|
2018-12-19 15:17:38 -05:00
|
|
|
$r3$.ɵlistener("someDirective", function MyComponent_Template_div_someDirective_0_listener($event) { return ctx.noop(); });
|
2018-08-20 09:20:12 -04:00
|
|
|
$r3$.ɵelementEnd();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
…
|
2018-11-19 12:48:14 -05:00
|
|
|
directives: [SomeDirective],
|
|
|
|
encapsulation: 2
|
2018-08-20 09:20:12 -04:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, MyComponentDefinition, 'Incorrect ChildComponent.ngComponentDef');
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2018-11-19 12:48:14 -05:00
|
|
|
});
|