Bug 1722535 - Make `HTMLEditor` call `HTMLEditUtils::GetBetterInsertionPoint()` with an editing host which is not limited in the `<body>` 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 `<body>` 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
This commit is contained in:
Masayuki Nakano 2021-08-02 08:23:49 +00:00
Родитель f2aa20ca98
Коммит c177665684
4 изменённых файлов: 48 добавлений и 17 удалений

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

@ -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());

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

@ -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.

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

@ -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]

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

@ -0,0 +1,42 @@
<!doctype html>
<html>
<head>
<title>Test for paste in temporarily created div element outside the body element</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script>
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(async () => {
const editor = document.querySelector("div[contenteditable]");
const heading = document.querySelector("h1");
getSelection().setBaseAndExtent(heading.firstChild, "So".length,
heading.firstChild, "Some te".length);
try {
await SimpleTest.promiseClipboardChange(
"me te", () => synthesizeKey("c", {accelKey: true}));
} catch (ex) {
ok(false, `Failed to copy selected text: ${ex}`);
SimpleTest.finish();
}
editor.focus();
editor.addEventListener("paste", () => {
const anotherEditor = document.createElement("div");
anotherEditor.setAttribute("contenteditable", "true");
document.documentElement.appendChild(anotherEditor);
anotherEditor.focus();
}, {once: true});
synthesizeKey("v", {accelKey: true});
const tempEditor = document.documentElement.lastChild;
is(tempEditor.nodeName.toLocaleLowerCase(), "div",
"Paste event handler should've inserted another editor");
is(tempEditor.textContent.trim(), "me te");
SimpleTest.finish();
});
</script>
</head>
<body>
<h1>Some text</h1>
<div contenteditable></div>
</body>
</html>