diff --git a/engine/src/flutter/third_party/accessibility/ax/BUILD.gn b/engine/src/flutter/third_party/accessibility/ax/BUILD.gn index fc07993a3f..9660d90975 100644 --- a/engine/src/flutter/third_party/accessibility/ax/BUILD.gn +++ b/engine/src/flutter/third_party/accessibility/ax/BUILD.gn @@ -106,7 +106,10 @@ source_set("ax") { "oleacc.lib", "uiautomationcore.lib", ] - deps = [ "//flutter/fml:string_conversion" ] + deps = [ + "//flutter/fml:fml", + "//third_party/icu:icui18n", + ] } public_deps = [ diff --git a/engine/src/flutter/third_party/accessibility/ax/platform/ax_platform_node_textrangeprovider_win.cc b/engine/src/flutter/third_party/accessibility/ax/platform/ax_platform_node_textrangeprovider_win.cc index 71f72d3395..7f08b2aaec 100644 --- a/engine/src/flutter/third_party/accessibility/ax/platform/ax_platform_node_textrangeprovider_win.cc +++ b/engine/src/flutter/third_party/accessibility/ax/platform/ax_platform_node_textrangeprovider_win.cc @@ -6,6 +6,7 @@ #include #include +#include #include "ax/ax_action_data.h" #include "ax/ax_range.h" @@ -14,6 +15,7 @@ #include "ax/platform/ax_platform_tree_manager.h" #include "base/win/variant_vector.h" #include "flutter/fml/platform/win/wstring_conversion.h" +#include "third_party/icu/source/i18n/unicode/usearch.h" #define UIA_VALIDATE_TEXTRANGEPROVIDER_CALL() \ if (!GetOwner() || !GetOwner()->GetDelegate() || !start() || \ @@ -433,28 +435,63 @@ HRESULT AXPlatformNodeTextRangeProviderWin::FindAttributeRange( return S_OK; } -static bool StringSearch(const std::u16string& search_string, - const std::u16string& find_in, - size_t* find_start, - size_t* find_length, - bool ignore_case, - bool backwards) { - // TODO(schectman) Respect ignore_case/i18n. - // https://github.com/flutter/flutter/issues/117013 - size_t match_pos; - if (backwards) { - match_pos = find_in.rfind(search_string); - } else { - match_pos = find_in.find(search_string); - } - if (match_pos == std::u16string::npos) { +static bool StringSearchBasic(const std::u16string_view search_string, + const std::u16string_view find_in, + size_t* find_start, + size_t* find_length, + bool backwards) { + size_t index = + backwards ? find_in.rfind(search_string) : find_in.find(search_string); + if (index == std::u16string::npos) { return false; } - *find_start = match_pos; - *find_length = search_string.length(); + *find_start = index; + *find_length = search_string.size(); return true; } +bool StringSearch(std::u16string_view search_string, + std::u16string_view find_in, + size_t* find_start, + size_t* find_length, + bool ignore_case, + bool backwards) { + UErrorCode status = U_ZERO_ERROR; + UCollator* col = ucol_open(uloc_getDefault(), &status); + UStringSearch* search = usearch_openFromCollator( + search_string.data(), search_string.size(), find_in.data(), + find_in.size(), col, nullptr, &status); + if (!U_SUCCESS(status)) { + if (search) { + usearch_close(search); + } + return StringSearchBasic(search_string, find_in, find_start, find_length, + backwards); + } + UCollator* collator = usearch_getCollator(search); + ucol_setStrength(collator, ignore_case ? UCOL_PRIMARY : UCOL_TERTIARY); + usearch_reset(search); + status = U_ZERO_ERROR; + usearch_setText(search, find_in.data(), find_in.size(), &status); + if (!U_SUCCESS(status)) { + if (search) { + usearch_close(search); + } + return StringSearchBasic(search_string, find_in, find_start, find_length, + backwards); + } + int32_t index = backwards ? usearch_last(search, &status) + : usearch_first(search, &status); + bool match = false; + if (U_SUCCESS(status) && index != USEARCH_DONE) { + match = true; + *find_start = static_cast(index); + *find_length = static_cast(usearch_getMatchedLength(search)); + } + usearch_close(search); + return match; +} + HRESULT AXPlatformNodeTextRangeProviderWin::FindText( BSTR string, BOOL backwards, diff --git a/engine/src/flutter/third_party/accessibility/ax/platform/ax_platform_node_textrangeprovider_win.h b/engine/src/flutter/third_party/accessibility/ax/platform/ax_platform_node_textrangeprovider_win.h index 78b6aa75ff..d586ac1657 100644 --- a/engine/src/flutter/third_party/accessibility/ax/platform/ax_platform_node_textrangeprovider_win.h +++ b/engine/src/flutter/third_party/accessibility/ax/platform/ax_platform_node_textrangeprovider_win.h @@ -280,6 +280,16 @@ class AX_EXPORT __declspec(uuid("3071e40d-a10d-45ff-a59f-6e8e1138e2c1")) TextRangeEndpoints endpoints_; }; +// Optionally case-insensitive or reverse string search. +// +// Exposed as non-static for use in testing. +bool StringSearch(std::u16string_view search_string, + std::u16string_view find_in, + size_t* find_start, + size_t* find_length, + bool ignore_case, + bool backwards); + } // namespace ui #endif // UI_ACCESSIBILITY_PLATFORM_AX_PLATFORM_NODE_TEXTRANGEPROVIDER_WIN_H_ diff --git a/engine/src/flutter/third_party/accessibility/ax/platform/ax_platform_node_textrangeprovider_win_unittest.cc b/engine/src/flutter/third_party/accessibility/ax/platform/ax_platform_node_textrangeprovider_win_unittest.cc index 02ce8a4a8d..c81728d66a 100644 --- a/engine/src/flutter/third_party/accessibility/ax/platform/ax_platform_node_textrangeprovider_win_unittest.cc +++ b/engine/src/flutter/third_party/accessibility/ax/platform/ax_platform_node_textrangeprovider_win_unittest.cc @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -17,6 +18,8 @@ #include "base/win/scoped_bstr.h" #include "base/win/scoped_safearray.h" #include "base/win/scoped_variant.h" +#include "flutter/fml/icu_util.h" +#include "third_party/icu/source/common/unicode/putil.h" using Microsoft::WRL::ComPtr; @@ -144,25 +147,25 @@ namespace ui { EXPECT_STREQ(expected_content, provider_content.Get()); \ } -#define EXPECT_UIA_FIND_TEXT(text_range_provider, search_term, ignore_case, \ - owner) \ - { \ - base::win::ScopedBstr find_string(search_term); \ - ComPtr text_range_provider_found; \ - EXPECT_HRESULT_SUCCEEDED(text_range_provider->FindText( \ - find_string.Get(), false, ignore_case, &text_range_provider_found)); \ - if (text_range_provider_found == nullptr) { \ - EXPECT_TRUE(false); \ - } else { \ - SetOwner(owner, text_range_provider_found.Get()); \ - base::win::ScopedBstr found_content; \ - EXPECT_HRESULT_SUCCEEDED( \ - text_range_provider_found->GetText(-1, found_content.Receive())); \ - if (ignore_case) \ - EXPECT_EQ(0, _wcsicmp(found_content.Get(), find_string.Get())); \ - else \ - EXPECT_EQ(0, wcscmp(found_content.Get(), find_string.Get())); \ - } \ +#define EXPECT_UIA_FIND_TEXT(text_range_provider, search_term, ignore_case, \ + owner) \ + { \ + base::win::ScopedBstr find_string(search_term); \ + ComPtr text_range_provider_found; \ + EXPECT_HRESULT_SUCCEEDED(text_range_provider->FindText( \ + find_string.Get(), false, ignore_case, &text_range_provider_found)); \ + if (text_range_provider_found == nullptr) { \ + EXPECT_TRUE(false); \ + } else { \ + SetOwner(owner, text_range_provider_found.Get()); \ + base::win::ScopedBstr found_content; \ + EXPECT_HRESULT_SUCCEEDED( \ + text_range_provider_found->GetText(-1, found_content.Receive())); \ + if (ignore_case) \ + EXPECT_TRUE(StringCompareICU(found_content.Get(), find_string.Get())); \ + else \ + EXPECT_EQ(0, wcscmp(found_content.Get(), find_string.Get())); \ + } \ } #define EXPECT_UIA_FIND_TEXT_NO_MATCH(text_range_provider, search_term, \ @@ -209,6 +212,16 @@ namespace ui { #define DCHECK_EQ(a, b) BASE_DCHECK((a) == (b)) +static bool StringCompareICU(BSTR left, BSTR right) { + size_t start, length; + if (!StringSearch(reinterpret_cast(left), + reinterpret_cast(right), &start, &length, true, + false)) { + return false; + } + return start == 0 && length == wcslen(left); +} + static AXNodePosition::AXPositionInstance CreateTextPosition( const AXNode& anchor, int text_offset, @@ -5094,9 +5107,20 @@ TEST_F(AXPlatformNodeTextRangeProviderTest, selection.Reset(); } -// TODO(schectman) Find text cannot ignore case yet. TEST_F(AXPlatformNodeTextRangeProviderTest, TestITextRangeProviderFindText) { - Init(BuildTextDocument({"some text", "more text"}, + // Initialize the ICU data from the icudtl.dat file, if it exists. + wchar_t buffer[MAX_PATH]; + GetModuleFileName(nullptr, buffer, MAX_PATH); + std::filesystem::path exec_path(buffer); + exec_path.remove_filename(); + exec_path.append("icudtl.dat"); + const std::string icudtl_path = exec_path.string(); + if (std::filesystem::exists(icudtl_path)) { + fml::icu::InitializeICU(icudtl_path); + } + + // \xC3\xA9 are the UTF8 bytes for codepoint 0xE9 - accented lowercase e. + Init(BuildTextDocument({"some text", "more text", "resum\xC3\xA9"}, false /* build_word_boundaries_offsets */, true /* place_text_on_one_line */)); @@ -5109,24 +5133,29 @@ TEST_F(AXPlatformNodeTextRangeProviderTest, TestITextRangeProviderFindText) { // Test Leaf kStaticText search. GetTextRangeProviderFromTextNode(range, root_node->children()[0]); EXPECT_UIA_FIND_TEXT(range, L"some text", false, owner); - // Some expectations like the one below are currently skipped until we can - // implement ignoreCase in FindText. - // EXPECT_UIA_FIND_TEXT(range, L"SoMe TeXt", false, owner); + EXPECT_UIA_FIND_TEXT(range, L"SoMe TeXt", true, owner); GetTextRangeProviderFromTextNode(range, root_node->children()[1]); EXPECT_UIA_FIND_TEXT(range, L"more", false, owner); - // EXPECT_UIA_FIND_TEXT(range, L"MoRe", true, owner); + EXPECT_UIA_FIND_TEXT(range, L"MoRe", true, owner); // Test searching for leaf content from ancestor. GetTextRangeProviderFromTextNode(range, root_node); EXPECT_UIA_FIND_TEXT(range, L"some text", false, owner); - // EXPECT_UIA_FIND_TEXT(range, L"SoMe TeXt", true, owner); + EXPECT_UIA_FIND_TEXT(range, L"SoMe TeXt", true, owner); EXPECT_UIA_FIND_TEXT(range, L"more text", false, owner); - // EXPECT_UIA_FIND_TEXT(range, L"MoRe TeXt", true, owner); + EXPECT_UIA_FIND_TEXT(range, L"MoRe TeXt", true, owner); EXPECT_UIA_FIND_TEXT(range, L"more", false, owner); + // Accented lowercase e. + EXPECT_UIA_FIND_TEXT(range, L"resum\xE9", false, owner); + // Accented uppercase +e. + EXPECT_UIA_FIND_TEXT(range, L"resum\xC9", true, owner); + EXPECT_UIA_FIND_TEXT(range, L"resume", true, owner); + EXPECT_UIA_FIND_TEXT(range, L"resumE", true, owner); // Test finding text that crosses a node boundary. EXPECT_UIA_FIND_TEXT(range, L"textmore", false, owner); // Test no match. EXPECT_UIA_FIND_TEXT_NO_MATCH(range, L"no match", false, owner); + EXPECT_UIA_FIND_TEXT_NO_MATCH(range, L"resume", false, owner); // Test if range returned is in expected anchor node. GetTextRangeProviderFromTextNode(range, root_node->children()[1]);