🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(web): ignore non-Node ancestors in inline comment selection (#17102)
cursor[bot]b79ff32d61f8
diff --git a/web/src/features/comments/hooks/useTextSelection.ts b/web/src/features/comments/hooks/useTextSelection.tsindex 76ae7d4a85..41114b3826 100644--- a/web/src/features/comments/hooks/useTextSelection.ts+++ b/web/src/features/comments/hooks/useTextSelection.ts@@ -1,5 +1,6 @@import { useEffect, useCallback, useRef } from "react";import { useInlineCommentSelectionOptional } from "../contexts/InlineCommentSelectionContext";+import { containsNode } from "../lib/containsNode";import { selectionToPath } from "../lib/selectionToPath";interface UseTextSelectionOptions {@@ -37,7 +38,8 @@ export function useTextSelection({const debounceRef = useRef<ReturnType<typeof setTimeout> | null>(null);const handleSelectionChange = useCallback(() => {- if (!enabled || !containerRef.current || !context) return;+ const container = containerRef.current;+ if (!enabled || !container || !context) return;if (debounceRef.current) clearTimeout(debounceRef.current);@@ -47,7 +49,7 @@ export function useTextSelection({// Only clear if the interaction was INSIDE our container// If user clicked elsewhere (like comment textarea), keep the pending selectionconst focusNode = selection?.focusNode;- if (focusNode && containerRef.current?.contains(focusNode)) {+ if (containsNode(container, focusNode)) {context.clearSelection();}// Otherwise, don't clear - user might be interacting with comment UI@@ -55,7 +57,7 @@ export function useTextSelection({}const range = selection.getRangeAt(0);- if (!containerRef.current?.contains(range.commonAncestorContainer)) {+ if (!containsNode(container, range.commonAncestorContainer)) {return; // Selection outside our container}@@ -66,11 +68,7 @@ export function useTextSelection({return;}- const result = selectionToPath(- selection,- containerRef.current,- effectiveDataField,- );+ const result = selectionToPath(selection, container, effectiveDataField);if (result) {// Get the position of the selection START (not the full bounding box)// This gives us where to position the comment bubblediff --git a/web/src/features/comments/lib/containsNode.clienttest.ts b/web/src/features/comments/lib/containsNode.clienttest.tsnew file mode 100644index 0000000000..b61ba5ebe7--- /dev/null+++ b/web/src/features/comments/lib/containsNode.clienttest.ts@@ -0,0 +1,46 @@+// @vitest-environment jsdom++import { containsNode } from "./containsNode";++describe("containsNode", () => {+ it("returns true when the container contains the node", () => {+ const container = document.createElement("div");+ const child = document.createTextNode("hello");+ container.appendChild(child);++ expect(containsNode(container, child)).toBe(true);+ });++ it("returns false when the node is outside the container", () => {+ const container = document.createElement("div");+ const outsider = document.createTextNode("elsewhere");++ expect(containsNode(container, outsider)).toBe(false);+ });++ it("returns false for a missing container or a non-Node", () => {+ const container = document.createElement("div");++ expect(containsNode(null, container)).toBe(false);+ expect(containsNode(undefined, container)).toBe(false);+ expect(containsNode(container, null)).toBe(false);+ expect(containsNode(container, undefined)).toBe(false);+ expect(containsNode(container, {})).toBe(false);+ expect(containsNode(container, window)).toBe(false);+ });++ it("returns false when contains throws the Firefox TypeError", () => {+ const container = document.createElement("div");+ const child = document.createTextNode("hello");+ container.appendChild(child);++ const contains = vi.spyOn(container, "contains").mockImplementation(() => {+ throw new TypeError(+ "Node.contains: Argument 1 does not implement interface Node.",+ );+ });++ expect(containsNode(container, child)).toBe(false);+ expect(contains).toHaveBeenCalledOnce();+ });+});diff --git a/web/src/features/comments/lib/containsNode.ts b/web/src/features/comments/lib/containsNode.tsnew file mode 100644index 0000000000..1a53829f8a--- /dev/null+++ b/web/src/features/comments/lib/containsNode.ts@@ -0,0 +1,18 @@+/**+ * Firefox's Node.contains() throws TypeError when the argument is not a+ * Node from this document — native anonymous content around inputs,+ * textareas, and media, or a cross-document range. Chrome returns false.+ */+export function containsNode(+ container: Node | null | undefined,+ node: unknown,+): boolean {+ if (!container || !(node instanceof Node)) {+ return false;+ }+ try {+ return container.contains(node);+ } catch {+ return false;+ }+}