Don't split text runs on underline style change

BUG=392154

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

Cr-Commit-Position: refs/heads/master@{#295444}
diff --git a/ui/gfx/render_text.cc b/ui/gfx/render_text.cc
index fe4c59c..bf98bc0c 100644
--- a/ui/gfx/render_text.cc
+++ b/ui/gfx/render_text.cc
@@ -412,10 +412,15 @@
   text_ = text;
 
   // Adjust ranged styles and colors to accommodate a new text length.
+  // Clear style ranges as they might break new text graphemes and apply
+  // the first style to the whole text instead.
   const size_t text_length = text_.length();
   colors_.SetMax(text_length);
-  for (size_t style = 0; style < NUM_TEXT_STYLES; ++style)
-    styles_[style].SetMax(text_length);
+  for (size_t style = 0; style < NUM_TEXT_STYLES; ++style) {
+    BreakList<bool>& break_list = styles_[style];
+    break_list.SetValue(break_list.breaks().begin()->second);
+    break_list.SetMax(text_length);
+  }
   cached_bounds_and_offset_valid_ = false;
 
   // Reset selection model. SetText should always followed by SetSelectionModel
@@ -651,7 +656,12 @@
 }
 
 void RenderText::ApplyStyle(TextStyle style, bool value, const Range& range) {
-  styles_[style].ApplyValue(value, range);
+  // Do not change styles mid-grapheme to avoid breaking ligatures.
+  const size_t start = IsValidCursorIndex(range.start()) ? range.start() :
+      IndexOfAdjacentGrapheme(range.start(), CURSOR_BACKWARD);
+  const size_t end = IsValidCursorIndex(range.end()) ? range.end() :
+      IndexOfAdjacentGrapheme(range.end(), CURSOR_FORWARD);
+  styles_[style].ApplyValue(value, Range(start, end));
 
   cached_bounds_and_offset_valid_ = false;
   ResetLayout();
diff --git a/ui/gfx/render_text_unittest.cc b/ui/gfx/render_text_unittest.cc
index 79476c4..5d377e7 100644
--- a/ui/gfx/render_text_unittest.cc
+++ b/ui/gfx/render_text_unittest.cc
@@ -171,17 +171,35 @@
   expected_italic.push_back(std::pair<size_t, bool>(7, true));
   EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
 
-  // Truncating the text should trim any corresponding breaks.
+  // Changing the text should clear any breaks except for the first one.
   render_text->SetText(ASCIIToUTF16("0123456"));
-  expected_italic.resize(4);
+  expected_italic.resize(1);
   EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
-  render_text->SetText(ASCIIToUTF16("01234"));
-  expected_italic.resize(3);
-  EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
-
-  // Appending text should extend the terminal styles without changing breaks.
+  render_text->ApplyStyle(ITALIC, false, Range(2, 4));
   render_text->SetText(ASCIIToUTF16("012345678"));
   EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
+  render_text->ApplyStyle(ITALIC, false, Range(0, 1));
+  render_text->SetText(ASCIIToUTF16("0123456"));
+  expected_italic.begin()->second = false;
+  EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
+  render_text->ApplyStyle(ITALIC, true, Range(2, 4));
+  render_text->SetText(ASCIIToUTF16("012345678"));
+  EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
+
+  // TODO(tmoniuszko): Enable when RenderTextMac::IsValidCursorIndex is
+  //                   implemented.
+#if !defined(OS_MACOSX)
+  // Styles shouldn't be changed mid-grapheme.
+  render_text->SetText(WideToUTF16(
+      L"0" L"\x0915\x093f" L"1" L"\x0915\x093f" L"2"));
+  render_text->ApplyStyle(UNDERLINE, true, Range(2, 5));
+  std::vector<std::pair<size_t, bool> > expected_underline;
+  expected_underline.push_back(std::pair<size_t, bool>(0, false));
+  expected_underline.push_back(std::pair<size_t, bool>(1, true));
+  expected_underline.push_back(std::pair<size_t, bool>(6, false));
+  EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsForTesting(
+      expected_underline));
+#endif  // OS_MACOSX
 }
 
 #if defined(OS_LINUX) && !defined(USE_OZONE)