Bug 1953933 - Make TextEditor::IsEmpty() return true if it's not been initialized yet r=m_kato

`GetTextNode()` requires that the instance has already been initialized since
it requires the native anonymous `<div>`.  And `IsEmpty()` is a public method.
Therefore, it should return `true` if there is no editor root, i.e., when it's
not been initialized yet after creation or destroyed.

Additionally, `TextControlState::GetValue()` should call
`TextEditor::ComputeTextValue()` if the editor has not been initialized because
it requires the native anonymous subtree to compute the value.  I'm not sure
whether this is a bug when `<textarea>` is being initialized.
(I guess that it should get the default value of the text control element or
`mValue` in the case, but we should do that in another bug separately.)

I couldn't find the crash testcase, so, this does not contain a new test.

Differential Revision: https://phabricator.services.mozilla.com/D241543
This commit is contained in:
Masayuki Nakano
2025-03-14 06:41:47 +00:00
parent 90bf053b3e
commit f43bf73bb2
2 changed files with 14 additions and 1 deletions

View File

@@ -480,6 +480,17 @@ already_AddRefed<Element> TextEditor::GetInputEventTargetElement() const {
}
bool TextEditor::IsEmpty() const {
// This is a public method. Therefore, it might have not been initialized yet
// when this is called. Let's return true in such case, but warn it because
// it may return different value than actual value which is stored by the
// text control element.
MOZ_ASSERT_IF(mInitSucceeded, GetRoot());
if (NS_WARN_IF(!GetRoot())) {
NS_ASSERTION(false,
"Make the root caller stop doing that before initializing or "
"after destroying the TextEditor");
return true;
}
const Text* const textNode = GetTextNode();
MOZ_DIAGNOSTIC_ASSERT_IF(textNode,
!Text::FromNodeOrNull(textNode->GetNextSibling()));