Commit 7004e38c authored by mpearson's avatar mpearson Committed by Commit bot

Omnibox: Fix for Bookmarks With Leading Spaces

These would cause DCHECK failures later in the system.

BUG=403518

Review URL: https://codereview.chromium.org/499343004

Cr-Commit-Position: refs/heads/master@{#291924}
parent a385d68d
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <vector> #include <vector>
#include "base/prefs/pref_service.h" #include "base/prefs/pref_service.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
#include "chrome/browser/autocomplete/history_provider.h" #include "chrome/browser/autocomplete/history_provider.h"
...@@ -27,6 +28,30 @@ using bookmarks::BookmarkMatch; ...@@ -27,6 +28,30 @@ using bookmarks::BookmarkMatch;
typedef std::vector<BookmarkMatch> BookmarkMatches; typedef std::vector<BookmarkMatch> BookmarkMatches;
namespace {
// Removes leading spaces from |title| before displaying, otherwise it looks
// funny. In the process, corrects |title_match_positions| so the correct
// characters are highlighted.
void CorrectTitleAndMatchPositions(
base::string16* title,
BookmarkMatch::MatchPositions* title_match_positions) {
size_t leading_whitespace_chars = title->length();
base::TrimWhitespace(*title, base::TRIM_LEADING, title);
leading_whitespace_chars-= title->length();
if (leading_whitespace_chars == 0)
return;
for (query_parser::Snippet::MatchPositions::iterator it =
title_match_positions->begin();
it != title_match_positions->end(); ++it) {
(*it) = query_parser::Snippet::MatchPosition(
it->first - leading_whitespace_chars,
it->second - leading_whitespace_chars);
}
}
} // namespace
// BookmarkProvider ------------------------------------------------------------ // BookmarkProvider ------------------------------------------------------------
BookmarkProvider::BookmarkProvider(Profile* profile) BookmarkProvider::BookmarkProvider(Profile* profile)
...@@ -147,6 +172,9 @@ AutocompleteMatch BookmarkProvider::BookmarkMatchToACMatch( ...@@ -147,6 +172,9 @@ AutocompleteMatch BookmarkProvider::BookmarkMatchToACMatch(
AutocompleteMatch match(this, 0, false, AutocompleteMatch match(this, 0, false,
AutocompleteMatchType::BOOKMARK_TITLE); AutocompleteMatchType::BOOKMARK_TITLE);
base::string16 title(bookmark_match.node->GetTitle()); base::string16 title(bookmark_match.node->GetTitle());
BookmarkMatch::MatchPositions new_title_match_positions =
bookmark_match.title_match_positions;
CorrectTitleAndMatchPositions(&title, &new_title_match_positions);
const GURL& url(bookmark_match.node->url()); const GURL& url(bookmark_match.node->url());
const base::string16& url_utf16 = base::UTF8ToUTF16(url.spec()); const base::string16& url_utf16 = base::UTF8ToUTF16(url.spec());
size_t inline_autocomplete_offset = URLPrefix::GetInlineAutocompleteOffset( size_t inline_autocomplete_offset = URLPrefix::GetInlineAutocompleteOffset(
......
...@@ -62,6 +62,8 @@ struct BookmarksTestInfo { ...@@ -62,6 +62,8 @@ struct BookmarksTestInfo {
{"worming burns #10", "http://www.burned.com/" }, {"worming burns #10", "http://www.burned.com/" },
{"worming burns #20", "http://www.worms.com/" }, {"worming burns #20", "http://www.worms.com/" },
{"jive music", "http://www.worms.com/" }, {"jive music", "http://www.worms.com/" },
// For testing strange spacing in bookmark titles.
{" hello1 hello2 ", "http://whatever.com/" },
}; };
class BookmarkProviderTest : public testing::Test { class BookmarkProviderTest : public testing::Test {
...@@ -253,6 +255,8 @@ TEST_F(BookmarkProviderTest, Positions) { ...@@ -253,6 +255,8 @@ TEST_F(BookmarkProviderTest, Positions) {
{"frankly frankly", 1, {{{0, 7}, {8, 15}, {0, 0}}}}, {"frankly frankly", 1, {{{0, 7}, {8, 15}, {0, 0}}}},
{"foobar foo", 1, {{{0, 6}, {7, 13}, {0, 0}}}}, {"foobar foo", 1, {{{0, 6}, {7, 13}, {0, 0}}}},
{"foo foobar", 1, {{{0, 6}, {7, 13}, {0, 0}}}}, {"foo foobar", 1, {{{0, 6}, {7, 13}, {0, 0}}}},
// This one makes sure that leading whitespace in the title is removed.
{"hello", 1, {{{0, 5}, {7, 12}, {0, 0}}}},
}; };
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) { for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment