Bug 1316482 - remove collapsible white spaces according to the White Space Processing Rules. draft
authorJeremy Chen <jeremychen@mozilla.com>
Thu, 12 Jan 2017 00:39:46 +0800
changeset 459262 02021b59d2c18489cfda92f868c82f4b730cea55
parent 459259 63ad56438630b389efdb01632e153143cf3008ec
child 459263 36324d8be0507dac19ecf861bb0cc8ec757d4885
push id41162
push userjichen@mozilla.com
push dateWed, 11 Jan 2017 16:40:21 +0000
bugs1316482
milestone53.0a1
Bug 1316482 - remove collapsible white spaces according to the White Space Processing Rules. This patch is an implementation of CSS Text 3 - 4.1.1 Phase 1 Step 1. According to the specification, if white space characters are considered collapsible, they should be removed before applying segment break transformation rules during the text transform. In this patch, a refactoring of text transformation logic has been made. Every run of consecutive document white space characters (spaces/tabs/segment breaks) is collected first. Then, we could apply the white space processing rules accordingly. MozReview-Commit-ID: 1JStjFk5TBs
layout/generic/nsTextFrameUtils.cpp
layout/generic/nsTextFrameUtils.h
--- a/layout/generic/nsTextFrameUtils.cpp
+++ b/layout/generic/nsTextFrameUtils.cpp
@@ -10,50 +10,180 @@
 #include "nsIContent.h"
 #include "nsStyleStruct.h"
 #include "nsTextFragment.h"
 #include "nsUnicharUtils.h"
 #include <algorithm>
 
 using namespace mozilla;
 
-static bool IsDiscardable(char16_t ch, uint32_t* aFlags)
+static bool
+IsDiscardable(char16_t ch, uint32_t* aFlags)
 {
   // Unlike IS_DISCARDABLE, we don't discard \r. \r will be ignored by gfxTextRun
   // and discarding it would force us to copy text in many cases of preformatted
   // text containing \r\n.
   if (ch == CH_SHY) {
     *aFlags |= nsTextFrameUtils::TEXT_HAS_SHY;
     return true;
   }
   return IsBidiControl(ch);
 }
 
-static bool IsDiscardable(uint8_t ch, uint32_t* aFlags)
+static bool
+IsDiscardable(uint8_t ch, uint32_t* aFlags)
 {
   if (ch == CH_SHY) {
     *aFlags |= nsTextFrameUtils::TEXT_HAS_SHY;
     return true;
   }
   return false;
 }
 
+static bool
+IsSegmentBreak(char16_t aCh)
+{
+  return aCh == '\n' || aCh == '\r';
+}
+
+static bool
+IsSpaceOrTab(char16_t aCh)
+{
+  return aCh == ' ' || aCh == '\t';
+}
+
+static bool
+IsSpaceOrTabOrSegmentBreak(char16_t aCh)
+{
+  return IsSpaceOrTab(aCh) || IsSegmentBreak(aCh);
+}
+
+static char16_t*
+TransformWhiteSpaces(const char16_t* aText, uint32_t aLength,
+                     uint32_t aBegin, uint32_t aEnd,
+                     bool aHasSegmentBreak,
+                     bool& aInWhitespace,
+                     char16_t* aOutput,
+                     uint32_t& aFlags,
+                     nsTextFrameUtils::CompressionMode aCompression,
+                     gfxSkipChars* aSkipChars)
+{
+  MOZ_ASSERT(aCompression == nsTextFrameUtils::COMPRESS_WHITESPACE ||
+             aCompression == nsTextFrameUtils::COMPRESS_WHITESPACE_NEWLINE,
+             "whitespaces should be skippable!!");
+  // Get the context preceding/following this white space range.
+  bool isSegmentBreakSkippable =
+    (aBegin > 0 && IS_ZERO_WIDTH_SPACE(aText[aBegin - 1])) ||
+    (aEnd < aLength && IS_ZERO_WIDTH_SPACE(aText[aEnd]));
+  if (!isSegmentBreakSkippable && aBegin > 0 && aEnd < aLength) {
+    uint32_t ucs4before;
+    uint32_t ucs4after;
+    if (aBegin > 1 &&
+        NS_IS_LOW_SURROGATE(aText[aBegin - 1]) &&
+        NS_IS_HIGH_SURROGATE(aText[aBegin - 2])) {
+      ucs4before = SURROGATE_TO_UCS4(aText[aBegin - 2], aText[aBegin - 1]);
+    } else {
+      ucs4before = aText[aBegin - 1];
+    }
+    if (aEnd + 1 < aLength &&
+        NS_IS_HIGH_SURROGATE(aText[aEnd]) &&
+        NS_IS_LOW_SURROGATE(aText[aEnd + 1])) {
+      ucs4after = SURROGATE_TO_UCS4(aText[aEnd], aText[aEnd + 1]);
+    } else {
+      ucs4after = aText[aEnd];
+    }
+    // Discard newlines between characters that have F, W, or H
+    // EastAsianWidth property and neither side is Hangul.
+    isSegmentBreakSkippable = IsSegmentBreakSkipChar(ucs4before) &&
+                              IsSegmentBreakSkipChar(ucs4after);
+  }
+
+  for (uint32_t i = aBegin; i < aEnd; ++i) {
+    char16_t ch = aText[i];
+    bool keepChar = false;
+    bool keepTransformedWhiteSpace = false;
+    if (IsDiscardable(ch, &aFlags)) {
+      aSkipChars->SkipChar();
+      continue;
+    }
+    if (IsSpaceOrTab(ch)) {
+      if (aHasSegmentBreak) {
+        // If white-space is set to normal, nowrap, or pre-line, white space
+        // characters are considered collapsible and all spaces and tabs
+        // immediately preceding or following a segment break are removed.
+        aSkipChars->SkipChar();
+        continue;
+      }
+
+      if (aInWhitespace) {
+        aSkipChars->SkipChar();
+        continue;
+      } else {
+        keepTransformedWhiteSpace = true;
+      }
+    } else {
+      // Apply Segment Break Transformation Rules (CSS Text 3 - 4.1.2) for
+      // segment break characters.
+      if (aCompression == nsTextFrameUtils::COMPRESS_WHITESPACE ||
+          // XXX: According to CSS Text 3, a lone CR should not always be
+          //      kept, but still go through the Segment Break Transformation
+          //      Rules. However, this is what current modern browser engines
+          //      (webkit/blink/edge) do. So, once we can get some clarity
+          //      from the specification issue, we should either remove the
+          //      lone CR condition here, or leave it here with this comment
+          //      being rephrased.
+          //      Please see https://github.com/w3c/csswg-drafts/issues/855.
+          ch == '\r') {
+        keepChar = true;
+      } else {
+        // aCompression == COMPRESS_WHITESPACE_NEWLINE
+
+        // Any collapsible segment break immediately following another
+        // collapsible segment break is removed.  Then the remaining segment
+        // break is either transformed into a space (U+0020) or removed
+        // depending on the context before and after the break.
+        if (isSegmentBreakSkippable || aInWhitespace) {
+          aSkipChars->SkipChar();
+          continue;
+        }
+        isSegmentBreakSkippable = true;
+        keepTransformedWhiteSpace = true;
+      }
+    }
+
+    if (keepChar) {
+      *aOutput++ = ch;
+      aSkipChars->KeepChar();
+      aInWhitespace = IsSpaceOrTab(ch);
+    } else if (keepTransformedWhiteSpace) {
+      if (ch != ' ') {
+        aFlags |= nsTextFrameUtils::TEXT_WAS_TRANSFORMED;
+      }
+      *aOutput++ = ' ';
+      aSkipChars->KeepChar();
+      aInWhitespace = true;
+    } else {
+      MOZ_ASSERT_UNREACHABLE("Should've skipped the character!!");
+    }
+  }
+  return aOutput;
+}
+
 char16_t*
 nsTextFrameUtils::TransformText(const char16_t* aText, uint32_t aLength,
                                 char16_t* aOutput,
                                 CompressionMode aCompression,
                                 uint8_t* aIncomingFlags,
                                 gfxSkipChars* aSkipChars,
                                 uint32_t* aAnalysisFlags)
 {
   uint32_t flags = 0;
   char16_t* outputStart = aOutput;
 
   bool lastCharArabic = false;
-
   if (aCompression == COMPRESS_NONE ||
       aCompression == COMPRESS_NONE_TRANSFORM_TO_SPACE) {
     // Skip discardables.
     uint32_t i;
     for (i = 0; i < aLength; ++i) {
       char16_t ch = aText[i];
       if (IsDiscardable(ch, &flags)) {
         aSkipChars->SkipChar();
@@ -81,77 +211,81 @@ nsTextFrameUtils::TransformText(const ch
       *aIncomingFlags &= ~INCOMING_ARABICCHAR;
     }
     *aIncomingFlags &= ~INCOMING_WHITESPACE;
   } else {
     bool inWhitespace = (*aIncomingFlags & INCOMING_WHITESPACE) != 0;
     uint32_t i;
     for (i = 0; i < aLength; ++i) {
       char16_t ch = aText[i];
-      bool nowInWhitespace;
-      if (ch == ' ' &&
-          (i + 1 >= aLength ||
-           !IsSpaceCombiningSequenceTail(&aText[i + 1], aLength - (i + 1)))) {
-        nowInWhitespace = true;
-      } else if (ch == '\n' && aCompression == COMPRESS_WHITESPACE_NEWLINE) {
-        if ((i > 0 && IS_ZERO_WIDTH_SPACE(aText[i - 1])) ||
-            (i + 1 < aLength && IS_ZERO_WIDTH_SPACE(aText[i + 1]))) {
-          aSkipChars->SkipChar();
-          continue;
-        }
-        uint32_t ucs4before;
-        uint32_t ucs4after;
-        if (i > 1 &&
-            NS_IS_LOW_SURROGATE(aText[i - 1]) &&
-            NS_IS_HIGH_SURROGATE(aText[i - 2])) {
-          ucs4before = SURROGATE_TO_UCS4(aText[i - 2], aText[i - 1]);
-        } else if (i > 0) {
-          ucs4before = aText[i - 1];
-        }
-        if (i + 2 < aLength &&
-            NS_IS_HIGH_SURROGATE(aText[i + 1]) &&
-            NS_IS_LOW_SURROGATE(aText[i + 2])) {
-          ucs4after = SURROGATE_TO_UCS4(aText[i + 1], aText[i + 2]);
-        } else if (i + 1 < aLength) {
-          ucs4after = aText[i + 1];
+      // CSS Text 3 - 4.1. The White Space Processing Rules
+      // White space processing in CSS affects only the document white space
+      // characters: spaces (U+0020), tabs (U+0009), and segment breaks.
+      // Since we need the context of segment breaks and their surrounding
+      // white spaces to proceed the white space processing, a consecutive run
+      // of spaces/tabs/segment breaks is collected in a first pass loop, then
+      // we apply the collapsing and transformation rules to this run in a
+      // second pass loop.
+      if (IsSpaceOrTabOrSegmentBreak(ch)) {
+        bool keepLastSpace = false;
+        bool hasSegmentBreak = IsSegmentBreak(ch);
+        uint32_t countTrailingDiscardables = 0;
+        uint32_t j;
+        for (j = i + 1; j < aLength &&
+                        (IsSpaceOrTabOrSegmentBreak(aText[j]) ||
+                         IsDiscardable(aText[j], &flags));
+             j++) {
+          if (IsSegmentBreak(aText[j])) {
+            hasSegmentBreak = true;
+          }
         }
-        if (i > 0 && IsSegmentBreakSkipChar(ucs4before) &&
-            i + 1 < aLength && IsSegmentBreakSkipChar(ucs4after)) {
-          // Discard newlines between characters that have F, W, or H
-          // EastAsianWidth property and neither side is Hangul.
-          aSkipChars->SkipChar();
-          continue;
+        // Exclude trailing discardables before checking space combining
+        // sequence tail.
+        for (; IsDiscardable(aText[j - 1], &flags); j--) {
+          countTrailingDiscardables++;
         }
-        nowInWhitespace = true;
-      } else {
-        nowInWhitespace = ch == '\t';
-      }
-
-      if (!nowInWhitespace) {
-        if (IsDiscardable(ch, &flags)) {
-          aSkipChars->SkipChar();
-          nowInWhitespace = inWhitespace;
-        } else {
-          *aOutput++ = ch;
-          aSkipChars->KeepChar();
-          lastCharArabic = IS_ARABIC_CHAR(ch);
+        // If the last white space is followed by a combining sequence tail,
+        // exclude it from the range of TransformWhiteSpaces.
+        if (aText[j - 1] == ' ' && j < aLength &&
+            IsSpaceCombiningSequenceTail(&aText[j], aLength - j)) {
+          keepLastSpace = true;
+          j--;
         }
-      } else {
-        if (inWhitespace) {
-          aSkipChars->SkipChar();
-        } else {
-          if (ch != ' ') {
-            flags |= TEXT_WAS_TRANSFORMED;
-          }
+        if (j > i) {
+          aOutput = TransformWhiteSpaces(aText, aLength, i, j, hasSegmentBreak,
+                                         inWhitespace, aOutput, flags,
+                                         aCompression, aSkipChars);
+        }
+        // We need to keep KeepChar()/SkipChar() in order, so process the
+        // last white space first, then process the trailing discardables.
+        if (keepLastSpace) {
+          keepLastSpace = false;
           *aOutput++ = ' ';
           aSkipChars->KeepChar();
+          lastCharArabic = false;
+          j++;
         }
+        for (; countTrailingDiscardables > 0; countTrailingDiscardables--) {
+          aSkipChars->SkipChar();
+          j++;
+        }
+        i = j - 1;
+        continue;
       }
-      inWhitespace = nowInWhitespace;
+      // Process characters other than the document white space characters.
+      if (IsDiscardable(ch, &flags)) {
+        aSkipChars->SkipChar();
+      } else {
+        *aOutput++ = ch;
+        aSkipChars->KeepChar();
+      }
+      lastCharArabic = IS_ARABIC_CHAR(ch);
+      inWhitespace = false;
     }
+
     if (lastCharArabic) {
       *aIncomingFlags |= INCOMING_ARABICCHAR;
     } else {
       *aIncomingFlags &= ~INCOMING_ARABICCHAR;
     }
     if (inWhitespace) {
       *aIncomingFlags |= INCOMING_WHITESPACE;
     } else {
--- a/layout/generic/nsTextFrameUtils.h
+++ b/layout/generic/nsTextFrameUtils.h
@@ -95,17 +95,17 @@ public:
     COMPRESS_NONE_TRANSFORM_TO_SPACE
   };
 
   /**
    * Create a text run from a run of Unicode text. The text may have whitespace
    * compressed. A preformatted tab is sent to the text run as a single space.
    * (Tab spacing must be performed by textframe later.) Certain other
    * characters are discarded.
-   * 
+   *
    * @param aCompression control what is compressed to a
    * single space character: no compression, compress spaces (not followed
    * by combining mark) and tabs, compress those plus newlines, or
    * no compression except newlines are discarded.
    * @param aIncomingFlags a flag indicating whether there was whitespace
    * or an Arabic character preceding this text. We set it to indicate if
    * there's an Arabic character or whitespace preceding the end of this text.
    */