From c177665684e80368ec0a1cba76388d35f3202c3a Mon Sep 17 00:00:00 2001 From: Masayuki Nakano Date: Mon, 2 Aug 2021 08:23:49 +0000 Subject: [PATCH] Bug 1722535 - Make `HTMLEditor` call `HTMLEditUtils::GetBetterInsertionPoint()` with an editing host which is not limited in the `` r=m_kato Previously, `HTMLEditor::GetBetterInsertionPoint()` didn't check whether given point is in an editing host or not. However, now `HTMLEditUtils::GetBetterInsertionPoint()` does it with editing host which is returned by `HTMLEditor::GetActiveEditingHost(LimitInBodyElement::No)`. However, the old behavior is exactly same as `HTMLEditor::GetActiveEditingHost(LimitInBodyElement::Yes)` if editing host is outside the `` element. For taking back the original behavior, we should call the method with the result of the latter. Differential Revision: https://phabricator.services.mozilla.com/D121370 --- editor/libeditor/HTMLEditor.cpp | 10 +---- editor/libeditor/HTMLEditorDataTransfer.cpp | 12 ++---- editor/libeditor/tests/mochitest.ini | 1 + ..._temporarily_created_div_outside_body.html | 42 +++++++++++++++++++ 4 files changed, 48 insertions(+), 17 deletions(-) create mode 100644 editor/libeditor/tests/test_pasting_in_temporarily_created_div_outside_body.html diff --git a/editor/libeditor/HTMLEditor.cpp b/editor/libeditor/HTMLEditor.cpp index 6d78cb4f214b..7a7831a24a80 100644 --- a/editor/libeditor/HTMLEditor.cpp +++ b/editor/libeditor/HTMLEditor.cpp @@ -1828,15 +1828,9 @@ nsresult HTMLEditor::InsertElementAtSelectionAsAction( return NS_OK; } - Element* editingHost = GetActiveEditingHost(); + Element* editingHost = GetActiveEditingHost(LimitInBodyElement::No); if (NS_WARN_IF(!editingHost)) { - // In theory, we should return NS_ERROR_FAILURE here, but we've not - // thrown exception in this case. Therefore, we should allow to use - // the root element instead for now. - editingHost = GetRoot(); - if (NS_WARN_IF(!editingHost)) { - return EditorBase::ToGenericNSResult(NS_ERROR_FAILURE); - } + return EditorBase::ToGenericNSResult(NS_ERROR_FAILURE); } EditorRawDOMPoint atAnchor(SelectionRef().AnchorRef()); diff --git a/editor/libeditor/HTMLEditorDataTransfer.cpp b/editor/libeditor/HTMLEditorDataTransfer.cpp index 3adaf8caf0b6..7b5a3ccd87b3 100644 --- a/editor/libeditor/HTMLEditorDataTransfer.cpp +++ b/editor/libeditor/HTMLEditorDataTransfer.cpp @@ -674,16 +674,10 @@ nsresult HTMLEditor::HTMLWithContextInserter::Run( } } - Element* editingHost = mHTMLEditor.GetActiveEditingHost(); + Element* editingHost = + mHTMLEditor.GetActiveEditingHost(HTMLEditor::LimitInBodyElement::No); if (NS_WARN_IF(!editingHost)) { - // In theory, we should return NS_ERROR_FAILURE here, but we've not - // thrown exception in this case. Therefore, we should allow to use - // the root element instead for now. - // XXX test_bug795418-2.html depends on this behavior - editingHost = mHTMLEditor.GetRoot(); - if (NS_WARN_IF(!editingHost)) { - return NS_ERROR_FAILURE; - } + return NS_ERROR_FAILURE; } // Adjust position based on the first node we are going to insert. diff --git a/editor/libeditor/tests/mochitest.ini b/editor/libeditor/tests/mochitest.ini index cc0114799337..b9af35c9c86e 100644 --- a/editor/libeditor/tests/mochitest.ini +++ b/editor/libeditor/tests/mochitest.ini @@ -282,6 +282,7 @@ skip-if = headless [test_password_per_word_operation.html] [test_password_unmask_API.html] [test_pasting_in_root_element.xhtml] +[test_pasting_in_temporarily_created_div_outside_body.html] [test_pasting_text_longer_than_maxlength.html] [test_resizers_appearance.html] [test_resizers_resizing_elements.html] diff --git a/editor/libeditor/tests/test_pasting_in_temporarily_created_div_outside_body.html b/editor/libeditor/tests/test_pasting_in_temporarily_created_div_outside_body.html new file mode 100644 index 000000000000..05250be0492e --- /dev/null +++ b/editor/libeditor/tests/test_pasting_in_temporarily_created_div_outside_body.html @@ -0,0 +1,42 @@ + + + +Test for paste in temporarily created div element outside the body element + + + + + + +

Some text

+
+ +