Bug 1940377 - part 8: Make HTMLEditor stop normalizing white-spaces during initialization r=m_kato

Enabling the new normalizer causes this assertion failure:
```
###!!! ASSERTION: Want to fire DOMNodeRemoved event, but it's not safe: 'Error', file /builds/worker/checkouts/gecko/dom/base/nsContentUtils.cpp:5586
#01: NS_DebugBreak [xpcom/base/nsDebugImpl.cpp:508]
#02: nsContentUtils::MaybeFireNodeRemoved(nsINode*, nsINode*) [dom/base/nsContentUtils.cpp:5587]
#03: nsINode::RemoveChild(nsINode&, mozilla::ErrorResult&) [dom/base/nsINode.cpp:0]
#04: mozilla::DeleteNodeTransaction::DoTransaction() [editor/libeditor/DeleteNodeTransaction.cpp:112]
#05: mozilla::EditorBase::DoTransactionInternal(nsITransaction*) [editor/libeditor/EditorBase.cpp:929]
#06: mozilla::EditorBase::DeleteNodeWithTransaction(nsIContent&) [editor/libeditor/EditorBase.cpp:2685]
#07: mozilla::WhiteSpaceVisibilityKeeper::NormalizeWhiteSpacesToSplitTextNodeAt(mozilla::HTMLEditor&, mozilla::EditorDOMPointBase<RefPtr<mozilla::dom::Text>, nsIContent*> const&, mozilla::EnumSet<mozilla::WhiteSpaceVisibilityKeeper::NormalizeOption, unsigned int>) [editor/libeditor/WhiteSpaceVisibilityKeeper.cpp:1384]
#08: mozilla::WhiteSpaceVisibilityKeeper::NormalizeWhiteSpacesToSplitAt(mozilla::HTMLEditor&, mozilla::EditorDOMPointBase<nsCOMPtr<nsINode>, nsCOMPtr<nsIContent> > const&, mozilla::EnumSet<mozilla::WhiteSpaceVisibilityKeeper::NormalizeOption, unsigned int>) [editor/libeditor/WhiteSpaceVisibilityKeeper.cpp:1599]
#09: mozilla::HTMLEditor::PrepareToInsertLineBreak(mozilla::HTMLEditor::LineBreakType, mozilla::EditorDOMPointBase<nsCOMPtr<nsINode>, nsCOMPtr<nsIContent> > const&) [editor/libeditor/HTMLEditor.cpp:4418]
#10: mozilla::EditorBase::InsertPaddingBRElementForEmptyLastLineWithTransaction(mozilla::EditorDOMPointBase<nsCOMPtr<nsINode>, nsCOMPtr<nsIContent> > const&) [editor/libeditor/EditorBase.cpp:2511]
#11: mozilla::HTMLEditor::InsertBRElementToEmptyListItemsAndTableCellsInRange(mozilla::RangeBoundaryBase<nsINode*, nsIContent*> const&, mozilla::RangeBoundaryBase<nsINode*, nsIContent*> const&) [editor/libeditor/HTMLEditSubActionHandler.cpp:0]
#12: mozilla::HTMLEditor::InitEditorContentAndSelection() [editor/libeditor/HTMLEditSubActionHandler.cpp:187]
#13: mozilla::HTMLEditor::Init(mozilla::dom::Document&, mozilla::ComposerCommandsUpdater&, unsigned int) [editor/libeditor/HTMLEditor.cpp:412]
#14: nsEditingSession::SetupEditorOnWindow(nsPIDOMWindowOuter&) [editor/composer/nsEditingSession.cpp:405]
```

The initialization is required only for empty table cells and empty list items
and they are typically not used at initial document.  So, I think that it's fine
to stop normalizing white-spaces before succeeded to initialize `HTMLEditor`.

If this change causes a web-compat issue in the wild, let's make it run
asynchronously.

Differential Revision: https://phabricator.services.mozilla.com/D239473
This commit is contained in:
Masayuki Nakano
2025-03-08 22:31:57 +00:00
parent dc6fec20e3
commit b2e66c86f6
2 changed files with 14 additions and 2 deletions

View File

@@ -4430,12 +4430,20 @@ Result<EditorDOMPoint, nsresult> HTMLEditor::PrepareToInsertLineBreak(
EditorUtils::IsNewLinePreformatted(aContent);
};
// If we're being initialized, we cannot normalize white-spaces because the
// normalizer may remove invisible `Text`, but it's not allowed during the
// initialization.
// FIXME: Anyway, we should not do this at initialization. This is required
// only for making users can put caret into empty table cells and list items.
const bool canNormalizeWhiteSpaces = mInitSucceeded;
if (!aPointToInsert.IsInTextNode()) {
if (NS_WARN_IF(
!CanInsertLineBreak(*aPointToInsert.ContainerAs<nsIContent>()))) {
return Err(NS_ERROR_FAILURE);
}
if (!StaticPrefs::editor_white_space_normalization_blink_compatible()) {
if (!canNormalizeWhiteSpaces ||
!StaticPrefs::editor_white_space_normalization_blink_compatible()) {
return aPointToInsert;
}
Result<EditorDOMPoint, nsresult> pointToInsertOrError =
@@ -4459,7 +4467,8 @@ Result<EditorDOMPoint, nsresult> HTMLEditor::PrepareToInsertLineBreak(
}
Result<EditorDOMPoint, nsresult> pointToInsertOrError =
StaticPrefs::editor_white_space_normalization_blink_compatible()
canNormalizeWhiteSpaces &&
StaticPrefs::editor_white_space_normalization_blink_compatible()
? WhiteSpaceVisibilityKeeper::NormalizeWhiteSpacesToSplitAt(
*this, aPointToInsert,
{WhiteSpaceVisibilityKeeper::NormalizeOption::