Skip to content

Commit 70d18b5

Browse files
committed
feat(compiler): change html parser to preserve comments
1 parent f1796d6 commit 70d18b5

6 files changed

Lines changed: 36 additions & 6 deletions

File tree

modules/angular2/src/compiler/html_ast.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ export class HtmlElementAst implements HtmlAst {
2323
visit(visitor: HtmlAstVisitor, context: any): any { return visitor.visitElement(this, context); }
2424
}
2525

26+
export class HtmlCommentAst implements HtmlAst {
27+
constructor(public value: string, public sourceSpan: ParseSourceSpan) {}
28+
visit(visitor: HtmlAstVisitor, context: any): any { return visitor.visitComment(this, context); }
29+
}
30+
2631
export interface HtmlAstVisitor {
2732
visitElement(ast: HtmlElementAst, context: any): any;
2833
visitAttr(ast: HtmlAttrAst, context: any): any;
2934
visitText(ast: HtmlTextAst, context: any): any;
35+
visitComment(ast: HtmlCommentAst, context: any): any;
3036
}
3137

3238
export function htmlVisitAll(visitor: HtmlAstVisitor, asts: HtmlAst[], context: any = null): any[] {

modules/angular2/src/compiler/html_parser.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111

1212
import {ListWrapper} from 'angular2/src/facade/collection';
1313

14-
import {HtmlAst, HtmlAttrAst, HtmlTextAst, HtmlElementAst} from './html_ast';
14+
import {HtmlAst, HtmlAttrAst, HtmlTextAst, HtmlCommentAst, HtmlElementAst} from './html_ast';
1515

1616
import {Injectable} from 'angular2/src/core/di';
1717
import {HtmlToken, HtmlTokenType, tokenizeHtml} from './html_lexer';
@@ -98,9 +98,11 @@ class TreeBuilder {
9898
this._advanceIf(HtmlTokenType.CDATA_END);
9999
}
100100

101-
private _consumeComment(startToken: HtmlToken) {
102-
this._advanceIf(HtmlTokenType.RAW_TEXT);
101+
private _consumeComment(token: HtmlToken) {
102+
var text = this._advanceIf(HtmlTokenType.RAW_TEXT);
103103
this._advanceIf(HtmlTokenType.COMMENT_END);
104+
var value = isPresent(text) ? text.parts[0].trim() : null;
105+
this._addToParent(new HtmlCommentAst(value, token.sourceSpan))
104106
}
105107

106108
private _consumeText(token: HtmlToken) {

modules/angular2/src/compiler/legacy_template.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ import {
88
isPresent
99
} from 'angular2/src/facade/lang';
1010

11-
import {HtmlAstVisitor, HtmlAttrAst, HtmlElementAst, HtmlTextAst, HtmlAst} from './html_ast';
11+
import {
12+
HtmlAstVisitor,
13+
HtmlAttrAst,
14+
HtmlElementAst,
15+
HtmlTextAst,
16+
HtmlCommentAst,
17+
HtmlAst
18+
} from './html_ast';
1219
import {HtmlParser, HtmlParseTreeResult} from './html_parser';
1320

1421
import {dashCaseToCamelCase, camelCaseToDashCase} from './util';
@@ -37,6 +44,8 @@ export class LegacyHtmlAstTransformer implements HtmlAstVisitor {
3744

3845
constructor(private dashCaseSelectors?: string[]) {}
3946

47+
visitComment(ast: HtmlCommentAst, context: any): any { return ast; }
48+
4049
visitElement(ast: HtmlElementAst, context: any): HtmlElementAst {
4150
this.visitingTemplateEl = ast.name.toLowerCase() == 'template';
4251
let attrs = ast.attrs.map(attr => attr.visit(this, null));

modules/angular2/src/compiler/template_normalizer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
HtmlTextAst,
2121
HtmlAttrAst,
2222
HtmlAst,
23+
HtmlCommentAst,
2324
htmlVisitAll
2425
} from './html_ast';
2526
import {HtmlParser} from './html_parser';
@@ -126,6 +127,7 @@ class TemplatePreparseVisitor implements HtmlAstVisitor {
126127
}
127128
return null;
128129
}
130+
visitComment(ast: HtmlCommentAst, context: any): any { return null; }
129131
visitAttr(ast: HtmlAttrAst, context: any): any { return null; }
130132
visitText(ast: HtmlTextAst, context: any): any { return null; }
131133
}

modules/angular2/src/compiler/template_parser.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
HtmlElementAst,
4242
HtmlAttrAst,
4343
HtmlTextAst,
44+
HtmlCommentAst,
4445
htmlVisitAll
4546
} from './html_ast';
4647

@@ -209,6 +210,8 @@ class TemplateParseVisitor implements HtmlAstVisitor {
209210
return new AttrAst(ast.name, ast.value, ast.sourceSpan);
210211
}
211212

213+
visitComment(ast: HtmlCommentAst, context: any): any { return null; }
214+
212215
visitElement(element: HtmlElementAst, component: Component): any {
213216
var nodeName = element.name;
214217
var preparsedElement = preparseElement(element);
@@ -676,6 +679,7 @@ class NonBindableVisitor implements HtmlAstVisitor {
676679
return new ElementAst(ast.name, htmlVisitAll(this, ast.attrs), [], [], [], [], children,
677680
ngContentIndex, ast.sourceSpan);
678681
}
682+
visitComment(ast: HtmlCommentAst, context: any): any { return null; }
679683
visitAttr(ast: HtmlAttrAst, context: any): AttrAst {
680684
return new AttrAst(ast.name, ast.value, ast.sourceSpan);
681685
}

modules/angular2/test/compiler/html_parser_spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
HtmlElementAst,
1818
HtmlAttrAst,
1919
HtmlTextAst,
20+
HtmlCommentAst,
2021
htmlVisitAll
2122
} from 'angular2/src/compiler/html_ast';
2223
import {ParseError, ParseLocation, ParseSourceSpan} from 'angular2/src/compiler/parse_util';
@@ -233,9 +234,9 @@ export function main() {
233234
});
234235

235236
describe('comments', () => {
236-
it('should ignore comments', () => {
237+
it('should preserve comments', () => {
237238
expect(humanizeDom(parser.parse('<!-- comment --><div></div>', 'TestComp')))
238-
.toEqual([[HtmlElementAst, 'div', 0]]);
239+
.toEqual([[HtmlCommentAst, 'comment', 0], [HtmlElementAst, 'div', 0]]);
239240
});
240241
});
241242

@@ -362,6 +363,12 @@ class Humanizer implements HtmlAstVisitor {
362363
return null;
363364
}
364365

366+
visitComment(ast: HtmlCommentAst, context: any): any {
367+
var res = this._appendContext(ast, [HtmlCommentAst, ast.value, this.elDepth]);
368+
this.result.push(res);
369+
return null;
370+
}
371+
365372
private _appendContext(ast: HtmlAst, input: any[]): any[] {
366373
if (!this.includeSourceSpan) return input;
367374
input.push(ast.sourceSpan.toString());

0 commit comments

Comments
 (0)