diff options
author | gab@chromium.org <gab@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-16 16:25:03 +0000 |
---|---|---|
committer | gab@chromium.org <gab@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-16 16:25:03 +0000 |
commit | 41ec57f78ce8f6f80e6afc2d19142c473ea33ff2 (patch) | |
tree | ae41a4cd7a55daeb1d49a7b602d027eb2a069833 /ui/base/text | |
parent | a8e9e8868242c2fdef96aa669666c857befb55ea (diff) | |
download | chromium_src-41ec57f78ce8f6f80e6afc2d19142c473ea33ff2.zip chromium_src-41ec57f78ce8f6f80e6afc2d19142c473ea33ff2.tar.gz chromium_src-41ec57f78ce8f6f80e6afc2d19142c473ea33ff2.tar.bz2 |
Elide long emails in the wrench and profile menus.
NOTE: This CL now only adds eliding functionality, but doesn't use it in the UI just yet.
Refer to bug 113133 for details (refer to comment 22 for a screenshot of the result: http://code.google.com/p/chromium/issues/detail?id=113133#c22).
BUG=113133
TEST=Make sure ui_unittests (ElideEmail and ElideEmailMoreSpace) pass.
Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=126445
Review URL: http://codereview.chromium.org/9489011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@127175 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base/text')
-rw-r--r-- | ui/base/text/text_elider.cc | 68 | ||||
-rw-r--r-- | ui/base/text/text_elider.h | 23 | ||||
-rw-r--r-- | ui/base/text/text_elider_unittest.cc | 80 |
3 files changed, 159 insertions, 12 deletions
diff --git a/ui/base/text/text_elider.cc b/ui/base/text/text_elider.cc index 525cea0..62ad7d0 100644 --- a/ui/base/text/text_elider.cc +++ b/ui/base/text/text_elider.cc @@ -149,12 +149,66 @@ string16 ElideComponentizedPath(const string16& url_path_prefix, } // namespace -// This function takes a GURL object and elides it. It returns a string -// which composed of parts from subdomain, domain, path, filename and query. -// A "..." is added automatically at the end if the elided string is bigger -// than the available pixel width. For available pixel width = 0, a formatted, -// but un-elided, string is returned. -// +string16 ElideEmail(const string16& email, + const gfx::Font& font, + int available_pixel_width) { + if (font.GetStringWidth(email) <= available_pixel_width) + return email; + + // Split the email into its local-part (username) and domain-part. The email + // spec technically allows for @ symbols in the local-part (username) of the + // email under some special requirements. It is guaranteed that there is no @ + // symbol in the domain part of the email however so splitting at the last @ + // symbol is safe. + const size_t split_index = email.find_last_of('@'); + DCHECK_NE(split_index, string16::npos); + string16 username = email.substr(0, split_index); + string16 domain = email.substr(split_index + 1); + DCHECK(!username.empty()); + DCHECK(!domain.empty()); + + const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis); + + // Subtract the @ symbol from the available width as it is mandatory. + const string16 kAtSignUTF16 = ASCIIToUTF16("@"); + available_pixel_width -= font.GetStringWidth(kAtSignUTF16); + + // Check whether eliding the domain is necessary: if eliding the username + // is sufficient, the domain will not be elided. + const int full_username_width = font.GetStringWidth(username); + const int available_domain_width = + available_pixel_width - + std::min(full_username_width, + font.GetStringWidth(username.substr(0, 1) + kEllipsisUTF16)); + if (font.GetStringWidth(domain) > available_domain_width) { + // Elide the domain so that it only takes half of the available width. + // Should the username not need all the width available in its half, the + // domain will occupy the leftover width. + // If |desired_domain_width| is greater than |available_domain_width|: the + // minimal username elision allowed by the specifications will not fit; thus + // |desired_domain_width| must be <= |available_domain_width| at all cost. + const int desired_domain_width = + std::min(available_domain_width, + std::max(available_pixel_width - full_username_width, + available_pixel_width / 2)); + domain = ElideText(domain, font, desired_domain_width, ELIDE_IN_MIDDLE); + // Failing to elide the domain such that at least one character remains + // (other than the ellipsis itself) remains: return a single ellipsis. + if (domain.length() <= 1U) + return kEllipsisUTF16; + } + + // Fit the username in the remaining width (at this point the elided username + // is guaranteed to fit with at least one character remaining given all the + // precautions taken earlier). + username = ElideText(username, + font, + available_pixel_width - font.GetStringWidth(domain), + ELIDE_AT_END); + + return username + kAtSignUTF16 + domain; +} + // TODO(pkasting): http://crbug.com/77883 This whole function gets // kerning/ligatures/etc. issues potentially wrong by assuming that the width of // a rendered string is always the sum of the widths of its substrings. Also I @@ -393,8 +447,6 @@ string16 ElideFilename(const FilePath& filename, return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); } -// This function adds an ellipsis at the end of the text if the text -// does not fit the given pixel width. string16 ElideText(const string16& text, const gfx::Font& font, int available_pixel_width, diff --git a/ui/base/text/text_elider.h b/ui/base/text/text_elider.h index caac9d8..68c392f 100644 --- a/ui/base/text/text_elider.h +++ b/ui/base/text/text_elider.h @@ -25,13 +25,28 @@ namespace ui { UI_EXPORT extern const char kEllipsis[]; +// Elides a well-formed email address (e.g. username@domain.com) to fit into +// |available_pixel_width| using the specified |font|. +// This function guarantees that the string returned will contain at least one +// character, other than the ellipses, on either side of the '@'. If it is +// impossible to achieve these requirements: only an ellipsis will be returned. +// If possible: this elides only the username portion of the |email|. Otherwise, +// the domain is elided in the middle so that it splits the available width +// equally with the elided username (should the username be short enough that it +// doesn't need half the available width: the elided domain will occupy that +// extra width). +UI_EXPORT string16 ElideEmail(const string16& email, + const gfx::Font& font, + int available_pixel_width); + // This function takes a GURL object and elides it. It returns a string // which composed of parts from subdomain, domain, path, filename and query. // A "..." is added automatically at the end if the elided string is bigger -// than the available pixel width. For available pixel width = 0, empty -// string is returned. |languages| is a comma separated list of ISO 639 -// language codes and is used to determine what characters are understood -// by a user. It should come from |prefs::kAcceptLanguages|. +// than the |available_pixel_width|. For |available_pixel_width| == 0, a +// formatted, but un-elided, string is returned. |languages| is a comma +// separated list of ISO 639 language codes and is used to determine what +// characters are understood by a user. It should come from +// |prefs::kAcceptLanguages|. // // Note: in RTL locales, if the URL returned by this function is going to be // displayed in the UI, then it is likely that the string needs to be marked diff --git a/ui/base/text/text_elider_unittest.cc b/ui/base/text/text_elider_unittest.cc index 217170c..aad021e 100644 --- a/ui/base/text/text_elider_unittest.cc +++ b/ui/base/text/text_elider_unittest.cc @@ -55,6 +55,86 @@ void RunUrlTest(Testcase* testcases, size_t num_testcases) { } // namespace +TEST(TextEliderTest, ElideEmail) { + const std::string kEllipsisStr(kEllipsis); + + // Test emails and their expected elided forms (from which the available + // widths will be derived). + // For elided forms in which both the username and domain must be elided: + // the result (how many characters are left on each side) can be font + // dependent. To avoid this, the username is prefixed with the characters + // expected to remain in the domain. + Testcase testcases[] = { + {"g@g.c", "g@g.c"}, + {"g@g.c", kEllipsisStr}, + {"ga@co.ca", "ga@c" + kEllipsisStr + "a"}, + {"short@small.com", "s" + kEllipsisStr + "@s" + kEllipsisStr}, + {"short@small.com", "s" + kEllipsisStr + "@small.com"}, + {"short@longbutlotsofspace.com", "short@longbutlotsofspace.com"}, + {"short@longbutnotverymuchspace.com", + "short@long" + kEllipsisStr + ".com"}, + {"la_short@longbutverytightspace.ca", + "la" + kEllipsisStr + "@l" + kEllipsisStr + "a"}, + {"longusername@gmail.com", "long" + kEllipsisStr + "@gmail.com"}, + {"elidetothemax@justfits.com", "e" + kEllipsisStr + "@justfits.com"}, + {"thatom_somelongemail@thatdoesntfit.com", + "thatom" + kEllipsisStr + "@tha" + kEllipsisStr + "om"}, + {"namefits@butthedomaindoesnt.com", + "namefits@butthedo" + kEllipsisStr + "snt.com"}, + {"widthtootight@nospace.com", kEllipsisStr}, + {"nospaceforusername@l", kEllipsisStr}, + {"little@littlespace.com", "l" + kEllipsisStr + "@l" + kEllipsisStr}, + {"l@llllllllllllllllllllllll.com", "l@lllll" + kEllipsisStr + ".com"}, + {"messed\"up@whyanat\"++@notgoogley.com", + "messed\"up@whyanat\"++@notgoogley.com"}, + {"messed\"up@whyanat\"++@notgoogley.com", + "messed\"up@why" + kEllipsisStr + "@notgoogley.com"}, + {"noca_messed\"up@whyanat\"++@notgoogley.ca", + "noca" + kEllipsisStr + "@no" + kEllipsisStr + "ca"}, + {"at\"@@@@@@@@@...@@.@.@.@@@\"@madness.com", + "at\"@@@@@@@@@...@@.@." + kEllipsisStr + "@madness.com"}, + // Special case: "m..." takes more than half of the available width; thus + // the domain must elide to "l..." and not "l...l" as it must allow enough + // space for the minimal username elision although its half of the + // available width would normally allow it to elide to "l...l". + {"mmmmm@llllllllll", "m" + kEllipsisStr + "@l" + kEllipsisStr}, + }; + + const gfx::Font font; + for (size_t i = 0; i < arraysize(testcases); ++i) { + const string16 expected_output = UTF8ToUTF16(testcases[i].output); + EXPECT_EQ(expected_output, + ElideEmail(UTF8ToUTF16(testcases[i].input), + font, + font.GetStringWidth(expected_output))); + } +} + +TEST(TextEliderTest, ElideEmailMoreSpace) { + const int test_width_factors[] = { + 100, + 10000, + 1000000, + }; + const std::string test_emails[] = { + "a@c", + "test@email.com", + "short@verysuperdupperlongdomain.com", + "supermegalongusername@withasuperlonnnggggdomain.gouv.qc.ca", + }; + + const gfx::Font font; + for (size_t i = 0; i < arraysize(test_width_factors); ++i) { + const int test_width = test_width_factors[i] * + font.GetAverageCharacterWidth(); + for (size_t j = 0; j < arraysize(test_emails); ++j) { + // Extra space is available: the email should not be elided. + const string16 test_email = UTF8ToUTF16(test_emails[j]); + EXPECT_EQ(test_email, ElideEmail(test_email, font, test_width)); + } + } +} + // Test eliding of commonplace URLs. TEST(TextEliderTest, TestGeneralEliding) { const std::string kEllipsisStr(kEllipsis); |