@@ -188,9 +188,15 @@ async function webviewPreloads(ctx: PreloadContext) {
188188 } , 0 ) ;
189189 } ;
190190
191- const isEditableElement = ( element : Element ) => {
192- return element . tagName . toLowerCase ( ) === 'input' || element . tagName . toLowerCase ( ) === 'textarea'
193- || ( 'editContext' in element && ! ! element . editContext ) ;
191+ const hasActiveEditableElement = (
192+ parent : Node | DocumentFragment ,
193+ root : ShadowRoot | Document = document
194+ ) : boolean => {
195+ const element = root . activeElement ;
196+ return ! ! ( element && parent . contains ( element )
197+ && ( element . matches ( ':read-write' ) || element . tagName . toLowerCase ( ) === 'select'
198+ || ( element . shadowRoot && hasActiveEditableElement ( element . shadowRoot , element . shadowRoot ) ) )
199+ ) ;
194200 } ;
195201
196202 // check if an input element is focused within the output element
@@ -202,7 +208,7 @@ async function webviewPreloads(ctx: PreloadContext) {
202208 }
203209
204210 const id = lastFocusedOutput ?. id ;
205- if ( id && ( isEditableElement ( activeElement ) || activeElement . tagName === 'SELECT' ) ) {
211+ if ( id && ( hasActiveEditableElement ( activeElement , window . document ) ) ) {
206212 postNotebookMessage < webviewMessages . IOutputInputFocusMessage > ( 'outputInputFocus' , { inputFocused : true , id } ) ;
207213
208214 activeElement . addEventListener ( 'blur' , ( ) => {
@@ -309,7 +315,7 @@ async function webviewPreloads(ctx: PreloadContext) {
309315 return ;
310316 }
311317 const activeElement = window . document . activeElement ;
312- if ( activeElement && isEditableElement ( activeElement ) ) {
318+ if ( activeElement && hasActiveEditableElement ( activeElement , window . document ) ) {
313319 ( activeElement as HTMLInputElement ) . select ( ) ;
314320 }
315321 } ;
@@ -335,7 +341,7 @@ async function webviewPreloads(ctx: PreloadContext) {
335341 return ;
336342 }
337343 const activeElement = window . document . activeElement ;
338- if ( activeElement && isEditableElement ( activeElement ) ) {
344+ if ( activeElement && hasActiveEditableElement ( activeElement , window . document ) ) {
339345 // Leave for default behavior.
340346 return ;
341347 }
@@ -363,7 +369,7 @@ async function webviewPreloads(ctx: PreloadContext) {
363369 return ;
364370 }
365371 const activeElement = window . document . activeElement ;
366- if ( activeElement && isEditableElement ( activeElement ) ) {
372+ if ( activeElement && hasActiveEditableElement ( activeElement , window . document ) ) {
367373 // The input element will handle this.
368374 return ;
369375 }
@@ -702,7 +708,7 @@ async function webviewPreloads(ctx: PreloadContext) {
702708 focusableElement . tabIndex = - 1 ;
703709 postNotebookMessage < webviewMessages . IOutputInputFocusMessage > ( 'outputInputFocus' , { inputFocused : false , id } ) ;
704710 } else {
705- const inputFocused = isEditableElement ( focusableElement ) ;
711+ const inputFocused = hasActiveEditableElement ( focusableElement , focusableElement . ownerDocument ) ;
706712 postNotebookMessage < webviewMessages . IOutputInputFocusMessage > ( 'outputInputFocus' , { inputFocused, id } ) ;
707713 }
708714
0 commit comments