diff --git a/modules/@angular/compiler/src/template_parser/template_parser.ts b/modules/@angular/compiler/src/template_parser/template_parser.ts index b02c84b0f8..f30af08761 100644 --- a/modules/@angular/compiler/src/template_parser/template_parser.ts +++ b/modules/@angular/compiler/src/template_parser/template_parser.ts @@ -428,10 +428,8 @@ class TemplateParseVisitor implements html.Visitor { let parsedElement: TemplateAst; if (preparsedElement.type === PreparsedElementType.NG_CONTENT) { - if (isPresent(element.children) && element.children.length > 0) { - this._reportError( - ` element cannot have content. must be immediately followed by `, - element.sourceSpan); + if (element.children && !element.children.every(_isEmptyTextNode)) { + this._reportError(` element cannot have content.`, element.sourceSpan); } parsedElement = new NgContentAst( @@ -1201,3 +1199,7 @@ export class PipeCollector extends RecursiveAstVisitor { function _isAnimationLabel(name: string): boolean { return name[0] == '@'; } + +function _isEmptyTextNode(node: html.Node): boolean { + return node instanceof html.Text && node.value.trim().length == 0; +} \ No newline at end of file diff --git a/modules/@angular/compiler/test/template_parser/template_parser_spec.ts b/modules/@angular/compiler/test/template_parser/template_parser_spec.ts index 71c5e87583..058616a14e 100644 --- a/modules/@angular/compiler/test/template_parser/template_parser_spec.ts +++ b/modules/@angular/compiler/test/template_parser/template_parser_spec.ts @@ -274,12 +274,17 @@ export function main() { }); it('should parse ngContent', () => { - var parsed = parse('', []); + const parsed = parse('', []); + expect(humanizeTplAst(parsed)).toEqual([[NgContentAst]]); + }); + + it('should parse ngContent when it contains WS only', () => { + const parsed = parse(' \n ', []); expect(humanizeTplAst(parsed)).toEqual([[NgContentAst]]); }); it('should parse ngContent regardless the namespace', () => { - var parsed = parse('', []); + const parsed = parse('', []); expect(humanizeTplAst(parsed)).toEqual([ [ElementAst, ':svg:svg'], [NgContentAst], @@ -1418,10 +1423,11 @@ Reference "#a" is defined several times ("
]#a>
}); describe('error cases', () => { - it('should report when ng-content has content', () => { + it('should report when ng-content has non WS content', () => { expect(() => parse('content', [])) - .toThrowError(`Template parse errors: - element cannot have content. must be immediately followed by ("[ERROR ->]content"): TestComp@0:0`); + .toThrowError( + `Template parse errors:\n` + + ` element cannot have content. ("[ERROR ->]content"): TestComp@0:0`); }); it('should treat *attr on a template element as valid',