X Tutup
Skip to content

Commit e67e195

Browse files
committed
fix(HtmlParser): ng-content is not a void element
fixes #5563 Closes #5586
1 parent 0614797 commit e67e195

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

modules/angular2/src/compiler/html_tags.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,6 @@ export class HtmlTagDefinition {
311311
// This implementation does not fully conform to the HTML5 spec.
312312
var TAG_DEFINITIONS: {[key: string]: HtmlTagDefinition} = {
313313
'link': new HtmlTagDefinition({isVoid: true}),
314-
'ng-content': new HtmlTagDefinition({isVoid: true}),
315314
'img': new HtmlTagDefinition({isVoid: true}),
316315
'input': new HtmlTagDefinition({isVoid: true}),
317316
'hr': new HtmlTagDefinition({isVoid: true}),

modules/angular2/src/compiler/template_parser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ class TemplateParseVisitor implements HtmlAstVisitor {
228228
hasInlineTemplates ? null : component.findNgContentIndex(elementCssSelector);
229229
var parsedElement;
230230
if (preparsedElement.type === PreparsedElementType.NG_CONTENT) {
231+
if (isPresent(element.children) && element.children.length > 0) {
232+
this._reportError(
233+
`<ng-content> element cannot have content. <ng-content> must be immediately followed by </ng-content>`,
234+
element.sourceSpan);
235+
}
231236
parsedElement =
232237
new NgContentAst(this.ngContentCount++, elementNgContentIndex, element.sourceSpan);
233238
} else if (isTemplateElement) {

modules/angular2/test/compiler/html_parser_spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ export function main() {
8686
});
8787

8888
it('should tolerate end tags for void elements when they have no content', () => {
89-
expect(humanizeDom(parser.parse('<ng-content></ng-content>', 'TestComp')))
90-
.toEqual([[HtmlElementAst, 'ng-content', 0]]);
89+
expect(humanizeDom(parser.parse('<input></input>', 'TestComp')))
90+
.toEqual([[HtmlElementAst, 'input', 0]]);
9191
});
9292

9393
it('should support optional end tags', () => {
@@ -215,27 +215,27 @@ export function main() {
215215
});
216216

217217
it('should report text content in void elements', () => {
218-
let errors = parser.parse('<ng-content>content</ng-content>', 'TestComp').errors;
218+
let errors = parser.parse('<input>content</input>', 'TestComp').errors;
219219
expect(errors.length).toEqual(1);
220220
expect(humanizeErrors(errors))
221221
.toEqual([
222222
[
223-
'ng-content',
224-
'Void elements do not have end tags (they can not have content) "ng-content"',
225-
'0:19'
223+
'input',
224+
'Void elements do not have end tags (they can not have content) "input"',
225+
'0:14'
226226
]
227227
]);
228228
});
229229

230230
it('should report html content in void elements', () => {
231-
let errors = parser.parse('<ng-content><p></p></ng-content>', 'TestComp').errors;
231+
let errors = parser.parse('<input><p></p></input>', 'TestComp').errors;
232232
expect(errors.length).toEqual(1);
233233
expect(humanizeErrors(errors))
234234
.toEqual([
235235
[
236-
'ng-content',
237-
'Void elements do not have end tags (they can not have content) "ng-content"',
238-
'0:19'
236+
'input',
237+
'Void elements do not have end tags (they can not have content) "input"',
238+
'0:14'
239239
]
240240
]);
241241
});

modules/angular2/test/compiler/template_parser_spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,12 @@ There is no directive with "exportAs" set to "dirA" ("<div [ERROR ->]#a="dirA"><
689689
});
690690

691691
describe('error cases', () => {
692+
it('should report when ng-content has content', () => {
693+
expect(() => parse('<ng-content>content</ng-content>', []))
694+
.toThrowError(`Template parse errors:
695+
<ng-content> element cannot have content. <ng-content> must be immediately followed by </ng-content> ("[ERROR ->]<ng-content>content</ng-content>"): TestComp@0:0`);
696+
});
697+
692698
it('should report invalid property names', () => {
693699
expect(() => parse('<div [invalid-prop]></div>', [])).toThrowError(`Template parse errors:
694700
Can't bind to 'invalidProp' since it isn't a known native property ("<div [ERROR ->][invalid-prop]></div>"): TestComp@0:5`);

0 commit comments

Comments
 (0)
X Tutup