{"record":{"id":"f6c224c2ada570d4","repo":"dotnet/wpf","slug":"sr-textrangeprovider-emptystringparameter","errorCode":null,"errorMessage":"SR.TextRangeProvider_EmptyStringParameter","messagePattern":"SR\\.TextRangeProvider_EmptyStringParameter","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextRangeAdaptor.cs","lineNumber":1751,"sourceCode":"                resultRange = new TextRangeAdaptor(_textAdaptor, attrStart, attrEnd, _textPeer);\n            }\n\n            return resultRange;\n        }\n\n        /// <summary>\n        /// Searches for an occurrence of text within the range.\n        /// </summary>\n        /// <param name=\"text\">The text to search for.</param>\n        /// <param name=\"backward\">true if the last occurring range should be returned instead of the first.</param>\n        /// <param name=\"ignoreCase\">true if case should be ignored for the purposes of comparison.</param>\n        /// <returns>A subrange with the specified text, or null if no such subrange exists.</returns>\n        ITextRangeProvider ITextRangeProvider.FindText(string text, bool backward, bool ignoreCase)\n        {\n            ArgumentNullException.ThrowIfNull(text);\n            if (text.Length == 0)\n            {\n                throw new ArgumentException(SR.Format(SR.TextRangeProvider_EmptyStringParameter, \"text\"));\n            }\n\n            Normalize();\n\n            if (_start.CompareTo(_end) == 0)\n            {\n                return null;\n            }\n\n            TextRangeAdaptor range = null;\n            FindFlags findFlags = FindFlags.None;\n            if (!ignoreCase)\n            {\n                findFlags |= FindFlags.MatchCase;\n            }\n            if (backward)\n            {\n                findFlags |= FindFlags.FindInReverse;","sourceCodeStart":1733,"sourceCodeEnd":1769,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextRangeAdaptor.cs#L1733-L1769","documentation":"TextRangeAdaptor implements UI Automation's ITextRangeProvider for WPF text controls. Its FindText method throws ArgumentException (SR.TextRangeProvider_EmptyStringParameter) when the search text is an empty string, because searching for an empty substring is meaningless. The library enforces non-null via ArgumentNullException.ThrowIfNull and non-empty via this check to give a clear error instead of undefined behavior.","triggerScenarios":"Calling ITextRangeProvider.FindText(text, backward, ignoreCase) on a WPF text range (obtained via UI Automation, e.g. from a TextBlock/TextBox/TextBoxBase automation peer) with text == \"\" (zero-length string).","commonSituations":"UI Automation clients that build search text dynamically from user input or a filter box and call FindText before validating the input; localization/unit tests passing default empty strings; truncation logic yielding an empty query.","solutions":["Ensure the search string is non-empty before calling FindText; skip the call or return no match when text.Length == 0.","If the string comes from user input, validate/trim it first and fall back to a different behavior (e.g. no filtering) when empty.","Catch ArgumentException around FindText in generic automation wrappers and treat it as 'no match' if an empty query is expected to be harmless."],"exampleFix":"// before\nrange.FindText(userQuery, false, false);\n\n// after\nif (!string.IsNullOrEmpty(userQuery))\n{\n    range.FindText(userQuery, false, false);\n}\nelse\n{\n    // treat empty query as 'no search'\n}","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(text)) throw new ArgumentOutOfRangeException(nameof(text)); // or skip the search\nrange.FindText(text, backward, ignoreCase);","typeGuard":"static bool IsValidSearchText(string? s) => !string.IsNullOrEmpty(s);","tryCatchPattern":null,"preventionTips":["Validate UI input for search boxes before issuing automation FindText calls.","Treat empty queries as 'no search' in application logic, never as a match-all.","Add unit tests for automation search helpers covering empty-string input."],"tags":["wpf","ui-automation","argument-validation","empty-string"],"backgroundTag":"empty-required-field","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"}