From 333f47a85abfe4ee7d801fe3e545dd5b4761bdbf Mon Sep 17 00:00:00 2001 From: Oleksandr Fediashov Date: Mon, 18 May 2026 12:17:51 +0200 Subject: [PATCH] perf: drop canOverrideNativeFocus runtime probe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `canOverrideNativeFocus` helper detected whether reassigning `HTMLElement.prototype.focus` was respected by the browser. The check was guarding against pre-IE9 behaviour where prototype overrides were ignored. Every browser keyborg currently supports honours the override, so the `_canOverrideNativeFocus` flag is effectively always true after the first call. Replace the conditional `details.isFocusedProgrammatically` write with an unconditional one — semantically identical when the override works, which is the only case we still ship for — and drop the probe entirely. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/FocusEvent.mts | 34 +++------------------------------- 1 file changed, 3 insertions(+), 31 deletions(-) diff --git a/src/FocusEvent.mts b/src/FocusEvent.mts index cacb6fafe..1cdba78e6 100644 --- a/src/FocusEvent.mts +++ b/src/FocusEvent.mts @@ -37,27 +37,6 @@ interface WindowWithKeyborgFocusEvent extends Window { __keyborgData?: KeyborgFocusEventData; } -function canOverrideNativeFocus(win: Window): boolean { - const HTMLElement = (win as WindowWithKeyborgFocusEvent).HTMLElement; - const origFocus = HTMLElement.prototype.focus; - - let isCustomFocusCalled = false; - - HTMLElement.prototype.focus = function focus(): void { - isCustomFocusCalled = true; - }; - - const btn = win.document.createElement("button"); - - btn.focus(); - - HTMLElement.prototype.focus = origFocus; - - return isCustomFocusCalled; -} - -let _canOverrideNativeFocus = false; - export interface KeyborgFocusInEventDetails { relatedTarget?: HTMLElement; isFocusedProgrammatically?: boolean; @@ -97,11 +76,6 @@ export function setupFocusEvent(win: Window): void { const kwin = win as WindowWithKeyborgFocusEvent; const doc = kwin.document; const proto = kwin.HTMLElement.prototype; - - if (!_canOverrideNativeFocus) { - _canOverrideNativeFocus = canOverrideNativeFocus(kwin); - } - const origFocus = proto.focus; if ((origFocus as KeyborgFocus).__keyborgNativeFocus) { @@ -258,12 +232,10 @@ export function setupFocusEvent(win: Window): void { // Tabster (and other users) can still use the legacy details field - keeping for backwards compat event.details = details; - if (_canOverrideNativeFocus || data[LAST_FOCUSED_PROGRAMMATICALLY]) { - details.isFocusedProgrammatically = - target === data[LAST_FOCUSED_PROGRAMMATICALLY]?.deref(); + details.isFocusedProgrammatically = + target === data[LAST_FOCUSED_PROGRAMMATICALLY]?.deref(); - data[LAST_FOCUSED_PROGRAMMATICALLY] = undefined; - } + data[LAST_FOCUSED_PROGRAMMATICALLY] = undefined; target.dispatchEvent(event); };