@@ -115,6 +115,44 @@ describe('buildSrcdoc', () => {
115115 expect ( bodyOpen ) . toBeLessThan ( contentIdx ) ;
116116 } ) ;
117117
118+ it ( 'shape 2 with </html>: inserts </body> BEFORE </html>, not after' , ( ) => {
119+ const html = '<html><head></head><body><p>open only</p></html>' ;
120+ const out = buildSrcdoc ( html ) ;
121+ const bodyClose = out . indexOf ( '</body>' ) ;
122+ const htmlClose = out . indexOf ( '</html>' ) ;
123+ expect ( bodyClose ) . toBeGreaterThanOrEqual ( 0 ) ;
124+ expect ( htmlClose ) . toBeGreaterThan ( bodyClose ) ;
125+ expect ( out ) . not . toMatch ( / < \/ h t m l \s * > [ \s \S ] * < \/ b o d y \s * > / i) ;
126+ } ) ;
127+
128+ it ( 'shape 4 with <html>...</html> shell: <body> after <html>, </body> before </html>' , ( ) => {
129+ const html = '<html><p>shell only</p></html>' ;
130+ const out = buildSrcdoc ( html ) ;
131+ const htmlOpen = out . search ( / < h t m l [ ^ > ] * > / i) ;
132+ const bodyOpen = out . search ( BODY_OPEN_RE ) ;
133+ const content = out . indexOf ( '<p>shell only</p>' ) ;
134+ const bodyClose = out . indexOf ( '</body>' ) ;
135+ const htmlClose = out . indexOf ( '</html>' ) ;
136+ expect ( htmlOpen ) . toBeLessThan ( bodyOpen ) ;
137+ expect ( bodyOpen ) . toBeLessThan ( content ) ;
138+ expect ( content ) . toBeLessThan ( bodyClose ) ;
139+ expect ( bodyClose ) . toBeLessThan ( htmlClose ) ;
140+ } ) ;
141+
142+ it ( 'shape 4 with </head> + </html>: <body> after </head>, </body> before </html>' , ( ) => {
143+ const html = '<html><head><title>t</title></head><p>x</p></html>' ;
144+ const out = buildSrcdoc ( html ) ;
145+ const headClose = out . indexOf ( '</head>' ) ;
146+ const bodyOpen = out . search ( BODY_OPEN_RE ) ;
147+ const content = out . indexOf ( '<p>x</p>' ) ;
148+ const bodyClose = out . indexOf ( '</body>' ) ;
149+ const htmlClose = out . indexOf ( '</html>' ) ;
150+ expect ( headClose ) . toBeLessThan ( bodyOpen ) ;
151+ expect ( bodyOpen ) . toBeLessThan ( content ) ;
152+ expect ( content ) . toBeLessThan ( bodyClose ) ;
153+ expect ( bodyClose ) . toBeLessThan ( htmlClose ) ;
154+ } ) ;
155+
118156 it ( 'shape 4: wraps content in <body>...</body> when neither tag is present' , ( ) => {
119157 const out = buildSrcdoc ( '<div>none</div>' ) ;
120158 expect ( out ) . toContain ( '<body>' ) ;
0 commit comments