From dd554016e6e50881b4e72b1f7dca0df35e5a7b07 Mon Sep 17 00:00:00 2001
From: Masayuki Nakano <masayuki@d-toybox.com>
Date: Fri, 27 May 2022 04:09:19 +0000
Subject: [PATCH] Bug 1770874 - part 8: Make `EditorBase::OnBlur` temporarily a
 virtual method r=m_kato

Similar to the previous patch, we should split the method into `TextEditor`
and `HTMLEditor`.

Differential Revision: https://phabricator.services.mozilla.com/D147144
---
 editor/libeditor/EditorBase.cpp | 29 -----------------------------
 editor/libeditor/EditorBase.h   |  2 +-
 editor/libeditor/HTMLEditor.cpp | 28 ++++++++++++++++++++++++++++
 editor/libeditor/HTMLEditor.h   |  1 +
 editor/libeditor/TextEditor.cpp |  7 +++++++
 editor/libeditor/TextEditor.h   |  2 ++
 6 files changed, 39 insertions(+), 30 deletions(-)

diff --git a/editor/libeditor/EditorBase.cpp b/editor/libeditor/EditorBase.cpp
index 773a6822c6f05..3cca74e4e8fcf 100644
--- a/editor/libeditor/EditorBase.cpp
+++ b/editor/libeditor/EditorBase.cpp
@@ -5689,35 +5689,6 @@ nsresult EditorBase::OnFocus(const nsINode& aOriginalEventTargetNode) {
   return NS_OK;
 }
 
-nsresult EditorBase::OnBlur(const EventTarget* aEventTarget) {
-  // check if something else is focused. If another element is focused, then
-  // we should not change the selection.
-  nsFocusManager* focusManager = nsFocusManager::GetFocusManager();
-  if (MOZ_UNLIKELY(!focusManager)) {
-    return NS_OK;
-  }
-
-  // If another element already has focus, we should not maintain the selection
-  // because we may not have the rights doing it.
-  if (focusManager->GetFocusedElement()) {
-    return NS_OK;
-  }
-
-  // If it's in the designMode, and blur occurs, the target must be the
-  // document node.  If a blur event is fired and the target is an element, it
-  // must be delayed blur event at initializing the `HTMLEditor`.
-  // TODO: Add automated tests for checking the case that the target node
-  //       is in a shadow DOM tree whose host is in design mode.
-  if (IsHTMLEditor() && AsHTMLEditor()->IsInDesignMode() &&
-      Element::FromEventTargetOrNull(aEventTarget)) {
-    return NS_OK;
-  }
-  nsresult rv = FinalizeSelection();
-  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
-                       "EditorBase::FinalizeSelection() failed");
-  return rv;
-}
-
 void EditorBase::HideCaret(bool aHide) {
   if (mHidingCaret == aHide) {
     return;
diff --git a/editor/libeditor/EditorBase.h b/editor/libeditor/EditorBase.h
index 3094a66a9b9ad..1f153b8b51089 100644
--- a/editor/libeditor/EditorBase.h
+++ b/editor/libeditor/EditorBase.h
@@ -607,7 +607,7 @@ class EditorBase : public nsIEditor,
    *
    * @param aEventTarget        The event target of the blur event.
    */
-  nsresult OnBlur(const dom::EventTarget* aEventTarget);
+  virtual nsresult OnBlur(const dom::EventTarget* aEventTarget) = 0;
 
   /** Resyncs spellchecking state (enabled/disabled).  This should be called
    * when anything that affects spellchecking state changes, such as the
diff --git a/editor/libeditor/HTMLEditor.cpp b/editor/libeditor/HTMLEditor.cpp
index 183f0c577b3b5..52ac883d17a70 100644
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -659,6 +659,34 @@ nsresult HTMLEditor::OnFocus(const nsINode& aOriginalEventTargetNode) {
   return EditorBase::OnFocus(aOriginalEventTargetNode);
 }
 
+nsresult HTMLEditor::OnBlur(const EventTarget* aEventTarget) {
+  // check if something else is focused. If another element is focused, then
+  // we should not change the selection.
+  nsFocusManager* focusManager = nsFocusManager::GetFocusManager();
+  if (MOZ_UNLIKELY(!focusManager)) {
+    return NS_OK;
+  }
+
+  // If another element already has focus, we should not maintain the selection
+  // because we may not have the rights doing it.
+  if (focusManager->GetFocusedElement()) {
+    return NS_OK;
+  }
+
+  // If it's in the designMode, and blur occurs, the target must be the
+  // document node.  If a blur event is fired and the target is an element, it
+  // must be delayed blur event at initializing the `HTMLEditor`.
+  // TODO: Add automated tests for checking the case that the target node
+  //       is in a shadow DOM tree whose host is in design mode.
+  if (IsInDesignMode() && Element::FromEventTargetOrNull(aEventTarget)) {
+    return NS_OK;
+  }
+  nsresult rv = FinalizeSelection();
+  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
+                       "EditorBase::FinalizeSelection() failed");
+  return rv;
+}
+
 Element* HTMLEditor::FindSelectionRoot(const nsINode& aNode) const {
   MOZ_ASSERT(aNode.IsDocument() || aNode.IsContent(),
              "aNode must be content or document node");
diff --git a/editor/libeditor/HTMLEditor.h b/editor/libeditor/HTMLEditor.h
index 603b0ba2fb7f9..1872684ed50da 100644
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -202,6 +202,7 @@ class HTMLEditor final : public EditorBase,
   nsresult GetPreferredIMEState(widget::IMEState* aState) final;
   MOZ_CAN_RUN_SCRIPT nsresult
   OnFocus(const nsINode& aOriginalEventTargetNode) final;
+  nsresult OnBlur(const dom::EventTarget* aEventTarget) final;
 
   /**
    * GetBackgroundColorState() returns what the background color of the
diff --git a/editor/libeditor/TextEditor.cpp b/editor/libeditor/TextEditor.cpp
index 3bb042a2bb935..79d853f28da81 100644
--- a/editor/libeditor/TextEditor.cpp
+++ b/editor/libeditor/TextEditor.cpp
@@ -683,6 +683,13 @@ nsresult TextEditor::OnFocus(const nsINode& aOriginalEventTargetNode) {
   return EditorBase::OnFocus(aOriginalEventTargetNode);
 }
 
+nsresult TextEditor::OnBlur(const EventTarget* aEventTarget) {
+  nsresult rv = FinalizeSelection();
+  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
+                       "EditorBase::FinalizeSelection() failed");
+  return rv;
+}
+
 nsresult TextEditor::SetAttributeOrEquivalent(Element* aElement,
                                               nsAtom* aAttribute,
                                               const nsAString& aValue,
diff --git a/editor/libeditor/TextEditor.h b/editor/libeditor/TextEditor.h
index 7b06e3f301753..8a0db195478d8 100644
--- a/editor/libeditor/TextEditor.h
+++ b/editor/libeditor/TextEditor.h
@@ -151,6 +151,8 @@ class TextEditor final : public EditorBase,
   MOZ_CAN_RUN_SCRIPT nsresult
   OnFocus(const nsINode& aOriginalEventTargetNode) final;
 
+  nsresult OnBlur(const dom::EventTarget* aEventTarget) final;
+
   /**
    * The maximum number of characters allowed.
    *   default: -1 (unlimited).
-- 
GitLab