@@ -428,35 +428,40 @@ function isRemovableContainer(etm: ElementToMigrate): boolean {
428428export function getMainBlock ( etm : ElementToMigrate , tmpl : string , offset : number ) :
429429 { start : string , middle : string , end : string } {
430430 const i18nAttr = etm . el . attrs . find ( x => x . name === 'i18n' ) ;
431+
432+ // removable containers are ng-templates or ng-containers that no longer need to exist
433+ // post migration
431434 if ( isRemovableContainer ( etm ) ) {
432- // this is the case where we're migrating and there's no need to keep the ng-container
433- const childStart = etm . el . children [ 0 ] . sourceSpan . start . offset - offset ;
434- const childEnd = etm . el . children [ etm . el . children . length - 1 ] . sourceSpan . end . offset - offset ;
435- const middle = tmpl . slice ( childStart , childEnd ) ;
436- return { start : '' , middle, end : '' } ;
435+ const { childStart, childEnd} = etm . getChildSpan ( offset ) ;
436+ return { start : '' , middle : tmpl . slice ( childStart , childEnd ) , end : '' } ;
437437 } else if ( isI18nTemplate ( etm , i18nAttr ) ) {
438- const childStart = etm . el . children [ 0 ] . sourceSpan . start . offset - offset ;
439- const childEnd = etm . el . children [ etm . el . children . length - 1 ] . sourceSpan . end . offset - offset ;
438+ // here we're removing an ng-template used for control flow and i18n and
439+ // converting it to an ng-container with i18n
440+ const { childStart, childEnd} = etm . getChildSpan ( offset ) ;
440441 return generatei18nContainer ( i18nAttr ! , tmpl . slice ( childStart , childEnd ) ) ;
441442 }
442443
444+ // the index of the start of the attribute adjusting for offset shift
443445 const attrStart = etm . attr . keySpan ! . start . offset - 1 - offset ;
444- const valEnd =
445- ( etm . attr . valueSpan ? ( etm . attr . valueSpan . end . offset + 1 ) : etm . attr . keySpan ! . end . offset ) -
446- offset ;
447446
448- let childStart = valEnd ;
449- let childEnd = valEnd ;
447+ // the index of the very end of the attribute value adjusted for offset shift
448+ const valEnd = etm . getValueEnd ( offset ) ;
450449
451- if ( etm . el . children . length > 0 ) {
452- childStart = etm . el . children [ 0 ] . sourceSpan . start . offset - offset ;
453- childEnd = etm . el . children [ etm . el . children . length - 1 ] . sourceSpan . end . offset - offset ;
454- }
450+ // the index of the children start and end span, if they exist. Otherwise use the value end.
451+ const { childStart, childEnd} =
452+ etm . hasChildren ( ) ? etm . getChildSpan ( offset ) : { childStart : valEnd , childEnd : valEnd } ;
455453
456- let start = tmpl . slice ( etm . start ( offset ) , attrStart ) ;
457- start += tmpl . slice ( valEnd , childStart ) ;
454+ // the beginning of the updated string in the main block, for example: <div some="attributes">
455+ let start = tmpl . slice ( etm . start ( offset ) , attrStart ) + tmpl . slice ( valEnd , childStart ) ;
456+
457+ if ( etm . shouldRemoveElseAttr ( ) ) {
458+ // this removes a bound ngIfElse attribute that's no longer needed
459+ start = start . replace ( etm . getElseAttrStr ( ) , '' ) ;
460+ }
458461
462+ // the middle is the actual contents of the element
459463 const middle = tmpl . slice ( childStart , childEnd ) ;
464+ // the end is the closing part of the element, example: </div>
460465 const end = tmpl . slice ( childEnd , etm . end ( offset ) ) ;
461466
462467 return { start, middle, end} ;
0 commit comments