From f7258ea52aa664512a082bab1f3ced98493896f9 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Fri, 1 Jul 2016 17:30:09 -0700 Subject: [PATCH] test(HtmlAst): add a serializer --- .../compiler/test/html_ast_serializer_spec.ts | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 modules/@angular/compiler/test/html_ast_serializer_spec.ts diff --git a/modules/@angular/compiler/test/html_ast_serializer_spec.ts b/modules/@angular/compiler/test/html_ast_serializer_spec.ts new file mode 100644 index 0000000000..30d19602af --- /dev/null +++ b/modules/@angular/compiler/test/html_ast_serializer_spec.ts @@ -0,0 +1,81 @@ +import {HtmlAst, HtmlAstVisitor, HtmlAttrAst, HtmlCommentAst, HtmlElementAst, HtmlExpansionAst, HtmlExpansionCaseAst, HtmlTextAst, htmlVisitAll} from '@angular/compiler/src/html_ast'; +import {HtmlParser} from '@angular/compiler/src/html_parser'; +import {beforeEach, ddescribe, describe, expect, it} from '@angular/core/testing/testing_internal'; + +export function main() { + ddescribe('HtmlAst serilaizer', () => { + var parser: HtmlParser; + + beforeEach(() => { parser = new HtmlParser(); }); + + it('should support element', () => { + const html = '

'; + const ast = parser.parse(html, 'url'); + expect(serializeHtmlAst(ast.rootNodes)).toEqual([html]); + }); + + it('should support attributes', () => { + const html = '

'; + const ast = parser.parse(html, 'url'); + expect(serializeHtmlAst(ast.rootNodes)).toEqual([html]); + }); + + it('should support text', () => { + const html = 'some text'; + const ast = parser.parse(html, 'url'); + expect(serializeHtmlAst(ast.rootNodes)).toEqual([html]); + }); + + it('should support expansion', () => { + const html = '{number, plural, =0 {none} =1 {one} other {many}}'; + const ast = parser.parse(html, 'url', true); + expect(serializeHtmlAst(ast.rootNodes)).toEqual([html]); + }); + + it('should support comment', () => { + const html = ''; + const ast = parser.parse(html, 'url', true); + expect(serializeHtmlAst(ast.rootNodes)).toEqual([html]); + }); + + it('should support nesting', () => { + const html = `
+ {{ interpolation }} + +

+ {number, plural, =0 {{sex, gender, other {?}}}} +

+
`; + const ast = parser.parse(html, 'url', true); + expect(serializeHtmlAst(ast.rootNodes)).toEqual([html]); + }); + }); +} + +class _SerializerVisitor implements HtmlAstVisitor { + visitElement(ast: HtmlElementAst, context: any): any { + return `<${ast.name}${this._visitAll(ast.attrs)}>${this._visitAll(ast.children)}`; + } + + visitAttr(ast: HtmlAttrAst, context: any): any { return ` ${ast.name}="${ast.value}"`; } + + visitText(ast: HtmlTextAst, context: any): any { return ast.value; } + + visitComment(ast: HtmlCommentAst, context: any): any { return ``; } + + visitExpansion(ast: HtmlExpansionAst, context: any): any { + return `{${ast.switchValue}, ${ast.type},${this._visitAll(ast.cases)}}`; + } + + visitExpansionCase(ast: HtmlExpansionCaseAst, context: any): any { + return ` ${ast.value} {${this._visitAll(ast.expression)}}`; + } + + private _visitAll(ast: HtmlAst[]) { return ast.map(a => a.visit(this, null)).join(''); } +} + +const serializerVisitor = new _SerializerVisitor(); + +export function serializeHtmlAst(ast: HtmlAst[]) { + return ast.map(a => a.visit(serializerVisitor, null)); +}