Bug 1554147: rename `mCommonParent` and encapsulate `mStartDepth` and `mEndDepth` in `ContextInfoDepth`. r=hsivonen
authorMirko Brodesser <mbrodesser@mozilla.com>
Mon, 27 May 2019 07:45:15 +0000
changeset 475685 7cf8441c08b6f9491985ecdcffdf87d372987d33
parent 475684 bfd32880a473069deb5d38dc6eeb8a5f49b9c4ed
child 475686 b9a2470096c313f2f210f04782b5dd97ae43d5a0
push id86408
push usermalexandru@mozilla.com
push dateMon, 27 May 2019 08:14:58 +0000
treeherderautoland@445271e41b4a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershsivonen
bugs1554147
milestone69.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 1554147: rename `mCommonParent` and encapsulate `mStartDepth` and `mEndDepth` in `ContextInfoDepth`. r=hsivonen In order to simplify `nsDocumentEncoder`. Differential Revision: https://phabricator.services.mozilla.com/D32468
dom/base/nsDocumentEncoder.cpp
--- a/dom/base/nsDocumentEncoder.cpp
+++ b/dom/base/nsDocumentEncoder.cpp
@@ -193,16 +193,21 @@ struct RangeBoundaryPathsAndOffsets {
   // The first node is the range's boundary node, the following one the
   // ancestors.
   ContainerPath mEndContainerPath;
   // The first offset represents where at the boundary node the range ends.
   // Each other offset is the index of the child relative to its parent.
   ContainerOffsets mEndContainerOffsets;
 };
 
+struct ContextInfoDepth {
+  uint32_t mStart = 0;
+  uint32_t mEnd = 0;
+};
+
 class nsDocumentEncoder : public nsIDocumentEncoder {
  public:
   nsDocumentEncoder();
 
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_CYCLE_COLLECTION_CLASS(nsDocumentEncoder)
   NS_DECL_NSIDOCUMENTENCODER
 
@@ -299,25 +304,24 @@ class nsDocumentEncoder : public nsIDocu
    private:
     nsDocumentEncoder* mEncoder;
   };
 
   nsCOMPtr<Document> mDocument;
   EncodingScope mEncodingScope;
   nsCOMPtr<nsIContentSerializer> mSerializer;
   Maybe<TextStreamer> mTextStreamer;
-  nsCOMPtr<nsINode> mCommonParent;
+  nsCOMPtr<nsINode> mCommonAncestorOfRange;
   nsCOMPtr<nsIDocumentEncoderNodeFixup> mNodeFixup;
 
   nsString mMimeType;
   const Encoding* mEncoding;
   uint32_t mFlags;
   uint32_t mWrapColumn;
-  uint32_t mStartDepth;
-  uint32_t mEndDepth;
+  ContextInfoDepth mContextInfoDepth;
   int32_t mStartRootIndex;
   int32_t mEndRootIndex;
   AutoTArray<nsINode*, 8> mCommonAncestors;
   RangeBoundaryPathsAndOffsets mRangeBoundaryPathsAndOffsets;
   AutoTArray<AutoTArray<nsINode*, 8>, 8> mRangeContexts;
   // Whether the serializer cares about being notified to scan elements to
   // keep track of whether they are preformatted.  This stores the out
   // argument of nsIContentSerializer::Init().
@@ -336,36 +340,35 @@ NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LA
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDocumentEncoder)
   NS_INTERFACE_MAP_ENTRY(nsIDocumentEncoder)
   NS_INTERFACE_MAP_ENTRY(nsISupports)
 NS_INTERFACE_MAP_END
 
 NS_IMPL_CYCLE_COLLECTION(nsDocumentEncoder, mDocument,
                          mEncodingScope.mSelection, mEncodingScope.mRange,
-                         mEncodingScope.mNode, mSerializer, mCommonParent)
+                         mEncodingScope.mNode, mSerializer, mCommonAncestorOfRange)
 
 nsDocumentEncoder::nsDocumentEncoder()
     : mEncoding(nullptr), mIsCopying(false), mCachedBuffer(nullptr) {
   Initialize();
   mMimeType.AssignLiteral("text/plain");
 }
 
 void nsDocumentEncoder::Initialize(bool aClearCachedSerializer) {
   mFlags = 0;
   mWrapColumn = 72;
-  mStartDepth = 0;
-  mEndDepth = 0;
+  mContextInfoDepth = {};
   mStartRootIndex = 0;
   mEndRootIndex = 0;
   mNeedsPreformatScanning = false;
   mHaltRangeHint = false;
   mDisableContextSerialize = false;
   mEncodingScope = {};
-  mCommonParent = nullptr;
+  mCommonAncestorOfRange = nullptr;
   mNodeFixup = nullptr;
   mRangeBoundaryPathsAndOffsets = {};
   if (aClearCachedSerializer) {
     mSerializer = nullptr;
   }
 }
 
 static bool ParentIsTR(nsIContent* aContent) {
@@ -425,20 +428,20 @@ nsresult nsDocumentEncoder::SerializeDep
           NS_ENSURE_SUCCESS(rv, rv);
           prevNode = nullptr;
         }
       }
 
       rv = SerializeRangeToString(range, aOutput);
       NS_ENSURE_SUCCESS(rv, rv);
       if (i == 0) {
-        firstRangeStartDepth = mStartDepth;
+        firstRangeStartDepth = mContextInfoDepth.mStart;
       }
     }
-    mStartDepth = firstRangeStartDepth;
+    mContextInfoDepth.mStart = firstRangeStartDepth;
 
     if (prevNode) {
       rv = SerializeNodeEnd(*prevNode, aOutput);
       NS_ENSURE_SUCCESS(rv, rv);
       mDisableContextSerialize = false;
       rv = SerializeRangeContextEnd(aOutput);
       NS_ENSURE_SUCCESS(rv, rv);
     }
@@ -824,24 +827,28 @@ nsresult nsDocumentEncoder::SerializeRan
       } else {
         int32_t endOffset = aRange->EndOffset();
         rv = SerializeNodeStart(*aNode, 0, endOffset, aString);
         NS_ENSURE_SUCCESS(rv, rv);
       }
       rv = SerializeNodeEnd(*aNode, aString);
       NS_ENSURE_SUCCESS(rv, rv);
     } else {
-      if (aNode != mCommonParent) {
+      if (aNode != mCommonAncestorOfRange) {
         if (IncludeInContext(aNode)) {
-          // halt the incrementing of mStartDepth/mEndDepth.  This is
+          // halt the incrementing of mContextInfoDepth.  This is
           // so paste client will include this node in paste.
           mHaltRangeHint = true;
         }
-        if ((startNode == content) && !mHaltRangeHint) mStartDepth++;
-        if ((endNode == content) && !mHaltRangeHint) mEndDepth++;
+        if ((startNode == content) && !mHaltRangeHint) {
+          ++mContextInfoDepth.mStart;
+        }
+        if ((endNode == content) && !mHaltRangeHint) {
+          ++mContextInfoDepth.mEnd;
+        }
 
         // serialize the start of this node
         rv = SerializeNodeStart(*aNode, 0, -1, aString);
         NS_ENSURE_SUCCESS(rv, rv);
       }
 
       const auto& startContainerOffsets =
           mRangeBoundaryPathsAndOffsets.mStartContainerOffsets;
@@ -895,17 +902,17 @@ nsresult nsDocumentEncoder::SerializeRan
 
           NS_ENSURE_SUCCESS(rv, rv);
 
           childAsNode = childAsNode->GetNextSibling();
         }
       }
 
       // serialize the end of this node
-      if (aNode != mCommonParent) {
+      if (aNode != mCommonAncestorOfRange) {
         rv = SerializeNodeEnd(*aNode, aString);
         NS_ENSURE_SUCCESS(rv, rv);
       }
     }
   }
   return NS_OK;
 }
 
@@ -958,46 +965,48 @@ nsresult nsDocumentEncoder::SerializeRan
   mRangeContexts.RemoveLastElement();
   return rv;
 }
 
 nsresult nsDocumentEncoder::SerializeRangeToString(nsRange* aRange,
                                                    nsAString& aOutputString) {
   if (!aRange || aRange->Collapsed()) return NS_OK;
 
-  mCommonParent = aRange->GetCommonAncestor();
+  mCommonAncestorOfRange = aRange->GetCommonAncestor();
 
-  if (!mCommonParent) return NS_OK;
+  if (!mCommonAncestorOfRange) {
+    return NS_OK;
+  }
 
   nsINode* startContainer = aRange->GetStartContainer();
   NS_ENSURE_TRUE(startContainer, NS_ERROR_FAILURE);
   int32_t startOffset = aRange->StartOffset();
 
   nsINode* endContainer = aRange->GetEndContainer();
   NS_ENSURE_TRUE(endContainer, NS_ERROR_FAILURE);
   int32_t endOffset = aRange->EndOffset();
 
-  mStartDepth = mEndDepth = 0;
+  mContextInfoDepth = {};
   mCommonAncestors.Clear();
 
   mRangeBoundaryPathsAndOffsets = {};
   auto& startContainerPath = mRangeBoundaryPathsAndOffsets.mStartContainerPath;
   auto& startContainerOffsets =
       mRangeBoundaryPathsAndOffsets.mStartContainerOffsets;
   auto& endContainerPath = mRangeBoundaryPathsAndOffsets.mEndContainerPath;
   auto& endContainerOffsets =
       mRangeBoundaryPathsAndOffsets.mEndContainerOffsets;
 
-  nsContentUtils::GetAncestors(mCommonParent, mCommonAncestors);
+  nsContentUtils::GetAncestors(mCommonAncestorOfRange, mCommonAncestors);
   nsContentUtils::GetAncestorsAndOffsets(
       startContainer, startOffset, &startContainerPath, &startContainerOffsets);
   nsContentUtils::GetAncestorsAndOffsets(
       endContainer, endOffset, &endContainerPath, &endContainerOffsets);
 
-  nsCOMPtr<nsIContent> commonContent = do_QueryInterface(mCommonParent);
+  nsCOMPtr<nsIContent> commonContent = do_QueryInterface(mCommonAncestorOfRange);
   mStartRootIndex = startContainerPath.IndexOf(commonContent);
   mEndRootIndex = endContainerPath.IndexOf(commonContent);
 
   nsresult rv = NS_OK;
 
   rv = SerializeRangeContextStart(mCommonAncestors, aOutputString);
   NS_ENSURE_SUCCESS(rv, rv);
 
@@ -1014,17 +1023,17 @@ nsresult nsDocumentEncoder::SerializeRan
       }
     }
     rv = SerializeNodeStart(*startContainer, startOffset, endOffset,
                             aOutputString);
     NS_ENSURE_SUCCESS(rv, rv);
     rv = SerializeNodeEnd(*startContainer, aOutputString);
     NS_ENSURE_SUCCESS(rv, rv);
   } else {
-    rv = SerializeRangeNodes(aRange, mCommonParent, aOutputString, 0);
+    rv = SerializeRangeNodes(aRange, mCommonAncestorOfRange, aOutputString, 0);
     NS_ENSURE_SUCCESS(rv, rv);
   }
   rv = SerializeRangeContextEnd(aOutputString);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return rv;
 }
 
@@ -1364,20 +1373,22 @@ nsHTMLCopyEncoder::EncodeToStringWithCon
   // leaf of ancestors might be text node.  If so discard it.
   int32_t count = mCommonAncestors.Length();
   int32_t i;
   nsCOMPtr<nsINode> node;
   if (count > 0) node = mCommonAncestors.ElementAt(0);
 
   if (node && IsTextNode(node)) {
     mCommonAncestors.RemoveElementAt(0);
-    // don't forget to adjust range depth info
-    if (mStartDepth) mStartDepth--;
-    if (mEndDepth) mEndDepth--;
-    // and the count
+    if (mContextInfoDepth.mStart) {
+      --mContextInfoDepth.mStart;
+    }
+    if (mContextInfoDepth.mEnd) {
+      --mContextInfoDepth.mEnd;
+    }
     count--;
   }
 
   i = count;
   while (i > 0) {
     node = mCommonAncestors.ElementAt(--i);
     rv = SerializeNodeStart(*node, 0, -1, aContextString);
     NS_ENSURE_SUCCESS(rv, rv);
@@ -1388,19 +1399,19 @@ nsHTMLCopyEncoder::EncodeToStringWithCon
     rv = SerializeNodeEnd(*node, aContextString);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   // encode range info : the start and end depth of the selection, where the
   // depth is distance down in the parent hierarchy.  Later we will need to add
   // leading/trailing whitespace info to this.
   nsAutoString infoString;
-  infoString.AppendInt(mStartDepth);
+  infoString.AppendInt(mContextInfoDepth.mStart);
   infoString.Append(char16_t(','));
-  infoString.AppendInt(mEndDepth);
+  infoString.AppendInt(mContextInfoDepth.mEnd);
   aInfoString = infoString;
 
   return rv;
 }
 
 bool nsHTMLCopyEncoder::IncludeInContext(nsINode* aNode) {
   nsCOMPtr<nsIContent> content(do_QueryInterface(aNode));