{"record":{"id":"901d079a60c51986","repo":"dotnet/wpf","slug":"sr-invalidstartnodefortextselection","errorCode":null,"errorMessage":"SR.InvalidStartNodeForTextSelection","messagePattern":"SR\\.InvalidStartNodeForTextSelection","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionProcessor.cs","lineNumber":145,"sourceCode":"            ArgumentNullException.ThrowIfNull(selection);\n\n            ITextPointer start;\n            ITextPointer end;\n            IList<TextSegment> textSegments = null;\n\n            TextSelectionHelper.CheckSelection(selection, out start, out end, out textSegments);\n            if (!(start is TextPointer))\n                throw new ArgumentException(SR.WrongSelectionType, nameof(selection));\n\n            ITextPointer elementStart;\n            ITextPointer elementEnd;\n\n            // If we can't get the start/end of the node then we can't generate a locator part\n            if (!GetNodesStartAndEnd(startNode, out elementStart, out elementEnd))\n                return null;\n\n            if (elementStart.CompareTo(end) > 0)\n                throw new ArgumentException(SR.InvalidStartNodeForTextSelection, nameof(startNode));\n\n            if (elementEnd.CompareTo(start) < 0)\n                throw new ArgumentException(SR.InvalidStartNodeForTextSelection, nameof(startNode));\n\n            ContentLocatorPart part = new ContentLocatorPart(CharacterRangeElementName);\n\n            int startOffset = 0;\n            int endOffset = 0;\n            for (int i = 0; i < textSegments.Count; i++)\n            {\n                GetTextSegmentValues(textSegments[i], elementStart, elementEnd, out startOffset, out endOffset);\n\n                part.NameValuePairs.Add(SegmentAttribute + i.ToString(NumberFormatInfo.InvariantInfo), startOffset.ToString(NumberFormatInfo.InvariantInfo) + TextSelectionProcessor.Separator + endOffset.ToString(NumberFormatInfo.InvariantInfo));\n            }\n\n            part.NameValuePairs.Add(CountAttribute, textSegments.Count.ToString(NumberFormatInfo.InvariantInfo));\n\n            List<ContentLocatorPart> res = new List<ContentLocatorPart>(1);","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionProcessor.cs#L127-L163","documentation":"GenerateLocatorParts checks that the startNode's text span actually overlaps the selection: if the node's element start lies after the selection end, the node cannot correspond to the selection and this ArgumentException is thrown.","triggerScenarios":"Calling GenerateLocatorParts(selection, startNode) where startNode is a DependencyObject positioned entirely after the selection in document order — typically a wrong or stale node (e.g. node from a different document/page or a mismatched DocumentPageView).","commonSituations":"Caching TextNodes across re-layout or document edits; passing the wrong page's start node when generating locators per page view; anchors whose offsets are stale after content changes.","solutions":["Pass the startNode that actually contains or precedes the selection (e.g. the node resolved from the same DocumentPageView).","Re-resolve the anchor/selection against the current document before generating locator parts.","Catch ArgumentException and recompute the start node via the selection processor."],"exampleFix":"// before\nvar part = processor.GenerateLocatorParts(selection, staleStartNode, pageView);\n// after\nvar correctNode = processor.GetSelectedNodes(selection).FirstOrDefault();\nvar part = correctNode != null\n    ? processor.GenerateLocatorParts(selection, correctNode, pageView)\n    : null;","handlingStrategy":"validation","validationCode":"bool overlaps = elementStart.CompareTo(selectionEnd) <= 0;\nif (!overlaps) throw new InvalidOperationException(\"startNode is after selection end\");","typeGuard":null,"tryCatchPattern":"try { part = processor.GenerateLocatorParts(selection, startNode, pageView); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"startNode\")) { part = null; /* skip node */ }","preventionTips":["Always derive startNode from the same selection/page view being anchored.","Do not cache TextNodes across re-layout or document edits.","Re-resolve anchors after any content change before generating locator parts."],"tags":["wpf","annotations","argumentexception","node-mismatch"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}