{"record":{"id":"ae07ee84bf1a925b","repo":"dotnet/wpf","slug":"sr-textprovider-invalidchildelement","errorCode":null,"errorMessage":"SR.TextProvider_InvalidChildElement","messagePattern":"SR\\.TextProvider_InvalidChildElement","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextAdaptor.cs","lineNumber":638,"sourceCode":"                                    rangeStart = position.CreatePointer(LogicalDirection.Forward);\n                                    position.MoveToNextContextPosition(LogicalDirection.Forward);\n                                    rangeEnd = position.CreatePointer(LogicalDirection.Backward);\n                                    break;\n                                }\n                            }\n                            position.MoveToNextContextPosition(LogicalDirection.Forward);\n                        }\n                    }\n                }\n                // Create range\n                if (rangeStart != null && rangeEnd != null)\n                {\n                    range = new TextRangeAdaptor(this, rangeStart, rangeEnd, _textPeer);\n                }\n            }\n            if (range == null)\n            {\n                throw new InvalidOperationException(SR.TextProvider_InvalidChildElement);\n            }\n            return range;\n        }\n\n        /// <summary>\n        /// Finds the degenerate range nearest to a screen coordinate.\n        /// </summary>\n        /// <param name=\"location\">The location in screen coordinates.\n        /// The provider should check that the coordinates are within the client\n        /// area of the provider, and should throw an InvalidOperation exception \n        /// if they are not.</param>\n        /// <returns>A degenerate range nearest the specified location.</returns>\n        ITextRangeProvider ITextProvider.RangeFromPoint(Point location)\n        {\n            TextRangeAdaptor range = null;\n            ITextView textView = GetUpdatedTextView();\n            if (textView != null)\n            {","sourceCodeStart":620,"sourceCodeEnd":656,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextAdaptor.cs#L620-L656","documentation":"TextAdaptor's ITextProvider.RangeFromChild throws InvalidOperationException when the supplied child element does not map to any text range within the provider's text container. The child (e.g. an embedded object) must belong to the document range; anything else cannot produce a valid range.","triggerScenarios":"A UI Automation client calls TextPattern.RangeFromChild with a child element that is not an embedded object of this text container (wrong peer, element from another control, or already-removed embedded element).","commonSituations":"Automation tests passing element handles cached from before content changed; clients mixing up children between two text controls; peers whose embedded UIElement was detached from the TextContainer.","solutions":["Pass only child elements obtained from the same TextProvider's document range (e.g. via GetChildren).","Re-acquire the child element after content changes instead of using a stale automation element.","Catch InvalidOperationException and treat the child as not part of this text range."],"exampleFix":"// before\nvar range = textPattern.RangeFromChild(someOtherElement);\n// after\nvar child = documentRange.GetChildren().Cast<AutomationElement>().FirstOrDefault(e => e == someOtherElement);\nif (child != null) { var range = textPattern.RangeFromChild(child); }","handlingStrategy":"validation","validationCode":"var children = docRange.GetChildren();\nif (!children.Cast<AutomationElement>().Any(e => e == child)) return; // child not in this document","typeGuard":"static bool BelongsToDocument(AutomationElement child, TextPattern p) =>\n    p.DocumentRange.GetChildren().Cast<AutomationElement>().Contains(child);","tryCatchPattern":"try { var r = textPattern.RangeFromChild(child); }\ncatch (InvalidOperationException) { /* child is not of this text container */ }","preventionTips":["Only pass children obtained from the same provider's DocumentRange","Re-fetch children after content changes","Never cache embedded-element peers across edits"],"tags":["wpf","ui-automation","text-pattern","invalid-operation"],"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"}