@@ -412,7 +412,14 @@ 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 (
416+ ctx : ITestContext ,
417+ options : ITerminalOptions | ITerminalInitOnlyOptions = { } ,
418+ testOptions : { useShadowDom ?: boolean , loadUnicodeGraphemesAddon ?: boolean } = { }
419+ ) : Promise < void > {
420+ testOptions . useShadowDom ??= false ;
421+ testOptions . loadUnicodeGraphemesAddon ??= true ;
422+
416423 await ctx . page . evaluate ( `
417424 if ('term' in window) {
418425 try {
@@ -423,10 +430,45 @@ export async function openTerminal(ctx: ITestContext, options: ITerminalOptions
423430 // HACK: Tests may have side effects that could cause the terminal not to be removed. This
424431 // assertion catches this case early.
425432 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 ( `
433+
434+ let script = `
427435 window.term = new window.Terminal(${ JSON . stringify ( { allowProposedApi : true , ...options } ) } );
428- window.term.open(document.querySelector('#terminal-container'));
429- ` ) ;
436+ let element = document.querySelector('#terminal-container');
437+
438+ // Remove shadow root if it exists
439+ const newElement = element.cloneNode(false);
440+ element.replaceWith(newElement);
441+ element = newElement
442+ ` ;
443+
444+
445+ if ( testOptions . useShadowDom ) {
446+ script += `
447+ const shadowRoot = element.attachShadow({ mode: "open" });
448+
449+ // Copy parent styles to shadow DOM
450+ const styles = Array.from(document.querySelectorAll('link[rel="stylesheet"]'));
451+ styles.forEach((styleEl) => {
452+ const clone = document.createElement('link');
453+ clone.rel = 'stylesheet';
454+ clone.href = styleEl.href;
455+ shadowRoot.appendChild(clone);
456+ });
457+
458+ // Create new element inside the shadow DOM
459+ element = document.createElement('div');
460+ element.style.width = '100%';
461+ element.style.height = '100%';
462+ shadowRoot.appendChild(element);
463+ ` ;
464+ }
465+
466+ script += `
467+ window.term.open(element);
468+ ` ;
469+
470+ await ctx . page . evaluate ( script ) ;
471+
430472 // HACK: This is a soft layer breaker that's temporarily included until unicode graphemes have
431473 // more complete integration tests. See https://github.com/xtermjs/xterm.js/pull/4519#discussion_r1285234453
432474 if ( testOptions . loadUnicodeGraphemesAddon ) {
0 commit comments