summaryrefslogtreecommitdiffstats
path: root/ui/base/text
diff options
context:
space:
mode:
authorshess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-13 20:19:24 +0000
committershess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-13 20:19:24 +0000
commite52c54d6dd1f4149bcbe17a66937b8a82d9b48be (patch)
tree9c9ab09a6baab7f79cf54fa21d8b66c5869d7265 /ui/base/text
parentea0ef9c34675505bc5f7d10e57c7d45b44439597 (diff)
downloadchromium_src-e52c54d6dd1f4149bcbe17a66937b8a82d9b48be.zip
chromium_src-e52c54d6dd1f4149bcbe17a66937b8a82d9b48be.tar.gz
chromium_src-e52c54d6dd1f4149bcbe17a66937b8a82d9b48be.tar.bz2
Revert 126445 - 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. Review URL: http://codereview.chromium.org/9489011 TBR=gab@chromium.org Review URL: https://chromiumcodereview.appspot.com/9693058 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@126451 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base/text')
-rw-r--r--ui/base/text/text_elider.cc68
-rw-r--r--ui/base/text/text_elider.h23
-rw-r--r--ui/base/text/text_elider_unittest.cc80
3 files changed, 12 insertions, 159 deletions
diff --git a/ui/base/text/text_elider.cc b/ui/base/text/text_elider.cc
index 62ad7d0..525cea0 100644
--- a/ui/base/text/text_elider.cc
+++ b/ui/base/text/text_elider.cc
@@ -149,66 +149,12 @@ string16 ElideComponentizedPath(const string16& url_path_prefix,
} // namespace
-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;
-}
-
+// 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.
+//
// 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
@@ -447,6 +393,8 @@ 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 68c392f..caac9d8 100644
--- a/ui/base/text/text_elider.h
+++ b/ui/base/text/text_elider.h
@@ -25,28 +25,13 @@ 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, 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|.
+// 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|.
//
// 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 f60e9a1..217170c 100644
--- a/ui/base/text/text_elider_unittest.cc
+++ b/ui/base/text/text_elider_unittest.cc
@@ -55,86 +55,6 @@ 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@lllllllll.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);