1+ import {
2+ HtmlAst ,
3+ HtmlAstVisitor ,
4+ HtmlElementAst ,
5+ HtmlAttrAst ,
6+ HtmlTextAst ,
7+ HtmlCommentAst ,
8+ HtmlExpansionAst ,
9+ HtmlExpansionCaseAst ,
10+ htmlVisitAll
11+ } from 'angular2/src/compiler/html_ast' ;
12+
13+ import { BaseException } from 'angular2/src/facade/exceptions' ;
14+
15+ /**
16+ * Expands special forms into elements.
17+ *
18+ * For example,
19+ *
20+ * ```
21+ * { messages.length, plural,
22+ * =0 {zero}
23+ * =1 {one}
24+ * =other {more than one}
25+ * }
26+ * ```
27+ *
28+ * will be expanded into
29+ *
30+ * ```
31+ * <ul [ngPlural]="messages.length">
32+ * <template [ngPluralCase]="0"><li i18n="plural_0">zero</li></template>
33+ * <template [ngPluralCase]="1"><li i18n="plural_1">one</li></template>
34+ * <template [ngPluralCase]="other"><li i18n="plural_other">more than one</li></template>
35+ * </ul>
36+ * ```
37+ */
38+ export class Expander implements HtmlAstVisitor {
39+ constructor ( ) { }
40+
41+ visitElement ( ast : HtmlElementAst , context : any ) : any {
42+ return new HtmlElementAst ( ast . name , ast . attrs , htmlVisitAll ( this , ast . children ) , ast . sourceSpan ,
43+ ast . startSourceSpan , ast . endSourceSpan ) ;
44+ }
45+
46+ visitAttr ( ast : HtmlAttrAst , context : any ) : any { return ast ; }
47+
48+ visitText ( ast : HtmlTextAst , context : any ) : any { return ast ; }
49+
50+ visitComment ( ast : HtmlCommentAst , context : any ) : any { return ast ; }
51+
52+ visitExpansion ( ast : HtmlExpansionAst , context : any ) : any {
53+ return ast . type == "plural" ? _expandPluralForm ( ast ) : _expandDefaultForm ( ast ) ;
54+ }
55+
56+ visitExpansionCase ( ast : HtmlExpansionCaseAst , context : any ) : any {
57+ throw new BaseException ( "Should not be reached" ) ;
58+ }
59+ }
60+
61+ function _expandPluralForm ( ast : HtmlExpansionAst ) : HtmlElementAst {
62+ let children = ast . cases . map (
63+ c => new HtmlElementAst (
64+ `template` ,
65+ [
66+ new HtmlAttrAst ( "[ngPluralCase]" , c . value , c . valueSourceSpan ) ,
67+ ] ,
68+ [
69+ new HtmlElementAst (
70+ `li` , [ new HtmlAttrAst ( "i18n" , `${ ast . type } _${ c . value } ` , c . valueSourceSpan ) ] ,
71+ c . expression , c . sourceSpan , c . sourceSpan , c . sourceSpan )
72+ ] ,
73+ c . sourceSpan , c . sourceSpan , c . sourceSpan ) ) ;
74+ let switchAttr = new HtmlAttrAst ( "[ngPlural]" , ast . switchValue , ast . switchValueSourceSpan ) ;
75+ return new HtmlElementAst ( "ul" , [ switchAttr ] , children , ast . sourceSpan , ast . sourceSpan ,
76+ ast . sourceSpan ) ;
77+ }
78+
79+ function _expandDefaultForm ( ast : HtmlExpansionAst ) : HtmlElementAst {
80+ let children = ast . cases . map (
81+ c => new HtmlElementAst (
82+ `template` ,
83+ [
84+ new HtmlAttrAst ( "[ngSwitchWhen]" , c . value , c . valueSourceSpan ) ,
85+ ] ,
86+ [
87+ new HtmlElementAst (
88+ `li` , [ new HtmlAttrAst ( "i18n" , `${ ast . type } _${ c . value } ` , c . valueSourceSpan ) ] ,
89+ c . expression , c . sourceSpan , c . sourceSpan , c . sourceSpan )
90+ ] ,
91+ c . sourceSpan , c . sourceSpan , c . sourceSpan ) ) ;
92+ let switchAttr = new HtmlAttrAst ( "[ngSwitch]" , ast . switchValue , ast . switchValueSourceSpan ) ;
93+ return new HtmlElementAst ( "ul" , [ switchAttr ] , children , ast . sourceSpan , ast . sourceSpan ,
94+ ast . sourceSpan ) ;
95+ }
0 commit comments