diff --git a/editor/libeditor/EditorDOMPoint.h b/editor/libeditor/EditorDOMPoint.h index 8b2ea1a3efa5..351aa7065138 100644 --- a/editor/libeditor/EditorDOMPoint.h +++ b/editor/libeditor/EditorDOMPoint.h @@ -284,6 +284,11 @@ class EditorDOMPointBase final { */ bool IsContainerElement() const { return mParent && mParent->IsElement(); } + /** + * Returns true if the container node is an editing host. + */ + [[nodiscard]] bool IsContainerEditableRoot() const; + /** * IsContainerHTMLElement() returns true if the container node is an HTML * element node and its node name is aTag. diff --git a/editor/libeditor/EditorUtils.cpp b/editor/libeditor/EditorUtils.cpp index 6c3adb150100..35f39de47e02 100644 --- a/editor/libeditor/EditorUtils.cpp +++ b/editor/libeditor/EditorUtils.cpp @@ -352,6 +352,15 @@ bool EditorDOMPointBase< EditorUtils::IsOnlyNewLinePreformatted(*ContainerAs()); } +template +bool EditorDOMPointBase::IsContainerEditableRoot() const { + if (MOZ_UNLIKELY(!mParent) || MOZ_UNLIKELY(!mParent->IsEditable()) || + NS_WARN_IF(mParent->IsInNativeAnonymousSubtree())) { + return false; + } + return HTMLEditUtils::ElementIsEditableRoot(*mParent); +} + /****************************************************************************** * mozilla::EditorDOMRangeBase *****************************************************************************/ diff --git a/editor/libeditor/HTMLEditSubActionHandler.cpp b/editor/libeditor/HTMLEditSubActionHandler.cpp index a17cae00c01d..b95fb73b4fc2 100644 --- a/editor/libeditor/HTMLEditSubActionHandler.cpp +++ b/editor/libeditor/HTMLEditSubActionHandler.cpp @@ -486,29 +486,26 @@ nsresult HTMLEditor::OnEndHandlingTopLevelEditSubActionInternal() { NS_WARNING("There was no selection range"); return NS_ERROR_EDITOR_UNEXPECTED_DOM_TREE; } - if (const RefPtr editingHost = - ComputeEditingHost(LimitInBodyElement::No)) { - Result - insertPaddingBRElementResultOrError = - InsertPaddingBRElementToMakeEmptyLineVisibleIfNeeded( - newCaretPosition, *editingHost); - if (MOZ_UNLIKELY(insertPaddingBRElementResultOrError.isErr())) { - NS_WARNING( - "HTMLEditor::" - "InsertPaddingBRElementToMakeEmptyLineVisibleIfNeeded() failed"); - return insertPaddingBRElementResultOrError.unwrapErr(); - } - nsresult rv = - insertPaddingBRElementResultOrError.unwrap().SuggestCaretPointTo( - *this, {SuggestCaret::OnlyIfHasSuggestion}); - if (NS_FAILED(rv)) { - NS_WARNING("CaretPoint::SuggestCaretPointTo() failed"); - return rv; - } - NS_WARNING_ASSERTION( - rv != NS_SUCCESS_EDITOR_BUT_IGNORED_TRIVIAL_ERROR, - "CaretPoint::SuggestCaretPointTo() failed, but ignored"); + Result + insertPaddingBRElementResultOrError = + InsertPaddingBRElementToMakeEmptyLineVisibleIfNeeded( + newCaretPosition); + if (MOZ_UNLIKELY(insertPaddingBRElementResultOrError.isErr())) { + NS_WARNING( + "HTMLEditor::" + "InsertPaddingBRElementToMakeEmptyLineVisibleIfNeeded() failed"); + return insertPaddingBRElementResultOrError.unwrapErr(); } + nsresult rv = + insertPaddingBRElementResultOrError.unwrap().SuggestCaretPointTo( + *this, {SuggestCaret::OnlyIfHasSuggestion}); + if (NS_FAILED(rv)) { + NS_WARNING("CaretPoint::SuggestCaretPointTo() failed"); + return rv; + } + NS_WARNING_ASSERTION( + rv != NS_SUCCESS_EDITOR_BUT_IGNORED_TRIVIAL_ERROR, + "CaretPoint::SuggestCaretPointTo() failed, but ignored"); } // add in any needed
s, and remove any unneeded ones. @@ -1208,7 +1205,7 @@ Result HTMLEditor::HandleInsertText( const InsertTextResult insertEmptyTextResult = insertEmptyTextResultOrError.unwrap(); nsresult rv = EnsureNoFollowingUnnecessaryLineBreak( - insertEmptyTextResult.EndOfInsertedTextRef(), *editingHost); + insertEmptyTextResult.EndOfInsertedTextRef()); if (NS_FAILED(rv)) { NS_WARNING( "HTMLEditor::EnsureNoFollowingUnnecessaryLineBreak() failed"); @@ -1257,7 +1254,7 @@ Result HTMLEditor::HandleInsertText( } InsertTextResult unwrappedReplacedTextResult = replaceTextResult.unwrap(); nsresult rv = EnsureNoFollowingUnnecessaryLineBreak( - unwrappedReplacedTextResult.EndOfInsertedTextRef(), *editingHost); + unwrappedReplacedTextResult.EndOfInsertedTextRef()); if (NS_FAILED(rv)) { NS_WARNING("HTMLEditor::EnsureNoFollowingUnnecessaryLineBreak() failed"); return Err(rv); @@ -1532,8 +1529,7 @@ Result HTMLEditor::HandleInsertText( mLastCollapsibleWhiteSpaceAppendedTextNode = currentPoint.ContainerAs(); } - nsresult rv = - EnsureNoFollowingUnnecessaryLineBreak(currentPoint, *editingHost); + nsresult rv = EnsureNoFollowingUnnecessaryLineBreak(currentPoint); if (NS_FAILED(rv)) { NS_WARNING("HTMLEditor::EnsureNoFollowingUnnecessaryLineBreak() failed"); return Err(rv); @@ -1661,7 +1657,8 @@ nsresult HTMLEditor::InsertLineBreakAsSubAction() { } const WSScanResult backwardScanFromBeforeBRElementResult = WSRunScanner::ScanPreviousVisibleNodeOrBlockBoundary( - editingHost, insertLineBreakResult.AtLineBreak(), + WSRunScanner::Scan::EditableNodes, + insertLineBreakResult.AtLineBreak(), BlockInlineCheck::UseComputedDisplayStyle); if (MOZ_UNLIKELY(backwardScanFromBeforeBRElementResult.Failed())) { NS_WARNING( @@ -1671,7 +1668,7 @@ nsresult HTMLEditor::InsertLineBreakAsSubAction() { const WSScanResult forwardScanFromAfterBRElementResult = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - editingHost, pointToPutCaret, + WSRunScanner::Scan::EditableNodes, pointToPutCaret, BlockInlineCheck::UseComputedDisplayStyle); if (MOZ_UNLIKELY(forwardScanFromAfterBRElementResult.Failed())) { NS_WARNING("WSRunScanner::ScanNextVisibleNodeOrBlockBoundary() failed"); @@ -1901,7 +1898,7 @@ HTMLEditor::InsertParagraphSeparatorAsSubAction(const Element& aEditingHost) { // table cell boundaries? Result caretPointOrError = HandleInsertParagraphInMailCiteElement(*mailCiteElement, - pointToInsert, aEditingHost); + pointToInsert); if (MOZ_UNLIKELY(caretPointOrError.isErr())) { NS_WARNING( "HTMLEditor::HandleInsertParagraphInMailCiteElement() failed"); @@ -2472,7 +2469,7 @@ Result HTMLEditor::HandleInsertBRElement( const WSScanResult forwardScanFromAfterBRElementResult = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, afterBRElement, + WSRunScanner::Scan::EditableNodes, afterBRElement, BlockInlineCheck::UseComputedDisplayStyle); if (MOZ_UNLIKELY(forwardScanFromAfterBRElementResult.Failed())) { NS_WARNING("WSRunScanner::ScanNextVisibleNodeOrBlockBoundary() failed"); @@ -2687,8 +2684,7 @@ Result HTMLEditor::HandleInsertLinefeed( } Result HTMLEditor::HandleInsertParagraphInMailCiteElement( - Element& aMailCiteElement, const EditorDOMPoint& aPointToSplit, - const Element& aEditingHost) { + Element& aMailCiteElement, const EditorDOMPoint& aPointToSplit) { MOZ_ASSERT(IsEditActionDataAvailable()); MOZ_ASSERT(aPointToSplit.IsSet()); NS_ASSERTION(!HTMLEditUtils::IsEmptyNode( @@ -2711,7 +2707,8 @@ Result HTMLEditor::HandleInsertParagraphInMailCiteElement( // mailquote may affect wrapping behavior, or font color, etc. const WSScanResult forwardScanFromPointToSplitResult = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, pointToSplit, BlockInlineCheck::UseHTMLDefaultStyle); + WSRunScanner::Scan::EditableNodes, pointToSplit, + BlockInlineCheck::UseHTMLDefaultStyle); if (forwardScanFromPointToSplitResult.Failed()) { return Err(NS_ERROR_FAILURE); } @@ -2831,7 +2828,7 @@ Result HTMLEditor::HandleInsertParagraphInMailCiteElement( // resultOfInsertingBRElement.inspect()? const WSScanResult backwardScanFromPointToCreateNewBRElementResult = WSRunScanner::ScanPreviousVisibleNodeOrBlockBoundary( - &aEditingHost, pointToCreateNewBRElement, + WSRunScanner::Scan::EditableNodes, pointToCreateNewBRElement, BlockInlineCheck::UseHTMLDefaultStyle); if (MOZ_UNLIKELY( backwardScanFromPointToCreateNewBRElementResult.Failed())) { @@ -2848,7 +2845,7 @@ Result HTMLEditor::HandleInsertParagraphInMailCiteElement( } const WSScanResult forwardScanFromPointAfterNewBRElementResult = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, + WSRunScanner::Scan::EditableNodes, EditorRawDOMPoint::After(pointToCreateNewBRElement), BlockInlineCheck::UseHTMLDefaultStyle); if (MOZ_UNLIKELY(forwardScanFromPointAfterNewBRElementResult.Failed())) { @@ -3407,8 +3404,7 @@ HTMLEditor::DeleteTextAndNormalizeSurroundingWhiteSpaces( { AutoTrackDOMPoint trackPointToPutCaret(RangeUpdaterRef(), &newCaretPosition); - nsresult rv = - EnsureNoFollowingUnnecessaryLineBreak(newCaretPosition, aEditingHost); + nsresult rv = EnsureNoFollowingUnnecessaryLineBreak(newCaretPosition); if (NS_FAILED(rv)) { NS_WARNING("HTMLEditor::EnsureNoFollowingUnnecessaryLineBreak() failed"); return Err(rv); @@ -3464,7 +3460,7 @@ bool HTMLEditor::CanInsertLineBreak(LineBreakType aLineBreakType, Result HTMLEditor::InsertPaddingBRElementToMakeEmptyLineVisibleIfNeeded( - const EditorDOMPoint& aPointToInsert, const Element& aEditingHost) { + const EditorDOMPoint& aPointToInsert) { MOZ_ASSERT(IsEditActionDataAvailable()); MOZ_ASSERT(aPointToInsert.IsSet()); @@ -3487,7 +3483,7 @@ HTMLEditor::InsertPaddingBRElementToMakeEmptyLineVisibleIfNeeded( // here. const WSScanResult previousThing = WSRunScanner::ScanPreviousVisibleNodeOrBlockBoundary( - &aEditingHost, aPointToInsert, + WSRunScanner::Scan::EditableNodes, aPointToInsert, BlockInlineCheck::UseComputedDisplayStyle); if (!previousThing.ReachedLineBoundary()) { return CreateLineBreakResult::NotHandled(); @@ -3497,7 +3493,7 @@ HTMLEditor::InsertPaddingBRElementToMakeEmptyLineVisibleIfNeeded( // line break here. const WSScanResult nextThing = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, aPointToInsert, + WSRunScanner::Scan::EditableNodes, aPointToInsert, BlockInlineCheck::UseComputedDisplayStyle); if (!nextThing.ReachedBlockBoundary()) { return CreateLineBreakResult::NotHandled(); @@ -7043,8 +7039,7 @@ Result HTMLEditor::CreateStyleForInsertText( NS_WARNING("AutoClonedRangeArray::AutoClonedRangeArray() failed"); return Err(NS_ERROR_FAILURE); } - nsresult rv = - SetInlinePropertiesAroundRanges(ranges, stylesToSet, aEditingHost); + nsresult rv = SetInlinePropertiesAroundRanges(ranges, stylesToSet); if (NS_FAILED(rv)) { NS_WARNING("HTMLEditor::SetInlinePropertiesAroundRanges() failed"); return Err(rv); @@ -9177,7 +9172,8 @@ HTMLEditor::HandleInsertParagraphInListItemElement( // the element is proper position. const WSScanResult forwardScanFromStartOfListItemResult = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, EditorRawDOMPoint(&rightListItemElement, 0u), + WSRunScanner::Scan::EditableNodes, + EditorRawDOMPoint(&rightListItemElement, 0u), BlockInlineCheck::UseComputedDisplayStyle); if (MOZ_UNLIKELY(forwardScanFromStartOfListItemResult.Failed())) { NS_WARNING("WSRunScanner::ScanNextVisibleNodeOrBlockBoundary() failed"); @@ -11212,7 +11208,7 @@ HTMLEditor::InsertPaddingBRElementIfNeeded( if (IsPlaintextMailComposer()) { const WSScanResult nextVisibleThing = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, aPoint, + WSRunScanner::Scan::EditableNodes, aPoint, BlockInlineCheck::UseComputedDisplayOutsideStyle); if (nextVisibleThing.ReachedBlockBoundary() && HTMLEditUtils::IsMailCite(*nextVisibleThing.ElementPtr()) && diff --git a/editor/libeditor/HTMLEditUtils.cpp b/editor/libeditor/HTMLEditUtils.cpp index e7a4de5a52e4..cdf6f06fe709 100644 --- a/editor/libeditor/HTMLEditUtils.cpp +++ b/editor/libeditor/HTMLEditUtils.cpp @@ -41,6 +41,7 @@ #include "nsError.h" // for NS_SUCCEEDED #include "nsGkAtoms.h" // for nsGkAtoms, nsGkAtoms::a, etc. #include "nsHTMLTags.h" +#include "nsIContentInlines.h" // for nsIContent::IsInDesignMode(), etc. #include "nsIFrameInlines.h" // for nsIFrame::IsFlexOrGridItem() #include "nsLiteralString.h" // for NS_LITERAL_STRING #include "nsNameSpaceManager.h" // for kNameSpaceID_None @@ -125,13 +126,13 @@ template EditorRawDOMPoint HTMLEditUtils::GetBetterInsertionPointFor( const nsIContent& aContentToInsert, const EditorDOMPoint& aPointToInsert); template EditorDOMPoint HTMLEditUtils::GetBetterCaretPositionToInsertText( - const EditorDOMPoint& aPoint, const Element& aEditingHost); + const EditorDOMPoint& aPoint); template EditorDOMPoint HTMLEditUtils::GetBetterCaretPositionToInsertText( - const EditorRawDOMPoint& aPoint, const Element& aEditingHost); + const EditorRawDOMPoint& aPoint); template EditorRawDOMPoint HTMLEditUtils::GetBetterCaretPositionToInsertText( - const EditorDOMPoint& aPoint, const Element& aEditingHost); + const EditorDOMPoint& aPoint); template EditorRawDOMPoint HTMLEditUtils::GetBetterCaretPositionToInsertText( - const EditorRawDOMPoint& aPoint, const Element& aEditingHost); + const EditorRawDOMPoint& aPoint); template Result HTMLEditUtils::ComputePointToPutCaretInElementIfOutside( @@ -152,48 +153,54 @@ template bool HTMLEditUtils::IsSameCSSColorValue(const nsACString& aColorA, const nsACString& aColorB); template Maybe HTMLEditUtils::GetFollowingUnnecessaryLineBreak( - const EditorDOMPoint& aPoint, const Element& aEditingHost); + const EditorDOMPoint& aPoint); template Maybe HTMLEditUtils::GetFollowingUnnecessaryLineBreak( - const EditorRawDOMPoint& aPoint, const Element& aEditingHost); + const EditorRawDOMPoint& aPoint); template Maybe HTMLEditUtils::GetFollowingUnnecessaryLineBreak( - const EditorDOMPointInText& aPoint, const Element& aEditingHost); + const EditorDOMPointInText& aPoint); template Maybe HTMLEditUtils::GetFollowingUnnecessaryLineBreak( - const EditorRawDOMPointInText& aPoint, const Element& aEditingHost); + const EditorRawDOMPointInText& aPoint); template Maybe -HTMLEditUtils::GetFollowingUnnecessaryLineBreak(const EditorDOMPoint& aPoint, - const Element& aEditingHost); -template Maybe -HTMLEditUtils::GetFollowingUnnecessaryLineBreak(const EditorRawDOMPoint& aPoint, - const Element& aEditingHost); +HTMLEditUtils::GetFollowingUnnecessaryLineBreak(const EditorDOMPoint& aPoint); template Maybe HTMLEditUtils::GetFollowingUnnecessaryLineBreak( - const EditorDOMPointInText& aPoint, const Element& aEditingHost); + const EditorRawDOMPoint& aPoint); template Maybe HTMLEditUtils::GetFollowingUnnecessaryLineBreak( - const EditorRawDOMPointInText& aPoint, const Element& aEditingHost); + const EditorDOMPointInText& aPoint); +template Maybe +HTMLEditUtils::GetFollowingUnnecessaryLineBreak( + const EditorRawDOMPointInText& aPoint); template bool HTMLEditUtils::PointIsImmediatelyBeforeCurrentBlockBoundary( const EditorDOMPoint& aPoint, - IgnoreInvisibleLineBreak aIgnoreInvisibleLineBreak, - const Element& aEditingHost); + IgnoreInvisibleLineBreak aIgnoreInvisibleLineBreak); template bool HTMLEditUtils::PointIsImmediatelyBeforeCurrentBlockBoundary( const EditorRawDOMPoint& aPoint, - IgnoreInvisibleLineBreak aIgnoreInvisibleLineBreak, - const Element& aEditingHost); + IgnoreInvisibleLineBreak aIgnoreInvisibleLineBreak); template bool HTMLEditUtils::PointIsImmediatelyBeforeCurrentBlockBoundary( const EditorDOMPointInText& aPoint, - IgnoreInvisibleLineBreak aIgnoreInvisibleLineBreak, - const Element& aEditingHost); + IgnoreInvisibleLineBreak aIgnoreInvisibleLineBreak); template bool HTMLEditUtils::PointIsImmediatelyBeforeCurrentBlockBoundary( const EditorRawDOMPointInText& aPoint, - IgnoreInvisibleLineBreak aIgnoreInvisibleLineBreak, - const Element& aEditingHost); + IgnoreInvisibleLineBreak aIgnoreInvisibleLineBreak); template Maybe HTMLEditUtils::GetUnnecessaryLineBreak( const Element& aBlockElement, ScanLineBreak aScanLineBreak); template Maybe HTMLEditUtils::GetUnnecessaryLineBreak( const Element& aBlockElement, ScanLineBreak aScanLineBreak); +bool HTMLEditUtils::ElementIsEditableRoot(const Element& aElement) { + MOZ_ASSERT(!aElement.IsInNativeAnonymousSubtree()); + if (NS_WARN_IF(!aElement.IsEditable()) || + NS_WARN_IF(!aElement.IsInComposedDoc())) { + return false; + } + return !aElement.GetParent() || // root element + !aElement.GetParent()->IsEditable() || // editing host + aElement.OwnerDoc()->GetBody() == &aElement; // the +} + bool HTMLEditUtils::CanContentsBeJoined(const nsIContent& aLeftContent, const nsIContent& aRightContent) { if (aLeftContent.NodeInfo()->NameAtom() != @@ -809,7 +816,7 @@ EditorDOMPoint HTMLEditUtils::LineRequiresPaddingLineBreakToBeVisible( } const WSScanResult nextThing = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, point, + WSRunScanner::Scan::EditableNodes, point, BlockInlineCheck::UseComputedDisplayOutsideStyle); if (nextThing.ReachedBlockBoundary()) { if (nextThing.ReachedCurrentBlockBoundary()) { @@ -831,7 +838,7 @@ EditorDOMPoint HTMLEditUtils::LineRequiresPaddingLineBreakToBeVisible( } const WSScanResult previousThing = WSRunScanner::ScanPreviousVisibleNodeOrBlockBoundary( - &aEditingHost, preferredPaddingLineBreakPoint, + WSRunScanner::Scan::EditableNodes, preferredPaddingLineBreakPoint, BlockInlineCheck::UseComputedDisplayOutsideStyle); if (previousThing.ContentIsText()) { if (MOZ_UNLIKELY(!previousThing.TextPtr()->TextDataLength())) { @@ -982,16 +989,15 @@ Element* HTMLEditUtils::GetElementOfImmediateBlockBoundary( template bool HTMLEditUtils::PointIsImmediatelyBeforeCurrentBlockBoundary( const EditorDOMPointBase& aPoint, - IgnoreInvisibleLineBreak aIgnoreInvisibleLineBreak, - const Element& aEditingHost) { + IgnoreInvisibleLineBreak aIgnoreInvisibleLineBreak) { MOZ_ASSERT(aPoint.IsSetAndValidInComposedDoc()); if (MOZ_UNLIKELY(!aPoint.IsInContentNode())) { return false; } - WSScanResult nextThing = + const WSScanResult nextThing = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, aPoint, + WSRunScanner::Scan::EditableNodes, aPoint, BlockInlineCheck::UseComputedDisplayOutsideStyle); if (nextThing.ReachedCurrentBlockBoundary()) { return true; @@ -1000,9 +1006,9 @@ bool HTMLEditUtils::PointIsImmediatelyBeforeCurrentBlockBoundary( if (aIgnoreInvisibleLineBreak == IgnoreInvisibleLineBreak::No) { return false; } - WSScanResult afterInvisibleBRThing = + const WSScanResult afterInvisibleBRThing = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, + WSRunScanner::Scan::EditableNodes, nextThing.PointAfterReachedContent(), BlockInlineCheck::UseComputedDisplayOutsideStyle); return afterInvisibleBRThing.ReachedCurrentBlockBoundary(); @@ -1011,9 +1017,9 @@ bool HTMLEditUtils::PointIsImmediatelyBeforeCurrentBlockBoundary( if (aIgnoreInvisibleLineBreak == IgnoreInvisibleLineBreak::No) { return false; } - WSScanResult afterPreformattedLineBreakThing = + const WSScanResult afterPreformattedLineBreakThing = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, + WSRunScanner::Scan::EditableNodes, nextThing.PointAfterReachedContent(), BlockInlineCheck::UseComputedDisplayOutsideStyle); return afterPreformattedLineBreakThing.ReachedCurrentBlockBoundary(); @@ -1192,22 +1198,23 @@ Maybe HTMLEditUtils::GetUnnecessaryLineBreak( template Maybe HTMLEditUtils::GetFollowingUnnecessaryLineBreak( - const EditorDOMPointType& aPoint, const Element& aEditingHost) { + const EditorDOMPointType& aPoint) { MOZ_ASSERT(aPoint.IsSetAndValid()); MOZ_ASSERT(aPoint.IsInContentNode()); - WSScanResult nextThing = + const WSScanResult nextThing = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, aPoint, BlockInlineCheck::UseComputedDisplayStyle); + WSRunScanner::Scan::EditableNodes, aPoint, + BlockInlineCheck::UseComputedDisplayStyle); if (!nextThing.ReachedBRElement() && !(nextThing.ReachedPreformattedLineBreak() && nextThing.PointAtReachedContent() .IsAtLastContent())) { return Nothing(); // no line break next to aPoint } - WSScanResult nextThingOfLineBreak = + const WSScanResult nextThingOfLineBreak = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, + WSRunScanner::Scan::EditableNodes, nextThing.PointAfterReachedContent(), BlockInlineCheck::UseComputedDisplayStyle); const Element* const blockElement = @@ -2540,12 +2547,12 @@ nsIContent* HTMLEditUtils::GetContentToPreserveInlineStyles( for (auto point = aPoint.template To(); point.IsSet();) { const WSScanResult nextVisibleThing = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, point, + WSRunScanner::Scan::EditableNodes, point, BlockInlineCheck::UseComputedDisplayOutsideStyle); if (nextVisibleThing.InVisibleOrCollapsibleCharacters()) { return nextVisibleThing.TextPtr(); } - if (nextVisibleThing.GetContent() == &aEditingHost) { + if (nextVisibleThing.IsContentEditableRoot()) { break; } // Ignore empty inline container elements because it's not visible for @@ -2631,10 +2638,9 @@ EditorDOMPointType HTMLEditUtils::GetBetterInsertionPointFor( // static template EditorDOMPointType HTMLEditUtils::GetBetterCaretPositionToInsertText( - const EditorDOMPointTypeInput& aPoint, const Element& aEditingHost) { + const EditorDOMPointTypeInput& aPoint) { MOZ_ASSERT(aPoint.IsSetAndValid()); - MOZ_ASSERT( - aPoint.GetContainer()->IsInclusiveFlatTreeDescendantOf(&aEditingHost)); + MOZ_ASSERT(HTMLEditUtils::IsSimplyEditableNode(*aPoint.GetContainer())); if (aPoint.IsInTextNode()) { return aPoint.template To(); @@ -2646,7 +2652,8 @@ EditorDOMPointType HTMLEditUtils::GetBetterCaretPositionToInsertText( if (aPoint.IsEndOfContainer()) { const WSScanResult previousThing = WSRunScanner::ScanPreviousVisibleNodeOrBlockBoundary( - &aEditingHost, aPoint, BlockInlineCheck::UseComputedDisplayStyle); + WSRunScanner::Scan::EditableNodes, aPoint, + BlockInlineCheck::UseComputedDisplayStyle); if (previousThing.InVisibleOrCollapsibleCharacters()) { return EditorDOMPointType::AtEndOf(*previousThing.TextPtr()); } @@ -2655,7 +2662,7 @@ EditorDOMPointType HTMLEditUtils::GetBetterCaretPositionToInsertText( *nsGkAtoms::textTagName)) { return aPoint.template To(); } - if (MOZ_UNLIKELY(aPoint.GetContainer() == &aEditingHost || + if (MOZ_UNLIKELY(aPoint.GetContainer()->IsEditingHost() || !aPoint.template GetContainerParentAs() || !HTMLEditUtils::CanNodeContain( *aPoint.template ContainerParentAs(), diff --git a/editor/libeditor/HTMLEditUtils.h b/editor/libeditor/HTMLEditUtils.h index 2b2e12831974..0e326539c361 100644 --- a/editor/libeditor/HTMLEditUtils.h +++ b/editor/libeditor/HTMLEditUtils.h @@ -76,6 +76,14 @@ class HTMLEditUtils final { return aNode.IsEditable(); } + /** + * Return true if aElement is an editing host which is either: + * - the root element + * - parent is not editable + * - the element of the document + */ + [[nodiscard]] static bool ElementIsEditableRoot(const Element& aElement); + /** * Return true if inclusive flat tree ancestor has `inert` state. */ @@ -540,8 +548,7 @@ class HTMLEditUtils final { template [[nodiscard]] static bool PointIsImmediatelyBeforeCurrentBlockBoundary( const EditorDOMPointBase& aPoint, - IgnoreInvisibleLineBreak aIgnoreInvisibleLineBreak, - const Element& aEditingHost); + IgnoreInvisibleLineBreak aIgnoreInvisibleLineBreak); /** * Return true if aRange crosses the inclusive ancestor block element at @@ -2071,8 +2078,7 @@ class HTMLEditUtils final { */ template [[nodiscard]] static Maybe - GetFollowingUnnecessaryLineBreak(const EditorDOMPointType& aPoint, - const Element& aEditingHost); + GetFollowingUnnecessaryLineBreak(const EditorDOMPointType& aPoint); /** * IsInTableCellSelectionMode() returns true when Gecko's editor thinks that @@ -2382,7 +2388,7 @@ class HTMLEditUtils final { */ template static EditorDOMPointType GetBetterCaretPositionToInsertText( - const EditorDOMPointTypeInput& aPoint, const Element& aEditingHost); + const EditorDOMPointTypeInput& aPoint); /** * ComputePointToPutCaretInElementIfOutside() returns a good point in aElement diff --git a/editor/libeditor/HTMLEditor.cpp b/editor/libeditor/HTMLEditor.cpp index 04b7e09274b7..5dbdaf01802d 100644 --- a/editor/libeditor/HTMLEditor.cpp +++ b/editor/libeditor/HTMLEditor.cpp @@ -1194,7 +1194,7 @@ nsresult HTMLEditor::MaybeCollapseSelectionAtFirstEditableNode( // the visible character. const WSScanResult scanResultInTextNode = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - editingHost, EditorRawDOMPoint(text, 0), + WSRunScanner::Scan::EditableNodes, EditorRawDOMPoint(text, 0), BlockInlineCheck::UseComputedDisplayStyle); if ((scanResultInTextNode.InVisibleOrCollapsibleCharacters() || scanResultInTextNode.ReachedPreformattedLineBreak()) && @@ -2295,8 +2295,7 @@ nsresult HTMLEditor::InsertElementAtSelectionAsAction( if (MOZ_LIKELY(aElement->IsInComposedDoc())) { const auto afterElement = EditorDOMPoint::After(*aElement); if (MOZ_LIKELY(afterElement.IsInContentNode())) { - nsresult rv = - EnsureNoFollowingUnnecessaryLineBreak(afterElement, *editingHost); + nsresult rv = EnsureNoFollowingUnnecessaryLineBreak(afterElement); if (NS_FAILED(rv)) { NS_WARNING( "HTMLEditor::EnsureNoFollowingUnnecessaryLineBreak() failed"); @@ -4519,8 +4518,7 @@ Result HTMLEditor::InsertLineBreak( } nsresult HTMLEditor::EnsureNoFollowingUnnecessaryLineBreak( - const EditorDOMPoint& aNextOrAfterModifiedPoint, - const Element& aEditingHost) { + const EditorDOMPoint& aNextOrAfterModifiedPoint) { MOZ_ASSERT(aNextOrAfterModifiedPoint.IsInContentNode()); // If the point is in a mailcite in plaintext mail composer (it is a @@ -4548,7 +4546,7 @@ nsresult HTMLEditor::EnsureNoFollowingUnnecessaryLineBreak( const Maybe unnecessaryLineBreak = HTMLEditUtils::GetFollowingUnnecessaryLineBreak( - aNextOrAfterModifiedPoint, aEditingHost); + aNextOrAfterModifiedPoint); if (MOZ_LIKELY(unnecessaryLineBreak.isNothing())) { return NS_OK; } @@ -7847,7 +7845,7 @@ nsresult HTMLEditor::OnModifyDocument(const DocumentModifiedEvent& aRunner) { } const WSScanResult nextThing = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - editingHost, + WSRunScanner::Scan::EditableNodes, atCollapsibleWhiteSpace.AfterContainer(), BlockInlineCheck::UseComputedDisplayStyle); if (!nextThing.ReachedBlockBoundary()) { @@ -7909,21 +7907,18 @@ void HTMLEditor::DocumentModifiedEvent::MaybeAppendNewInvisibleWhiteSpace( !aContentWillBeRemoved->IsHTMLElement(nsGkAtoms::br)) { return; } - const Element* const editingHost = - const_cast(aContentWillBeRemoved)->GetEditingHost(); - if (MOZ_UNLIKELY(!editingHost)) { - return; - } const WSScanResult nextThing = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - editingHost, EditorRawDOMPoint::After(*aContentWillBeRemoved), + WSRunScanner::Scan::EditableNodes, + EditorRawDOMPoint::After(*aContentWillBeRemoved), BlockInlineCheck::UseComputedDisplayStyle); if (!nextThing.ReachedBlockBoundary()) { return; } const WSScanResult previousThing = WSRunScanner::ScanPreviousVisibleNodeOrBlockBoundary( - editingHost, EditorRawDOMPoint(aContentWillBeRemoved), + WSRunScanner::Scan::EditableNodes, + EditorRawDOMPoint(aContentWillBeRemoved), BlockInlineCheck::UseComputedDisplayOutsideStyle); if (!previousThing.ContentIsText() || !previousThing.IsContentEditable()) { return; diff --git a/editor/libeditor/HTMLEditor.h b/editor/libeditor/HTMLEditor.h index ad369daef7b7..93f255bd0015 100644 --- a/editor/libeditor/HTMLEditor.h +++ b/editor/libeditor/HTMLEditor.h @@ -1213,14 +1213,12 @@ class HTMLEditor final : public EditorBase, * * @param aMailCiteElement The mail-cite element which should be split. * @param aPointToSplit The point to split. - * @param aEditingHost The editing host. * @return Candidate caret position where is at inserted *
element into the split point. */ [[nodiscard]] MOZ_CAN_RUN_SCRIPT Result HandleInsertParagraphInMailCiteElement(Element& aMailCiteElement, - const EditorDOMPoint& aPointToSplit, - const Element& aEditingHost); + const EditorDOMPoint& aPointToSplit); /** * HandleInsertBRElement() inserts a
element into aPointToBreak. @@ -1657,7 +1655,7 @@ class HTMLEditor final : public EditorBase, */ [[nodiscard]] MOZ_CAN_RUN_SCRIPT Result InsertPaddingBRElementToMakeEmptyLineVisibleIfNeeded( - const EditorDOMPoint& aPointToInsert, const Element& aEditingHost); + const EditorDOMPoint& aPointToInsert); /** * Insert a padding
if aPoint is in an empty block. @@ -3400,12 +3398,10 @@ class HTMLEditor final : public EditorBase, * content. * If you deleted something, this should be * end of the deleted range. - * @param aEditingHost The editing host. */ [[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult EnsureNoFollowingUnnecessaryLineBreak( - const EditorDOMPoint& aNextOrAfterModifiedPoint, - const Element& aEditingHost); + const EditorDOMPoint& aNextOrAfterModifiedPoint); /** * IndentAsSubAction() indents the content around Selection. @@ -3455,8 +3451,7 @@ class HTMLEditor final : public EditorBase, template [[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult SetInlinePropertiesAroundRanges( AutoClonedRangeArray& aRanges, - const AutoTArray& aStylesToSet, - const Element& aEditingHost); + const AutoTArray& aStylesToSet); /** * RemoveInlinePropertiesAsSubAction() removes specified styles from diff --git a/editor/libeditor/HTMLEditorDataTransfer.cpp b/editor/libeditor/HTMLEditorDataTransfer.cpp index 420433bd1665..c30fcefdb54a 100644 --- a/editor/libeditor/HTMLEditorDataTransfer.cpp +++ b/editor/libeditor/HTMLEditorDataTransfer.cpp @@ -903,7 +903,7 @@ Result HTMLEditor::HTMLWithContextInserter::Run( lastInsertedPoint.inspect().NextPointOrAfterContainer(); if (MOZ_LIKELY(afterLastInsertedContent.IsInContentNode())) { nsresult rv = mHTMLEditor.EnsureNoFollowingUnnecessaryLineBreak( - afterLastInsertedContent, mEditingHost); + afterLastInsertedContent); if (NS_FAILED(rv)) { NS_WARNING( "HTMLEditor::EnsureNoFollowingUnnecessaryLineBreak() failed"); diff --git a/editor/libeditor/HTMLEditorDeleteHandler.cpp b/editor/libeditor/HTMLEditorDeleteHandler.cpp index dc1ff0c6904d..b281ac13dbf5 100644 --- a/editor/libeditor/HTMLEditorDeleteHandler.cpp +++ b/editor/libeditor/HTMLEditorDeleteHandler.cpp @@ -2373,8 +2373,8 @@ Result HTMLEditor::AutoDeleteRangesHandler:: if (MOZ_LIKELY(pointToPutCaret.IsInContentNode())) { AutoTrackDOMPoint trackPointToPutCaret(aHTMLEditor.RangeUpdaterRef(), &pointToPutCaret); - nsresult rv = aHTMLEditor.EnsureNoFollowingUnnecessaryLineBreak( - pointToPutCaret, aEditingHost); + nsresult rv = + aHTMLEditor.EnsureNoFollowingUnnecessaryLineBreak(pointToPutCaret); if (NS_FAILED(rv)) { NS_WARNING("HTMLEditor::EnsureNoFollowingUnnecessaryLineBreak() failed"); return Err(rv); @@ -2581,8 +2581,8 @@ Result HTMLEditor::AutoDeleteRangesHandler:: if (MOZ_LIKELY(pointToPutCaret.IsInContentNode())) { AutoTrackDOMPoint trackPointToPutCaret(aHTMLEditor.RangeUpdaterRef(), &pointToPutCaret); - nsresult rv = aHTMLEditor.EnsureNoFollowingUnnecessaryLineBreak( - pointToPutCaret, aEditingHost); + nsresult rv = + aHTMLEditor.EnsureNoFollowingUnnecessaryLineBreak(pointToPutCaret); if (NS_FAILED(rv)) { NS_WARNING("HTMLEditor::EnsureNoFollowingUnnecessaryLineBreak() failed"); return Err(rv); @@ -2756,8 +2756,8 @@ HTMLEditor::AutoDeleteRangesHandler::HandleDeleteAtomicContent( if (MOZ_LIKELY(pointToPutCaret.IsInContentNode())) { AutoTrackDOMPoint trackPointToPutCaret(aHTMLEditor.RangeUpdaterRef(), &pointToPutCaret); - nsresult rv = aHTMLEditor.EnsureNoFollowingUnnecessaryLineBreak( - pointToPutCaret, aEditingHost); + nsresult rv = + aHTMLEditor.EnsureNoFollowingUnnecessaryLineBreak(pointToPutCaret); if (NS_FAILED(rv)) { NS_WARNING("HTMLEditor::EnsureNoFollowingUnnecessaryLineBreak() failed"); return Err(rv); @@ -3586,7 +3586,7 @@ bool HTMLEditor::AutoDeleteRangesHandler::AutoBlockElementsJoiner:: } const WSScanResult prevVisibleThingBeforeCurrentBlock = WSRunScanner::ScanPreviousVisibleNodeOrBlockBoundary( - &aEditingHost, + WSRunScanner::Scan::EditableNodes, EditorRawDOMPoint( inclusiveAncestorOfRightChildBlockOrError.inspect()), BlockInlineCheck::UseComputedDisplayOutsideStyle); @@ -3605,7 +3605,7 @@ bool HTMLEditor::AutoDeleteRangesHandler::AutoBlockElementsJoiner:: MOZ_ASSERT(atPrecedingLineBreak.IsSet()); const WSScanResult prevVisibleThingBeforeLineBreak = WSRunScanner::ScanPreviousVisibleNodeOrBlockBoundary( - &aEditingHost, atPrecedingLineBreak, + WSRunScanner::Scan::EditableNodes, atPrecedingLineBreak, BlockInlineCheck::UseComputedDisplayOutsideStyle); if (prevVisibleThingBeforeLineBreak.ReachedBRElement() || prevVisibleThingBeforeLineBreak.ReachedPreformattedLineBreak() || @@ -4214,7 +4214,8 @@ bool HTMLEditor::AutoDeleteRangesHandler::AutoBlockElementsJoiner:: // rough check. const WSScanResult nextVisibleThingOfEndBoundary = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, EditorRawDOMPoint(aRangeToDelete.EndRef()), + WSRunScanner::Scan::EditableNodes, + EditorRawDOMPoint(aRangeToDelete.EndRef()), BlockInlineCheck::UseComputedDisplayOutsideStyle); if (!nextVisibleThingOfEndBoundary.ReachedCurrentBlockBoundary()) { MOZ_ASSERT(mLeftContent->IsElement()); @@ -4227,7 +4228,8 @@ bool HTMLEditor::AutoDeleteRangesHandler::AutoBlockElementsJoiner:: if (MOZ_LIKELY(mostDistantBlockOrError.inspect())) { const WSScanResult prevVisibleThingOfStartBoundary = WSRunScanner::ScanPreviousVisibleNodeOrBlockBoundary( - &aEditingHost, EditorRawDOMPoint(aRangeToDelete.StartRef()), + WSRunScanner::Scan::EditableNodes, + EditorRawDOMPoint(aRangeToDelete.StartRef()), BlockInlineCheck::UseComputedDisplayOutsideStyle); if (prevVisibleThingOfStartBoundary.ReachedBRElement()) { // If the range start after a
followed by the block boundary, @@ -4235,7 +4237,7 @@ bool HTMLEditor::AutoDeleteRangesHandler::AutoBlockElementsJoiner:: // not a part of empty line like `
abc
{
]def`. const WSScanResult nextVisibleThingOfBR = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, + WSRunScanner::Scan::EditableNodes, EditorRawDOMPoint::After( *prevVisibleThingOfStartBoundary.GetContent()), BlockInlineCheck::UseComputedDisplayOutsideStyle); @@ -4249,7 +4251,7 @@ bool HTMLEditor::AutoDeleteRangesHandler::AutoBlockElementsJoiner:: } const WSScanResult prevVisibleThingOfBR = WSRunScanner::ScanPreviousVisibleNodeOrBlockBoundary( - &aEditingHost, + WSRunScanner::Scan::EditableNodes, EditorRawDOMPoint( prevVisibleThingOfStartBoundary.GetContent()), BlockInlineCheck::UseComputedDisplayOutsideStyle); @@ -4264,7 +4266,7 @@ bool HTMLEditor::AutoDeleteRangesHandler::AutoBlockElementsJoiner:: .ReachedPreformattedLineBreak()) { const WSScanResult nextVisibleThingOfLineBreak = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, + WSRunScanner::Scan::EditableNodes, prevVisibleThingOfStartBoundary .PointAfterReachedContent(), BlockInlineCheck::UseComputedDisplayOutsideStyle); @@ -4279,7 +4281,7 @@ bool HTMLEditor::AutoDeleteRangesHandler::AutoBlockElementsJoiner:: } const WSScanResult prevVisibleThingOfLineBreak = WSRunScanner::ScanPreviousVisibleNodeOrBlockBoundary( - &aEditingHost, + WSRunScanner::Scan::EditableNodes, prevVisibleThingOfStartBoundary .PointAtReachedContent(), BlockInlineCheck::UseComputedDisplayOutsideStyle); @@ -4296,7 +4298,7 @@ bool HTMLEditor::AutoDeleteRangesHandler::AutoBlockElementsJoiner:: mLeftContent); const WSScanResult firstVisibleThingInBlock = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, + WSRunScanner::Scan::EditableNodes, EditorRawDOMPoint( prevVisibleThingOfStartBoundary.ElementPtr(), 0), BlockInlineCheck::UseComputedDisplayOutsideStyle); @@ -4309,7 +4311,7 @@ bool HTMLEditor::AutoDeleteRangesHandler::AutoBlockElementsJoiner:: } else if (prevVisibleThingOfStartBoundary.ReachedOtherBlockElement()) { const WSScanResult firstVisibleThingAfterBlock = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, + WSRunScanner::Scan::EditableNodes, EditorRawDOMPoint::After( *prevVisibleThingOfStartBoundary.ElementPtr()), BlockInlineCheck::UseComputedDisplayOutsideStyle); @@ -4903,8 +4905,8 @@ Result HTMLEditor::AutoDeleteRangesHandler:: const WSScanResult prevVisibleThing = WSRunScanner::ScanPreviousVisibleNodeOrBlockBoundary( - aAncestorLimiter, aPoint, - BlockInlineCheck::UseComputedDisplayOutsideStyle); + WSRunScanner::Scan::EditableNodes, aPoint, + BlockInlineCheck::UseComputedDisplayOutsideStyle, aAncestorLimiter); if (!ReachedCurrentBlockBoundaryWhichWeCanCross(prevVisibleThing)) { return nullptr; } @@ -4914,8 +4916,8 @@ Result HTMLEditor::AutoDeleteRangesHandler:: for (Element* ancestorBlock = prevVisibleThing.ElementPtr(); ancestorBlock;) { const WSScanResult prevVisibleThing = WSRunScanner::ScanPreviousVisibleNodeOrBlockBoundary( - aAncestorLimiter, EditorRawDOMPoint(ancestorBlock), - BlockInlineCheck::UseComputedDisplayOutsideStyle); + WSRunScanner::Scan::EditableNodes, EditorRawDOMPoint(ancestorBlock), + BlockInlineCheck::UseComputedDisplayOutsideStyle, aAncestorLimiter); if (!ReachedCurrentBlockBoundaryWhichWeCanCross(prevVisibleThing)) { return ancestorBlock; } @@ -4981,7 +4983,8 @@ void HTMLEditor::AutoDeleteRangesHandler::AutoBlockElementsJoiner:: const WSScanResult prevVisibleThingOfStartBoundary = WSRunScanner::ScanPreviousVisibleNodeOrBlockBoundary( - &aEditingHost, EditorRawDOMPoint(aRangeToDelete.StartRef()), + WSRunScanner::Scan::EditableNodes, + EditorRawDOMPoint(aRangeToDelete.StartRef()), BlockInlineCheck::UseComputedDisplayOutsideStyle); // If the range starts after an invisible
of empty line immediately // before the most distant inclusive ancestor of the right block like @@ -4991,13 +4994,13 @@ void HTMLEditor::AutoDeleteRangesHandler::AutoBlockElementsJoiner:: prevVisibleThingOfStartBoundary.ReachedPreformattedLineBreak()) { const WSScanResult prevVisibleThingOfPreviousLineBreak = WSRunScanner::ScanPreviousVisibleNodeOrBlockBoundary( - &aEditingHost, + WSRunScanner::Scan::EditableNodes, prevVisibleThingOfStartBoundary .PointAtReachedContent(), BlockInlineCheck::UseComputedDisplayOutsideStyle); const WSScanResult nextVisibleThingOfPreviousBR = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, + WSRunScanner::Scan::EditableNodes, prevVisibleThingOfStartBoundary .PointAfterReachedContent(), BlockInlineCheck::UseComputedDisplayOutsideStyle); @@ -5028,8 +5031,9 @@ void HTMLEditor::AutoDeleteRangesHandler::AutoBlockElementsJoiner:: while (true) { WSScanResult scanResult = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - mLeftContent->AsElement(), scanStartPoint, - BlockInlineCheck::UseComputedDisplayOutsideStyle); + WSRunScanner::Scan::EditableNodes, scanStartPoint, + BlockInlineCheck::UseComputedDisplayOutsideStyle, + mLeftContent->AsElement()); if (scanResult.ReachedBlockBoundary() || scanResult.ReachedInlineEditingHostBoundary()) { return lastScanResult; @@ -5181,7 +5185,7 @@ Result HTMLEditor::AutoDeleteRangesHandler:: mMode != Mode::DeletePrecedingLinesAndContentInRange && HTMLEditUtils::PointIsImmediatelyBeforeCurrentBlockBoundary( EditorRawDOMPoint(aRangeToDelete.StartRef()), - HTMLEditUtils::IgnoreInvisibleLineBreak::Yes, aEditingHost); + HTMLEditUtils::IgnoreInvisibleLineBreak::Yes); const PutCaretTo putCaretTo = [&]() { // When we delete only preceding lines of the right child block, we should // put caret into start of the right block. @@ -5405,8 +5409,8 @@ Result HTMLEditor::AutoDeleteRangesHandler:: aHTMLEditor.RangeUpdaterRef(), &moveFirstLineResult); AutoTrackDOMPoint trackPointToPutCaret( aHTMLEditor.RangeUpdaterRef(), &pointToPutCaret); - nsresult rv = aHTMLEditor.EnsureNoFollowingUnnecessaryLineBreak( - aPoint, aEditingHost); + nsresult rv = + aHTMLEditor.EnsureNoFollowingUnnecessaryLineBreak(aPoint); NS_WARNING_ASSERTION( NS_SUCCEEDED(rv), "HTMLEditor::EnsureNoFollowingUnnecessaryLineBreak() failed"); @@ -5496,7 +5500,7 @@ Result HTMLEditor::AutoDeleteRangesHandler:: } WSScanResult nextThing = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, + WSRunScanner::Scan::EditableNodes, deleteContentResult.DeleteRangeRef().EndRef(), BlockInlineCheck::UseComputedDisplayOutsideStyle); return nextThing.ReachedBRElement() || @@ -5645,8 +5649,8 @@ nsresult HTMLEditor::AutoDeleteRangesHandler::DeleteUnnecessaryNodes( if (MOZ_LIKELY(range.EndRef().IsInContentNode())) { AutoTrackDOMRange trackRange(aHTMLEditor.RangeUpdaterRef(), &range); - nsresult rv = aHTMLEditor.EnsureNoFollowingUnnecessaryLineBreak( - range.EndRef(), aEditingHost); + nsresult rv = + aHTMLEditor.EnsureNoFollowingUnnecessaryLineBreak(range.EndRef()); if (NS_FAILED(rv)) { NS_WARNING("HTMLEditor::EnsureNoFollowingUnnecessaryLineBreak() failed"); return Err(rv); @@ -5737,7 +5741,7 @@ HTMLEditor::AutoDeleteRangesHandler::DeleteParentBlocksWithTransactionIfEmpty( if (wsScannerForPoint.GetEndReasonContent()->GetNextSibling()) { const WSScanResult scanResult = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - editingHost, + WSRunScanner::Scan::EditableNodes, EditorRawDOMPoint::After( *wsScannerForPoint.GetEndReasonContent()), BlockInlineCheck::UseComputedDisplayOutsideStyle); @@ -8349,7 +8353,7 @@ HTMLEditor::AutoDeleteRangesHandler::ExtendOrShrinkRangeToDelete( for (;;) { const WSScanResult backwardScanFromStartResult = WSRunScanner::ScanPreviousVisibleNodeOrBlockBoundary( - closestEditingHost, rangeToDelete.StartRef(), + WSRunScanner::Scan::EditableNodes, rangeToDelete.StartRef(), BlockInlineCheck::UseComputedDisplayOutsideStyle); if (!backwardScanFromStartResult.ReachedCurrentBlockBoundary() && !backwardScanFromStartResult.ReachedInlineEditingHostBoundary()) { diff --git a/editor/libeditor/HTMLEditorNestedClasses.h b/editor/libeditor/HTMLEditorNestedClasses.h index b6bf57c6dbff..f0cd45931a7c 100644 --- a/editor/libeditor/HTMLEditorNestedClasses.h +++ b/editor/libeditor/HTMLEditorNestedClasses.h @@ -78,8 +78,7 @@ class MOZ_STACK_CLASS HTMLEditor::AutoInlineStyleSetter final * See comments in the definition what this does. */ Result ExtendOrShrinkRangeToApplyTheStyle( - const HTMLEditor& aHTMLEditor, const EditorDOMRange& aRange, - const Element& aEditingHost) const; + const HTMLEditor& aHTMLEditor, const EditorDOMRange& aRange) const; /** * Returns next/previous sibling of aContent or an ancestor of it if it's @@ -104,7 +103,6 @@ class MOZ_STACK_CLASS HTMLEditor::AutoInlineStyleSetter final * @param aHTMLEditor The editor. * @param aCandidatePointToInsert The point where the caller wants to * insert new text. - * @param aEditingHost The editing host. * @return If this creates new empty text node returns it. * If this couldn't create new empty text node due to * the point or aEditingHost cannot have text node, @@ -112,9 +110,8 @@ class MOZ_STACK_CLASS HTMLEditor::AutoInlineStyleSetter final * Otherwise, returns error. */ [[nodiscard]] MOZ_CAN_RUN_SCRIPT static Result, nsresult> - GetEmptyTextNodeToApplyNewStyle(HTMLEditor& aHTMLEditor, - const EditorDOMPoint& aCandidatePointToInsert, - const Element& aEditingHost); + GetEmptyTextNodeToApplyNewStyle( + HTMLEditor& aHTMLEditor, const EditorDOMPoint& aCandidatePointToInsert); private: [[nodiscard]] MOZ_CAN_RUN_SCRIPT Result ApplyStyle( diff --git a/editor/libeditor/HTMLStyleEditor.cpp b/editor/libeditor/HTMLStyleEditor.cpp index 259d1c264b18..13582ac0844f 100644 --- a/editor/libeditor/HTMLStyleEditor.cpp +++ b/editor/libeditor/HTMLStyleEditor.cpp @@ -75,12 +75,10 @@ template nsresult HTMLEditor::SetInlinePropertiesAsSubAction( template nsresult HTMLEditor::SetInlinePropertiesAroundRanges( AutoClonedRangeArray& aRanges, - const AutoTArray& aStylesToSet, - const Element& aEditingHost); + const AutoTArray& aStylesToSet); template nsresult HTMLEditor::SetInlinePropertiesAroundRanges( AutoClonedRangeArray& aRanges, - const AutoTArray& aStylesToSet, - const Element& aEditingHost); + const AutoTArray& aStylesToSet); nsresult HTMLEditor::SetInlinePropertyAsAction(nsStaticAtom& aProperty, nsStaticAtom* aAttribute, @@ -310,8 +308,7 @@ nsresult HTMLEditor::SetInlinePropertiesAsSubAction( AutoTransactionsConserveSelection dontChangeMySelection(*this); AutoClonedSelectionRangeArray selectionRanges(SelectionRef()); - nsresult rv = SetInlinePropertiesAroundRanges(selectionRanges, aStylesToSet, - aEditingHost); + nsresult rv = SetInlinePropertiesAroundRanges(selectionRanges, aStylesToSet); if (NS_FAILED(rv)) { NS_WARNING("HTMLEditor::SetInlinePropertiesAroundRanges() failed"); return rv; @@ -329,8 +326,7 @@ nsresult HTMLEditor::SetInlinePropertiesAsSubAction( template nsresult HTMLEditor::SetInlinePropertiesAroundRanges( AutoClonedRangeArray& aRanges, - const AutoTArray& aStylesToSet, - const Element& aEditingHost) { + const AutoTArray& aStylesToSet) { MOZ_ASSERT(!aRanges.HasSavedRanges()); for (const EditorInlineStyleAndValue& styleToSet : aStylesToSet) { AutoInlineStyleSetter inlineStyleSetter(styleToSet); @@ -371,8 +367,7 @@ nsresult HTMLEditor::SetInlinePropertiesAroundRanges( } } Result rangeOrError = - inlineStyleSetter.ExtendOrShrinkRangeToApplyTheStyle(*this, range, - aEditingHost); + inlineStyleSetter.ExtendOrShrinkRangeToApplyTheStyle(*this, range); if (MOZ_UNLIKELY(rangeOrError.isErr())) { NS_WARNING( "HTMLEditor::ExtendOrShrinkRangeToApplyTheStyle() failed, but " @@ -394,7 +389,7 @@ nsresult HTMLEditor::SetInlinePropertiesAroundRanges( if (range.Collapsed()) { Result, nsresult> emptyTextNodeOrError = AutoInlineStyleSetter::GetEmptyTextNodeToApplyNewStyle( - *this, range.StartRef(), aEditingHost); + *this, range.StartRef()); if (MOZ_UNLIKELY(emptyTextNodeOrError.isErr())) { NS_WARNING( "AutoInlineStyleSetter::GetEmptyTextNodeToApplyNewStyle() " @@ -579,11 +574,10 @@ nsresult HTMLEditor::SetInlinePropertiesAroundRanges( // static Result, nsresult> HTMLEditor::AutoInlineStyleSetter::GetEmptyTextNodeToApplyNewStyle( - HTMLEditor& aHTMLEditor, const EditorDOMPoint& aCandidatePointToInsert, - const Element& aEditingHost) { + HTMLEditor& aHTMLEditor, const EditorDOMPoint& aCandidatePointToInsert) { auto pointToInsertNewText = HTMLEditUtils::GetBetterCaretPositionToInsertText( - aCandidatePointToInsert, aEditingHost); + aCandidatePointToInsert); if (MOZ_UNLIKELY(!pointToInsertNewText.IsSet())) { return RefPtr(); // cannot insert text there } @@ -1925,8 +1919,7 @@ EditorRawDOMRange HTMLEditor::AutoInlineStyleSetter:: Result HTMLEditor::AutoInlineStyleSetter::ExtendOrShrinkRangeToApplyTheStyle( - const HTMLEditor& aHTMLEditor, const EditorDOMRange& aRange, - const Element& aEditingHost) const { + const HTMLEditor& aHTMLEditor, const EditorDOMRange& aRange) const { if (NS_WARN_IF(!aRange.IsPositioned())) { return Err(NS_ERROR_FAILURE); } @@ -1945,7 +1938,7 @@ HTMLEditor::AutoInlineStyleSetter::ExtendOrShrinkRangeToApplyTheStyle( if (range.EndRef().IsInContentNode()) { const WSScanResult nextContentData = WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( - &aEditingHost, range.EndRef(), + WSRunScanner::Scan::EditableNodes, range.EndRef(), BlockInlineCheck::UseComputedDisplayOutsideStyle); if (nextContentData.ReachedInvisibleBRElement() && nextContentData.BRElementPtr()->GetParentElement() && diff --git a/editor/libeditor/WSRunScanner.h b/editor/libeditor/WSRunScanner.h index d7a1960112f0..7660840c74e4 100644 --- a/editor/libeditor/WSRunScanner.h +++ b/editor/libeditor/WSRunScanner.h @@ -238,6 +238,11 @@ class MOZ_STACK_CLASS WSScanResult final { */ bool IsContentEditable() const { return mContent && mContent->IsEditable(); } + [[nodiscard]] bool IsContentEditableRoot() const { + return mContent && mContent->IsElement() && + HTMLEditUtils::ElementIsEditableRoot(*mContent->AsElement()); + } + /** * Offset_Deprecated() returns meaningful value only when * InVisibleOrCollapsibleCharacters() returns true or the scanner reached to @@ -455,10 +460,10 @@ class MOZ_STACK_CLASS WSRunScanner final { const EditorDOMPointBase& aPoint) const; template static WSScanResult ScanInclusiveNextVisibleNodeOrBlockBoundary( - const Element* aEditingHost, const EditorDOMPointBase& aPoint, - BlockInlineCheck aBlockInlineCheck) { - return WSRunScanner(Scan::EditableNodes, aPoint, aBlockInlineCheck, - aEditingHost) + Scan aScanMode, const EditorDOMPointBase& aPoint, + BlockInlineCheck aBlockInlineCheck, + const Element* aAncestorLimiter = nullptr) { + return WSRunScanner(aScanMode, aPoint, aBlockInlineCheck, aAncestorLimiter) .ScanInclusiveNextVisibleNodeOrBlockBoundaryFrom(aPoint); } @@ -473,10 +478,10 @@ class MOZ_STACK_CLASS WSRunScanner final { const EditorDOMPointBase& aPoint) const; template static WSScanResult ScanPreviousVisibleNodeOrBlockBoundary( - const Element* aEditingHost, const EditorDOMPointBase& aPoint, - BlockInlineCheck aBlockInlineCheck) { - return WSRunScanner(Scan::EditableNodes, aPoint, aBlockInlineCheck, - aEditingHost) + Scan aScanMode, const EditorDOMPointBase& aPoint, + BlockInlineCheck aBlockInlineCheck, + const Element* aAncestorLimiter = nullptr) { + return WSRunScanner(aScanMode, aPoint, aBlockInlineCheck, aAncestorLimiter) .ScanPreviousVisibleNodeOrBlockBoundaryFrom(aPoint); }