@@ -12,6 +12,7 @@ import {
1212
1313import { BaseException } from 'angular2/src/facade/exceptions' ;
1414
15+
1516/**
1617 * Expands special forms into elements.
1718 *
@@ -35,7 +36,18 @@ import {BaseException} from 'angular2/src/facade/exceptions';
3536 * </ul>
3637 * ```
3738 */
38- export class Expander implements HtmlAstVisitor {
39+ export function expandNodes ( nodes : HtmlAst [ ] ) : ExpansionResult {
40+ let e = new _Expander ( ) ;
41+ let n = htmlVisitAll ( e , nodes ) ;
42+ return new ExpansionResult ( n , e . expanded ) ;
43+ }
44+
45+ export class ExpansionResult {
46+ constructor ( public nodes : HtmlAst [ ] , public expanded : boolean ) { }
47+ }
48+
49+ class _Expander implements HtmlAstVisitor {
50+ expanded : boolean = false ;
3951 constructor ( ) { }
4052
4153 visitElement ( ast : HtmlElementAst , context : any ) : any {
@@ -50,6 +62,7 @@ export class Expander implements HtmlAstVisitor {
5062 visitComment ( ast : HtmlCommentAst , context : any ) : any { return ast ; }
5163
5264 visitExpansion ( ast : HtmlExpansionAst , context : any ) : any {
65+ this . expanded = true ;
5366 return ast . type == "plural" ? _expandPluralForm ( ast ) : _expandDefaultForm ( ast ) ;
5467 }
5568
@@ -59,36 +72,44 @@ export class Expander implements HtmlAstVisitor {
5972}
6073
6174function _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 ) ) ;
75+ let children = ast . cases . map ( c => {
76+ let expansionResult = expandNodes ( c . expression ) ;
77+ let i18nAttrs = expansionResult . expanded ?
78+ [ ] :
79+ [ new HtmlAttrAst ( "i18n" , `${ ast . type } _${ c . value } ` , c . valueSourceSpan ) ] ;
80+
81+ return new HtmlElementAst ( `template` ,
82+ [
83+ new HtmlAttrAst ( "ngPluralCase" , c . value , c . valueSourceSpan ) ,
84+ ] ,
85+ [
86+ new HtmlElementAst ( `li` , i18nAttrs , expansionResult . nodes ,
87+ c . sourceSpan , c . sourceSpan , c . sourceSpan )
88+ ] ,
89+ c . sourceSpan , c . sourceSpan , c . sourceSpan ) ;
90+ } ) ;
7491 let switchAttr = new HtmlAttrAst ( "[ngPlural]" , ast . switchValue , ast . switchValueSourceSpan ) ;
7592 return new HtmlElementAst ( "ul" , [ switchAttr ] , children , ast . sourceSpan , ast . sourceSpan ,
7693 ast . sourceSpan ) ;
7794}
7895
7996function _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 ) ) ;
97+ let children = ast . cases . map ( c => {
98+ let expansionResult = expandNodes ( c . expression ) ;
99+ let i18nAttrs = expansionResult . expanded ?
100+ [ ] :
101+ [ new HtmlAttrAst ( "i18n" , `${ ast . type } _${ c . value } ` , c . valueSourceSpan ) ] ;
102+
103+ return new HtmlElementAst ( `template` ,
104+ [
105+ new HtmlAttrAst ( "ngSwitchWhen" , c . value , c . valueSourceSpan ) ,
106+ ] ,
107+ [
108+ new HtmlElementAst ( `li` , i18nAttrs , expansionResult . nodes ,
109+ c . sourceSpan , c . sourceSpan , c . sourceSpan )
110+ ] ,
111+ c . sourceSpan , c . sourceSpan , c . sourceSpan ) ;
112+ } ) ;
92113 let switchAttr = new HtmlAttrAst ( "[ngSwitch]" , ast . switchValue , ast . switchValueSourceSpan ) ;
93114 return new HtmlElementAst ( "ul" , [ switchAttr ] , children , ast . sourceSpan , ast . sourceSpan ,
94115 ast . sourceSpan ) ;
0 commit comments