From 9f1aec970a04aa2e93a58fd2d47abdea86dac544 Mon Sep 17 00:00:00 2001 From: Masayuki Nakano Date: Thu, 22 Jun 2017 15:21:31 +0900 Subject: [PATCH] Bug 1374207 - part4: Element classes should use TextEditor class instead of nIEditor r=smaug Unfortunately, nsGenericHTMLElement::GetAssociatedEditor() cannot use concrete classes because it may return nsIEditor which is set via nsIDocShell.editor. The editor set to nsIDocShell may be implemented by JS since nsIEditor isn't marked as builtinclass. MozReview-Commit-ID: 6GY9LOYp4hM --HG-- extra : rebase_source : 3e0464067b30daf8254805458c5358d7ea644be8 --- dom/base/Element.cpp | 6 +++--- dom/base/Element.h | 5 +++-- dom/base/FragmentOrElement.cpp | 6 +++--- dom/base/nsINode.cpp | 26 ++++++++++++++++---------- dom/base/nsINode.h | 5 +++-- dom/html/HTMLBodyElement.cpp | 8 +++++--- dom/html/HTMLInputElement.cpp | 11 +++++++++-- dom/html/HTMLInputElement.h | 11 +++++++++-- dom/html/HTMLTextAreaElement.cpp | 4 ++-- dom/html/HTMLTextAreaElement.h | 7 +++++-- dom/html/nsGenericHTMLElement.cpp | 24 ++++++++++-------------- dom/html/nsGenericHTMLElement.h | 6 +----- dom/html/nsITextControlElement.h | 4 ++-- 13 files changed, 71 insertions(+), 52 deletions(-) diff --git a/dom/base/Element.cpp b/dom/base/Element.cpp index 3c6b7a365575..fcd62a077c24 100644 --- a/dom/base/Element.cpp +++ b/dom/base/Element.cpp @@ -67,6 +67,7 @@ #include "mozilla/EventStates.h" #include "mozilla/InternalMutationEvent.h" #include "mozilla/MouseEvents.h" +#include "mozilla/TextEditor.h" #include "mozilla/TextEvents.h" #include "nsNodeUtils.h" #include "mozilla/dom/DirectionalityUtils.h" @@ -103,7 +104,6 @@ #include "nsICategoryManager.h" #include "nsIDOMDocumentType.h" #include "nsGenericHTMLElement.h" -#include "nsIEditor.h" #include "nsContentCreatorFunctions.h" #include "nsIControllers.h" #include "nsView.h" @@ -3942,8 +3942,8 @@ Element::InsertAdjacentText( InsertAdjacent(aWhere, textNode, aError); } -nsIEditor* -Element::GetEditorInternal() +TextEditor* +Element::GetTextEditorInternal() { nsCOMPtr textCtrl = do_QueryInterface(this); return textCtrl ? textCtrl->GetTextEditor() : nullptr; diff --git a/dom/base/Element.h b/dom/base/Element.h index 9fc101003f11..734206ab8d6e 100644 --- a/dom/base/Element.h +++ b/dom/base/Element.h @@ -62,6 +62,7 @@ class nsDOMStringMap; namespace mozilla { class DeclarationBlock; +class TextEditor; namespace dom { struct AnimationFilter; struct ScrollIntoViewOptions; @@ -1265,9 +1266,9 @@ public: nsINode* GetScopeChainParent() const override; /** - * Locate an nsIEditor rooted at this content node, if there is one. + * Locate a TextEditor rooted at this content node, if there is one. */ - nsIEditor* GetEditorInternal(); + mozilla::TextEditor* GetTextEditorInternal(); /** * Helper method for NS_IMPL_BOOL_ATTR macro. diff --git a/dom/base/FragmentOrElement.cpp b/dom/base/FragmentOrElement.cpp index e4415ca060d4..26d420f1ed2b 100644 --- a/dom/base/FragmentOrElement.cpp +++ b/dom/base/FragmentOrElement.cpp @@ -24,6 +24,7 @@ #include "mozilla/EventListenerManager.h" #include "mozilla/EventStates.h" #include "mozilla/ServoRestyleManager.h" +#include "mozilla/TextEditor.h" #include "mozilla/URLExtraData.h" #include "mozilla/dom/Attr.h" #include "nsDOMAttributeMap.h" @@ -93,7 +94,6 @@ #include "nsNodeInfoManager.h" #include "nsICategoryManager.h" #include "nsGenericHTMLElement.h" -#include "nsIEditor.h" #include "nsContentCreatorFunctions.h" #include "nsIControllers.h" #include "nsView.h" @@ -2289,8 +2289,8 @@ FragmentOrElement::GetMarkup(bool aIncludeSelf, nsAString& aMarkup) if (IsEditable()) { nsCOMPtr elem = do_QueryInterface(this); - nsIEditor* editor = elem ? elem->GetEditorInternal() : nullptr; - if (editor && editor->OutputsMozDirty()) { + TextEditor* textEditor = elem ? elem->GetTextEditorInternal() : nullptr; + if (textEditor && textEditor->OutputsMozDirty()) { flags &= ~nsIDocumentEncoder::OutputIgnoreMozDirty; } } diff --git a/dom/base/nsINode.cpp b/dom/base/nsINode.cpp index 0133e849763e..e8978726bda2 100644 --- a/dom/base/nsINode.cpp +++ b/dom/base/nsINode.cpp @@ -22,6 +22,7 @@ #include "mozilla/MemoryReporting.h" #include "mozilla/ServoBindings.h" #include "mozilla/Telemetry.h" +#include "mozilla/TextEditor.h" #include "mozilla/TimeStamp.h" #include "mozilla/css/StyleRule.h" #include "mozilla/dom/Element.h" @@ -230,24 +231,29 @@ static nsIContent* GetEditorRootContent(nsIEditor* aEditor) } nsIContent* -nsINode::GetTextEditorRootContent(nsIEditor** aEditor) +nsINode::GetTextEditorRootContent(TextEditor** aTextEditor) { - if (aEditor) - *aEditor = nullptr; + if (aTextEditor) { + *aTextEditor = nullptr; + } for (nsINode* node = this; node; node = node->GetParentNode()) { if (!node->IsElement() || !node->IsHTMLElement()) continue; - nsCOMPtr editor = - static_cast(node)->GetEditorInternal(); - if (!editor) + RefPtr textEditor = + static_cast(node)->GetTextEditorInternal(); + if (!textEditor) { continue; + } - nsIContent* rootContent = GetEditorRootContent(editor); - if (aEditor) - editor.swap(*aEditor); - return rootContent; + MOZ_ASSERT(!textEditor->AsHTMLEditor(), + "If it were an HTML editor, needs to use GetRootElement()"); + Element* rootElement = textEditor->GetRoot(); + if (aTextEditor) { + textEditor.forget(aTextEditor); + } + return rootElement; } return nullptr; } diff --git a/dom/base/nsINode.h b/dom/base/nsINode.h index 1e6b9b7d2668..c0f499b0dcc5 100644 --- a/dom/base/nsINode.h +++ b/dom/base/nsINode.h @@ -41,7 +41,6 @@ class nsIContent; class nsIDocument; class nsIDOMElement; class nsIDOMNodeList; -class nsIEditor; class nsIFrame; class nsIMutationObserver; class nsINode; @@ -55,6 +54,7 @@ class nsDOMMutationObserver; namespace mozilla { class EventListenerManager; +class TextEditor; namespace dom { /** * @return true if aChar is what the WHATWG defines as a 'ascii whitespace'. @@ -1242,7 +1242,8 @@ public: * an editor. Note that this should be only used for getting input or textarea * editor's root content. This method doesn't support HTML editors. */ - nsIContent* GetTextEditorRootContent(nsIEditor** aEditor = nullptr); + nsIContent* GetTextEditorRootContent( + mozilla::TextEditor** aTextEditor = nullptr); /** * Get the nearest selection root, ie. the node that will be selected if the diff --git a/dom/html/HTMLBodyElement.cpp b/dom/html/HTMLBodyElement.cpp index 424355e96c5c..3a6653a903f0 100644 --- a/dom/html/HTMLBodyElement.cpp +++ b/dom/html/HTMLBodyElement.cpp @@ -7,6 +7,7 @@ #include "HTMLBodyElement.h" #include "mozilla/dom/HTMLBodyElementBinding.h" #include "mozilla/GenericSpecifiedValuesInlines.h" +#include "mozilla/TextEditor.h" #include "nsAttrValueInlines.h" #include "nsGkAtoms.h" #include "nsStyleConsts.h" @@ -394,9 +395,9 @@ HTMLBodyElement::IsAttributeMapped(const nsIAtom* aAttribute) const already_AddRefed HTMLBodyElement::GetAssociatedEditor() { - nsCOMPtr editor = GetEditorInternal(); - if (editor) { - return editor.forget(); + RefPtr textEditor = GetTextEditorInternal(); + if (textEditor) { + return textEditor.forget(); } // Make sure this is the actual body of the document @@ -415,6 +416,7 @@ HTMLBodyElement::GetAssociatedEditor() return nullptr; } + nsCOMPtr editor; docShell->GetEditor(getter_AddRefs(editor)); return editor.forget(); } diff --git a/dom/html/HTMLInputElement.cpp b/dom/html/HTMLInputElement.cpp index f2f78dd16933..764739b71634 100644 --- a/dom/html/HTMLInputElement.cpp +++ b/dom/html/HTMLInputElement.cpp @@ -70,6 +70,7 @@ #include "mozilla/EventStates.h" #include "mozilla/GenericSpecifiedValuesInlines.h" #include "mozilla/InternalMutationEvent.h" +#include "mozilla/TextEditor.h" #include "mozilla/TextEvents.h" #include "mozilla/TouchEvents.h" @@ -2617,6 +2618,12 @@ HTMLInputElement::SetUserInput(const nsAString& aValue) nsIEditor* HTMLInputElement::GetEditor() +{ + return GetTextEditorFromState(); +} + +TextEditor* +HTMLInputElement::GetTextEditorFromState() { nsTextEditorState* state = GetEditorState(); if (state) { @@ -2625,10 +2632,10 @@ HTMLInputElement::GetEditor() return nullptr; } -NS_IMETHODIMP_(nsIEditor*) +NS_IMETHODIMP_(TextEditor*) HTMLInputElement::GetTextEditor() { - return GetEditor(); + return GetTextEditorFromState(); } NS_IMETHODIMP_(nsISelectionController*) diff --git a/dom/html/HTMLInputElement.h b/dom/html/HTMLInputElement.h index 7c728346b0c8..ac702d487e24 100644 --- a/dom/html/HTMLInputElement.h +++ b/dom/html/HTMLInputElement.h @@ -175,7 +175,9 @@ public: // nsIDOMNSEditableElement NS_IMETHOD GetEditor(nsIEditor** aEditor) override { - return nsGenericHTMLElement::GetEditor(aEditor); + nsCOMPtr editor = GetEditor(); + editor.forget(aEditor); + return NS_OK; } NS_IMETHOD SetUserInput(const nsAString& aInput) override; @@ -242,7 +244,7 @@ public: NS_IMETHOD_(void) GetDefaultValueFromContent(nsAString& aValue) override; NS_IMETHOD_(bool) ValueChanged() const override; NS_IMETHOD_(void) GetTextEditorValue(nsAString& aValue, bool aIgnoreWrap) const override; - NS_IMETHOD_(nsIEditor*) GetTextEditor() override; + NS_IMETHOD_(mozilla::TextEditor*) GetTextEditor() override; NS_IMETHOD_(nsISelectionController*) GetSelectionController() override; NS_IMETHOD_(nsFrameSelection*) GetConstFrameSelection() override; NS_IMETHOD BindToFrame(nsTextControlFrame* aFrame) override; @@ -873,6 +875,9 @@ public: bool MozIsTextField(bool aExcludePassword); + /** + * GetEditor() is for webidl bindings. + */ nsIEditor* GetEditor(); void SetUserInput(const nsAString& aInput, @@ -1122,6 +1127,8 @@ protected: void FreeData(); nsTextEditorState *GetEditorState() const; + mozilla::TextEditor* GetTextEditorFromState(); + /** * Manages the internal data storage across type changes. */ diff --git a/dom/html/HTMLTextAreaElement.cpp b/dom/html/HTMLTextAreaElement.cpp index 8092ffd69151..cb214c2e9eb0 100644 --- a/dom/html/HTMLTextAreaElement.cpp +++ b/dom/html/HTMLTextAreaElement.cpp @@ -259,10 +259,10 @@ HTMLTextAreaElement::GetValueInternal(nsAString& aValue, bool aIgnoreWrap) const mState.GetValue(aValue, aIgnoreWrap); } -NS_IMETHODIMP_(nsIEditor*) +NS_IMETHODIMP_(TextEditor*) HTMLTextAreaElement::GetTextEditor() { - return GetEditor(); + return mState.GetTextEditor(); } NS_IMETHODIMP_(nsISelectionController*) diff --git a/dom/html/HTMLTextAreaElement.h b/dom/html/HTMLTextAreaElement.h index 1db0b1b0bbf8..1d389f82470b 100644 --- a/dom/html/HTMLTextAreaElement.h +++ b/dom/html/HTMLTextAreaElement.h @@ -20,6 +20,7 @@ #include "mozilla/dom/HTMLInputElementBinding.h" #include "nsGkAtoms.h" +#include "mozilla/TextEditor.h" #include "nsTextEditorState.h" class nsIControllers; @@ -67,7 +68,9 @@ public: // nsIDOMNSEditableElement NS_IMETHOD GetEditor(nsIEditor** aEditor) override { - return nsGenericHTMLElement::GetEditor(aEditor); + nsCOMPtr editor = GetEditor(); + editor.forget(aEditor); + return NS_OK; } NS_IMETHOD SetUserInput(const nsAString& aInput) override; @@ -94,7 +97,7 @@ public: NS_IMETHOD_(void) GetDefaultValueFromContent(nsAString& aValue) override; NS_IMETHOD_(bool) ValueChanged() const override; NS_IMETHOD_(void) GetTextEditorValue(nsAString& aValue, bool aIgnoreWrap) const override; - NS_IMETHOD_(nsIEditor*) GetTextEditor() override; + NS_IMETHOD_(mozilla::TextEditor*) GetTextEditor() override; NS_IMETHOD_(nsISelectionController*) GetSelectionController() override; NS_IMETHOD_(nsFrameSelection*) GetConstFrameSelection() override; NS_IMETHOD BindToFrame(nsTextControlFrame* aFrame) override; diff --git a/dom/html/nsGenericHTMLElement.cpp b/dom/html/nsGenericHTMLElement.cpp index 4767e162860c..747128e9d430 100644 --- a/dom/html/nsGenericHTMLElement.cpp +++ b/dom/html/nsGenericHTMLElement.cpp @@ -11,8 +11,9 @@ #include "mozilla/EventStateManager.h" #include "mozilla/EventStates.h" #include "mozilla/GenericSpecifiedValuesInlines.h" -#include "mozilla/MouseEvents.h" #include "mozilla/Likely.h" +#include "mozilla/MouseEvents.h" +#include "mozilla/TextEditor.h" #include "nscore.h" #include "nsGenericHTMLElement.h" @@ -1877,13 +1878,15 @@ nsGenericHTMLFormElement::GetForm(nsIDOMHTMLFormElement** aForm) nsIContent::IMEState nsGenericHTMLFormElement::GetDesiredIMEState() { - nsIEditor* editor = GetEditorInternal(); - if (!editor) + TextEditor* textEditor = GetTextEditorInternal(); + if (!textEditor) { return nsGenericHTMLElement::GetDesiredIMEState(); + } IMEState state; - nsresult rv = editor->GetPreferredIMEState(&state); - if (NS_FAILED(rv)) + nsresult rv = textEditor->GetPreferredIMEState(&state); + if (NS_FAILED(rv)) { return nsGenericHTMLElement::GetDesiredIMEState(); + } return state; } @@ -2606,20 +2609,13 @@ nsGenericHTMLElement::DispatchSimulatedClick(nsGenericHTMLElement* aElement, return EventDispatcher::Dispatch(ToSupports(aElement), aPresContext, &event); } -nsresult -nsGenericHTMLElement::GetEditor(nsIEditor** aEditor) -{ - NS_IF_ADDREF(*aEditor = GetEditorInternal()); - return NS_OK; -} - already_AddRefed nsGenericHTMLElement::GetAssociatedEditor() { // If contenteditable is ever implemented, it might need to do something different here? - nsCOMPtr editor = GetEditorInternal(); - return editor.forget(); + RefPtr textEditor = GetTextEditorInternal(); + return textEditor.forget(); } bool diff --git a/dom/html/nsGenericHTMLElement.h b/dom/html/nsGenericHTMLElement.h index bebac89d9623..a8b67b3f68c7 100644 --- a/dom/html/nsGenericHTMLElement.h +++ b/dom/html/nsGenericHTMLElement.h @@ -37,6 +37,7 @@ class EventChainPreVisitor; class EventChainVisitor; class EventListenerManager; class EventStates; +class TextEditor; namespace dom { class HTMLFormElement; class HTMLMenuElement; @@ -812,11 +813,6 @@ public: */ static bool InNavQuirksMode(nsIDocument* aDoc); - /** - * Locate an nsIEditor rooted at this content node, if there is one. - */ - nsresult GetEditor(nsIEditor** aEditor); - /** * Helper method for NS_IMPL_URI_ATTR macro. * Gets the absolute URI value of an attribute, by resolving any relative diff --git a/dom/html/nsITextControlElement.h b/dom/html/nsITextControlElement.h index b548e88ad493..8e7e0c8e0607 100644 --- a/dom/html/nsITextControlElement.h +++ b/dom/html/nsITextControlElement.h @@ -11,7 +11,6 @@ #include "nsCOMPtr.h" class nsIContent; class nsAString; -class nsIEditor; class nsISelectionController; class nsFrameSelection; class nsTextControlFrame; @@ -19,6 +18,7 @@ class nsTextControlFrame; namespace mozilla { class ErrorResult; +class TextEditor; namespace dom { class Element; @@ -109,7 +109,7 @@ public: * The return value is null if the control does not support an editor * (for example, if it is a checkbox.) */ - NS_IMETHOD_(nsIEditor*) GetTextEditor() = 0; + NS_IMETHOD_(mozilla::TextEditor*) GetTextEditor() = 0; /** * Get the selection controller object associated with the text editor.