Bug 1771448 - part 3: Get rid of `EditorBase::GetEditorRoot` r=m_kato

It returns the anonymous `<div>` element if the instance is a `TextEditor`, and
compute editing host otherwise.  So it's unclear what it returns.  Additionally,
all users except `EditorBase::CreateTransactionForCollapsedRange` are the
methods of `HTMLEditor`.  Therefore, we should remove it and unwrap the code
which it's done.

Differential Revision: https://phabricator.services.mozilla.com/D147503
This commit is contained in:
Masayuki Nakano 2022-05-31 03:41:01 +00:00
Родитель bd948d52e9
Коммит 3d7afa934d
8 изменённых файлов: 35 добавлений и 60 удалений

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

@ -3902,6 +3902,8 @@ EditorBase::CreateTransactionForCollapsedRange(
// build a transaction for deleting the appropriate data
// XXX: this has to come from rule section
const Element* const anonymousDivOrEditingHost =
IsTextEditor() ? GetRoot() : AsHTMLEditor()->GetActiveEditingHost();
if (aHowToHandleCollapsedRange == HowToHandleCollapsedRange::ExtendBackward &&
point.IsStartOfContainer()) {
MOZ_ASSERT(IsHTMLEditor());
@ -3909,7 +3911,7 @@ EditorBase::CreateTransactionForCollapsedRange(
// of previous editable content.
nsIContent* previousEditableContent = HTMLEditUtils::GetPreviousContent(
*point.GetContainer(), {WalkTreeOption::IgnoreNonEditableNode},
GetEditorRoot());
anonymousDivOrEditingHost);
if (!previousEditableContent) {
NS_WARNING("There was no editable content before the collapsed range");
return nullptr;
@ -3957,7 +3959,7 @@ EditorBase::CreateTransactionForCollapsedRange(
// next editable content.
nsIContent* nextEditableContent = HTMLEditUtils::GetNextContent(
*point.GetContainer(), {WalkTreeOption::IgnoreNonEditableNode},
GetEditorRoot());
anonymousDivOrEditingHost);
if (!nextEditableContent) {
NS_WARNING("There was no editable content after the collapsed range");
return nullptr;
@ -4024,10 +4026,10 @@ EditorBase::CreateTransactionForCollapsedRange(
aHowToHandleCollapsedRange == HowToHandleCollapsedRange::ExtendBackward
? HTMLEditUtils::GetPreviousContent(
point, {WalkTreeOption::IgnoreNonEditableNode},
GetEditorRoot())
anonymousDivOrEditingHost)
: HTMLEditUtils::GetNextContent(
point, {WalkTreeOption::IgnoreNonEditableNode},
GetEditorRoot());
anonymousDivOrEditingHost);
if (!editableContent) {
NS_WARNING("There was no editable content around the collapsed range");
return nullptr;
@ -4040,10 +4042,10 @@ EditorBase::CreateTransactionForCollapsedRange(
HowToHandleCollapsedRange::ExtendBackward
? HTMLEditUtils::GetPreviousContent(
*editableContent, {WalkTreeOption::IgnoreNonEditableNode},
GetEditorRoot())
anonymousDivOrEditingHost)
: HTMLEditUtils::GetNextContent(
*editableContent, {WalkTreeOption::IgnoreNonEditableNode},
GetEditorRoot());
anonymousDivOrEditingHost);
}
if (!editableContent) {
NS_WARNING(
@ -5334,8 +5336,6 @@ nsresult EditorBase::FinalizeSelection() {
return NS_OK;
}
Element* EditorBase::GetEditorRoot() const { return GetRoot(); }
Element* EditorBase::GetExposedRoot() const {
Element* rootElement = GetRoot();
if (!rootElement || !rootElement->IsInNativeAnonymousSubtree()) {

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

@ -1952,12 +1952,6 @@ class EditorBase : public nsIEditor,
const EditorDOMPoint& aPreviouslySelectedStart,
const dom::AbstractRange* aRange = nullptr);
/**
* Likewise, but gets the editor's root instead, which is different for HTML
* editors.
*/
virtual Element* GetEditorRoot() const;
/**
* Whether the editor is active on the DOM window. Note that when this
* returns true but GetFocusedElement() returns null, it means that this

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

@ -374,9 +374,9 @@ void HTMLEditor::HideGrabberInternal() {
nsresult HTMLEditor::ShowGrabberInternal(Element& aElement) {
MOZ_ASSERT(IsEditActionDataAvailable());
const RefPtr<Element> editorRoot = GetEditorRoot();
if (NS_WARN_IF(!editorRoot) ||
NS_WARN_IF(!aElement.IsInclusiveDescendantOf(editorRoot))) {
const RefPtr<Element> editingHost = GetActiveEditingHost();
if (NS_WARN_IF(!editingHost) ||
NS_WARN_IF(!aElement.IsInclusiveDescendantOf(editingHost))) {
return NS_ERROR_UNEXPECTED;
}

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

@ -6525,16 +6525,12 @@ EditorDOMPoint HTMLEditor::GetCurrentHardLineStartPoint(
aEditSubAction == EditSubAction::eOutdent ||
aEditSubAction == EditSubAction::eSetOrClearAlignment ||
aEditSubAction == EditSubAction::eCreateOrRemoveBlock;
Element* const editorRoot = GetEditorRoot();
if (!editorRoot) {
break;
}
// XXX So, does this check whether the container is removable or not? It
// seems that here can be rewritten as obviously what here tries to
// check.
if (!point.GetContainerParent()->IsInclusiveDescendantOf(editorRoot) &&
if (!point.GetContainerParent()->IsInclusiveDescendantOf(&aEditingHost) &&
(blockLevelAction ||
!point.GetContainer()->IsInclusiveDescendantOf(editorRoot))) {
!point.GetContainer()->IsInclusiveDescendantOf(&aEditingHost))) {
break;
}
@ -6674,12 +6670,8 @@ EditorDOMPoint HTMLEditor::GetCurrentHardLineEndPoint(
// XXX Maybe returning parent of editing host is really error prone since
// everybody need to check whether the end point is in editing host
// when they touch there.
Element* const editorRoot = GetEditorRoot();
if (!editorRoot) {
break;
}
if (!point.GetContainer()->IsInclusiveDescendantOf(editorRoot) &&
!point.GetContainerParent()->IsInclusiveDescendantOf(editorRoot)) {
if (!point.GetContainer()->IsInclusiveDescendantOf(&aEditingHost) &&
!point.GetContainerParent()->IsInclusiveDescendantOf(&aEditingHost)) {
break;
}
@ -6812,7 +6804,7 @@ already_AddRefed<nsRange> HTMLEditor::CreateRangeIncludingAdjuscentWhiteSpaces(
return nullptr;
}
const Element* editingHost = GetActiveEditingHost();
const Element* const editingHost = GetActiveEditingHost();
if (NS_WARN_IF(!editingHost)) {
return nullptr;
}
@ -6842,13 +6834,9 @@ already_AddRefed<nsRange> HTMLEditor::CreateRangeIncludingAdjuscentWhiteSpaces(
MOZ_ALWAYS_TRUE(startPoint.RewindOffset());
}
}
const RefPtr<Element> editorRoot = GetEditorRoot();
if (!editorRoot) {
return nullptr;
}
if (!startPoint.GetChildOrContainerIfDataNode() ||
!startPoint.GetChildOrContainerIfDataNode()->IsInclusiveDescendantOf(
editorRoot)) {
editingHost)) {
return nullptr;
}
if (endPoint.IsInTextNode()) {
@ -6865,7 +6853,7 @@ already_AddRefed<nsRange> HTMLEditor::CreateRangeIncludingAdjuscentWhiteSpaces(
}
if (!lastRawPoint.GetChildOrContainerIfDataNode() ||
!lastRawPoint.GetChildOrContainerIfDataNode()->IsInclusiveDescendantOf(
editorRoot)) {
editingHost)) {
return nullptr;
}
@ -6897,7 +6885,7 @@ already_AddRefed<nsRange> HTMLEditor::CreateRangeExtendedToHardLineStartAndEnd(
return nullptr;
}
const Element* editingHost = GetActiveEditingHost();
const Element* const editingHost = GetActiveEditingHost();
if (NS_WARN_IF(!editingHost)) {
return nullptr;
}
@ -6920,15 +6908,11 @@ already_AddRefed<nsRange> HTMLEditor::CreateRangeExtendedToHardLineStartAndEnd(
// XXX GetCurrentHardLineStartPoint() may return point of editing
// host. Perhaps, we should change it and stop checking it here
// since this check may be expensive.
Element* const editorRoot = GetEditorRoot();
if (!editorRoot) {
return nullptr;
}
// XXX If the container is an element in the editor root but it points end of
// XXX If the container is an element in the editing host but it points end of
// the container, this returns nullptr. Is it intentional?
if (!startPoint.GetChildOrContainerIfDataNode() ||
!startPoint.GetChildOrContainerIfDataNode()->IsInclusiveDescendantOf(
editorRoot)) {
editingHost)) {
return nullptr;
}
endPoint = GetCurrentHardLineEndPoint(endPoint, *editingHost);
@ -6937,11 +6921,11 @@ already_AddRefed<nsRange> HTMLEditor::CreateRangeExtendedToHardLineStartAndEnd(
// XXX GetCurrentHardLineEndPoint() may return point of editing host.
// Perhaps, we should change it and stop checking it here since this
// check may be expensive.
// XXX If the container is an element in the editor root but it points end of
// XXX If the container is an element in the editing host but it points end of
// the container, this returns nullptr. Is it intentional?
if (!lastRawPoint.GetChildOrContainerIfDataNode() ||
!lastRawPoint.GetChildOrContainerIfDataNode()->IsInclusiveDescendantOf(
editorRoot)) {
editingHost)) {
return nullptr;
}

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

@ -2079,10 +2079,10 @@ NS_IMETHODIMP HTMLEditor::SelectElement(Element* aElement) {
nsresult HTMLEditor::SelectContentInternal(nsIContent& aContentToSelect) {
MOZ_ASSERT(IsEditActionDataAvailable());
// Must be sure that element is contained in the editor root node
const RefPtr<Element> editorRoot = GetEditorRoot();
if (NS_WARN_IF(!editorRoot) ||
NS_WARN_IF(!aContentToSelect.IsInclusiveDescendantOf(editorRoot))) {
// Must be sure that element is contained in the editing host
const RefPtr<Element> editingHost = GetActiveEditingHost();
if (NS_WARN_IF(!editingHost) ||
NS_WARN_IF(!aContentToSelect.IsInclusiveDescendantOf(editingHost))) {
return NS_ERROR_FAILURE;
}
@ -4293,8 +4293,8 @@ bool HTMLEditor::SetCaretInTableCell(Element* aElement) {
!HTMLEditUtils::IsAnyTableElement(aElement)) {
return false;
}
const RefPtr<Element> editorRoot = GetEditorRoot();
if (!editorRoot || !aElement->IsInclusiveDescendantOf(editorRoot)) {
const RefPtr<Element> editingHost = GetActiveEditingHost();
if (!editingHost || !aElement->IsInclusiveDescendantOf(editingHost)) {
return false;
}
@ -6561,8 +6561,6 @@ already_AddRefed<Element> HTMLEditor::GetInputEventTargetElement() const {
return nullptr;
}
Element* HTMLEditor::GetEditorRoot() const { return GetActiveEditingHost(); }
nsresult HTMLEditor::OnModifyDocument() {
MOZ_ASSERT(mPendingDocumentModifiedRunner,
"HTMLEditor::OnModifyDocument() should be called via a runner");

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

@ -789,7 +789,6 @@ class HTMLEditor final : public EditorBase,
[[nodiscard]] MOZ_CAN_RUN_SCRIPT Result<EditorDOMPoint, nsresult>
RemoveBlockContainerWithTransaction(Element& aElement);
Element* GetEditorRoot() const final;
MOZ_CAN_RUN_SCRIPT nsresult RemoveAttributeOrEquivalent(
Element* aElement, nsAtom* aAttribute, bool aSuppressTransaction) final;
MOZ_CAN_RUN_SCRIPT nsresult SetAttributeOrEquivalent(

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

@ -323,9 +323,9 @@ nsresult HTMLEditor::ShowResizersInternal(Element& aResizedElement) {
return NS_ERROR_FAILURE;
}
const RefPtr<Element> editorRoot = GetEditorRoot();
if (NS_WARN_IF(!editorRoot) ||
NS_WARN_IF(!aResizedElement.IsInclusiveDescendantOf(editorRoot))) {
const RefPtr<Element> editingHost = GetActiveEditingHost();
if (NS_WARN_IF(!editingHost) ||
NS_WARN_IF(!aResizedElement.IsInclusiveDescendantOf(editingHost))) {
return NS_ERROR_UNEXPECTED;
}

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

@ -36,9 +36,9 @@ nsresult HTMLEditor::ShowInlineTableEditingUIInternal(Element& aCellElement) {
return NS_OK;
}
const RefPtr<Element> editorRoot = GetEditorRoot();
if (NS_WARN_IF(!editorRoot) ||
NS_WARN_IF(!aCellElement.IsInclusiveDescendantOf(editorRoot))) {
const RefPtr<Element> editingHost = GetActiveEditingHost();
if (NS_WARN_IF(!editingHost) ||
NS_WARN_IF(!aCellElement.IsInclusiveDescendantOf(editingHost))) {
return NS_ERROR_FAILURE;
}