Truly independent web browser
LibUnicode: Never return substring matches before the requested offset
Zaggy10244073a21228b5
diff --git a/Libraries/LibUnicode/Collator.cpp b/Libraries/LibUnicode/Collator.cppindex f8b08f5e35f..aa9b4486f97 100644--- a/Libraries/LibUnicode/Collator.cpp+++ b/Libraries/LibUnicode/Collator.cpp@@ -193,19 +193,28 @@ public:if (m_pattern.isEmpty() || start_offset > static_cast<size_t>(m_text.length()))return {};- UErrorCode status = U_ZERO_ERROR;- m_search->setOffset(static_cast<i32>(start_offset), status);- verify_icu_success(status);-- auto const match_start = m_search->next(status);- verify_icu_success(status);- if (match_start == USEARCH_DONE)- return {};+ // ICU snaps offsets inside a surrogate pair or contraction backwards, so step forward until a match doesn't.+ auto search_offset = static_cast<i32>(start_offset);+ while (search_offset < m_text.length()) {+ UErrorCode status = U_ZERO_ERROR;+ m_search->setOffset(search_offset, status);+ verify_icu_success(status);++ auto const match_start = m_search->next(status);+ verify_icu_success(status);+ if (match_start == USEARCH_DONE)+ return {};++ if (static_cast<size_t>(match_start) >= start_offset) {+ return Collator::Match {+ .start = static_cast<size_t>(match_start),+ .end = static_cast<size_t>(match_start + m_search->getMatchedLength()),+ };+ }- return Collator::Match {- .start = static_cast<size_t>(match_start),- .end = static_cast<size_t>(match_start + m_search->getMatchedLength()),- };+ search_offset = m_text.moveIndex32(search_offset, 1);+ }+ return {};}private:diff --git a/Tests/LibUnicode/CMakeLists.txt b/Tests/LibUnicode/CMakeLists.txtindex 352e863c84b..0c809dafbfa 100644--- a/Tests/LibUnicode/CMakeLists.txt+++ b/Tests/LibUnicode/CMakeLists.txt@@ -2,6 +2,7 @@ ladybird_test_suite(LibUnicode)set(TEST_SOURCESTestBidi.cpp+ TestCollator.cppTestDisplayNames.cppTestFullwidthMapping.cppTestIDNA.cppdiff --git a/Tests/LibUnicode/TestCollator.cpp b/Tests/LibUnicode/TestCollator.cppnew file mode 100644index 00000000000..9efbcf8d4dc--- /dev/null+++ b/Tests/LibUnicode/TestCollator.cpp@@ -0,0 +1,77 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibTest/TestCase.h>++#include <AK/Utf16String.h>+#include <LibUnicode/Collator.h>++static NonnullOwnPtr<Unicode::Collator> create_search_collator()+{+ return Unicode::Collator::create({}, Unicode::Usage::Search, {}, Unicode::Sensitivity::Base, Unicode::CaseFirst::False, false, false);+}++TEST_CASE(substring_search_from_inside_a_surrogate_pair)+{+ auto collator = create_search_collator();+ auto haystack = Utf16String::from_utf8("a \U0001FAE9 b \U0001FAE9"sv);+ auto searcher = collator->create_substring_searcher(haystack, u"\U0001FAE9"sv);++ auto match = searcher->find_from(0);+ VERIFY(match.has_value());+ EXPECT_EQ(match->start, 2u);+ EXPECT_EQ(match->end, 4u);++ match = searcher->find_from(3);+ VERIFY(match.has_value());+ EXPECT_EQ(match->start, 7u);+ EXPECT_EQ(match->end, 9u);++ EXPECT(!searcher->find_from(8).has_value());+}++TEST_CASE(substring_search_finds_overlapping_match_after_a_surrogate_pair)+{+ auto collator = create_search_collator();+ auto haystack = Utf16String::from_utf8("\U0001FAE9\U0001FAE9\U0001FAE9"sv);+ auto searcher = collator->create_substring_searcher(haystack, u"\U0001FAE9\U0001FAE9"sv);++ auto match = searcher->find_from(1);+ VERIFY(match.has_value());+ EXPECT_EQ(match->start, 2u);+ EXPECT_EQ(match->end, 6u);+}++TEST_CASE(substring_search_from_inside_the_base_of_an_emoji_modifier_sequence)+{+ auto collator = create_search_collator();+ auto haystack = Utf16String::from_utf8("\U0001F44D\U0001F3FD \U0001F44D"sv);+ auto searcher = collator->create_substring_searcher(haystack, u"\U0001F44D"sv);++ auto match = searcher->find_from(0);+ VERIFY(match.has_value());+ EXPECT_EQ(match->start, 0u);+ EXPECT_EQ(match->end, 2u);++ match = searcher->find_from(1);+ VERIFY(match.has_value());+ EXPECT_EQ(match->start, 5u);+ EXPECT_EQ(match->end, 7u);+}++TEST_CASE(substring_search_finds_overlapping_match_after_a_contraction)+{+ auto collator = create_search_collator();+ auto haystack = Utf16String::from_utf8("=\u0338=\u0338=\u0338"sv);+ auto searcher = collator->create_substring_searcher(haystack, u"=\u0338=\u0338"sv);++ auto match = searcher->find_from(1);+ VERIFY(match.has_value());+ EXPECT_EQ(match->start, 2u);+ EXPECT_EQ(match->end, 6u);++ EXPECT(!searcher->find_from(3).has_value());+}diff --git a/Tests/LibWeb/Text/expected/scroll-to-text-fragment/generate-text-fragment-url.txt b/Tests/LibWeb/Text/expected/scroll-to-text-fragment/generate-text-fragment-url.txtindex 04f1eaa0eb4..c4db5390eb0 100644--- a/Tests/LibWeb/Text/expected/scroll-to-text-fragment/generate-text-fragment-url.txt+++ b/Tests/LibWeb/Text/expected/scroll-to-text-fragment/generate-text-fragment-url.txt@@ -1,8 +1,8 @@Harness status: OK-Found 29 tests+Found 32 tests-29 Pass+32 PassPass An exact directive is preferred for unique selections shorter than 300 code pointsPass A short selection receives the shortest available contextPass Selection endpoints inside words expand to word boundaries@@ -28,6 +28,9 @@ Pass Editable text outside the selection does not prevent generationPass Selections partly containing an editing host are rejectedPass Word-boundary expansion into editable content is rejectedPass Selections containing form controls are rejected+Pass A unique emoji selection is not counted as ambiguous+Pass Repeated emoji are disambiguated without restarting inside a surrogate pair+Pass Emoji matches inside a modifier sequence are skippedPass Selections in design-mode documents are rejectedPass Selections outside the searchable text are rejectedPass Whitespace-only and collapsed selections are rejecteddiff --git a/Tests/LibWeb/Text/input/scroll-to-text-fragment/generate-text-fragment-url.html b/Tests/LibWeb/Text/input/scroll-to-text-fragment/generate-text-fragment-url.htmlindex 5e42ee591fb..45afd54c8cc 100644--- a/Tests/LibWeb/Text/input/scroll-to-text-fragment/generate-text-fragment-url.html+++ b/Tests/LibWeb/Text/input/scroll-to-text-fragment/generate-text-fragment-url.html@@ -231,6 +231,30 @@ test(() => {assert_equals(generate(range), null);}, "Selections containing form controls are rejected");+test(() => {+ setFixture(`<p id="target">before \u{1FAE9} after</p>`);+ const range = rangeWithin(target, 7, 9);+ assert_equals(generate(range), "https://example.com/page.html#:~:text=before-,%F0%9F%AB%A9");+}, "A unique emoji selection is not counted as ambiguous");++test(() => {+ setFixture(`+ <p>left \u{1FAE9} right</p>+ <p id="target">before \u{1FAE9} after</p>+ `);+ const range = rangeWithin(target, 7, 9);+ assert_equals(generate(range), "https://example.com/page.html#:~:text=before-,%F0%9F%AB%A9");+}, "Repeated emoji are disambiguated without restarting inside a surrogate pair");++test(() => {+ setFixture(`+ <p>left \u{1F44D}\u{1F3FD} right</p>+ <p id="target">before \u{1F44D} after</p>+ `);+ const range = rangeWithin(target, 7, 9);+ assert_equals(generate(range), "https://example.com/page.html#:~:text=before-,%F0%9F%91%8D");+}, "Emoji matches inside a modifier sequence are skipped");+test(() => {setFixture(`<p id="target">these five words form unique text</p>`);const range = rangeWithin(target, 0, target.textContent.length);