Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/compiler/src/render3/r3_template_transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ class HtmlAstToIvyAst implements html.Visitor {
let parsedElement: t.Content | t.Template | t.Element | undefined;
if (preparsedElement.type === PreparsedElementType.NG_CONTENT) {
const selector = preparsedElement.selectAttr;

this.reportUnsupportedNgContentBindings(parsedProperties, boundEvents);

const attrs: t.TextAttribute[] = element.attrs.map((attr) => this.visitAttribute(attr));

parsedElement = new t.Content(
selector,
attrs,
Expand Down Expand Up @@ -298,6 +302,27 @@ class HtmlAstToIvyAst implements html.Visitor {
: this._visitTextWithInterpolation(text.value, text.sourceSpan, text.tokens, text.i18n);
}

private reportUnsupportedNgContentBindings(
properties: ParsedProperty[],
events: t.BoundEvent[],
): void {
const reported = new Set<string>();

for (const binding of [...properties, ...events]) {
const sourceSpan = binding.sourceSpan;
const key = `${sourceSpan.start.offset}:${sourceSpan.end.offset}`;
if (reported.has(key)) {
continue;
}
reported.add(key);

this.reportError(
`Property and event bindings are not supported on <ng-content>. Binding "${sourceSpan.toString()}" will be ignored.`,
sourceSpan,
);
}
}

visitExpansion(expansion: html.Expansion): t.Icu | null {
if (!expansion.i18n) {
// do not generate Icu in case it was created
Expand Down
30 changes: 30 additions & 0 deletions packages/compiler/test/render3/r3_template_transform_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,36 @@ describe('R3 template transform', () => {
]);
});

it('should report an error for property binding on ngContent', () => {
const res = parse('<ng-content [foo]="bar"></ng-content>', {ignoreError: true});
expect(res.errors.length).toBe(1);
expect(res.errors[0].msg).toContain(
'Property and event bindings are not supported on <ng-content>',
);
expect(res.errors[0].msg).toContain('[foo]');
expectFromR3Nodes(res.nodes).toEqual([['Content', '*']]);
});

it('should report an error for event binding on ngContent', () => {
const res = parse('<ng-content (click)="handler()"></ng-content>', {ignoreError: true});
expect(res.errors.length).toBe(1);
expect(res.errors[0].msg).toContain(
'Property and event bindings are not supported on <ng-content>',
);
expect(res.errors[0].msg).toContain('(click)');
});

it('should report an error for each binding on ngContent', () => {
const res = parse('<ng-content [foo]="a" (bar)="b()"></ng-content>', {ignoreError: true});
expect(res.errors.length).toBe(2);
});

it('should report a single error for two-way binding on ngContent', () => {
const res = parse('<ng-content [(foo)]="bar"></ng-content>', {ignoreError: true});
expect(res.errors.length).toBe(1);
expect(res.errors[0].msg).toContain('[(foo)]');
});

it('should indicate whether an element is void', () => {
const nodes = parse('<input><div></div>').nodes as t.Element[];
expect(nodes[0].name).toBe('input');
Expand Down
Loading