@@ -48,6 +48,7 @@ export class ProtoViewBuilder {
4848
4949 var apiElementBinders = [ ] ;
5050 var transitiveContentTagCount = 0 ;
51+ var boundTextNodeCount = 0 ;
5152 ListWrapper . forEach ( this . elements , ( ebb : ElementBinderBuilder ) => {
5253 var propertySetters = new Map ( ) ;
5354 var hostActions = new Map ( ) ;
@@ -103,9 +104,10 @@ export class ProtoViewBuilder {
103104 textBindings : ebb . textBindings ,
104105 readAttributes : ebb . readAttributes
105106 } ) ) ;
106- var elementIsEmpty = this . _isEmptyElement ( ebb . element ) ;
107+ var childNodeInfo = this . _analyzeChildNodes ( ebb . element , ebb . textBindingNodes ) ;
108+ boundTextNodeCount += ebb . textBindingNodes . length ;
107109 renderElementBinders . push ( new ElementBinder ( {
108- textNodeIndices : ebb . textBindingIndices ,
110+ textNodeIndices : childNodeInfo . boundTextNodeIndices ,
109111 contentTagSelector : ebb . contentTagSelector ,
110112 parentIndex : parentIndex ,
111113 distanceToParent : ebb . distanceToParent ,
@@ -117,34 +119,50 @@ export class ProtoViewBuilder {
117119 globalEvents : ebb . eventBuilder . buildGlobalEvents ( ) ,
118120 hostActions : hostActions ,
119121 propertySetters : propertySetters ,
120- elementIsEmpty : elementIsEmpty
122+ elementIsEmpty : childNodeInfo . elementIsEmpty
121123 } ) ) ;
122124 } ) ;
123125 return new api . ProtoViewDto ( {
124126 render : new DomProtoViewRef ( new DomProtoView ( {
125127 element : this . rootElement ,
126128 elementBinders : renderElementBinders ,
127- transitiveContentTagCount : transitiveContentTagCount
129+ transitiveContentTagCount : transitiveContentTagCount ,
130+ boundTextNodeCount : boundTextNodeCount
128131 } ) ) ,
129132 type : this . type ,
130133 elementBinders : apiElementBinders ,
131134 variableBindings : this . variableBindings
132135 } ) ;
133136 }
134137
135- _isEmptyElement ( el ) {
136- var childNodes = DOM . childNodes ( el ) ;
138+ // Note: We need to calculate the next node indices not until the compilation is complete,
139+ // as the compiler might change the order of elements.
140+ private _analyzeChildNodes ( parentElement : /*element*/ any ,
141+ boundTextNodes : List < /*node*/ any > ) : _ChildNodesInfo {
142+ var childNodes = DOM . childNodes ( DOM . templateAwareRoot ( parentElement ) ) ;
143+ var boundTextNodeIndices = [ ] ;
144+ var indexInBoundTextNodes = 0 ;
145+ var elementIsEmpty = true ;
137146 for ( var i = 0 ; i < childNodes . length ; i ++ ) {
138147 var node = childNodes [ i ] ;
139- if ( ( DOM . isTextNode ( node ) && DOM . getText ( node ) . trim ( ) . length > 0 ) ||
140- ( DOM . isElementNode ( node ) ) ) {
141- return false ;
148+ if ( indexInBoundTextNodes < boundTextNodes . length &&
149+ node === boundTextNodes [ indexInBoundTextNodes ] ) {
150+ boundTextNodeIndices . push ( i ) ;
151+ indexInBoundTextNodes ++ ;
152+ elementIsEmpty = false ;
153+ } else if ( ( DOM . isTextNode ( node ) && DOM . getText ( node ) . trim ( ) . length > 0 ) ||
154+ ( DOM . isElementNode ( node ) ) ) {
155+ elementIsEmpty = false ;
142156 }
143157 }
144- return true ;
158+ return new _ChildNodesInfo ( boundTextNodeIndices , elementIsEmpty ) ;
145159 }
146160}
147161
162+ class _ChildNodesInfo {
163+ constructor ( public boundTextNodeIndices : List < number > , public elementIsEmpty : boolean ) { }
164+ }
165+
148166export class ElementBinderBuilder {
149167 parent : ElementBinderBuilder = null ;
150168 distanceToParent : number = 0 ;
@@ -154,7 +172,7 @@ export class ElementBinderBuilder {
154172 variableBindings : Map < string , string > = new Map ( ) ;
155173 eventBindings : List < api . EventBinding > = [ ] ;
156174 eventBuilder : EventBuilder = new EventBuilder ( ) ;
157- textBindingIndices : List < number > = [ ] ;
175+ textBindingNodes : List < /*node*/ any > = [ ] ;
158176 textBindings : List < ASTWithSource > = [ ] ;
159177 contentTagSelector : string = null ;
160178 readAttributes : Map < string , string > = new Map ( ) ;
@@ -214,8 +232,8 @@ export class ElementBinderBuilder {
214232 this . eventBindings . push ( this . eventBuilder . add ( name , expression , target ) ) ;
215233 }
216234
217- bindText ( index , expression ) {
218- this . textBindingIndices . push ( index ) ;
235+ bindText ( textNode , expression ) {
236+ this . textBindingNodes . push ( textNode ) ;
219237 this . textBindings . push ( expression ) ;
220238 }
221239
0 commit comments