@@ -195,7 +195,7 @@ export class Parser<T extends TreeAdapterTypeMap> implements TokenHandler, Stack
195195 const parser = new this ( opts , documentMock , fragmentContext ) ;
196196
197197 if ( parser . fragmentContextID === $ . TEMPLATE ) {
198- parser . tmplInsertionModeStack . unshift ( InsertionMode . IN_TEMPLATE ) ;
198+ parser . tmplInsertionModeStack . push ( InsertionMode . IN_TEMPLATE ) ;
199199 }
200200
201201 parser . _initTokenizerForFragmentParsing ( ) ;
@@ -239,8 +239,7 @@ export class Parser<T extends TreeAdapterTypeMap> implements TokenHandler, Stack
239239 protected currentNotInHTML = false ;
240240
241241 /**
242- * The template insertion mode stack is maintained from the left.
243- * Ie. the topmost element will always have index 0.
242+ * The topmost template insertion mode is the last element.
244243 *
245244 * @internal
246245 */
@@ -755,7 +754,7 @@ export class Parser<T extends TreeAdapterTypeMap> implements TokenHandler, Stack
755754 return ;
756755 }
757756 case $ . TEMPLATE : {
758- this . insertionMode = this . tmplInsertionModeStack [ 0 ] ;
757+ this . insertionMode = this . tmplInsertionModeStack [ this . tmplInsertionModeStack . length - 1 ] ;
759758 return ;
760759 }
761760 case $ . HTML : {
@@ -1771,7 +1770,7 @@ function startTagInHead<T extends TreeAdapterTypeMap>(p: Parser<T>, token: TagTo
17711770 p . activeFormattingElements . insertMarker ( ) ;
17721771 p . framesetOk = false ;
17731772 p . insertionMode = InsertionMode . IN_TEMPLATE ;
1774- p . tmplInsertionModeStack . unshift ( InsertionMode . IN_TEMPLATE ) ;
1773+ p . tmplInsertionModeStack . push ( InsertionMode . IN_TEMPLATE ) ;
17751774 break ;
17761775 }
17771776 case $ . HEAD : {
@@ -1817,7 +1816,7 @@ function templateEndTagInHead<T extends TreeAdapterTypeMap>(p: Parser<T>, token:
18171816
18181817 p . openElements . popUntilTagNamePopped ( $ . TEMPLATE ) ;
18191818 p . activeFormattingElements . clearToLastMarker ( ) ;
1820- p . tmplInsertionModeStack . shift ( ) ;
1819+ p . tmplInsertionModeStack . pop ( ) ;
18211820 p . _resetInsertionMode ( ) ;
18221821 } else {
18231822 p . _err ( token , ERR . endTagWithoutMatchingOpenElement ) ;
@@ -3498,32 +3497,32 @@ function startTagInTemplate<T extends TreeAdapterTypeMap>(p: Parser<T>, token: T
34983497 case $ . TBODY :
34993498 case $ . TFOOT :
35003499 case $ . THEAD : {
3501- p . tmplInsertionModeStack [ 0 ] = InsertionMode . IN_TABLE ;
3500+ p . tmplInsertionModeStack [ p . tmplInsertionModeStack . length - 1 ] = InsertionMode . IN_TABLE ;
35023501 p . insertionMode = InsertionMode . IN_TABLE ;
35033502 startTagInTable ( p , token ) ;
35043503 break ;
35053504 }
35063505 case $ . COL : {
3507- p . tmplInsertionModeStack [ 0 ] = InsertionMode . IN_COLUMN_GROUP ;
3506+ p . tmplInsertionModeStack [ p . tmplInsertionModeStack . length - 1 ] = InsertionMode . IN_COLUMN_GROUP ;
35083507 p . insertionMode = InsertionMode . IN_COLUMN_GROUP ;
35093508 startTagInColumnGroup ( p , token ) ;
35103509 break ;
35113510 }
35123511 case $ . TR : {
3513- p . tmplInsertionModeStack [ 0 ] = InsertionMode . IN_TABLE_BODY ;
3512+ p . tmplInsertionModeStack [ p . tmplInsertionModeStack . length - 1 ] = InsertionMode . IN_TABLE_BODY ;
35143513 p . insertionMode = InsertionMode . IN_TABLE_BODY ;
35153514 startTagInTableBody ( p , token ) ;
35163515 break ;
35173516 }
35183517 case $ . TD :
35193518 case $ . TH : {
3520- p . tmplInsertionModeStack [ 0 ] = InsertionMode . IN_ROW ;
3519+ p . tmplInsertionModeStack [ p . tmplInsertionModeStack . length - 1 ] = InsertionMode . IN_ROW ;
35213520 p . insertionMode = InsertionMode . IN_ROW ;
35223521 startTagInRow ( p , token ) ;
35233522 break ;
35243523 }
35253524 default : {
3526- p . tmplInsertionModeStack [ 0 ] = InsertionMode . IN_BODY ;
3525+ p . tmplInsertionModeStack [ p . tmplInsertionModeStack . length - 1 ] = InsertionMode . IN_BODY ;
35273526 p . insertionMode = InsertionMode . IN_BODY ;
35283527 startTagInBody ( p , token ) ;
35293528 }
@@ -3538,9 +3537,12 @@ function endTagInTemplate<T extends TreeAdapterTypeMap>(p: Parser<T>, token: Tag
35383537
35393538function eofInTemplate < T extends TreeAdapterTypeMap > ( p : Parser < T > , token : EOFToken ) : void {
35403539 if ( p . openElements . tmplCount > 0 ) {
3541- p . openElements . popUntilTagNamePopped ( $ . TEMPLATE ) ;
3542- p . activeFormattingElements . clearToLastMarker ( ) ;
3543- p . tmplInsertionModeStack . shift ( ) ;
3540+ // EOF closes every open template.
3541+ do {
3542+ p . openElements . popUntilTagNamePopped ( $ . TEMPLATE ) ;
3543+ p . activeFormattingElements . clearToLastMarker ( ) ;
3544+ p . tmplInsertionModeStack . pop ( ) ;
3545+ } while ( p . openElements . tmplCount > 0 ) ;
35443546 p . _resetInsertionMode ( ) ;
35453547 p . onEof ( token ) ;
35463548 } else {
0 commit comments