Bug 1487301 - Part 1. FindSelectionRoot should return Element. r=masayuki
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Thu, 20 Sep 2018 18:53:35 +0900
changeset 495870 21f4840bb7d391c6fd198d593c677a8e8cdda427
parent 495869 61b6c0110deabb51d9fafec1350c4362be7f9758
child 495871 f25bc716b4cd5a813536b7d469661f33aa004310
push id9984
push userffxbld-merge
push dateMon, 15 Oct 2018 21:07:35 +0000
treeherdermozilla-beta@183d27ea8570 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmasayuki
bugs1487301
milestone64.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 1487301 - Part 1. FindSelectionRoot should return Element. r=masayuki FindSelectionRoot isn't const method and returns already_AddRefed<nsIContent>. But this method doesn't modify any members and nodes, so we can change to const method Also, this method already returns Element, so it shouldn't return nsIContent. Differential Revision: https://phabricator.services.mozilla.com/D6373
editor/libeditor/EditorBase.cpp
editor/libeditor/EditorBase.h
editor/libeditor/HTMLEditor.cpp
editor/libeditor/HTMLEditor.h
--- a/editor/libeditor/EditorBase.cpp
+++ b/editor/libeditor/EditorBase.cpp
@@ -4662,21 +4662,20 @@ EditorBase::HandleInlineSpellCheck(EditS
   }
   return mInlineSpellChecker->SpellCheckAfterEditorChange(
                                 aEditSubAction, aSelection,
                                 previousSelectedNode, previousSelectedOffset,
                                 aStartContainer, aStartOffset, aEndContainer,
                                 aEndOffset);
 }
 
-already_AddRefed<nsIContent>
-EditorBase::FindSelectionRoot(nsINode* aNode)
-{
-  nsCOMPtr<nsIContent> rootContent = GetRoot();
-  return rootContent.forget();
+Element*
+EditorBase::FindSelectionRoot(nsINode* aNode) const
+{
+  return GetRoot();
 }
 
 void
 EditorBase::InitializeSelectionAncestorLimit(Selection& aSelection,
                                              nsIContent& aAncestorLimit)
 {
   aSelection.SetAncestorLimiter(&aAncestorLimit);
 }
--- a/editor/libeditor/EditorBase.h
+++ b/editor/libeditor/EditorBase.h
@@ -654,17 +654,17 @@ public:
 
   /**
    * FindSelectionRoot() returns a selection root of this editor when aNode
    * gets focus.  aNode must be a content node or a document node.  When the
    * target isn't a part of this editor, returns nullptr.  If this is for
    * designMode, you should set the document node to aNode except that an
    * element in the document has focus.
    */
-  virtual already_AddRefed<nsIContent> FindSelectionRoot(nsINode* aNode);
+  virtual Element* FindSelectionRoot(nsINode* aNode) const;
 
   /**
    * This method has to be called by EditorEventListener::Focus.
    * All actions that have to be done when the editor is focused needs to be
    * added here.
    */
   void OnFocus(dom::EventTarget* aFocusEventTarget);
 
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -457,62 +457,58 @@ HTMLEditor::UpdateRootElement()
       // If there is no HTML body element,
       // we should use the document root element instead.
       mRootElement = doc->GetDocumentElement();
     }
     // else leave it null, for lack of anything better.
   }
 }
 
-already_AddRefed<nsIContent>
-HTMLEditor::FindSelectionRoot(nsINode* aNode)
+Element*
+HTMLEditor::FindSelectionRoot(nsINode* aNode) const
 {
   if (NS_WARN_IF(!aNode)) {
     return nullptr;
   }
 
   MOZ_ASSERT(aNode->IsDocument() || aNode->IsContent(),
              "aNode must be content or document node");
 
-  nsCOMPtr<nsIDocument> doc = aNode->GetComposedDoc();
+  nsIDocument* doc = aNode->GetComposedDoc();
   if (!doc) {
     return nullptr;
   }
 
-  nsCOMPtr<nsIContent> content;
   if (aNode->IsInUncomposedDoc() &&
       (doc->HasFlag(NODE_IS_EDITABLE) || !aNode->IsContent())) {
-    content = doc->GetRootElement();
-    return content.forget();
-  }
-  content = aNode->AsContent();
+    return doc->GetRootElement();
+  }
 
   // XXX If we have readonly flag, shouldn't return the element which has
   // contenteditable="true"?  However, such case isn't there without chrome
   // permission script.
   if (IsReadonly()) {
     // We still want to allow selection in a readonly editor.
-    content = GetRoot();
-    return content.forget();
-  }
-
+    return GetRoot();
+  }
+
+  nsIContent* content = aNode->AsContent();
   if (!content->HasFlag(NODE_IS_EDITABLE)) {
     // If the content is in read-write state but is not editable itself,
     // return it as the selection root.
     if (content->IsElement() &&
         content->AsElement()->State().HasState(NS_EVENT_STATE_MOZ_READWRITE)) {
-      return content.forget();
+      return content->AsElement();
     }
     return nullptr;
   }
 
   // For non-readonly editors we want to find the root of the editable subtree
   // containing aContent.
-  content = content->GetEditingHost();
-  return content.forget();
+  return content->GetEditingHost();
 }
 
 void
 HTMLEditor::CreateEventListeners()
 {
   // Don't create the handler twice
   if (!mEventListener) {
     mEventListener = new HTMLEditorEventListener();
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -142,18 +142,17 @@ public:
                             int32_t* outNumTestsFailed) override;
 
   virtual nsresult HandleKeyPressEvent(
                      WidgetKeyboardEvent* aKeyboardEvent) override;
   virtual nsIContent* GetFocusedContent() override;
   virtual already_AddRefed<nsIContent> GetFocusedContentForIME() override;
   virtual bool IsActiveInDOMWindow() override;
   virtual dom::EventTarget* GetDOMEventTarget() override;
-  virtual already_AddRefed<nsIContent> FindSelectionRoot(
-                                         nsINode *aNode) override;
+  virtual Element* FindSelectionRoot(nsINode *aNode) const override;
   virtual bool IsAcceptableInputEvent(WidgetGUIEvent* aGUIEvent) override;
   virtual nsresult GetPreferredIMEState(widget::IMEState* aState) override;
 
   /**
    * PasteAsQuotationAsAction() pastes content in clipboard with newly created
    * blockquote element.  If the editor is in plaintext mode, will paste the
    * content with appending ">" to start of each line.
    *