Bug 1445569 - part 1: Create WSRunObject constructor which takes |const Editor(Raw)DOMPoint&| instead of |nsINode*| and offset in it r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Thu, 15 Mar 2018 17:56:20 +0900
changeset 768340 5ff91c2ffad82fd63b441d2cdacb545258cd0e68
parent 768008 34999abdc68add4c7f19f062b16824bf8c09a324
child 768341 f98da05ecfd7aed13427c1607bebd90d75d51a7c
push id102858
push usermasayuki@d-toybox.com
push dateFri, 16 Mar 2018 03:57:40 +0000
reviewersm_kato
bugs1445569
milestone61.0a1
Bug 1445569 - part 1: Create WSRunObject constructor which takes |const Editor(Raw)DOMPoint&| instead of |nsINode*| and offset in it r?m_kato The following patches make some WSRunObject users use EditorRawDOMPoint or EditorDOMPoint instead of a pair of nsINode and offset in it. Then, the code becomes too long like: > WSRunObject object(mHTMLEditor, pointToDoSomething.GetContainer(), > pointToDoSomething.Offset()); This is ugly and not easier to read than: > WSRunObject object(mHTMLEditor, pointToDoSomething); So, we should create alternative constructor of WSRunObject first. MozReview-Commit-ID: GiNWRBLl7zB
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/HTMLEditorDataTransfer.cpp
editor/libeditor/WSRunObject.cpp
editor/libeditor/WSRunObject.h
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -1344,18 +1344,17 @@ HTMLEditRules::WillInsertText(EditAction
     if (inString->IsEmpty()) {
       rv = mHTMLEditor->InsertTextImpl(*doc, *inString, pointToInsert.AsRaw());
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
       return NS_OK;
     }
 
-    WSRunObject wsObj(mHTMLEditor,
-                      pointToInsert.GetContainer(), pointToInsert.Offset());
+    WSRunObject wsObj(mHTMLEditor, pointToInsert);
     rv = wsObj.InsertText(*doc, *inString, pointToInsert.AsRaw());
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
     return NS_OK;
   }
 
   // aAction == kInsertText
@@ -1463,18 +1462,17 @@ HTMLEditRules::WillInsertText(EditAction
           }
         } else {
           subStrLen = tString.Length() - oldPos;
           pos = tString.Length();
         }
 
         nsDependentSubstring subStr(tString, oldPos, subStrLen);
         NS_ENSURE_STATE(mHTMLEditor);
-        WSRunObject wsObj(mHTMLEditor, currentPoint.GetContainer(),
-                          currentPoint.Offset());
+        WSRunObject wsObj(mHTMLEditor, currentPoint);
 
         // is it a tab?
         if (subStr.Equals(tabStr)) {
           EditorRawDOMPoint pointAfterInsertedSpaces;
           rv = wsObj.InsertText(*doc, spacesStr, currentPoint.AsRaw(),
                                 &pointAfterInsertedSpaces);
           if (NS_WARN_IF(NS_FAILED(rv))) {
             return rv;
@@ -1852,18 +1850,17 @@ HTMLEditRules::InsertBRElement(Selection
   RefPtr<Element> brElement;
   if (IsPlaintextEditor()) {
     brElement = htmlEditor->CreateBR(aPointToBreak.AsRaw());
     if (NS_WARN_IF(!brElement)) {
       return NS_ERROR_FAILURE;
     }
   } else {
     EditorDOMPoint pointToBreak(aPointToBreak);
-    WSRunObject wsObj(htmlEditor, pointToBreak.GetContainer(),
-                      pointToBreak.Offset());
+    WSRunObject wsObj(htmlEditor, pointToBreak);
     int32_t visOffset = 0;
     WSType wsType;
     nsCOMPtr<nsINode> visNode;
     wsObj.PriorVisibleNode(pointToBreak.GetContainer(), pointToBreak.Offset(),
                            address_of(visNode), &visOffset, &wsType);
     if (wsType & WSType::block) {
       brElementIsAfterBlock = true;
     }
@@ -1918,18 +1915,17 @@ HTMLEditRules::InsertBRElement(Selection
     }
     return NS_OK;
   }
 
   EditorDOMPoint afterBRElement(brElement);
   DebugOnly<bool> advanced = afterBRElement.AdvanceOffset();
   NS_WARNING_ASSERTION(advanced,
     "Failed to advance offset after the new <br> element");
-  WSRunObject wsObj(htmlEditor, afterBRElement.GetContainer(),
-                    afterBRElement.Offset());
+  WSRunObject wsObj(htmlEditor, afterBRElement);
   nsCOMPtr<nsINode> maybeSecondBRNode;
   int32_t visOffset = 0;
   WSType wsType;
   wsObj.NextVisibleNode(afterBRElement.GetContainer(), afterBRElement.Offset(),
                         address_of(maybeSecondBRNode), &visOffset, &wsType);
   if (wsType == WSType::br) {
     // The next thing after the break we inserted is another break.  Move the
     // second break to be the first break's sibling.  This will prevent them
@@ -2878,18 +2874,17 @@ HTMLEditRules::InsertBRIfNeeded(Selectio
   }
 
   // inline elements don't need any br
   if (!IsBlockNode(*atStartOfSelection.GetContainer())) {
     return NS_OK;
   }
 
   // examine selection
-  WSRunObject wsObj(htmlEditor, atStartOfSelection.GetContainer(),
-                    atStartOfSelection.Offset());
+  WSRunObject wsObj(htmlEditor, atStartOfSelection);
   if (((wsObj.mStartReason & WSType::block) ||
        (wsObj.mStartReason & WSType::br)) &&
       (wsObj.mEndReason & WSType::block)) {
     // if we are tucked between block boundaries then insert a br
     // first check that we are allowed to
     if (htmlEditor->CanContainTag(*atStartOfSelection.GetContainer(),
                                   *nsGkAtoms::br)) {
       RefPtr<Element> br =
--- a/editor/libeditor/HTMLEditorDataTransfer.cpp
+++ b/editor/libeditor/HTMLEditorDataTransfer.cpp
@@ -342,18 +342,17 @@ HTMLEditor::DoInsertHTMLWithContext(cons
       GetBetterInsertionPointFor(nodeList[0], GetStartPoint(selection));
     if (NS_WARN_IF(!pointToInsert.IsSet())) {
       return NS_ERROR_FAILURE;
     }
 
     // if there are any invisible br's after our insertion point, remove them.
     // this is because if there is a br at end of what we paste, it will make
     // the invisible br visible.
-    WSRunObject wsObj(this, pointToInsert.GetContainer(),
-                      pointToInsert.Offset());
+    WSRunObject wsObj(this, pointToInsert);
     if (wsObj.mEndReasonNode &&
         TextEditUtils::IsBreak(wsObj.mEndReasonNode) &&
         !IsVisibleBRElement(wsObj.mEndReasonNode)) {
       AutoEditorDOMPointChildInvalidator lockOffset(pointToInsert);
       rv = DeleteNode(wsObj.mEndReasonNode);
       NS_ENSURE_SUCCESS(rv, rv);
     }
 
--- a/editor/libeditor/WSRunObject.cpp
+++ b/editor/libeditor/WSRunObject.cpp
@@ -29,16 +29,28 @@
 #include "nsTextFragment.h"
 
 namespace mozilla {
 
 using namespace dom;
 
 const char16_t kNBSP = 160;
 
+template WSRunObject::WSRunObject(HTMLEditor* aHTMLEditor,
+                                  const EditorDOMPoint& aPoint);
+template WSRunObject::WSRunObject(HTMLEditor* aHTMLEditor,
+                                  const EditorRawDOMPoint& aPoint);
+
+template<typename PT, typename CT>
+WSRunObject::WSRunObject(HTMLEditor* aHTMLEditor,
+                         const EditorDOMPointBase<PT, CT>& aPoint)
+  : WSRunObject(aHTMLEditor, aPoint.GetContainer(), aPoint.Offset())
+{
+}
+
 WSRunObject::WSRunObject(HTMLEditor* aHTMLEditor,
                          nsINode* aNode,
                          int32_t aOffset)
   : mNode(aNode)
   , mOffset(aOffset)
   , mPRE(false)
   , mStartOffset(0)
   , mEndOffset(0)
--- a/editor/libeditor/WSRunObject.h
+++ b/editor/libeditor/WSRunObject.h
@@ -158,16 +158,19 @@ public:
     kBlockEnd,
     kAfterBlock
   };
 
   enum {eBefore = 1};
   enum {eAfter  = 1 << 1};
   enum {eBoth   = eBefore | eAfter};
 
+  template<typename PT, typename CT>
+  WSRunObject(HTMLEditor* aHTMLEditor,
+              const EditorDOMPointBase<PT, CT>& aPoint);
   WSRunObject(HTMLEditor* aHTMLEditor, nsINode* aNode, int32_t aOffset);
   ~WSRunObject();
 
   // ScrubBlockBoundary removes any non-visible whitespace at the specified
   // location relative to a block node.
   static nsresult ScrubBlockBoundary(HTMLEditor* aHTMLEditor,
                                      BlockBoundary aBoundary,
                                      nsINode* aBlock,