Bug 1463327 - part 2: Change scope of some methods of TextEditor which won't be called by non-helper classes of editing to protected r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Tue, 22 May 2018 16:40:44 +0900
changeset 799309 337b122bd4224adf7c0015334785959de58c7e5c
parent 799308 c056dfaaa95630fe2fd0bfd9ddcd3db0f012b110
child 799310 9c29466fcf3168465c53eac2ea89e45a3af6ad87
push id111000
push usermasayuki@d-toybox.com
push dateThu, 24 May 2018 12:43:37 +0000
reviewersm_kato
bugs1463327
milestone62.0a1
Bug 1463327 - part 2: Change scope of some methods of TextEditor which won't be called by non-helper classes of editing to protected r?m_kato TextEditor has 2 type of public methods. One is true-public methods. I.e., they should be able to be called by anybody. E.g., command handlers, event listeners, or JS via nsIEditor interface. The other is semi-public methods. They are not called by the above examples but called by other classes which are helper classes to handle edit actions. E.g., TextEditRules, HTMLEditRules, HTMLEditUtils, CSSEditUtils and Transaction classes. When we will implement InputEvent.inputType, we need to create new stack class and create its instance at every true-public methods to manage current inputType (like TextEditRules::AutoSafeEditorData). Therefore, it should not happen that new code starts to call semi-public methods without the new stack class instance. For preventing this issue, we should make TextEditor have only the true-public methods as public. The other public methods should be protected and their users should be friend classes. Then, we can protect such method from external classes. Note that this patch just moves the methods without any changes in TextEditor.h. MozReview-Commit-ID: Db3H6d1V8IU
editor/libeditor/HTMLEditor.cpp
editor/libeditor/HTMLEditor.h
editor/libeditor/HTMLEditorDataTransfer.cpp
editor/libeditor/TextEditor.cpp
editor/libeditor/TextEditor.h
editor/libeditor/TextEditorDataTransfer.cpp
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -529,17 +529,17 @@ HTMLEditor::SetFlags(uint32_t aFlags)
   // Sets mCSSAware to correspond to aFlags. This toggles whether CSS is
   // used to style elements in the editor. Note that the editor is only CSS
   // aware by default in Composer and in the mail editor.
   mCSSAware = !NoCSS() && !IsMailEditor();
 
   return NS_OK;
 }
 
-NS_IMETHODIMP
+nsresult
 HTMLEditor::InitRules()
 {
   if (!mRules) {
     // instantiate the rules for the html editor
     mRules = new HTMLEditRules();
   }
   return mRules->Init(this);
 }
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -630,17 +630,17 @@ protected:
     RefPtr<HTMLEditor> mHTMLEditor;
     bool mIsSafe;
     nsCOMPtr<nsIDocument> mSourceDoc;
     nsCOMPtr<nsINode> mDestinationNode;
     int32_t mDestOffset;
     bool mDoDeleteSelection;
   };
 
-  NS_IMETHOD InitRules() override;
+  virtual nsresult InitRules() override;
 
   virtual void CreateEventListeners() override;
   virtual nsresult InstallEventListeners() override;
   virtual void RemoveEventListeners() override;
 
   bool ShouldReplaceRootElement();
   void NotifyRootChanged();
   Element* GetBodyElement();
@@ -812,17 +812,17 @@ protected:
                         bool aIsSafe,
                         nsIDocument* aSourceDoc,
                         nsINode* aDestinationNode,
                         int32_t aDestOffset,
                         bool aDoDeleteSelection);
 
   // factored methods for handling insertion of data from transferables
   // (drag&drop or clipboard)
-  NS_IMETHOD PrepareTransferable(nsITransferable** transferable) override;
+  virtual nsresult PrepareTransferable(nsITransferable** transferable) override;
   nsresult PrepareHTMLTransferable(nsITransferable** transferable);
   nsresult InsertFromTransferable(nsITransferable* transferable,
                                     nsIDocument* aSourceDoc,
                                     const nsAString& aContextStr,
                                     const nsAString& aInfoStr,
                                     bool havePrivateHTMLFlavor,
                                     bool aDoDeleteSelection);
   virtual nsresult InsertFromDataTransfer(dom::DataTransfer* aDataTransfer,
--- a/editor/libeditor/HTMLEditorDataTransfer.cpp
+++ b/editor/libeditor/HTMLEditorDataTransfer.cpp
@@ -738,17 +738,17 @@ HTMLEditor::StripFormattingNodes(nsICont
       nsresult rv = StripFormattingNodes(*child, aListOnly);
       NS_ENSURE_SUCCESS(rv, rv);
       child = previous.forget();
     }
   }
   return NS_OK;
 }
 
-NS_IMETHODIMP
+nsresult
 HTMLEditor::PrepareTransferable(nsITransferable** aTransferable)
 {
   return NS_OK;
 }
 
 nsresult
 HTMLEditor::PrepareHTMLTransferable(nsITransferable** aTransferable)
 {
--- a/editor/libeditor/TextEditor.cpp
+++ b/editor/libeditor/TextEditor.cpp
@@ -323,17 +323,17 @@ TextEditor::UpdateMetaCharset(nsIDocumen
                                   Substring(originalStart, start) +
                                     charsetEquals +
                                     NS_ConvertASCIItoUTF16(aCharacterSet));
     return NS_SUCCEEDED(rv);
   }
   return false;
 }
 
-NS_IMETHODIMP
+nsresult
 TextEditor::InitRules()
 {
   if (!mRules) {
     // instantiate the rules for this text editor
     mRules = new TextEditRules();
   }
   return mRules->Init(this);
 }
--- a/editor/libeditor/TextEditor.h
+++ b/editor/libeditor/TextEditor.h
@@ -17,17 +17,16 @@
 
 class nsIContent;
 class nsIDocumentEncoder;
 class nsIOutputStream;
 class nsISelectionController;
 class nsITransferable;
 
 namespace mozilla {
-
 class AutoEditInitRulesTrigger;
 enum class EditAction : int32_t;
 
 namespace dom {
 class DragEvent;
 class Selection;
 } // namespace dom
 
@@ -80,161 +79,72 @@ public:
   /** Can we paste |aTransferable| or, if |aTransferable| is null, will a call
     * to pasteTransferable later possibly succeed if given an instance of
     * nsITransferable then? True if the doc is modifiable, and, if
     * |aTransfeable| is non-null, we have pasteable data in |aTransfeable|.
     */
   virtual bool CanPasteTransferable(nsITransferable* aTransferable);
 
   // Overrides of EditorBase
-  virtual nsresult RemoveAttributeOrEquivalent(
-                     Element* aElement,
-                     nsAtom* aAttribute,
-                     bool aSuppressTransaction) override;
-  virtual nsresult SetAttributeOrEquivalent(Element* aElement,
-                                            nsAtom* aAttribute,
-                                            const nsAString& aValue,
-                                            bool aSuppressTransaction) override;
-  using EditorBase::RemoveAttributeOrEquivalent;
-  using EditorBase::SetAttributeOrEquivalent;
-
   virtual nsresult Init(nsIDocument& aDoc, Element* aRoot,
                         nsISelectionController* aSelCon, uint32_t aFlags,
                         const nsAString& aValue) override;
 
   nsresult DocumentIsEmpty(bool* aIsEmpty);
 
-  /**
-   * All editor operations which alter the doc should be prefaced
-   * with a call to StartOperation, naming the action and direction.
-   */
-  virtual nsresult StartOperation(EditAction opID,
-                                  nsIEditor::EDirection aDirection) override;
-
-  /**
-   * All editor operations which alter the doc should be followed
-   * with a call to EndOperation.
-   */
-  virtual nsresult EndOperation() override;
-
-  /**
-   * Make the given selection span the entire document.
-   */
-  virtual nsresult SelectEntireDocument(Selection* aSelection) override;
-
   virtual nsresult HandleKeyPressEvent(
                      WidgetKeyboardEvent* aKeyboardEvent) override;
 
   virtual dom::EventTarget* GetDOMEventTarget() override;
 
   virtual already_AddRefed<nsIContent> GetInputEventTargetContent() override;
 
   /**
+   * InsertTextAsAction() inserts aStringToInsert at selection.
+   * Although this method is implementation of nsIPlaintextEditor.insertText(),
+   * this treats the input is an edit action.
+   *
+   * @param aStringToInsert     The string to insert.
+   */
+  nsresult InsertTextAsAction(const nsAString& aStringToInsert);
+
+  /**
    * DeleteSelectionAsAction() removes selection content or content around
    * caret with transactions.  This should be used for handling it as an
    * edit action.
    *
    * @param aDirection          How much range should be removed.
    * @param aStripWrappers      Whether the parent blocks should be removed
    *                            when they become empty.
    */
   nsresult DeleteSelectionAsAction(EDirection aDirection,
                                    EStripWrappers aStripWrappers);
 
   /**
-   * DeleteSelectionWithTransaction() removes selected content or content
-   * around caret with transactions.
-   *
-   * @param aDirection          How much range should be removed.
-   * @param aStripWrappers      Whether the parent blocks should be removed
-   *                            when they become empty.
-   */
-  virtual nsresult
-  DeleteSelectionWithTransaction(EDirection aAction,
-                                 EStripWrappers aStripWrappers);
-
-  /**
-   * OnInputText() is called when user inputs text with keyboard or something.
-   *
-   * @param aStringToInsert     The string to insert.
-   */
-  nsresult OnInputText(const nsAString& aStringToInsert);
-
-  /**
-   * OnInputParagraphSeparator() is called when user tries to separate current
-   * paragraph with Enter key press or something.
-   */
-  nsresult OnInputParagraphSeparator();
-
-  /**
-   * InsertTextAsAction() inserts aStringToInsert at selection.
-   * Although this method is implementation of nsIPlaintextEditor.insertText(),
-   * this treats the input is an edit action.
-   *
-   * @param aStringToInsert     The string to insert.
-   */
-  nsresult InsertTextAsAction(const nsAString& aStringToInsert);
-
-  /**
-   * InsertParagraphSeparatorAsAction() inserts a line break if it's TextEditor
-   * or inserts new paragraph if it's HTMLEditor and it's possible.
-   * Although, this method is implementation of
-   * nsIPlaintextEditor.insertLineBreak(), this treats the input is an edit
-   * action.
-   */
-  nsresult InsertParagraphSeparatorAsAction();
-
-  nsresult InsertTextAt(const nsAString& aStringToInsert,
-                        nsINode* aDestinationNode,
-                        int32_t aDestOffset,
-                        bool aDoDeleteSelection);
-
-  virtual nsresult InsertFromDataTransfer(dom::DataTransfer* aDataTransfer,
-                                          int32_t aIndex,
-                                          nsIDocument* aSourceDoc,
-                                          nsINode* aDestinationNode,
-                                          int32_t aDestOffset,
-                                          bool aDoDeleteSelection) override;
-
-  virtual nsresult InsertFromDrop(dom::DragEvent* aDropEvent) override;
-
-  /**
-   * Extends the selection for given deletion operation
-   * If done, also update aAction to what's actually left to do after the
-   * extension.
-   */
-  nsresult ExtendSelectionForDelete(Selection* aSelection,
-                                    nsIEditor::EDirection* aAction);
-
-  /**
-   * Return true if the data is safe to insert as the source and destination
-   * principals match, or we are in a editor context where this doesn't matter.
-   * Otherwise, the data must be sanitized first.
-   */
-  bool IsSafeToInsertData(nsIDocument* aSourceDoc);
-
-  static void GetDefaultEditorPrefs(int32_t& aNewLineHandling,
-                                    int32_t& aCaretStyle);
-
-  /**
     * The maximum number of characters allowed.
     *   default: -1 (unlimited).
     */
   int32_t MaxTextLength() const { return mMaxTextLength; }
   void SetMaxTextLength(int32_t aLength) { mMaxTextLength = aLength; }
 
   /**
    * Replace existed string with a string.
    * This is fast path to replace all string when using single line control.
    *
    * @ param aString   the string to be set
    */
   nsresult SetText(const nsAString& aString);
 
   /**
+   * OnInputParagraphSeparator() is called when user tries to separate current
+   * paragraph with Enter key press or something.
+   */
+  nsresult OnInputParagraphSeparator();
+
+  /**
    * OnCompositionStart() is called when editor receives eCompositionStart
    * event which should be handled in this editor.
    */
   nsresult OnCompositionStart(WidgetCompositionEvent& aCompositionStartEvent);
 
   /**
    * OnCompositionChange() is called when editor receives an eCompositioChange
    * event which should be handled in this editor.
@@ -247,27 +157,42 @@ public:
 
   /**
    * OnCompositionEnd() is called when editor receives an eCompositionChange
    * event and it's followed by eCompositionEnd event and after
    * OnCompositionChange() is called.
    */
   void OnCompositionEnd(WidgetCompositionEvent& aCompositionEndEvent);
 
-protected:
-  virtual ~TextEditor();
+protected: // May be called by friends.
+  // Overrides of EditorBase
+  virtual nsresult RemoveAttributeOrEquivalent(
+                     Element* aElement,
+                     nsAtom* aAttribute,
+                     bool aSuppressTransaction) override;
+  virtual nsresult SetAttributeOrEquivalent(Element* aElement,
+                                            nsAtom* aAttribute,
+                                            const nsAString& aValue,
+                                            bool aSuppressTransaction) override;
+  using EditorBase::RemoveAttributeOrEquivalent;
+  using EditorBase::SetAttributeOrEquivalent;
 
-  NS_IMETHOD InitRules();
-  void BeginEditorInit();
-  nsresult EndEditorInit();
+  virtual nsresult InsertFromDrop(dom::DragEvent* aDropEvent) override;
 
-  already_AddRefed<nsIDocumentEncoder> GetAndInitDocEncoder(
-                                         const nsAString& aFormatType,
-                                         uint32_t aFlags,
-                                         const nsACString& aCharset);
+  /**
+   * DeleteSelectionWithTransaction() removes selected content or content
+   * around caret with transactions.
+   *
+   * @param aDirection          How much range should be removed.
+   * @param aStripWrappers      Whether the parent blocks should be removed
+   *                            when they become empty.
+   */
+  virtual nsresult
+  DeleteSelectionWithTransaction(EDirection aAction,
+                                 EStripWrappers aStripWrappers);
 
   /**
    * InsertBrElementWithTransaction() creates a <br> element and inserts it
    * before aPointToInsert.  Then, tries to collapse selection at or after the
    * new <br> node if aSelect is not eNone.
    *
    * @param aSelection          The selection of this editor.
    * @param aPointToInsert      The DOM point where should be <br> node inserted
@@ -283,20 +208,99 @@ protected:
   template<typename PT, typename CT>
   already_AddRefed<Element>
   InsertBrElementWithTransaction(
     Selection& aSelection,
     const EditorDOMPointBase<PT, CT>& aPointToInsert,
     EDirection aSelect = eNone);
 
   /**
+   * Extends the selection for given deletion operation
+   * If done, also update aAction to what's actually left to do after the
+   * extension.
+   */
+  nsresult ExtendSelectionForDelete(Selection* aSelection,
+                                    nsIEditor::EDirection* aAction);
+
+  static void GetDefaultEditorPrefs(int32_t& aNewLineHandling,
+                                    int32_t& aCaretStyle);
+
+protected: // Called by helper classes.
+  /**
+   * All editor operations which alter the doc should be prefaced
+   * with a call to StartOperation, naming the action and direction.
+   */
+  virtual nsresult StartOperation(EditAction opID,
+                                  nsIEditor::EDirection aDirection) override;
+
+  /**
+   * All editor operations which alter the doc should be followed
+   * with a call to EndOperation.
+   */
+  virtual nsresult EndOperation() override;
+
+  void BeginEditorInit();
+  nsresult EndEditorInit();
+
+protected: // Shouldn't be used by friend classes
+  virtual ~TextEditor();
+
+  /**
+   * Make the given selection span the entire document.
+   */
+  virtual nsresult SelectEntireDocument(Selection* aSelection) override;
+
+  /**
+   * OnInputText() is called when user inputs text with keyboard or something.
+   *
+   * @param aStringToInsert     The string to insert.
+   */
+  nsresult OnInputText(const nsAString& aStringToInsert);
+
+  /**
+   * InsertParagraphSeparatorAsAction() inserts a line break if it's TextEditor
+   * or inserts new paragraph if it's HTMLEditor and it's possible.
+   * Although, this method is implementation of
+   * nsIPlaintextEditor.insertLineBreak(), this treats the input is an edit
+   * action.
+   */
+  nsresult InsertParagraphSeparatorAsAction();
+
+  nsresult InsertTextAt(const nsAString& aStringToInsert,
+                        nsINode* aDestinationNode,
+                        int32_t aDestOffset,
+                        bool aDoDeleteSelection);
+
+  virtual nsresult InsertFromDataTransfer(dom::DataTransfer* aDataTransfer,
+                                          int32_t aIndex,
+                                          nsIDocument* aSourceDoc,
+                                          nsINode* aDestinationNode,
+                                          int32_t aDestOffset,
+                                          bool aDoDeleteSelection) override;
+
+  /**
+   * Return true if the data is safe to insert as the source and destination
+   * principals match, or we are in a editor context where this doesn't matter.
+   * Otherwise, the data must be sanitized first.
+   */
+  bool IsSafeToInsertData(nsIDocument* aSourceDoc);
+
+  virtual nsresult InitRules();
+
+  already_AddRefed<nsIDocumentEncoder> GetAndInitDocEncoder(
+                                         const nsAString& aFormatType,
+                                         uint32_t aFlags,
+                                         const nsACString& aCharset);
+
+  /**
    * Factored methods for handling insertion of data from transferables
    * (drag&drop or clipboard).
    */
-  NS_IMETHOD PrepareTransferable(nsITransferable** transferable);
+  virtual nsresult PrepareTransferable(nsITransferable** transferable);
+
   nsresult InsertTextFromTransferable(nsITransferable* transferable);
 
   /**
    * DeleteSelectionAndCreateElement() creates a element whose name is aTag.
    * And insert it into the DOM tree after removing the selected content.
    *
    * @param aTag                The element name to be created.
    * @return                    Created new element.
@@ -349,17 +353,16 @@ protected:
   nsString mCachedDocumentEncoderType;
   int32_t mWrapColumn;
   int32_t mMaxTextLength;
   int32_t mInitTriggerCounter;
   int32_t mNewlineHandling;
   int32_t mCaretStyle;
 
   friend class AutoEditInitRulesTrigger;
-  friend class HTMLEditRules;
   friend class TextEditRules;
 };
 
 } // namespace mozilla
 
 mozilla::TextEditor*
 nsIEditor::AsTextEditor()
 {
--- a/editor/libeditor/TextEditorDataTransfer.cpp
+++ b/editor/libeditor/TextEditorDataTransfer.cpp
@@ -41,17 +41,17 @@
 
 class nsILoadContext;
 class nsISupports;
 
 namespace mozilla {
 
 using namespace dom;
 
-NS_IMETHODIMP
+nsresult
 TextEditor::PrepareTransferable(nsITransferable** transferable)
 {
   // Create generic Transferable for getting the data
   nsresult rv = CallCreateInstance("@mozilla.org/widget/transferable;1", transferable);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Get the nsITransferable interface for getting the data from the clipboard
   if (transferable) {