Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

Escape: tell text fields apart by tag name, not instanceof Element

Pixel (kwant bot)88da81bc2e94
diff --git a/frontend/src/escape.ts b/frontend/src/escape.ts
index d4abcf13..8c8ca30a 100644
--- a/frontend/src/escape.ts
+++ b/frontend/src/escape.ts
@@ -12,8 +12,11 @@ import { trapFocusTarget } from './imports';
const TEXT_TYPES = new Set(['text', 'search', 'url', 'email', 'password', 'tel', 'number']);
type TextField = HTMLInputElement | HTMLTextAreaElement;
-export const isTextField = (el: EventTarget | null): el is TextField =>
- el instanceof Element && (el.tagName === 'TEXTAREA' || (el.tagName === 'INPUT' && TEXT_TYPES.has((el as HTMLInputElement).type)));
+// By tag name, not `instanceof Element`: that needs the page's own Element, which a test page may not share.
+export const isTextField = (el: EventTarget | null): el is TextField => {
+ const tag = (el as Element | null)?.tagName;
+ return tag === 'TEXTAREA' || (tag === 'INPUT' && TEXT_TYPES.has((el as HTMLInputElement).type));
+};
/** Props for an inline editor's root (usually its form). `close` is left out for one that stays on screen. */
export function editorProps(unsaved: boolean, close?: () => void) {
diff --git a/frontend/tests/afterLaunch.test.mjs b/frontend/tests/afterLaunch.test.mjs
index 94da19cd..661b6ca9 100644
--- a/frontend/tests/afterLaunch.test.mjs
+++ b/frontend/tests/afterLaunch.test.mjs
@@ -42,6 +42,7 @@ const m = await loadBundled(`
export { SpaceApp } from './src/App';
export { ManageTeamModal } from './src/components/ManageTeamModal';
export { RestoreSection } from './src/components/MissionBoardView';
+ export { isTextField } from './src/escape';
`);
const { React } = m;
const noop = () => {};
@@ -163,6 +164,22 @@ test('after launch 1: the space rename keeps a changed name on Escape; Settings
v.unmount();
});
+test('after launch 1: telling a text field apart needs no global Element (test pages without one threw on every focus)', () => {
+ const was = g.Element;
+ delete g.Element;
+ try {
+ assert.equal(m.isTextField(document.createElement('textarea')), true);
+ assert.equal(m.isTextField(document.createElement('input')), true);
+ const box = document.createElement('input');
+ box.type = 'checkbox';
+ assert.equal(m.isTextField(box), false);
+ assert.equal(m.isTextField(document.createElement('button')), false);
+ assert.equal(m.isTextField(null), false);
+ } finally {
+ g.Element = was;
+ }
+});
+
// ---------------------------------------------------------------------------
// 2. Escape closes the bot editor and Manage team; focus goes back to the opener