File tree Expand file tree Collapse file tree 2 files changed +52
-7
lines changed 
src/material/schematics/ng-generate/mdc-migration/rules Expand file tree Collapse file tree 2 files changed +52
-7
lines changed Original file line number Diff line number Diff line change @@ -190,6 +190,41 @@ describe('#visitElements', () => {
190190    it ( 'should add value to existing attribute that does not have a value' ,  async  ( )  =>  { 
191191      runAddAttributeTest ( '<a add></a>' ,  '<a add="val"></a>' ) ; 
192192    } ) ; 
193+ 
194+     it ( 'should handle all forms of indentation' ,  async  ( )  =>  { 
195+       runAddAttributeTest ( 
196+         '<a *ngFor="let item of items">' , 
197+         '<a add="val" *ngFor="let item of items">' , 
198+       ) ; 
199+       runAddAttributeTest ( 
200+         ` 
201+         <a 
202+           *ngFor="let item of items">` , 
203+         ` 
204+         <a 
205+           add="val" 
206+           *ngFor="let item of items">` , 
207+       ) ; 
208+       runAddAttributeTest ( 
209+         ` 
210+         <a *ngFor="let item of items" 
211+         >` , 
212+         ` 
213+         <a add="val" *ngFor="let item of items" 
214+         >` , 
215+       ) ; 
216+       runAddAttributeTest ( 
217+         ` 
218+         <a 
219+           [attr]=" 
220+           val">` , 
221+         ` 
222+         <a 
223+           add="val" 
224+           [attr]=" 
225+           val">` , 
226+       ) ; 
227+     } ) ; 
193228  } ) ; 
194229
195230  describe ( 'remove attribute tests' ,  ( )  =>  { 
Original file line number Diff line number Diff line change @@ -163,14 +163,24 @@ export function updateAttribute(
163163    return  `${ prefix }   ${ attrText } ${ suffix }  ` ; 
164164  } 
165165
166-   const  binding  =  node . attributes [ 0 ]  ??  node . inputs [ 0 ]  ??  node . outputs [ 0 ] ; 
167-   if  ( binding )  { 
168-     const  ctx  =  binding . sourceSpan . start . getContext ( binding . sourceSpan . start . col  +  1 ,  1 ) ! ; 
169-     const  indentation  =  ctx . before ; 
170-     return  prefix  +  indentation  +  attrText  +  suffix ; 
171-   } 
166+   const  indentation  =  parseIndentation ( html ,  node ) ; 
167+   return  prefix  +  indentation  +  attrText  +  suffix ; 
168+ } 
169+ 
170+ function  parseIndentation ( html : string ,  node : TmplAstElement ) : string  { 
171+   let  whitespace  =  '' ; 
172+   let  startOffset  =  node . startSourceSpan . start . offset  +  node . name . length  +  1 ; 
172173
173-   return  prefix  +  attrText  +  suffix ; 
174+   // Starting after the start source span's tagname, 
175+   // read and store each char until we reach a non-whitespace char. 
176+ 
177+   for  ( let  i  =  startOffset ;  i  <  html . length  -  1 ;  i ++ )  { 
178+     if  ( ! / \s / . test ( html . charAt ( i ) ) )  { 
179+       break ; 
180+     } 
181+     whitespace  +=  html . charAt ( i ) ; 
182+   } 
183+   return  whitespace ; 
174184} 
175185
176186/** 
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments