Bug 1574852 - part 12: Move HTMLEditRules::SplitMailCites() to HTMLEditor r=m_kato

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Masayuki Nakano 2019-08-22 08:29:16 +00:00
parent afaaa019c8
commit d0797846a4
3 changed files with 46 additions and 48 deletions

View File

@ -1673,7 +1673,13 @@ EditActionResult HTMLEditRules::WillInsertParagraphSeparator() {
// Split any mailcites in the way. Should we abort this if we encounter // Split any mailcites in the way. Should we abort this if we encounter
// table cell boundaries? // table cell boundaries?
if (IsMailEditor()) { if (IsMailEditor()) {
EditActionResult result = SplitMailCites(); EditorDOMPoint pointToSplit(EditorBase::GetStartPoint(*SelectionRefPtr()));
if (NS_WARN_IF(!pointToSplit.IsSet())) {
return EditActionIgnored(NS_ERROR_FAILURE);
}
EditActionResult result =
MOZ_KnownLive(HTMLEditorRef()).SplitMailCiteElements(pointToSplit);
if (NS_WARN_IF(result.Failed())) { if (NS_WARN_IF(result.Failed())) {
return result; return result;
} }
@ -2039,20 +2045,19 @@ nsresult HTMLEditRules::InsertBRElement(const EditorDOMPoint& aPointToBreak) {
return NS_OK; return NS_OK;
} }
EditActionResult HTMLEditRules::SplitMailCites() { EditActionResult HTMLEditor::SplitMailCiteElements(
MOZ_ASSERT(IsEditorDataAvailable()); const EditorDOMPoint& aPointToSplit) {
MOZ_ASSERT(IsEditActionDataAvailable());
MOZ_ASSERT(aPointToSplit.IsSet());
EditorDOMPoint pointToSplit(EditorBase::GetStartPoint(*SelectionRefPtr())); RefPtr<Element> citeNode =
if (NS_WARN_IF(!pointToSplit.IsSet())) { GetMostAncestorMailCiteElement(*aPointToSplit.GetContainer());
return EditActionIgnored(NS_ERROR_FAILURE);
}
RefPtr<Element> citeNode = HTMLEditorRef().GetMostAncestorMailCiteElement(
*pointToSplit.GetContainer());
if (!citeNode) { if (!citeNode) {
return EditActionIgnored(); return EditActionIgnored();
} }
EditorDOMPoint pointToSplit(aPointToSplit);
// If our selection is just before a break, nudge it to be just after it. // If our selection is just before a break, nudge it to be just after it.
// This does two things for us. It saves us the trouble of having to add // This does two things for us. It saves us the trouble of having to add
// a break here ourselves to preserve the "blockness" of the inline span // a break here ourselves to preserve the "blockness" of the inline span
@ -2062,7 +2067,7 @@ EditActionResult HTMLEditRules::SplitMailCites() {
// The latter can confuse a user if they click there and start typing, // The latter can confuse a user if they click there and start typing,
// because being in the mailquote may affect wrapping behavior, or font // because being in the mailquote may affect wrapping behavior, or font
// color, etc. // color, etc.
WSRunObject wsObj(&HTMLEditorRef(), pointToSplit); WSRunObject wsObj(this, pointToSplit);
nsCOMPtr<nsINode> visNode; nsCOMPtr<nsINode> visNode;
WSType wsType; WSType wsType;
wsObj.NextVisibleNode(pointToSplit, address_of(visNode), nullptr, &wsType); wsObj.NextVisibleNode(pointToSplit, address_of(visNode), nullptr, &wsType);
@ -2080,12 +2085,9 @@ EditActionResult HTMLEditRules::SplitMailCites() {
return EditActionIgnored(NS_ERROR_FAILURE); return EditActionIgnored(NS_ERROR_FAILURE);
} }
SplitNodeResult splitCiteNodeResult = SplitNodeResult splitCiteNodeResult = SplitNodeDeepWithTransaction(
MOZ_KnownLive(HTMLEditorRef()) *citeNode, pointToSplit, SplitAtEdges::eDoNotCreateEmptyContainer);
.SplitNodeDeepWithTransaction( if (NS_WARN_IF(Destroyed())) {
*citeNode, pointToSplit,
SplitAtEdges::eDoNotCreateEmptyContainer);
if (NS_WARN_IF(!CanHandleEditAction())) {
return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED); return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED);
} }
if (NS_WARN_IF(splitCiteNodeResult.Failed())) { if (NS_WARN_IF(splitCiteNodeResult.Failed())) {
@ -2113,9 +2115,8 @@ EditActionResult HTMLEditRules::SplitMailCites() {
EditorDOMPoint endOfPreviousNodeOfSplitPoint; EditorDOMPoint endOfPreviousNodeOfSplitPoint;
endOfPreviousNodeOfSplitPoint.SetToEndOf(previousNodeOfSplitPoint); endOfPreviousNodeOfSplitPoint.SetToEndOf(previousNodeOfSplitPoint);
RefPtr<Element> invisibleBrElement = RefPtr<Element> invisibleBrElement =
MOZ_KnownLive(HTMLEditorRef()) InsertBRElementWithTransaction(endOfPreviousNodeOfSplitPoint);
.InsertBRElementWithTransaction(endOfPreviousNodeOfSplitPoint); if (NS_WARN_IF(Destroyed())) {
if (NS_WARN_IF(!CanHandleEditAction())) {
return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED); return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED);
} }
NS_WARNING_ASSERTION(invisibleBrElement, NS_WARNING_ASSERTION(invisibleBrElement,
@ -2128,9 +2129,8 @@ EditActionResult HTMLEditRules::SplitMailCites() {
// cite node, <br> should be inserted before the current cite. // cite node, <br> should be inserted before the current cite.
EditorDOMPoint pointToInsertBrNode(splitCiteNodeResult.SplitPoint()); EditorDOMPoint pointToInsertBrNode(splitCiteNodeResult.SplitPoint());
RefPtr<Element> brElement = RefPtr<Element> brElement =
MOZ_KnownLive(HTMLEditorRef()) InsertBRElementWithTransaction(pointToInsertBrNode);
.InsertBRElementWithTransaction(pointToInsertBrNode); if (NS_WARN_IF(Destroyed())) {
if (NS_WARN_IF(!CanHandleEditAction())) {
return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED); return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED);
} }
if (NS_WARN_IF(!brElement)) { if (NS_WARN_IF(!brElement)) {
@ -2147,7 +2147,7 @@ EditActionResult HTMLEditRules::SplitMailCites() {
NS_WARNING_ASSERTION(!error.Failed(), "Failed to set interline position"); NS_WARNING_ASSERTION(!error.Failed(), "Failed to set interline position");
error = NS_OK; error = NS_OK;
SelectionRefPtr()->Collapse(atBrNode, error); SelectionRefPtr()->Collapse(atBrNode, error);
if (NS_WARN_IF(!CanHandleEditAction())) { if (NS_WARN_IF(Destroyed())) {
error.SuppressException(); error.SuppressException();
return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED); return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED);
} }
@ -2164,7 +2164,7 @@ EditActionResult HTMLEditRules::SplitMailCites() {
EditorDOMPoint pointToCreateNewBrNode(atBrNode.GetContainer(), EditorDOMPoint pointToCreateNewBrNode(atBrNode.GetContainer(),
atBrNode.Offset()); atBrNode.Offset());
WSRunObject wsObj(&HTMLEditorRef(), pointToCreateNewBrNode); WSRunObject wsObj(this, pointToCreateNewBrNode);
WSType wsType; WSType wsType;
wsObj.PriorVisibleNode(pointToCreateNewBrNode, nullptr, nullptr, &wsType); wsObj.PriorVisibleNode(pointToCreateNewBrNode, nullptr, nullptr, &wsType);
if (wsType == WSType::normalWS || wsType == WSType::text || if (wsType == WSType::normalWS || wsType == WSType::text ||
@ -2173,15 +2173,14 @@ EditActionResult HTMLEditRules::SplitMailCites() {
DebugOnly<bool> advanced = pointAfterNewBrNode.AdvanceOffset(); DebugOnly<bool> advanced = pointAfterNewBrNode.AdvanceOffset();
NS_WARNING_ASSERTION(advanced, NS_WARNING_ASSERTION(advanced,
"Failed to advance offset after the <br> node"); "Failed to advance offset after the <br> node");
WSRunObject wsObjAfterBR(&HTMLEditorRef(), pointAfterNewBrNode); WSRunObject wsObjAfterBR(this, pointAfterNewBrNode);
wsObjAfterBR.NextVisibleNode(pointAfterNewBrNode, &wsType); wsObjAfterBR.NextVisibleNode(pointAfterNewBrNode, &wsType);
if (wsType == WSType::normalWS || wsType == WSType::text || if (wsType == WSType::normalWS || wsType == WSType::text ||
wsType == WSType::special || wsType == WSType::special ||
// In case we're at the very end. // In case we're at the very end.
wsType == WSType::thisBlock) { wsType == WSType::thisBlock) {
brElement = MOZ_KnownLive(HTMLEditorRef()) brElement = InsertBRElementWithTransaction(pointToCreateNewBrNode);
.InsertBRElementWithTransaction(pointToCreateNewBrNode); if (NS_WARN_IF(Destroyed())) {
if (NS_WARN_IF(!CanHandleEditAction())) {
return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED); return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED);
} }
if (NS_WARN_IF(!brElement)) { if (NS_WARN_IF(!brElement)) {
@ -2197,16 +2196,14 @@ EditActionResult HTMLEditRules::SplitMailCites() {
// delete any empty cites // delete any empty cites
bool bEmptyCite = false; bool bEmptyCite = false;
if (previousNodeOfSplitPoint) { if (previousNodeOfSplitPoint) {
nsresult rv = HTMLEditorRef().IsEmptyNode(previousNodeOfSplitPoint, nsresult rv =
&bEmptyCite, true, false); IsEmptyNode(previousNodeOfSplitPoint, &bEmptyCite, true, false);
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(NS_FAILED(rv))) {
return EditActionIgnored(rv); return EditActionIgnored(rv);
} }
if (bEmptyCite) { if (bEmptyCite) {
rv = MOZ_KnownLive(HTMLEditorRef()) rv = DeleteNodeWithTransaction(MOZ_KnownLive(*previousNodeOfSplitPoint));
.DeleteNodeWithTransaction( if (NS_WARN_IF(Destroyed())) {
MOZ_KnownLive(*previousNodeOfSplitPoint));
if (NS_WARN_IF(!CanHandleEditAction())) {
return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED); return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED);
} }
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(NS_FAILED(rv))) {
@ -2216,14 +2213,13 @@ EditActionResult HTMLEditRules::SplitMailCites() {
} }
if (citeNode) { if (citeNode) {
nsresult rv = nsresult rv = IsEmptyNode(citeNode, &bEmptyCite, true, false);
HTMLEditorRef().IsEmptyNode(citeNode, &bEmptyCite, true, false);
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(NS_FAILED(rv))) {
return EditActionIgnored(rv); return EditActionIgnored(rv);
} }
if (bEmptyCite) { if (bEmptyCite) {
rv = MOZ_KnownLive(HTMLEditorRef()).DeleteNodeWithTransaction(*citeNode); rv = DeleteNodeWithTransaction(*citeNode);
if (NS_WARN_IF(!CanHandleEditAction())) { if (NS_WARN_IF(Destroyed())) {
return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED); return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED);
} }
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(NS_FAILED(rv))) {

View File

@ -148,15 +148,6 @@ class HTMLEditRules : public TextEditRules {
MOZ_CAN_RUN_SCRIPT MOZ_CAN_RUN_SCRIPT
MOZ_MUST_USE nsresult InsertBRElement(const EditorDOMPoint& aInsertToBreak); MOZ_MUST_USE nsresult InsertBRElement(const EditorDOMPoint& aInsertToBreak);
/**
* SplitMailCites() splits mail-cite elements at start of Selection if
* Selection starts from inside a mail-cite element. Of course, if it's
* necessary, this inserts <br> node to new left nodes or existing right
* nodes.
*/
MOZ_CAN_RUN_SCRIPT
MOZ_MUST_USE EditActionResult SplitMailCites();
/** /**
* Called before deleting selected contents. This method actually removes * Called before deleting selected contents. This method actually removes
* selected contents. * selected contents.

View File

@ -42,6 +42,7 @@ class nsRange;
namespace mozilla { namespace mozilla {
class AutoSelectionSetterAfterTableEdit; class AutoSelectionSetterAfterTableEdit;
class AutoSetTemporaryAncestorLimiter; class AutoSetTemporaryAncestorLimiter;
class EditActionResult;
class EmptyEditableFunctor; class EmptyEditableFunctor;
class ResizerSelectionListener; class ResizerSelectionListener;
enum class EditSubAction : int32_t; enum class EditSubAction : int32_t;
@ -1176,6 +1177,16 @@ class HTMLEditor final : public TextEditor,
*/ */
Element* GetMostAncestorMailCiteElement(nsINode& aNode) const; Element* GetMostAncestorMailCiteElement(nsINode& aNode) const;
/**
* SplitMailCiteElements() splits mail-cite elements at start of Selection if
* Selection starts from inside a mail-cite element. Of course, if it's
* necessary, this inserts <br> node to new left nodes or existing right
* nodes.
* XXX This modifies Selection, but should return SplitNodeResult() instead.
*/
MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE EditActionResult
SplitMailCiteElements(const EditorDOMPoint& aPointToSplit);
protected: // Called by helper classes. protected: // Called by helper classes.
virtual void OnStartToHandleTopLevelEditSubAction( virtual void OnStartToHandleTopLevelEditSubAction(
EditSubAction aEditSubAction, nsIEditor::EDirection aDirection) override; EditSubAction aEditSubAction, nsIEditor::EDirection aDirection) override;