From 733498ac35ff1c47da4be9045e4580d1512bd4f0 Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Sat, 23 Apr 2016 19:28:17 +0900 Subject: [PATCH] Bug 1190172 part 9 - Clean up nsHTMLEditor::ReturnInHeader; r=ehsan --- editor/libeditor/nsHTMLEditRules.cpp | 125 ++++++++++++--------------- editor/libeditor/nsHTMLEditRules.h | 4 +- 2 files changed, 56 insertions(+), 73 deletions(-) diff --git a/editor/libeditor/nsHTMLEditRules.cpp b/editor/libeditor/nsHTMLEditRules.cpp index 446d881f4f9d..9239eeeb764d 100644 --- a/editor/libeditor/nsHTMLEditRules.cpp +++ b/editor/libeditor/nsHTMLEditRules.cpp @@ -1571,8 +1571,7 @@ nsHTMLEditRules::WillInsertBreak(Selection& aSelection, bool* aCancel, return NS_OK; } else if (nsHTMLEditUtils::IsHeader(*blockParent)) { // Headers: close (or split) header - ReturnInHeader(&aSelection, GetAsDOMNode(blockParent), GetAsDOMNode(node), - offset); + ReturnInHeader(aSelection, *blockParent, node, offset); *aHandled = true; return NS_OK; } else if (blockParent->IsHTMLElement(nsGkAtoms::p)) { @@ -6287,101 +6286,85 @@ nsHTMLEditRules::IsInListItem(nsINode* aNode) } -/////////////////////////////////////////////////////////////////////////// -// ReturnInHeader: do the right thing for returns pressed in headers -// +/** + * ReturnInHeader: do the right thing for returns pressed in headers + */ nsresult -nsHTMLEditRules::ReturnInHeader(Selection* aSelection, - nsIDOMNode *aHeader, - nsIDOMNode *aNode, +nsHTMLEditRules::ReturnInHeader(Selection& aSelection, + Element& aHeader, + nsINode& aNode, int32_t aOffset) { - nsCOMPtr header = do_QueryInterface(aHeader); - nsCOMPtr node = do_QueryInterface(aNode); - NS_ENSURE_TRUE(aSelection && header && node, NS_ERROR_NULL_POINTER); - - // remeber where the header is - int32_t offset; - nsCOMPtr headerParent = nsEditor::GetNodeLocation(aHeader, &offset); - - // get ws code to adjust any ws NS_ENSURE_STATE(mHTMLEditor); + nsCOMPtr kungFuDeathGrip(mHTMLEditor); + + // Remember where the header is + nsCOMPtr headerParent = aHeader.GetParentNode(); + int32_t offset = headerParent ? headerParent->IndexOf(&aHeader) : -1; + + // Get ws code to adjust any ws + nsCOMPtr node = &aNode; nsresult res = nsWSRunObject::PrepareToSplitAcrossBlocks(mHTMLEditor, address_of(node), &aOffset); NS_ENSURE_SUCCESS(res, res); - // split the header + // Split the header NS_ENSURE_STATE(node->IsContent()); - NS_ENSURE_STATE(mHTMLEditor); - mHTMLEditor->SplitNodeDeep(*header, *node->AsContent(), aOffset); + mHTMLEditor->SplitNodeDeep(aHeader, *node->AsContent(), aOffset); - // if the leftand heading is empty, put a mozbr in it - nsCOMPtr prevItem; - NS_ENSURE_STATE(mHTMLEditor); - mHTMLEditor->GetPriorHTMLSibling(aHeader, address_of(prevItem)); - if (prevItem && nsHTMLEditUtils::IsHeader(prevItem)) - { - bool bIsEmptyNode; - NS_ENSURE_STATE(mHTMLEditor); - res = mHTMLEditor->IsEmptyNode(prevItem, &bIsEmptyNode); + // If the left-hand heading is empty, put a mozbr in it + nsCOMPtr prevItem = mHTMLEditor->GetPriorHTMLSibling(&aHeader); + if (prevItem && nsHTMLEditUtils::IsHeader(*prevItem)) { + bool isEmptyNode; + res = mHTMLEditor->IsEmptyNode(prevItem, &isEmptyNode); NS_ENSURE_SUCCESS(res, res); - if (bIsEmptyNode) { - res = CreateMozBR(prevItem, 0); + if (isEmptyNode) { + res = CreateMozBR(prevItem->AsDOMNode(), 0); NS_ENSURE_SUCCESS(res, res); } } - // if the new (righthand) header node is empty, delete it + // If the new (righthand) header node is empty, delete it bool isEmpty; - res = IsEmptyBlock(aHeader, &isEmpty, true); + res = IsEmptyBlock(aHeader.AsDOMNode(), &isEmpty, true); NS_ENSURE_SUCCESS(res, res); - if (isEmpty) - { - NS_ENSURE_STATE(mHTMLEditor); - res = mHTMLEditor->DeleteNode(aHeader); + if (isEmpty) { + res = mHTMLEditor->DeleteNode(&aHeader); NS_ENSURE_SUCCESS(res, res); - // layout tells the caret to blink in a weird place - // if we don't place a break after the header. - nsCOMPtr sibling; - NS_ENSURE_STATE(mHTMLEditor); - res = mHTMLEditor->GetNextHTMLSibling(headerParent, offset+1, address_of(sibling)); - NS_ENSURE_SUCCESS(res, res); - if (!sibling || !nsTextEditUtils::IsBreak(sibling)) - { + // Layout tells the caret to blink in a weird place if we don't place a + // break after the header. + nsCOMPtr sibling = + mHTMLEditor->GetNextHTMLSibling(headerParent, offset + 1); + if (!sibling || !sibling->IsHTMLElement(nsGkAtoms::br)) { ClearCachedStyles(); - NS_ENSURE_STATE(mHTMLEditor); mHTMLEditor->mTypeInState->ClearAllProps(); - // create a paragraph - NS_NAMED_LITERAL_STRING(pType, "p"); - nsCOMPtr pNode; - NS_ENSURE_STATE(mHTMLEditor); - res = mHTMLEditor->CreateNode(pType, headerParent, offset+1, getter_AddRefs(pNode)); - NS_ENSURE_SUCCESS(res, res); + // Create a paragraph + nsCOMPtr pNode = + mHTMLEditor->CreateNode(nsGkAtoms::p, headerParent, offset + 1); + NS_ENSURE_STATE(pNode); - // append a
to it - nsCOMPtr brNode; - NS_ENSURE_STATE(mHTMLEditor); - res = mHTMLEditor->CreateBR(pNode, 0, address_of(brNode)); - NS_ENSURE_SUCCESS(res, res); + // Append a
to it + nsCOMPtr brNode = mHTMLEditor->CreateBR(pNode, 0); + NS_ENSURE_STATE(brNode); - // set selection to before the break - res = aSelection->Collapse(pNode, 0); - } - else - { - headerParent = nsEditor::GetNodeLocation(sibling, &offset); - // put selection after break - res = aSelection->Collapse(headerParent,offset+1); + // Set selection to before the break + res = aSelection.Collapse(pNode, 0); + NS_ENSURE_SUCCESS(res, res); + } else { + headerParent = sibling->GetParentNode(); + offset = headerParent ? headerParent->IndexOf(sibling) : -1; + // Put selection after break + res = aSelection.Collapse(headerParent, offset + 1); + NS_ENSURE_SUCCESS(res, res); } + } else { + // Put selection at front of righthand heading + res = aSelection.Collapse(&aHeader, 0); + NS_ENSURE_SUCCESS(res, res); } - else - { - // put selection at front of righthand heading - res = aSelection->Collapse(aHeader,0); - } - return res; + return NS_OK; } /////////////////////////////////////////////////////////////////////////// diff --git a/editor/libeditor/nsHTMLEditRules.h b/editor/libeditor/nsHTMLEditRules.h index 18c2fc960bcc..12eb1c4949da 100644 --- a/editor/libeditor/nsHTMLEditRules.h +++ b/editor/libeditor/nsHTMLEditRules.h @@ -205,8 +205,8 @@ protected: int32_t* aIndex, Lists aLists = Lists::yes, Tables aTables = Tables::yes); mozilla::dom::Element* IsInListItem(nsINode* aNode); - nsresult ReturnInHeader(mozilla::dom::Selection* aSelection, - nsIDOMNode* aHeader, nsIDOMNode* aTextNode, + nsresult ReturnInHeader(mozilla::dom::Selection& aSelection, + mozilla::dom::Element& aHeader, nsINode& aNode, int32_t aOffset); nsresult ReturnInParagraph(mozilla::dom::Selection* aSelection, nsIDOMNode* aHeader, nsIDOMNode* aTextNode,