diff --git a/editor/libeditor/HTMLAnonymousNodeEditor.cpp b/editor/libeditor/HTMLAnonymousNodeEditor.cpp index f2e6dca7ca64..36598eee8cc7 100644 --- a/editor/libeditor/HTMLAnonymousNodeEditor.cpp +++ b/editor/libeditor/HTMLAnonymousNodeEditor.cpp @@ -20,6 +20,7 @@ #include "nsComputedDOMStyle.h" #include "nsDebug.h" #include "nsError.h" +#include "nsFocusManager.h" #include "nsGenericHTMLElement.h" #include "nsGkAtoms.h" #include "nsAtom.h" @@ -380,7 +381,8 @@ nsresult HTMLEditor::RefreshEditingUI() { if (editingHost && editingHost->IsContentEditablePlainTextOnly()) { return NS_OK; } - MOZ_ASSERT(editingHost == selectionContainerElement->GetEditingHost()); + MOZ_ASSERT_IF(editingHost, + editingHost == selectionContainerElement->GetEditingHost()); // what's its tag? RefPtr focusElement = std::move(selectionContainerElement); diff --git a/editor/libeditor/HTMLEditor.cpp b/editor/libeditor/HTMLEditor.cpp index 8e46d0fb66a5..76cabaabe857 100644 --- a/editor/libeditor/HTMLEditor.cpp +++ b/editor/libeditor/HTMLEditor.cpp @@ -7033,12 +7033,28 @@ Element* HTMLEditor::ComputeEditingHostInternal( if (aContent) { return aContent; } - // If the selection has focus node, let's look for its editing host because - // selection ranges may be visible for users. - nsIContent* const selectionFocusNode = nsIContent::FromNodeOrNull( - SelectionRef().GetMayCrossShadowBoundaryFocusNode()); - if (selectionFocusNode) { - return selectionFocusNode; + // If there are selection ranges, let's look for their common ancestor's + // editing host because selection ranges may be visible for users. + nsIContent* selectionCommonAncestor = nullptr; + for (uint32_t i : IntegerRange(SelectionRef().RangeCount())) { + nsRange* range = SelectionRef().GetRangeAt(i); + MOZ_ASSERT(range); + nsIContent* commonAncestor = + nsIContent::FromNodeOrNull(range->GetCommonAncestorContainer( + IgnoreErrors(), AllowRangeCrossShadowBoundary::Yes)); + if (MOZ_UNLIKELY(!commonAncestor)) { + continue; + } + if (!selectionCommonAncestor) { + selectionCommonAncestor = commonAncestor; + } else { + selectionCommonAncestor = + nsContentUtils::GetCommonFlattenedTreeAncestorForSelection( + commonAncestor, selectionCommonAncestor); + } + } + if (selectionCommonAncestor) { + return selectionCommonAncestor; } // Otherwise, let's use the focused element in the window. nsPIDOMWindowInner* const innerWindow = document->GetInnerWindow(); diff --git a/testing/web-platform/tests/editing/crashtests/selectall-after-enableobjectresizing.html b/testing/web-platform/tests/editing/crashtests/selectall-after-enableobjectresizing.html new file mode 100644 index 000000000000..2aa1ffee8c7d --- /dev/null +++ b/testing/web-platform/tests/editing/crashtests/selectall-after-enableobjectresizing.html @@ -0,0 +1,18 @@ + + + + + + + +
+ +
+