@@ -412,7 +412,7 @@ class TerminalCoreProxy {
412412 }
413413}
414414
415- export async function openTerminal ( ctx : ITestContext , options : ITerminalOptions | ITerminalInitOnlyOptions = { } , testOptions : { loadUnicodeGraphemesAddon : boolean } = { loadUnicodeGraphemesAddon : true } ) : Promise < void > {
415+ export async function openTerminal ( ctx : ITestContext , options : ITerminalOptions | ITerminalInitOnlyOptions = { } , { useShadowDom = false , loadUnicodeGraphemesAddon = true } : { loadUnicodeGraphemesAddon ? : boolean , useShadowDom ?: boolean } = { } ) : Promise < void > {
416416 await ctx . page . evaluate ( `
417417 if ('term' in window) {
418418 try {
@@ -423,13 +423,48 @@ export async function openTerminal(ctx: ITestContext, options: ITerminalOptions
423423 // HACK: Tests may have side effects that could cause the terminal not to be removed. This
424424 // assertion catches this case early.
425425 strictEqual ( await ctx . page . evaluate ( `document.querySelector('#terminal-container').children.length` ) , 0 , 'there must be no terminals on the page' ) ;
426- await ctx . page . evaluate ( `
426+
427+ let script = `
427428 window.term = new window.Terminal(${ JSON . stringify ( { allowProposedApi : true , ...options } ) } );
428- window.term.open(document.querySelector('#terminal-container'));
429- ` ) ;
429+ let element = document.querySelector('#terminal-container');
430+
431+ // Remove shadow root if it exists
432+ const newElement = element.cloneNode(false);
433+ element.replaceWith(newElement);
434+ element = newElement
435+ ` ;
436+
437+
438+ if ( useShadowDom ) {
439+ script += `
440+ const shadowRoot = element.attachShadow({ mode: "open" });
441+
442+ // Copy parent styles to shadow DOM
443+ const styles = Array.from(document.querySelectorAll('link[rel="stylesheet"]'));
444+ styles.forEach((styleEl) => {
445+ const clone = document.createElement('link');
446+ clone.rel = 'stylesheet';
447+ clone.href = styleEl.href;
448+ shadowRoot.appendChild(clone);
449+ });
450+
451+ // Create new element inside the shadow DOM
452+ element = document.createElement('div');
453+ element.style.width = '100%';
454+ element.style.height = '100%';
455+ shadowRoot.appendChild(element);
456+ ` ;
457+ }
458+
459+ script += `
460+ window.term.open(element);
461+ ` ;
462+
463+ await ctx . page . evaluate ( script ) ;
464+
430465 // HACK: This is a soft layer breaker that's temporarily included until unicode graphemes have
431466 // more complete integration tests. See https://github.com/xtermjs/xterm.js/pull/4519#discussion_r1285234453
432- if ( testOptions . loadUnicodeGraphemesAddon ) {
467+ if ( loadUnicodeGraphemesAddon ) {
433468 await ctx . page . evaluate ( `
434469 window.unicode = new UnicodeGraphemesAddon();
435470 window.term.loadAddon(window.unicode);
0 commit comments