summaryrefslogtreecommitdiffstats
path: root/ui/base/text
diff options
context:
space:
mode:
authorasvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-05-15 02:43:10 +0000
committerasvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-05-15 02:43:10 +0000
commit49f47568522c5a83c851f4333592521a1dae86dd (patch)
tree6ec1337c70abcf2478700f2dd0878f340ac864b7 /ui/base/text
parent3342f48dd4eab0bcc4381a4cab16c4f7e31c41fa (diff)
downloadchromium_src-49f47568522c5a83c851f4333592521a1dae86dd.zip
chromium_src-49f47568522c5a83c851f4333592521a1dae86dd.tar.gz
chromium_src-49f47568522c5a83c851f4333592521a1dae86dd.tar.bz2
Modify ui::ElideRectangleText() to honor trailing newlines.
Trailing whitespace without a \n character is still trimmed away. Adds additional tests for this. Also, makes the code use ContainsOnlyWhitespace() from base and makes the implementation of ContainsOnlyWhitespace() shorter (based on the code that was in text_elider.cc.) BUG=126297 TEST=New tests in text_elider_unittest.cc Review URL: https://chromiumcodereview.appspot.com/10386016 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@137061 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base/text')
-rw-r--r--ui/base/text/text_elider.cc23
-rw-r--r--ui/base/text/text_elider_unittest.cc31
2 files changed, 34 insertions, 20 deletions
diff --git a/ui/base/text/text_elider.cc b/ui/base/text/text_elider.cc
index bfd7f90..ff3ad3a 100644
--- a/ui/base/text/text_elider.cc
+++ b/ui/base/text/text_elider.cc
@@ -793,6 +793,7 @@ class RectangleText {
wrap_behavior_(wrap_behavior),
current_width_(0),
current_height_(0),
+ last_line_ended_in_lf_(false),
lines_(lines),
full_(false) {}
@@ -812,9 +813,6 @@ class RectangleText {
bool Finalize();
private:
- // Returns |true| if |text| is entirely composed of whitespace.
- bool IsWhitespaceString(const string16& text) const;
-
// Add a line to the rectangular region at the current position,
// either by itself or by breaking it into words.
void AddLine(const string16& line);
@@ -863,6 +861,9 @@ class RectangleText {
// The current line of text.
string16 current_line_;
+ // Indicates whether the last line ended with \n.
+ bool last_line_ended_in_lf_;
+
// The output vector of lines.
std::vector<string16>* lines_;
@@ -881,7 +882,8 @@ void RectangleText::AddString(const string16& input) {
// The BREAK_NEWLINE iterator will keep the trailing newline character,
// except in the case of the last line, which may not have one. Remove
// the newline character, if it exists.
- if (!line.empty() && line[line.length() - 1] == '\n')
+ last_line_ended_in_lf_ = !line.empty() && line[line.length() - 1] == '\n';
+ if (last_line_ended_in_lf_)
line.resize(line.length() - 1);
AddLine(line);
}
@@ -895,16 +897,14 @@ bool RectangleText::Finalize() {
// completely, if it's just whitespace.
if (!full_ && !lines_->empty()) {
TrimWhitespace(lines_->back(), TRIM_TRAILING, &lines_->back());
- if (lines_->back().empty())
+ if (lines_->back().empty() && !last_line_ended_in_lf_)
lines_->pop_back();
}
+ if (last_line_ended_in_lf_)
+ lines_->push_back(string16());
return full_;
}
-bool RectangleText::IsWhitespaceString(const string16& text) const {
- return text.find_first_not_of(kWhitespaceUTF16) == string16::npos;
-}
-
void RectangleText::AddLine(const string16& line) {
const int line_width = font_.GetStringWidth(line);
if (line_width < available_pixel_width_) {
@@ -925,7 +925,7 @@ void RectangleText::AddLine(const string16& line) {
const int line = lines_->size() - lines_added;
TrimWhitespace(lines_->at(line), TRIM_TRAILING, &lines_->at(line));
}
- if (IsWhitespaceString(word)) {
+ if (ContainsOnlyWhitespace(word)) {
// Skip the first space if the previous line was carried over.
current_width_ = 0;
current_line_.clear();
@@ -996,7 +996,8 @@ int RectangleText::AddWord(const string16& word) {
// Append the non-trimmed word, in case more words are added after.
AddToCurrentLine(word);
} else {
- lines_added = AddWordOverflow(word);
+ lines_added = AddWordOverflow(wrap_behavior_ == ui::IGNORE_LONG_WORDS ?
+ trimmed : word);
}
return lines_added;
}
diff --git a/ui/base/text/text_elider_unittest.cc b/ui/base/text/text_elider_unittest.cc
index aad021e..e290a6e 100644
--- a/ui/base/text/text_elider_unittest.cc
+++ b/ui/base/text/text_elider_unittest.cc
@@ -530,6 +530,14 @@ TEST(TextEliderTest, ElideRectangleText) {
{ "\nTest", test_width, line_height, true, "" },
{ "\n\nTest", test_width, line_height * 3, false, "||Test" },
{ "\n\nTest", test_width, line_height * 2, true, "|" },
+ { "Test\n", 2 * test_width, line_height * 5, false, "Test|" },
+ { "Test\n\n", 2 * test_width, line_height * 5, false, "Test||" },
+ { "Test\n\n\n", 2 * test_width, line_height * 5, false, "Test|||" },
+ { "Test\nTest\n\n", 2 * test_width, line_height * 5, false, "Test|Test||" },
+ { "Test\n\nTest\n", 2 * test_width, line_height * 5, false, "Test||Test|" },
+ { "Test\n\n\nTest", 2 * test_width, line_height * 5, false, "Test|||Test" },
+ { "Te ", test_width, line_height, false, "Te" },
+ { "Te Te Test", test_width, 3 * line_height, false, "Te|Te|Test" },
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
@@ -541,10 +549,12 @@ TEST(TextEliderTest, ElideRectangleText) {
cases[i].available_pixel_height,
TRUNCATE_LONG_WORDS,
&lines));
- if (cases[i].output)
- EXPECT_EQ(cases[i].output, UTF16ToUTF8(JoinString(lines, '|')));
- else
- EXPECT_TRUE(lines.empty());
+ if (cases[i].output) {
+ const std::string result = UTF16ToUTF8(JoinString(lines, '|'));
+ EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!";
+ } else {
+ EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!";
+ }
}
}
@@ -579,10 +589,12 @@ TEST(TextEliderTest, ElideRectangleTextPunctuation) {
cases[i].available_pixel_height,
wrap_behavior,
&lines));
- if (cases[i].output)
- EXPECT_EQ(cases[i].output, UTF16ToUTF8(JoinString(lines, '|')));
- else
- EXPECT_TRUE(lines.empty());
+ if (cases[i].output) {
+ const std::string result = UTF16ToUTF8(JoinString(lines, '|'));
+ EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!";
+ } else {
+ EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!";
+ }
}
}
@@ -638,7 +650,8 @@ TEST(TextEliderTest, ElideRectangleTextLongWords) {
&lines);
std::string expected_output(cases[i].output);
ReplaceSubstringsAfterOffset(&expected_output, 0, "...", kEllipsis);
- EXPECT_EQ(expected_output, UTF16ToUTF8(JoinString(lines, '|')));
+ const std::string result = UTF16ToUTF8(JoinString(lines, '|'));
+ EXPECT_EQ(expected_output, result) << "Case " << i << " failed!";
}
}