Bug 1872863 - Part 2. IME focus should be updated when focused element becomes editable. r=masayuki

When focused element becomes editable by `contentedtiable=true`, IME
content observer isn't created on some situations. Then IME focus isn't
set.

At first, when focused element becomes non-editable,
`FocusedElementOrDocumentBecomesNotEditable` will destroy IME content
observer, but editor might not be destroyed completely since HTML content
has multiple `contenteditable` and has focus.

Then, when focused element becomes editable again,
`FocusedElementOrDocumentBecomesEditable` will check selection ancestor
limit, but since editor isn't destroyed by previous `contenteditable`
change, we already have this limit. Then we don't create IME content
observer.

So we should set current IME state and create IME content observer when
becoming editable.

Differential Revision: https://phabricator.services.mozilla.com/D202409
This commit is contained in:
Makoto Kato 2024-02-27 15:00:33 +00:00
Родитель cf76a88aa0
Коммит 2fddc3b3c8
3 изменённых файлов: 128 добавлений и 2 удалений

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

@ -719,14 +719,33 @@ void HTMLEditor::UpdateRootElement() {
nsresult HTMLEditor::FocusedElementOrDocumentBecomesEditable(
Document& aDocument, Element* aElement) {
const bool isInDesignMode =
(IsInDesignMode() && (!aElement || aElement->IsInDesignMode()));
// If we should've already handled focus event, selection limiter should not
// be set. Therefore, if it's set, we should do nothing here.
// be set. However, IMEStateManager is not notified the pseudo focus change
// in this case. Therefore, we need to notify IMEStateManager of this.
if (GetSelectionAncestorLimiter()) {
if (isInDesignMode) {
return NS_OK;
}
// Although editor is already initialized due to re-used, ISM may not
// create IME content observer yet. So we have to create it.
IMEState newState;
nsresult rv = GetPreferredIMEState(&newState);
if (NS_FAILED(rv)) {
NS_WARNING("EditorBase::GetPreferredIMEState() failed");
return NS_OK;
}
if (const RefPtr<Element> focusedElement = GetFocusedElement()) {
MOZ_ASSERT(focusedElement == aElement);
IMEStateManager::UpdateIMEState(newState, focusedElement, *this);
}
return NS_OK;
}
// If we should be in the design mode, we want to handle focus event fired
// on the document node. Therefore, we should emulate it here.
if (IsInDesignMode() && (!aElement || aElement->IsInDesignMode())) {
if (isInDesignMode) {
MOZ_ASSERT(&aDocument == GetDocument());
nsresult rv = OnFocus(aDocument);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "HTMLEditor::OnFocus() failed");

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

@ -82,6 +82,9 @@ support-files = "file_test_clipboard_asyncSetData.js"
["test_composition_text_querycontent.xhtml"]
support-files = ["window_composition_text_querycontent.xhtml"]
["test_ime_focus_with_multiple_contenteditable.html"]
support-files = ["file_ime_state_test_helper.js"]
["test_ime_state_in_contenteditable_on_readonly_change_in_parent.html"]
support-files = [
"file_ime_state_test_helper.js",

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

@ -0,0 +1,104 @@
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1872863
-->
<head>
<title>Test IME state and focus with multiple contenteditable elements</title>
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
<script src="file_ime_state_test_helper.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
<style>
#div0 {
width: 200px;
height: 50px;
}
#div3 {
background-color: blue;
width: 200px;
height: 100px;
}
#div4 {
width: 200px;
height: 50px;
}
</style>
<script>
add_task(async function test_modify_contenteditable_on_focused_element() {
await SimpleTest.promiseFocus();
const tipWrapper = new TIPWrapper(window);
ok(tipWrapper.isAvailable(), "TextInputProcessor should've been initialized");
const div1 = document.getElementById("div1");
const div2 = document.getElementById("div2");
const div3 = document.getElementById("div3");
div1.addEventListener("mousedown", e => {
div2.contentEditable = true;
});
div3.addEventListener("mousedown", e => {
div2.contentEditable = false;
e.preventDefault();
});
// Set focus by mouse then contenteditable becomes true by script.
const promiseFocus = new Promise(resolve => {
div2.addEventListener("focus", resolve, { once: true });
});
synthesizeMouseAtCenter(div1, {});
await promiseFocus;
is(
SpecialPowers.DOMWindowUtils.IMEStatus,
SpecialPowers.DOMWindowUtils.IME_STATUS_ENABLED,
"IMEStatus is enabled on contenteditable=true"
);
ok(tipWrapper.IMEHasFocus, "IME has focus");
// Move focus by mouse then contenteditable becomes false by script.
const promiseMouseUp = new Promise(resolve => {
div3.addEventListener("mouseup", resolve, { once: true });
});
synthesizeMouseAtCenter(div3, {});
await promiseMouseUp;
is(
SpecialPowers.DOMWindowUtils.IMEStatus,
SpecialPowers.DOMWindowUtils.IME_STATUS_DISABLED,
"IMEStatus is disabled on contenteditable=false"
);
ok(!tipWrapper.IMEHasFocus, "IME losts focus after contenteditable=false");
// contenteditable changes to true on focused element.
const promiseMouseUp2 = new Promise(resolve => {
div1.addEventListener("mouseup", resolve, { once: true });
});
synthesizeMouseAtCenter(div1, {});
await promiseMouseUp2;
is(
SpecialPowers.DOMWindowUtils.IMEStatus,
SpecialPowers.DOMWindowUtils.IME_STATUS_ENABLED,
"IMEStatus is enabled on contenteditable=true"
);
ok(tipWrapper.IMEHasFocus, "IME has focus after contenteditable=true again");
});
</script>
</head>
<body>
<div id="div0"><div id="div1">
<div id="div2" contenteditable="false"><div>foo</div></div>
</div></div>
<div id="div3"></div>
<div id="div4" contenteditable></div>
</body>
</html>