Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,15 @@ async function webviewPreloads(ctx: PreloadContext) {
}, 0);
};

const isEditableElement = (element: Element) => {
return element.tagName.toLowerCase() === 'input' || element.tagName.toLowerCase() === 'textarea'
|| ('editContext' in element && !!element.editContext);
const hasActiveEditableElement = (
parent: Node | DocumentFragment,
root: ShadowRoot | Document = document
): boolean => {
const element = root.activeElement;
return !!(element && parent.contains(element)
&& (element.matches(':read-write') || element.tagName.toLowerCase() === 'select'
|| (element.shadowRoot && hasActiveEditableElement(element.shadowRoot, element.shadowRoot)))
);
};

// check if an input element is focused within the output element
Expand All @@ -202,7 +208,7 @@ async function webviewPreloads(ctx: PreloadContext) {
}

const id = lastFocusedOutput?.id;
if (id && (isEditableElement(activeElement) || activeElement.tagName === 'SELECT')) {
if (id && (hasActiveEditableElement(activeElement, window.document))) {
postNotebookMessage<webviewMessages.IOutputInputFocusMessage>('outputInputFocus', { inputFocused: true, id });

activeElement.addEventListener('blur', () => {
Expand Down Expand Up @@ -309,7 +315,7 @@ async function webviewPreloads(ctx: PreloadContext) {
return;
}
const activeElement = window.document.activeElement;
if (activeElement && isEditableElement(activeElement)) {
if (activeElement && hasActiveEditableElement(activeElement, window.document)) {
(activeElement as HTMLInputElement).select();
}
};
Expand All @@ -335,7 +341,7 @@ async function webviewPreloads(ctx: PreloadContext) {
return;
}
const activeElement = window.document.activeElement;
if (activeElement && isEditableElement(activeElement)) {
if (activeElement && hasActiveEditableElement(activeElement, window.document)) {
// Leave for default behavior.
return;
}
Expand Down Expand Up @@ -363,7 +369,7 @@ async function webviewPreloads(ctx: PreloadContext) {
return;
}
const activeElement = window.document.activeElement;
if (activeElement && isEditableElement(activeElement)) {
if (activeElement && hasActiveEditableElement(activeElement, window.document)) {
// The input element will handle this.
return;
}
Expand Down Expand Up @@ -702,7 +708,7 @@ async function webviewPreloads(ctx: PreloadContext) {
focusableElement.tabIndex = -1;
postNotebookMessage<webviewMessages.IOutputInputFocusMessage>('outputInputFocus', { inputFocused: false, id });
} else {
const inputFocused = isEditableElement(focusableElement);
const inputFocused = hasActiveEditableElement(focusableElement, focusableElement.ownerDocument);
postNotebookMessage<webviewMessages.IOutputInputFocusMessage>('outputInputFocus', { inputFocused, id });
}

Expand Down
Loading