Skip to content

Commit 7a17e78

Browse files
[Omnibox]: Additional bump for exact title matches (#28101)
1 parent e002afd commit 7a17e78

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

components/omnibox/browser/brave_bookmark_provider.cc

+32-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ void BraveBookmarkProvider::Start(const AutocompleteInput& input,
3030
// We need to bump the relevance of the bookmark if we ever want it to rank
3131
// high enough to be the default match.
3232
constexpr int kContainsQueryBump = 350;
33+
// Note: This is in addition to the kContainsQueryBump.
34+
constexpr int kExactTitleBump = 200;
35+
3336
bool modified = false;
3437

3538
auto lower_text = base::ToLowerASCII(input.text());
@@ -40,14 +43,40 @@ void BraveBookmarkProvider::Start(const AutocompleteInput& input,
4043

4144
// We only allow the bookmark to be the default match if the input is
4245
// literally contained in the title or URL.
43-
auto lower_contents = base::ToLowerASCII(match.contents);
46+
auto lower_description = base::ToLowerASCII(match.description);
4447
auto bump_match =
45-
base::Contains(lower_contents, lower_text) ||
46-
base::Contains(base::ToLowerASCII(match.description), lower_text);
48+
base::Contains(base::ToLowerASCII(match.contents), lower_text) ||
49+
base::Contains(lower_description, lower_text);
4750
if (!bump_match) {
4851
continue;
4952
}
5053

54+
// By default |contents| is the folder the bookmark is in if there are no
55+
// matches in the URL. Instead, we want to should show the URL that will be
56+
// opened so the user knows what will happen when they select the result.
57+
// Note: Bookmark paths are prefixed with a "/" to indicate they are
58+
// relative to the bookmark root.
59+
if (match.contents.starts_with(u"/")) {
60+
// This is the same formatting used on Bookmark URLs normally.
61+
match.contents = url_formatter::FormatUrl(
62+
match.destination_url,
63+
url_formatter::kFormatUrlOmitHTTPS |
64+
url_formatter::kFormatUrlOmitDefaults |
65+
url_formatter::kFormatUrlOmitTrivialSubdomains,
66+
base::UnescapeRule::SPACES, nullptr, nullptr, nullptr);
67+
// In this scenario we're matching the title, so its okay to display no
68+
// matches on the URL.
69+
match.contents_class = {
70+
ACMatchClassification(0, ACMatchClassification::URL)};
71+
}
72+
73+
// Additional bump if the title is an exact match - this helps people with
74+
// short bookmark titles for jumping to pages (like "sa" for
75+
// "chrome://settings/appearance")
76+
if (lower_description == lower_text) {
77+
match.relevance += kExactTitleBump;
78+
}
79+
5180
match.SetAllowedToBeDefault(input);
5281

5382
modified = true;

components/omnibox/browser/brave_bookmark_provider_unittest.cc

+26
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,29 @@ TEST_F(BraveBookmarkProviderTest, ContainsQueryBumpsRelevance) {
122122
// Note: 1199 is the max relevance score for a bookmark upstream.
123123
EXPECT_GT(provider_->matches()[0].relevance, 1199);
124124
}
125+
126+
TEST_F(BraveBookmarkProviderTest, ExactTitleMatchBumpsRelevance) {
127+
prefs()->SetBoolean(omnibox::kBookmarkSuggestionsEnabled, true);
128+
client_.GetBookmarkModel()->AddURL(client_.GetBookmarkModel()->other_node(),
129+
0, u"Hellow",
130+
GURL("https://example.com/one"));
131+
provider_->Start(CreateAutocompleteInput("Hello"), true);
132+
EXPECT_EQ(provider_->matches().size(), 2u);
133+
134+
EXPECT_EQ(provider_->matches()[0].description, u"Hello");
135+
EXPECT_TRUE(provider_->matches()[0].allowed_to_be_default_match);
136+
137+
EXPECT_EQ(provider_->matches()[1].description, u"Hellow");
138+
EXPECT_TRUE(provider_->matches()[1].allowed_to_be_default_match);
139+
140+
EXPECT_GT(provider_->matches()[0].relevance,
141+
provider_->matches()[1].relevance);
142+
}
143+
144+
TEST_F(BraveBookmarkProviderTest, TitleOnlyMatchSetsURL) {
145+
prefs()->SetBoolean(omnibox::kBookmarkSuggestionsEnabled, true);
146+
provider_->Start(CreateAutocompleteInput("Hello"), true);
147+
EXPECT_EQ(provider_->matches().size(), 1u);
148+
EXPECT_TRUE(provider_->matches()[0].allowed_to_be_default_match);
149+
EXPECT_EQ(provider_->matches()[0].contents, u"example.com");
150+
}

0 commit comments

Comments
 (0)