From b2e66c86f685678106fbfd91a7b4d9a08d852da5 Mon Sep 17 00:00:00 2001 From: Masayuki Nakano Date: Sat, 8 Mar 2025 22:31:57 +0000 Subject: [PATCH] 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, nsIContent*> const&, mozilla::EnumSet) [editor/libeditor/WhiteSpaceVisibilityKeeper.cpp:1384] #08: mozilla::WhiteSpaceVisibilityKeeper::NormalizeWhiteSpacesToSplitAt(mozilla::HTMLEditor&, mozilla::EditorDOMPointBase, nsCOMPtr > const&, mozilla::EnumSet) [editor/libeditor/WhiteSpaceVisibilityKeeper.cpp:1599] #09: mozilla::HTMLEditor::PrepareToInsertLineBreak(mozilla::HTMLEditor::LineBreakType, mozilla::EditorDOMPointBase, nsCOMPtr > const&) [editor/libeditor/HTMLEditor.cpp:4418] #10: mozilla::EditorBase::InsertPaddingBRElementForEmptyLastLineWithTransaction(mozilla::EditorDOMPointBase, nsCOMPtr > const&) [editor/libeditor/EditorBase.cpp:2511] #11: mozilla::HTMLEditor::InsertBRElementToEmptyListItemsAndTableCellsInRange(mozilla::RangeBoundaryBase const&, mozilla::RangeBoundaryBase 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 --- editor/libeditor/HTMLEditSubActionHandler.cpp | 3 +++ editor/libeditor/HTMLEditor.cpp | 13 +++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/editor/libeditor/HTMLEditSubActionHandler.cpp b/editor/libeditor/HTMLEditSubActionHandler.cpp index 5cccc5e610f9..95790fe06792 100644 --- a/editor/libeditor/HTMLEditSubActionHandler.cpp +++ b/editor/libeditor/HTMLEditSubActionHandler.cpp @@ -180,6 +180,9 @@ nsresult HTMLEditor::InitEditorContentAndSelection() { return NS_OK; } + // FIXME: This is odd to update the DOM for making users can put caret in + // empty table cells and list items. We should make it possible without + // the hacky
. rv = InsertBRElementToEmptyListItemsAndTableCellsInRange( RawRangeBoundary(bodyOrDocumentElement, 0u), RawRangeBoundary(bodyOrDocumentElement, diff --git a/editor/libeditor/HTMLEditor.cpp b/editor/libeditor/HTMLEditor.cpp index 9646db9f908d..c48d7b136413 100644 --- a/editor/libeditor/HTMLEditor.cpp +++ b/editor/libeditor/HTMLEditor.cpp @@ -4430,12 +4430,20 @@ Result 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()))) { 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 pointToInsertOrError = @@ -4459,7 +4467,8 @@ Result HTMLEditor::PrepareToInsertLineBreak( } Result pointToInsertOrError = - StaticPrefs::editor_white_space_normalization_blink_compatible() + canNormalizeWhiteSpaces && + StaticPrefs::editor_white_space_normalization_blink_compatible() ? WhiteSpaceVisibilityKeeper::NormalizeWhiteSpacesToSplitAt( *this, aPointToInsert, {WhiteSpaceVisibilityKeeper::NormalizeOption::