⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
test: add regression test for relatedTarget TypeError in editor mouseleave
justinfc361d24f9c6
diff --git a/apps/web/src/components/activity/comment-editor.tsx b/apps/web/src/components/activity/comment-editor.tsxindex 003776b4e..4210d73dd 100644--- a/apps/web/src/components/activity/comment-editor.tsx+++ b/apps/web/src/components/activity/comment-editor.tsx@@ -70,6 +70,7 @@ import {isYouTubeUrl,normalizeUrl,} from "@/lib/editor-url-utils";+import { isInCodeBlockLanguagePicker } from "@/lib/is-in-codeblock-language-picker";import { getSharedShikiHighlighter } from "@/lib/shiki-highlighter";import { toast } from "@/lib/toast";import { uploadTaskImage } from "@/lib/upload-task-image";@@ -1368,7 +1369,7 @@ export default function CommentEditor({const handleEditorMouseLeave = useCallback((event: ReactMouseEvent<HTMLElement>) => {const relatedTarget = event.relatedTarget;- if (relatedTarget instanceof Element && relatedTarget.closest(".kaneo-codeblock-language")) return;+ if (isInCodeBlockLanguagePicker(relatedTarget)) return;if (isCodeLanguageMenuOpen) return;if (codeLanguageHideTimeoutRef.current !== null) {diff --git a/apps/web/src/components/task/task-description.tsx b/apps/web/src/components/task/task-description.tsxindex d58612c66..77244db9c 100644--- a/apps/web/src/components/task/task-description.tsx+++ b/apps/web/src/components/task/task-description.tsx@@ -66,6 +66,7 @@ import {isYouTubeUrl,normalizeUrl,} from "@/lib/editor-url-utils";+import { isInCodeBlockLanguagePicker } from "@/lib/is-in-codeblock-language-picker";import { getSharedShikiHighlighter } from "@/lib/shiki-highlighter";import { toast } from "@/lib/toast";import { uploadTaskImage } from "@/lib/upload-task-image";@@ -1328,7 +1329,7 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {const handleEditorMouseLeave = useCallback((event: ReactMouseEvent<HTMLElement>) => {const relatedTarget = event.relatedTarget;- if (relatedTarget instanceof Element && relatedTarget.closest(".kaneo-codeblock-language")) return;+ if (isInCodeBlockLanguagePicker(relatedTarget)) return;if (isCodeLanguageMenuOpen) return;hoveredCodeBlockElementRef.current = null;setHoveredCodeBlock(null);diff --git a/apps/web/src/lib/is-in-codeblock-language-picker.test.ts b/apps/web/src/lib/is-in-codeblock-language-picker.test.tsnew file mode 100644index 000000000..8115e2e32--- /dev/null+++ b/apps/web/src/lib/is-in-codeblock-language-picker.test.ts@@ -0,0 +1,37 @@+import { describe, expect, it } from "vitest";+import { isInCodeBlockLanguagePicker } from "./is-in-codeblock-language-picker";++describe("isInCodeBlockLanguagePicker", () => {+ it("returns false when target is null", () => {+ expect(isInCodeBlockLanguagePicker(null)).toBe(false);+ });++ it("does not throw and returns false when target is a non-Element EventTarget (Text node)", () => {+ // Regression: event.relatedTarget can be a Text node (when the pointer+ // moves between text runs). Calling .closest() on it throws TypeError,+ // which used to crash handleEditorMouseLeave. The helper's+ // `instanceof Element` guard must short-circuit first.+ const text = document.createTextNode("x");+ expect(() => isInCodeBlockLanguagePicker(text)).not.toThrow();+ expect(isInCodeBlockLanguagePicker(text)).toBe(false);+ });++ it("returns true when target is the picker element itself", () => {+ const picker = document.createElement("div");+ picker.className = "kaneo-codeblock-language";+ expect(isInCodeBlockLanguagePicker(picker)).toBe(true);+ });++ it("returns true when target is a descendant of the picker", () => {+ const picker = document.createElement("div");+ picker.className = "kaneo-codeblock-language";+ const child = document.createElement("span");+ picker.appendChild(child);+ expect(isInCodeBlockLanguagePicker(child)).toBe(true);+ });++ it("returns false when target is an Element outside the picker", () => {+ const other = document.createElement("div");+ expect(isInCodeBlockLanguagePicker(other)).toBe(false);+ });+});diff --git a/apps/web/src/lib/is-in-codeblock-language-picker.ts b/apps/web/src/lib/is-in-codeblock-language-picker.tsnew file mode 100644index 000000000..c6d6680ad--- /dev/null+++ b/apps/web/src/lib/is-in-codeblock-language-picker.ts@@ -0,0 +1,17 @@+/**+ * Returns true when an event target is inside the code-block language picker.+ *+ * `MouseEvent.relatedTarget` is typed as `EventTarget | null` and at runtime+ * can be a non-Element node (e.g. a Text node when the pointer moves between+ * text runs) or `null`. Calling `.closest()` on either throws, so the+ * `instanceof Element` guard is load-bearing for the mouseleave handlers in+ * the comment and task-description editors.+ */+export function isInCodeBlockLanguagePicker(+ target: EventTarget | null,+): boolean {+ return (+ target instanceof Element &&+ target.closest(".kaneo-codeblock-language") !== null+ );+}