][bar.foo]>"): TestComp@0:3`);
- });
-
- describe('errors', () => {
- it('should throw error when binding to an unknown property', () => {
- expect(() => parse('', []))
- .toThrowError(`Template parse errors:
+ describe('errors', () => {
+ it('should throw error when binding to an unknown property', () => {
+ expect(() => parse('', []))
+ .toThrowError(`Template parse errors:
Can't bind to 'invalidProp' since it isn't a known property of 'my-component'.
1. If 'my-component' is an Angular component and it has 'invalidProp' input, then verify that it is part of this module.
2. If 'my-component' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message.
("][invalidProp]="bar">"): TestComp@0:14`);
- });
+ });
- it('should throw error when binding to an unknown element w/o bindings', () => {
- expect(() => parse('', [])).toThrowError(`Template parse errors:
+ it('should throw error when binding to an unknown element w/o bindings', () => {
+ expect(() => parse('', [])).toThrowError(`Template parse errors:
'unknown' is not a known element:
1. If 'unknown' is an Angular component, then verify that it is part of this module.
2. If 'unknown' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("[ERROR ->]"): TestComp@0:0`);
- });
+ });
- it('should throw error when binding to an unknown custom element w/o bindings',
- () => {
- expect(() => parse('', []))
- .toThrowError(`Template parse errors:
+ it('should throw error when binding to an unknown custom element w/o bindings', () => {
+ expect(() => parse('', [])).toThrowError(`Template parse errors:
'un-known' is not a known element:
1. If 'un-known' is an Angular component, then verify that it is part of this module.
2. If 'un-known' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("[ERROR ->]"): TestComp@0:0`);
- });
+ });
- it('should throw error when binding to an invalid property', () => {
- expect(() => parse('', []))
- .toThrowError(`Template parse errors:
+ it('should throw error when binding to an invalid property', () => {
+ expect(() => parse('', []))
+ .toThrowError(`Template parse errors:
Binding to property 'onEvent' is disallowed for security reasons ("][onEvent]="bar">"): TestComp@0:14`);
- });
+ });
- it('should throw error when binding to an invalid attribute', () => {
- expect(() => parse('', []))
- .toThrowError(`Template parse errors:
+ it('should throw error when binding to an invalid attribute', () => {
+ expect(() => parse('', []))
+ .toThrowError(`Template parse errors:
Binding to attribute 'onEvent' is disallowed for security reasons ("][attr.onEvent]="bar">"): TestComp@0:14`);
- });
- });
+ });
+ });
- it('should parse bound properties via [...] and not report them as attributes', () => {
- expect(humanizeTplAst(parse('
', []))).toEqual([
- [ElementAst, 'div'],
- [BoundElementPropertyAst, PropertyBindingType.Property, 'prop', 'v', null]
- ]);
- });
+ it('should parse bound properties via [...] and not report them as attributes', () => {
+ expect(humanizeTplAst(parse('
', []))).toEqual([
+ [ElementAst, 'div'],
+ [BoundElementPropertyAst, PropertyBindingType.Property, 'prop', 'v', null]
+ ]);
+ });
- it('should parse bound properties via bind- and not report them as attributes', () => {
- expect(humanizeTplAst(parse('
', []))).toEqual([
- [ElementAst, 'div'],
- [BoundElementPropertyAst, PropertyBindingType.Property, 'prop', 'v', null]
- ]);
- });
+ it('should parse bound properties via bind- and not report them as attributes', () => {
+ expect(humanizeTplAst(parse('
', []))).toEqual([
+ [ElementAst, 'div'],
+ [BoundElementPropertyAst, PropertyBindingType.Property, 'prop', 'v', null]
+ ]);
+ });
- it('should parse bound properties via {{...}} and not report them as attributes',
- () => {
- expect(humanizeTplAst(parse('
', []))).toEqual([
+ it('should parse bound properties via {{...}} and not report them as attributes', () => {
+ expect(humanizeTplAst(parse('
', []))).toEqual([
+ [ElementAst, 'div'],
+ [BoundElementPropertyAst, PropertyBindingType.Property, 'prop', '{{ v }}', null]
+ ]);
+ });
+
+ it('should parse bound properties via bind-animate- and not report them as attributes',
+ () => {
+ expect(humanizeTplAst(parse('
', [], [], [])))
+ .toEqual([
[ElementAst, 'div'],
[
- BoundElementPropertyAst, PropertyBindingType.Property, 'prop', '{{ v }}', null
+ BoundElementPropertyAst, PropertyBindingType.Animation, 'someAnimation',
+ 'value2', null
]
]);
- });
+ });
- it('should parse bound properties via bind-animate- and not report them as attributes',
- () => {
- expect(
- humanizeTplAst(parse('
', [], [], [])))
- .toEqual([
- [ElementAst, 'div'],
- [
- BoundElementPropertyAst, PropertyBindingType.Animation, 'someAnimation',
- 'value2', null
- ]
- ]);
- });
+ it('should throw an error when parsing detects non-bound properties via @ that contain a value',
+ () => {
+ expect(() => { parse('
', [], [], []); })
+ .toThrowError(
+ /Assigning animation triggers via @prop="exp" attributes with an expression is invalid. Use property bindings \(e.g. \[@prop\]="exp"\) or use an attribute without a value \(e.g. @prop\) instead. \("
\]@someAnimation="value2">"\): TestComp@0:5/);
+ });
- it('should throw an error when parsing detects non-bound properties via @ that contain a value',
- () => {
- expect(() => { parse('
', [], [], []); })
- .toThrowError(
- /Assigning animation triggers via @prop="exp" attributes with an expression is invalid. Use property bindings \(e.g. \[@prop\]="exp"\) or use an attribute without a value \(e.g. @prop\) instead. \("
\]@someAnimation="value2">"\): TestComp@0:5/);
- });
+ it('should not issue a warning when host attributes contain a valid property-bound animation trigger',
+ () => {
+ const animationEntries = [new CompileAnimationEntryMetadata('prop', [])];
+ const dirA =
+ CompileDirectiveMetadata
+ .create({
+ selector: 'div',
+ template: new CompileTemplateMetadata({animations: animationEntries}),
+ type: createTypeMeta({
+ reference: {filePath: someModuleUrl, name: 'DirA'},
+ }),
+ host: {'[@prop]': 'expr'}
+ })
+ .toSummary();
- it('should not issue a warning when host attributes contain a valid property-bound animation trigger',
- () => {
- const animationEntries = [new CompileAnimationEntryMetadata('prop', [])];
- const dirA =
- CompileDirectiveMetadata
- .create({
- selector: 'div',
- template: new CompileTemplateMetadata({animations: animationEntries}),
- type: createTypeMeta({
- reference: {filePath: someModuleUrl, name: 'DirA'},
- }),
- host: {'[@prop]': 'expr'}
- })
- .toSummary();
+ humanizeTplAst(parse('', [dirA]));
+ expect(console.warnings.length).toEqual(0);
+ });
- humanizeTplAst(parse('', [dirA]));
- expect(console.warnings.length).toEqual(0);
- });
-
- it('should throw descriptive error when a host binding is not a string expression', () => {
- const dirA =
- CompileDirectiveMetadata
- .create({
- selector: 'broken',
- type: createTypeMeta({reference: {filePath: someModuleUrl, name: 'DirA'}}),
- host: {'[class.foo]': null}
- })
- .toSummary();
-
- expect(() => { parse('', [dirA]); })
- .toThrowError(
- `Template parse errors:\nValue of the host property binding "class.foo" needs to be a string representing an expression but got "null" (object) ("[ERROR ->]"): TestComp@0:0, Directive DirA`);
- });
-
- it('should throw descriptive error when a host event is not a string expression', () => {
- const dirA =
- CompileDirectiveMetadata
- .create({
- selector: 'broken',
- type: createTypeMeta({reference: {filePath: someModuleUrl, name: 'DirA'}}),
- host: {'(click)': null}
- })
- .toSummary();
-
- expect(() => { parse('', [dirA]); })
- .toThrowError(
- `Template parse errors:\nValue of the host listener "click" needs to be a string representing an expression but got "null" (object) ("[ERROR ->]"): TestComp@0:0, Directive DirA`);
- });
-
- it('should not issue a warning when an animation property is bound without an expression',
- () => {
- humanizeTplAst(parse('
', [], [], []));
- expect(console.warnings.length).toEqual(0);
- });
-
- it('should parse bound properties via [@] and not report them as attributes', () => {
- expect(humanizeTplAst(parse('
', []))
- .toThrowError(/Empty expressions are not allowed/);
-
- expect(() => parse('
', []))
- .toThrowError(/Empty expressions are not allowed/);
- });
-
- it('should parse bound events via (...) and not report them as attributes', () => {
- expect(humanizeTplAst(parse('
', [dirA])[0];
- expect(elAst.providers.length).toBe(1);
- expect(elAst.providers[0].providerType).toBe(ProviderAstType.Directive);
- expect(elAst.providers[0].providers[0].useClass).toBe(dirA.type);
- });
+ expect(() => { parse('', [dirA]); })
+ .toThrowError(
+ `Template parse errors:\nValue of the host listener "click" needs to be a string representing an expression but got "null" (object) ("[ERROR ->]"): TestComp@0:0, Directive DirA`);
+ });
- it('should use the public providers of a directive', () => {
- const provider = createProvider('service');
- const dirA = createDir('[dirA]', {providers: [provider]});
- const elAst: ElementAst = parse('
', [dirA])[0];
- expect(elAst.providers.length).toBe(2);
- expect(elAst.providers[1].providerType).toBe(ProviderAstType.PublicService);
- expect(elAst.providers[1].providers).toEqual([provider]);
- });
+ it('should not issue a warning when an animation property is bound without an expression',
+ () => {
+ humanizeTplAst(parse('
', [], [], []));
+ expect(console.warnings.length).toEqual(0);
+ });
- it('should use the private providers of a component', () => {
- const provider = createProvider('service');
- const comp = createDir('my-comp', {viewProviders: [provider]});
- const elAst: ElementAst = parse('', [comp])[0];
- expect(elAst.providers.length).toBe(2);
- expect(elAst.providers[1].providerType).toBe(ProviderAstType.PrivateService);
- expect(elAst.providers[1].providers).toEqual([provider]);
- });
+ it('should parse bound properties via [@] and not report them as attributes', () => {
+ expect(humanizeTplAst(parse('
', [dirA, dirB]))
- .toThrowError(
- `Template parse errors:\n` +
- `Mixing multi and non multi provider is not possible for token service0 ("[ERROR ->]
', [dirA, dirB]))
+ .toThrowError(
+ `Template parse errors:\n` +
+ `Mixing multi and non multi provider is not possible for token service0 ("[ERROR ->]
', [dirA]))).toEqual([
- [ElementAst, 'div'], [ReferenceAst, 'a', null]
- ]);
- });
-
- });
-
- it('should work with *... and use the attribute name as property binding name', () => {
- expect(humanizeTplAst(parse('
', [ngIf]))).toEqual([
- [EmbeddedTemplateAst], [DirectiveAst, ngIf],
- [BoundDirectivePropertyAst, 'ngIf', 'test'], [ElementAst, 'div']
- ]);
- });
-
- it('should work with *... and empty value', () => {
- expect(humanizeTplAst(parse('
', [ngIf]))).toEqual([
- [EmbeddedTemplateAst], [DirectiveAst, ngIf],
- [BoundDirectivePropertyAst, 'ngIf', 'null'], [ElementAst, 'div']
- ]);
- });
- });
});
- describe('content projection', () => {
- let compCounter: number;
- beforeEach(() => { compCounter = 0; });
+ it('should not throw error when there is same reference name in different templates',
+ () => {
+ expect(() => parse('
', [createComp('div', ['*'])])))
- .toEqual([['div', null], ['#text({{hello}})', 0], ['span', 0]]);
- });
-
- it('should match the element when there is an inline template', () => {
- expect(humanizeContentProjection(parse('
', [dirA]))).toEqual([
+ [ElementAst, 'div'], [ReferenceAst, 'a', null]
+ ]);
+ });
+ });
- it('should split 2 classes', () => { expect(splitClasses('a b')).toEqual(['a', 'b']); });
-
- it('should trim classes', () => { expect(splitClasses(' a b ')).toEqual(['a', 'b']); });
+ describe('explicit templates', () => {
+ it('should create embedded templates for elements', () => {
+ expect(humanizeTplAst(parse('', [
+ ]))).toEqual([[EmbeddedTemplateAst]]);
+ expect(humanizeTplAst(parse('', [
+ ]))).toEqual([[EmbeddedTemplateAst]]);
});
- describe('error cases', () => {
- it('should report when ng-content has non WS content', () => {
- expect(() => parse('content', []))
- .toThrowError(
- `Template parse errors:\n` +
- ` element cannot have content. ("[ERROR ->]content"): TestComp@0:0`);
- });
+ it('should create embedded templates for elements regardless the namespace',
+ () => {
+ expect(humanizeTplAst(parse('', []))).toEqual([
+ [ElementAst, ':svg:svg'],
+ [EmbeddedTemplateAst],
+ ]);
+ });
- it('should treat *attr on a template element as valid',
- () => { expect(() => parse('', [])).not.toThrowError(); });
+ it('should support references via #...', () => {
+ expect(humanizeTplAst(parse('', []))).toEqual([
+ [EmbeddedTemplateAst],
+ [ReferenceAst, 'a', createIdentifierToken(Identifiers.TemplateRef)]
+ ]);
+ });
- it('should treat template attribute on a template element as valid',
- () => { expect(() => parse('', [])).not.toThrowError(); });
+ it('should support references via ref-...', () => {
+ expect(humanizeTplAst(parse('', []))).toEqual([
+ [EmbeddedTemplateAst],
+ [ReferenceAst, 'a', createIdentifierToken(Identifiers.TemplateRef)]
+ ]);
+ });
- it('should report when mutliple *attrs are used on the same element', () => {
- expect(() => parse('
', [])).toThrowError(`Template parse errors:
-Can't have multiple template bindings on one element. Use only one attribute named 'template' or prefixed with * ("
]*ngFor>"): TestComp@0:11`);
- });
+ it('should parse variables via let-...', () => {
+ expect(humanizeTplAst(parse('', [
+ ]))).toEqual([[EmbeddedTemplateAst], [VariableAst, 'a', 'b']]);
+ });
- it('should report when mix of template and *attrs are used on the same element', () => {
- expect(() => parse('', []))
- .toThrowError(`Template parse errors:
-Can't have multiple template bindings on one element. Use only one attribute named 'template' or prefixed with * ("]*ngFor>"): TestComp@0:22`);
- });
+ it('should not locate directives in variables', () => {
+ const dirA =
+ CompileDirectiveMetadata
+ .create({
+ selector: '[a]',
+ type: createTypeMeta({reference: {filePath: someModuleUrl, name: 'DirA'}})
+ })
+ .toSummary();
+ expect(humanizeTplAst(parse('', [dirA]))).toEqual([
+ [EmbeddedTemplateAst], [VariableAst, 'a', 'b']
+ ]);
+ });
- it('should report invalid property names', () => {
- expect(() => parse('', []))
- .toThrowError(`Template parse errors:
-Can't bind to 'invalidProp' since it isn't a known property of 'div'. ("
][invalidProp]>
"): TestComp@0:5`);
- });
+ });
- it('should report invalid host property names', () => {
+ describe('inline templates', () => {
+ it('should wrap the element into an EmbeddedTemplateAST', () => {
+ expect(humanizeTplAst(parse('
', [
+ ]))).toEqual([[EmbeddedTemplateAst], [ElementAst, 'div']]);
+ });
+
+ it('should wrap the element with data-template attribute into an EmbeddedTemplateAST ',
+ () => {
+ expect(humanizeTplAst(parse('
', [])))
+ .toThrowError(/Parser Error: Unexpected token # at column 1/);
+ });
+
+ it('should parse variables via let ...', () => {
+ expect(humanizeTplAst(parse('
', [
+ ]))).toEqual([[EmbeddedTemplateAst], [VariableAst, 'a', 'b'], [ElementAst, 'div']]);
+ });
+
+ describe('directives', () => {
+ it('should locate directives in property bindings', () => {
const dirA =
CompileDirectiveMetadata
.create({
- selector: 'div',
+ selector: '[a=b]',
type: createTypeMeta({reference: {filePath: someModuleUrl, name: 'DirA'}}),
- host: {'[invalidProp]': 'someProp'}
- })
- .toSummary();
- expect(() => parse('', [dirA])).toThrowError(`Template parse errors:
-Can't bind to 'invalidProp' since it isn't a known property of 'div'. ("[ERROR ->]"): TestComp@0:0, Directive DirA`);
- });
-
- it('should report errors in expressions', () => {
- expect(() => parse('', [])).toThrowError(`Template parse errors:
-Parser Error: Unexpected token 'b' at column 3 in [a b] in TestComp@0:5 ("
][prop]="a b">
"): TestComp@0:5`);
- });
-
- it('should not throw on invalid property names if the property is used by a directive',
- () => {
- const dirA =
- CompileDirectiveMetadata
- .create({
- selector: 'div',
- type: createTypeMeta({reference: {filePath: someModuleUrl, name: 'DirA'}}),
- inputs: ['invalidProp']
- })
- .toSummary();
- expect(() => parse('', [dirA])).not.toThrow();
- });
-
- it('should not allow more than 1 component per element', () => {
- const dirA =
- CompileDirectiveMetadata
- .create({
- selector: 'div',
- isComponent: true,
- type: createTypeMeta({reference: {filePath: someModuleUrl, name: 'DirA'}}),
- template: new CompileTemplateMetadata({ngContentSelectors: []})
+ inputs: ['a']
})
.toSummary();
const dirB =
CompileDirectiveMetadata
.create({
- selector: 'div',
- isComponent: true,
- type: createTypeMeta({reference: {filePath: someModuleUrl, name: 'DirB'}}),
- template: new CompileTemplateMetadata({ngContentSelectors: []})
+ selector: '[b]',
+ type: createTypeMeta({reference: {filePath: someModuleUrl, name: 'DirB'}})
})
.toSummary();
- expect(() => parse('
', [dirB, dirA]))
- .toThrowError(
- `Template parse errors:\n` +
- `More than one component matched on this element.\n` +
- `Make sure that only one component's selector can match a given element.\n` +
- `Conflicting components: DirB,DirA ("[ERROR ->]
"): TestComp@0:0`);
- });
-
- it('should not allow components or element bindings nor dom events on explicit embedded templates',
- () => {
- const dirA =
- CompileDirectiveMetadata
- .create({
- selector: '[a]',
- isComponent: true,
- type: createTypeMeta({reference: {filePath: someModuleUrl, name: 'DirA'}}),
- template: new CompileTemplateMetadata({ngContentSelectors: []})
- })
- .toSummary();
- expect(() => parse('', [dirA]))
- .toThrowError(`Template parse errors:
-Event binding e not emitted by any directive on an embedded template. Make sure that the event name is spelled correctly and all directives are listed in the "@NgModule.declarations". ("](e)="f">"): TestComp@0:18
-Components on an embedded template: DirA ("[ERROR ->]"): TestComp@0:0
-Property binding a not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". ("[ERROR ->]"): TestComp@0:0`);
- });
-
- it('should not allow components or element bindings on inline embedded templates', () => {
- const dirA =
- CompileDirectiveMetadata
- .create({
- selector: '[a]',
- isComponent: true,
- type: createTypeMeta({reference: {filePath: someModuleUrl, name: 'DirA'}}),
- template: new CompileTemplateMetadata({ngContentSelectors: []})
- })
- .toSummary();
- expect(() => parse('', [dirA])).toThrowError(`Template parse errors:
-Components on an embedded template: DirA ("[ERROR ->]"): TestComp@0:0
-Property binding a not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". ("[ERROR ->]"): TestComp@0:0`);
- });
- });
-
- describe('ignore elements', () => {
- it('should ignore a', []))).toEqual([[TextAst, 'a']]);
-
- });
-
- it('should ignore a', []))).toEqual([[TextAst, 'a']]);
- });
-
- describe('', () => {
-
- it('should keep elements if they have an absolute non package: url',
- () => {
- expect(humanizeTplAst(parse('a', [])))
- .toEqual([
- [ElementAst, 'link'], [AttrAst, 'rel', 'stylesheet'],
- [AttrAst, 'href', 'http://someurl'], [TextAst, 'a']
- ]);
- });
-
- it('should keep elements if they have no uri', () => {
- expect(humanizeTplAst(parse('a', [
- ]))).toEqual([[ElementAst, 'link'], [AttrAst, 'rel', 'stylesheet'], [TextAst, 'a']]);
- expect(humanizeTplAst(parse('a', [
- ]))).toEqual([[ElementAst, 'link'], [AttrAst, 'REL', 'stylesheet'], [TextAst, 'a']]);
- });
-
- it('should ignore elements if they have a relative uri', () => {
- expect(humanizeTplAst(parse('a', [
- ]))).toEqual([[TextAst, 'a']]);
- expect(humanizeTplAst(parse('a', [
- ]))).toEqual([[TextAst, 'a']]);
- });
-
- it('should ignore elements if they have a package: uri', () => {
- expect(humanizeTplAst(parse('a', [
- ]))).toEqual([[TextAst, 'a']]);
- });
-
- });
-
- it('should ignore bindings on children of elements with ngNonBindable', () => {
- expect(humanizeTplAst(parse('
{{b}}
', [
- ]))).toEqual([[ElementAst, 'div'], [AttrAst, 'ngNonBindable', ''], [TextAst, '{{b}}']]);
- });
-
- it('should keep nested children of elements with ngNonBindable', () => {
- expect(humanizeTplAst(parse('