Bug 1733876 Part 2 - Run line wrapping algorithm only if content of current line is not empty. r=m_kato,mbrodesser
☠☠ backed out by eb78120056f6 ☠ ☠
authorTing-Yu Lin <tlin@mozilla.com>
Thu, 07 Oct 2021 17:10:14 +0000
changeset 595127 2a6422635d37be218266ace7bf57ecf906130577
parent 595126 dd5f9ce8a8313355cade917b42ecd51f00b46f99
child 595128 b06880832da2d4f628526e8df0e4b3c7d50d1093
push id151065
push useraethanyc@gmail.com
push dateThu, 07 Oct 2021 17:12:56 +0000
treeherderautoland@f7f60a86bf47 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato, mbrodesser
bugs1733876
milestone95.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1733876 Part 2 - Run line wrapping algorithm only if content of current line is not empty. r=m_kato,mbrodesser With this edge case explicitly considered, it will be easier for the following patches to simplify logic in `FindWrapIndexForContent()`. Differential Revision: https://phabricator.services.mozilla.com/D127382
dom/serializers/nsPlainTextSerializer.cpp
--- a/dom/serializers/nsPlainTextSerializer.cpp
+++ b/dom/serializers/nsPlainTextSerializer.cpp
@@ -113,16 +113,17 @@ void nsPlainTextSerializer::CurrentLine:
 void nsPlainTextSerializer::CurrentLine::ResetContentAndIndentationHeader() {
   mContent.Truncate();
   mIndentation.mHeader.Truncate();
 }
 
 int32_t nsPlainTextSerializer::CurrentLine::FindWrapIndexForContent(
     const uint32_t aWrapColumn, const uint32_t aContentWidth,
     mozilla::intl::LineBreaker* aLineBreaker) const {
+  MOZ_ASSERT(!mContent.IsEmpty());
   MOZ_ASSERT(aContentWidth < std::numeric_limits<int32_t>::max());
   MOZ_ASSERT(static_cast<int32_t>(aContentWidth) ==
              GetUnicharStringWidth(mContent));
 
   const uint32_t prefixwidth = DeterminePrefixWidth();
   int32_t goodSpace = mContent.Length();
 
   if (aLineBreaker) {
@@ -144,17 +145,17 @@ int32_t nsPlainTextSerializer::CurrentLi
                     // position next to space
     }
   } else {
     // In this case we don't want strings, especially CJK-ones, to be split.
     // See
     // https://bugzilla.mozilla.org/show_bug.cgi?id=333064 for more
     // information.
 
-    if (mContent.IsEmpty() || aWrapColumn < prefixwidth) {
+    if (aWrapColumn < prefixwidth) {
       goodSpace = NS_LINEBREAKER_NEED_MORE_TEXT;
     } else {
       goodSpace = std::min(aWrapColumn - prefixwidth, mContent.Length() - 1);
       while (goodSpace >= 0 &&
              !nsCRT::IsAsciiSpace(mContent.CharAt(goodSpace))) {
         goodSpace--;
       }
     }
@@ -1231,28 +1232,31 @@ static bool IsSpaceStuffable(const char1
 
 void nsPlainTextSerializer::MaybeWrapAndOutputCompleteLines() {
   if (!mSettings.MayWrap()) {
     return;
   }
 
   const uint32_t prefixwidth = mCurrentLine.DeterminePrefixWidth();
 
-  // The width of the line as it will appear on the screen (approx.).
-  uint32_t currentLineContentWidth =
-      GetUnicharStringWidth(mCurrentLine.mContent);
-
   // Yes, wrap!
   // The "+4" is to avoid wrap lines that only would be a couple
   // of letters too long. We give this bonus only if the
   // wrapcolumn is more than 20.
   const uint32_t wrapColumn = mSettings.GetWrapColumn();
   uint32_t bonuswidth = (wrapColumn > 20) ? 4 : 0;
 
-  while (currentLineContentWidth + prefixwidth > wrapColumn + bonuswidth) {
+  while (!mCurrentLine.mContent.IsEmpty()) {
+    // The width of the line as it will appear on the screen (approx.).
+    const uint32_t currentLineContentWidth =
+        GetUnicharStringWidth(mCurrentLine.mContent);
+    if (currentLineContentWidth + prefixwidth <= wrapColumn + bonuswidth) {
+      break;
+    }
+
     const int32_t goodSpace = mCurrentLine.FindWrapIndexForContent(
         wrapColumn, currentLineContentWidth, mLineBreaker);
 
     const int32_t contentLength = mCurrentLine.mContent.Length();
     if ((goodSpace < contentLength) && (goodSpace > 0)) {
       // Found a place to break
 
       // -1 (trim a char at the break position)
@@ -1276,17 +1280,16 @@ void nsPlainTextSerializer::MaybeWrapAnd
                 0  // We space-stuff quoted lines anyway
         ) {
           // Space stuffing a la RFC 2646 (format=flowed).
           mCurrentLine.mContent.Append(char16_t(' '));
           // XXX doesn't seem to work correctly for ' '
         }
       }
       mCurrentLine.mContent.Append(restOfContent);
-      currentLineContentWidth = GetUnicharStringWidth(mCurrentLine.mContent);
       mEmptyLines = -1;
     } else {
       // Nothing to do. Hopefully we get more data later
       // to use for a place to break line
       break;
     }
   }
 }