Bug 1770133 - part 3: Make `EditorBase::GetFocusedContent()` return `Element*` instead of `nsIContent*` r=m_kato

Differential Revision: https://phabricator.services.mozilla.com/D147134
This commit is contained in:
Masayuki Nakano 2022-05-26 07:03:20 +00:00
Родитель 6817b54c9d
Коммит f93d054430
6 изменённых файлов: 59 добавлений и 72 удалений

Просмотреть файл

@ -460,8 +460,7 @@ nsresult EditorBase::PostCreateInternal() {
}
// update nsTextStateManager and caret if we have focus
if (RefPtr<Element> focusedElement =
Element::FromNodeOrNull(GetFocusedContent())) {
if (RefPtr<Element> focusedElement = GetFocusedElement()) {
DebugOnly<nsresult> rvIgnored = InitializeSelection(*focusedElement);
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rvIgnored),
@ -718,8 +717,7 @@ NS_IMETHODIMP EditorBase::SetFlags(uint32_t aFlags) {
// Might be changing editable state, so, we need to reset current IME state
// if we're focused and the flag change causes IME state change.
if (RefPtr<Element> focusedElement =
Element::FromNodeOrNull(GetFocusedContent())) {
if (RefPtr<Element> focusedElement = GetFocusedElement()) {
IMEState newState;
nsresult rv = GetPreferredIMEState(&newState);
NS_WARNING_ASSERTION(
@ -5366,7 +5364,7 @@ void EditorBase::ReinitializeSelection(Element& aElement) {
if (NS_WARN_IF(!presContext)) {
return;
}
RefPtr<Element> focusedElement = Element::FromNodeOrNull(GetFocusedContent());
RefPtr<Element> focusedElement = GetFocusedElement();
IMEStateManager::OnFocusInEditor(*presContext, focusedElement, *this);
}
@ -5527,9 +5525,9 @@ nsresult EditorBase::SetTextDirectionTo(TextDirection aTextDirection) {
return NS_OK;
}
nsIContent* EditorBase::GetFocusedContent() const {
EventTarget* piTarget = GetDOMEventTarget();
if (!piTarget) {
Element* EditorBase::GetFocusedElement() const {
EventTarget* eventTarget = GetDOMEventTarget();
if (!eventTarget) {
return nullptr;
}
@ -5538,10 +5536,11 @@ nsIContent* EditorBase::GetFocusedContent() const {
return nullptr;
}
nsIContent* content = focusManager->GetFocusedElement();
MOZ_ASSERT((content == piTarget) == SameCOMIdentity(content, piTarget));
Element* focusedElement = focusManager->GetFocusedElement();
MOZ_ASSERT((focusedElement == eventTarget) ==
SameCOMIdentity(focusedElement, eventTarget));
return (content == piTarget) ? content : nullptr;
return (focusedElement == eventTarget) ? focusedElement : nullptr;
}
bool EditorBase::IsActiveInDOMWindow() const {
@ -5574,12 +5573,9 @@ bool EditorBase::IsAcceptableInputEvent(WidgetGUIEvent* aGUIEvent) const {
// If this is dispatched by using cordinates but this editor doesn't have
// focus, we shouldn't handle it.
if (aGUIEvent->IsUsingCoordinates()) {
nsIContent* focusedContent = GetFocusedContent();
if (!focusedContent) {
if (aGUIEvent->IsUsingCoordinates() && !GetFocusedElement()) {
return false;
}
}
// If a composition event isn't dispatched via widget, we need to ignore them
// since they cannot be managed by TextComposition. E.g., the event was

Просмотреть файл

@ -572,9 +572,9 @@ class EditorBase : public nsIEditor,
}
/**
* Get the focused content, if we're focused. Returns null otherwise.
* Get the focused element, if we're focused. Returns null otherwise.
*/
virtual nsIContent* GetFocusedContent() const;
virtual Element* GetFocusedElement() const;
/**
* Whether the aGUIEvent should be handled by this editor or not. When this
@ -1957,7 +1957,7 @@ class EditorBase : public nsIEditor,
/**
* Whether the editor is active on the DOM window. Note that when this
* returns true but GetFocusedContent() returns null, it means that this
* returns true but GetFocusedElement() returns null, it means that this
* editor was focused when the DOM window was active.
*/
virtual bool IsActiveInDOMWindow() const;

Просмотреть файл

@ -278,11 +278,8 @@ nsPresContext* EditorEventListener::GetPresContext() const {
bool EditorEventListener::EditorHasFocus() {
MOZ_ASSERT(!DetachedFromEditor());
nsCOMPtr<nsIContent> focusedContent = mEditorBase->GetFocusedContent();
if (!focusedContent) {
return false;
}
return !!focusedContent->GetComposedDoc();
const Element* focusedElement = mEditorBase->GetFocusedElement();
return focusedElement && focusedElement->IsInComposedDoc();
}
NS_IMPL_ISUPPORTS(EditorEventListener, nsIDOMEventListener)
@ -661,8 +658,7 @@ nsresult EditorEventListener::MouseClick(WidgetMouseEvent* aMouseClickEvent) {
// consumed.
if (EditorHasFocus()) {
if (RefPtr<nsPresContext> presContext = GetPresContext()) {
RefPtr<Element> focusedElement =
Element::FromNodeOrNull(mEditorBase->GetFocusedContent());
RefPtr<Element> focusedElement = mEditorBase->GetFocusedElement();
IMEStateManager::OnClickInEditor(*presContext, focusedElement,
*aMouseClickEvent);
if (DetachedFromEditor()) {
@ -734,8 +730,7 @@ bool EditorEventListener::NotifyIMEOfMouseButtonEvent(
if (NS_WARN_IF(!presContext)) {
return false;
}
RefPtr<Element> focusedElement =
Element::FromNodeOrNull(mEditorBase->GetFocusedContent());
RefPtr<Element> focusedElement = mEditorBase->GetFocusedElement();
return IMEStateManager::OnMouseButtonEventInEditor(
*presContext, focusedElement, *aMouseEvent);
}
@ -1168,8 +1163,7 @@ nsresult EditorEventListener::Focus(InternalFocusEvent* aFocusEvent) {
if (MOZ_UNLIKELY(NS_WARN_IF(!presContext))) {
return NS_OK;
}
RefPtr<Element> focusedElement =
Element::FromNodeOrNull(editorBase->GetFocusedContent());
RefPtr<Element> focusedElement = editorBase->GetFocusedElement();
IMEStateManager::OnFocusInEditor(*presContext, focusedElement, *editorBase);
return NS_OK;

Просмотреть файл

@ -6099,20 +6099,20 @@ nsresult HTMLEditor::GetReturnInParagraphCreatesNewParagraph(
return NS_OK;
}
nsIContent* HTMLEditor::GetFocusedContent() const {
Element* HTMLEditor::GetFocusedElement() const {
nsFocusManager* focusManager = nsFocusManager::GetFocusManager();
if (NS_WARN_IF(!focusManager)) {
return nullptr;
}
nsIContent* focusedContent = focusManager->GetFocusedElement();
Element* const focusedElement = focusManager->GetFocusedElement();
Document* document = GetDocument();
if (NS_WARN_IF(!document)) {
return nullptr;
}
const bool inDesignMode = IsInDesignMode();
if (!focusedContent) {
if (!focusedElement) {
// in designMode, nobody gets focus in most cases.
if (inDesignMode && OurWindowHasFocus()) {
return document->GetRootElement();
@ -6122,8 +6122,8 @@ nsIContent* HTMLEditor::GetFocusedContent() const {
if (inDesignMode) {
return OurWindowHasFocus() &&
focusedContent->IsInclusiveDescendantOf(document)
? focusedContent
focusedElement->IsInclusiveDescendantOf(document)
? focusedElement
: nullptr;
}
@ -6131,12 +6131,12 @@ nsIContent* HTMLEditor::GetFocusedContent() const {
// If the focused content isn't editable, or it has independent selection,
// we don't have focus.
if (!focusedContent->HasFlag(NODE_IS_EDITABLE) ||
focusedContent->HasIndependentSelection()) {
if (!focusedElement->HasFlag(NODE_IS_EDITABLE) ||
focusedElement->HasIndependentSelection()) {
return nullptr;
}
// If our window is focused, we're focused.
return OurWindowHasFocus() ? focusedContent : nullptr;
return OurWindowHasFocus() ? focusedElement : nullptr;
}
bool HTMLEditor::IsActiveInDOMWindow() const {
@ -6342,20 +6342,19 @@ Element* HTMLEditor::GetBodyElement() const {
}
nsINode* HTMLEditor::GetFocusedNode() const {
nsIContent* focusedContent = GetFocusedContent();
if (!focusedContent) {
Element* focusedElement = GetFocusedElement();
if (!focusedElement) {
return nullptr;
}
// focusedContent might be non-null even focusManager->GetFocusedContent()
// focusedElement might be non-null even focusManager->GetFocusedElement()
// is null. That's the designMode case, and in that case our
// FocusedContent() returns the root element, but we want to return
// the document.
nsFocusManager* focusManager = nsFocusManager::GetFocusManager();
NS_ASSERTION(focusManager, "Focus manager is null");
Element* focusedElement = focusManager->GetFocusedElement();
if (focusedElement) {
if ((focusedElement = focusManager->GetFocusedElement())) {
return focusedElement;
}

Просмотреть файл

@ -194,7 +194,7 @@ class HTMLEditor final : public EditorBase,
MOZ_CAN_RUN_SCRIPT nsresult
HandleKeyPressEvent(WidgetKeyboardEvent* aKeyboardEvent) final;
nsIContent* GetFocusedContent() const final;
Element* GetFocusedElement() const final;
bool IsActiveInDOMWindow() const final;
dom::EventTarget* GetDOMEventTarget() const final;
Element* FindSelectionRoot(nsINode* aNode) const final;

Просмотреть файл

@ -733,43 +733,41 @@ EditorSpellCheck::UpdateCurrentDictionary(
nsresult rv;
RefPtr<EditorSpellCheck> kungFuDeathGrip = this;
uint32_t flags = 0;
mEditor->GetFlags(&flags);
// Get language with html5 algorithm
nsCOMPtr<nsIContent> rootContent;
if (HTMLEditor* htmlEditor = mEditor->GetAsHTMLEditor()) {
if (flags & nsIEditor::eEditorMailMask) {
// Always determine the root content for a mail editor,
// even if not focused, to enable further processing below.
rootContent = htmlEditor->GetActiveEditingHost();
} else {
rootContent = htmlEditor->GetFocusedContent();
const RefPtr<Element> rootEditableElement =
[](const EditorBase& aEditorBase) -> Element* {
if (!aEditorBase.IsHTMLEditor()) {
return aEditorBase.GetRoot();
}
} else {
rootContent = mEditor->GetRoot();
if (aEditorBase.IsMailEditor()) {
// Shouldn't run spellcheck in a mail editor without focus
// (bug 1507543)
Element* const editingHost =
aEditorBase.AsHTMLEditor()->GetActiveEditingHost();
if (!editingHost) {
return nullptr;
}
// Try to get topmost document's document element for embedded mail
// editor (bug 967494)
Document* parentDoc =
editingHost->OwnerDoc()->GetInProcessParentDocument();
if (!parentDoc) {
return editingHost;
}
return parentDoc->GetDocumentElement();
}
return aEditorBase.AsHTMLEditor()->GetFocusedElement();
}(*mEditor);
if (!rootContent) {
if (!rootEditableElement) {
return NS_ERROR_FAILURE;
}
// Try to get topmost document's document element for embedded mail editor.
if (flags & nsIEditor::eEditorMailMask) {
RefPtr<Document> ownerDoc = rootContent->OwnerDoc();
Document* parentDoc = ownerDoc->GetInProcessParentDocument();
if (parentDoc) {
rootContent = parentDoc->GetDocumentElement();
if (!rootContent) {
return NS_ERROR_FAILURE;
}
}
}
RefPtr<DictionaryFetcher> fetcher =
new DictionaryFetcher(this, aCallback, mDictionaryFetcherGroup);
rootContent->GetLang(fetcher->mRootContentLang);
RefPtr<Document> doc = rootContent->GetComposedDoc();
rootEditableElement->GetLang(fetcher->mRootContentLang);
RefPtr<Document> doc = rootEditableElement->GetComposedDoc();
NS_ENSURE_STATE(doc);
doc->GetContentLanguage(fetcher->mRootDocContentLang);