diff --git a/modules/angular2/src/compiler/html_ast.ts b/modules/angular2/src/compiler/html_ast.ts index 974e53e1c7..c18be31942 100644 --- a/modules/angular2/src/compiler/html_ast.ts +++ b/modules/angular2/src/compiler/html_ast.ts @@ -23,10 +23,16 @@ export class HtmlElementAst implements HtmlAst { visit(visitor: HtmlAstVisitor, context: any): any { return visitor.visitElement(this, context); } } +export class HtmlCommentAst implements HtmlAst { + constructor(public value: string, public sourceSpan: ParseSourceSpan) {} + visit(visitor: HtmlAstVisitor, context: any): any { return visitor.visitComment(this, context); } +} + export interface HtmlAstVisitor { visitElement(ast: HtmlElementAst, context: any): any; visitAttr(ast: HtmlAttrAst, context: any): any; visitText(ast: HtmlTextAst, context: any): any; + visitComment(ast: HtmlCommentAst, context: any): any; } export function htmlVisitAll(visitor: HtmlAstVisitor, asts: HtmlAst[], context: any = null): any[] { diff --git a/modules/angular2/src/compiler/html_parser.ts b/modules/angular2/src/compiler/html_parser.ts index f7ed4461b4..bdce900fb6 100644 --- a/modules/angular2/src/compiler/html_parser.ts +++ b/modules/angular2/src/compiler/html_parser.ts @@ -11,7 +11,7 @@ import { import {ListWrapper} from 'angular2/src/facade/collection'; -import {HtmlAst, HtmlAttrAst, HtmlTextAst, HtmlElementAst} from './html_ast'; +import {HtmlAst, HtmlAttrAst, HtmlTextAst, HtmlCommentAst, HtmlElementAst} from './html_ast'; import {Injectable} from 'angular2/src/core/di'; import {HtmlToken, HtmlTokenType, tokenizeHtml} from './html_lexer'; @@ -98,9 +98,11 @@ class TreeBuilder { this._advanceIf(HtmlTokenType.CDATA_END); } - private _consumeComment(startToken: HtmlToken) { - this._advanceIf(HtmlTokenType.RAW_TEXT); + private _consumeComment(token: HtmlToken) { + var text = this._advanceIf(HtmlTokenType.RAW_TEXT); this._advanceIf(HtmlTokenType.COMMENT_END); + var value = isPresent(text) ? text.parts[0].trim() : null; + this._addToParent(new HtmlCommentAst(value, token.sourceSpan)) } private _consumeText(token: HtmlToken) { diff --git a/modules/angular2/src/compiler/legacy_template.ts b/modules/angular2/src/compiler/legacy_template.ts index 33be52c603..009810c645 100644 --- a/modules/angular2/src/compiler/legacy_template.ts +++ b/modules/angular2/src/compiler/legacy_template.ts @@ -8,7 +8,14 @@ import { isPresent } from 'angular2/src/facade/lang'; -import {HtmlAstVisitor, HtmlAttrAst, HtmlElementAst, HtmlTextAst, HtmlAst} from './html_ast'; +import { + HtmlAstVisitor, + HtmlAttrAst, + HtmlElementAst, + HtmlTextAst, + HtmlCommentAst, + HtmlAst +} from './html_ast'; import {HtmlParser, HtmlParseTreeResult} from './html_parser'; import {dashCaseToCamelCase, camelCaseToDashCase} from './util'; @@ -37,6 +44,8 @@ export class LegacyHtmlAstTransformer implements HtmlAstVisitor { constructor(private dashCaseSelectors?: string[]) {} + visitComment(ast: HtmlCommentAst, context: any): any { return ast; } + visitElement(ast: HtmlElementAst, context: any): HtmlElementAst { this.visitingTemplateEl = ast.name.toLowerCase() == 'template'; let attrs = ast.attrs.map(attr => attr.visit(this, null)); diff --git a/modules/angular2/src/compiler/template_normalizer.ts b/modules/angular2/src/compiler/template_normalizer.ts index 6e4c1bf049..905793bcdf 100644 --- a/modules/angular2/src/compiler/template_normalizer.ts +++ b/modules/angular2/src/compiler/template_normalizer.ts @@ -20,6 +20,7 @@ import { HtmlTextAst, HtmlAttrAst, HtmlAst, + HtmlCommentAst, htmlVisitAll } from './html_ast'; import {HtmlParser} from './html_parser'; @@ -126,6 +127,7 @@ class TemplatePreparseVisitor implements HtmlAstVisitor { } return null; } + visitComment(ast: HtmlCommentAst, context: any): any { return null; } visitAttr(ast: HtmlAttrAst, context: any): any { return null; } visitText(ast: HtmlTextAst, context: any): any { return null; } } diff --git a/modules/angular2/src/compiler/template_parser.ts b/modules/angular2/src/compiler/template_parser.ts index f86065433a..875e168999 100644 --- a/modules/angular2/src/compiler/template_parser.ts +++ b/modules/angular2/src/compiler/template_parser.ts @@ -41,6 +41,7 @@ import { HtmlElementAst, HtmlAttrAst, HtmlTextAst, + HtmlCommentAst, htmlVisitAll } from './html_ast'; @@ -209,6 +210,8 @@ class TemplateParseVisitor implements HtmlAstVisitor { return new AttrAst(ast.name, ast.value, ast.sourceSpan); } + visitComment(ast: HtmlCommentAst, context: any): any { return null; } + visitElement(element: HtmlElementAst, component: Component): any { var nodeName = element.name; var preparsedElement = preparseElement(element); @@ -676,6 +679,7 @@ class NonBindableVisitor implements HtmlAstVisitor { return new ElementAst(ast.name, htmlVisitAll(this, ast.attrs), [], [], [], [], children, ngContentIndex, ast.sourceSpan); } + visitComment(ast: HtmlCommentAst, context: any): any { return null; } visitAttr(ast: HtmlAttrAst, context: any): AttrAst { return new AttrAst(ast.name, ast.value, ast.sourceSpan); } diff --git a/modules/angular2/test/compiler/html_parser_spec.ts b/modules/angular2/test/compiler/html_parser_spec.ts index 05bc86c67c..b87337c2ba 100644 --- a/modules/angular2/test/compiler/html_parser_spec.ts +++ b/modules/angular2/test/compiler/html_parser_spec.ts @@ -17,6 +17,7 @@ import { HtmlElementAst, HtmlAttrAst, HtmlTextAst, + HtmlCommentAst, htmlVisitAll } from 'angular2/src/compiler/html_ast'; import {ParseError, ParseLocation, ParseSourceSpan} from 'angular2/src/compiler/parse_util'; @@ -233,9 +234,9 @@ export function main() { }); describe('comments', () => { - it('should ignore comments', () => { + it('should preserve comments', () => { expect(humanizeDom(parser.parse('
', 'TestComp'))) - .toEqual([[HtmlElementAst, 'div', 0]]); + .toEqual([[HtmlCommentAst, 'comment', 0], [HtmlElementAst, 'div', 0]]); }); }); @@ -362,6 +363,12 @@ class Humanizer implements HtmlAstVisitor { return null; } + visitComment(ast: HtmlCommentAst, context: any): any { + var res = this._appendContext(ast, [HtmlCommentAst, ast.value, this.elDepth]); + this.result.push(res); + return null; + } + private _appendContext(ast: HtmlAst, input: any[]): any[] { if (!this.includeSourceSpan) return input; input.push(ast.sourceSpan.toString());