Bug 1601585 - Make `Document::EditingStateChanged()` stop calling `HTMLEditor::BeginingOfDocument()` when there is a pending state change for `contenteditable` elements and there is a selection range r=smaug

For backward compatibility, `Document::EditingStateChanged()` calls
`HTMLEditor::BeginingOfDocument()` to initialize selection when the document
becomes editable first time.  However, it checks whether
`Document::mEditingState` was ``EditingState::eOff` or not.  This looks enough,
but not so because `Document::MaybeEditingStateChanged()` calls it
asynchronously if `contenteditable` element appears at not safe to run script.
Therefore, this patch makes it check `Document::mContentEditableCount` value
which is modified synchronously from `Element::AfterSetAttr()` (additionally,
this makes it check whether there is at least a range in normal selection too,
though).

Differential Revision: https://phabricator.services.mozilla.com/D62814

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Masayuki Nakano 2020-02-15 08:18:43 +00:00
Родитель 6725ec2b0e
Коммит 9d4fda135f
1 изменённых файлов: 22 добавлений и 3 удалений

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

@ -5387,6 +5387,27 @@ nsresult Document::EditingStateChanged() {
RefPtr<PresShell> presShell = GetPresShell();
NS_ENSURE_TRUE(presShell, NS_ERROR_FAILURE);
// If we're entering the design mode from non-editable state, put the
// selection at the beginning of the document for compatibility reasons.
bool collapseSelectionAtBeginningOfDocument =
designMode && oldState == EditingState::eOff;
// However, mEditingState may be eOff even if there is some
// `contenteditable` area and selection has been initialized for it because
// mEditingState for `contenteditable` may have been scheduled to modify
// when safe. In such case, we should not reinitialize selection.
if (collapseSelectionAtBeginningOfDocument && mContentEditableCount) {
Selection* selection =
presShell->GetSelection(nsISelectionController::SELECTION_NORMAL);
NS_WARNING_ASSERTION(selection, "Why don't we have Selection?");
if (selection && selection->RangeCount()) {
// Perhaps, we don't need to check whether the selection is in
// an editing host or not because all contents will be editable
// in designMode. (And we don't want to make this code so complicated
// because of legacy API.)
collapseSelectionAtBeginningOfDocument = false;
}
}
MOZ_ASSERT(mStyleSetFilled);
// Before making this window editable, we need to modify UA style sheet
@ -5451,9 +5472,7 @@ nsresult Document::EditingStateChanged() {
return NS_OK;
}
// If we're entering the design mode, put the selection at the beginning of
// the document for compatibility reasons.
if (designMode && oldState == EditingState::eOff) {
if (collapseSelectionAtBeginningOfDocument) {
htmlEditor->BeginningOfDocument();
}