From aff11ba1b350e442e88d72b11a4ada8fcf054864 Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Sun, 11 Oct 2015 18:59:11 +0300 Subject: [PATCH] Bug 1156062 part 3 - New helper nsHTMLEditor::GetBlock; r=ehsan --- editor/libeditor/nsHTMLEditRules.cpp | 81 ++++++---------------------- editor/libeditor/nsHTMLEditor.cpp | 40 ++++++-------- editor/libeditor/nsHTMLEditor.h | 1 + 3 files changed, 33 insertions(+), 89 deletions(-) diff --git a/editor/libeditor/nsHTMLEditRules.cpp b/editor/libeditor/nsHTMLEditRules.cpp index 24ede7d7ebc2..7b238482ccff 100644 --- a/editor/libeditor/nsHTMLEditRules.cpp +++ b/editor/libeditor/nsHTMLEditRules.cpp @@ -849,14 +849,8 @@ nsHTMLEditRules::GetAlignment(bool *aMixed, nsIHTMLEditor::EAlignment *aAlign) NS_NAMED_LITERAL_STRING(typeAttrName, "align"); nsIAtom *dummyProperty = nullptr; - nsCOMPtr blockParent; NS_ENSURE_STATE(mHTMLEditor); - if (mHTMLEditor->IsBlockNode(nodeToExamine)) - blockParent = nodeToExamine->AsElement(); - else { - NS_ENSURE_STATE(mHTMLEditor); - blockParent = mHTMLEditor->GetBlockNodeParent(nodeToExamine); - } + nsCOMPtr blockParent = mHTMLEditor->GetBlock(*nodeToExamine); NS_ENSURE_TRUE(blockParent, NS_ERROR_FAILURE); @@ -2339,21 +2333,9 @@ nsHTMLEditRules::WillDeleteSelection(Selection* aSelection, } // Figure out block parents - nsCOMPtr leftParent; - if (IsBlockNode(GetAsDOMNode(startNode))) { - leftParent = startNode->AsElement(); - } else { - NS_ENSURE_STATE(mHTMLEditor); - leftParent = mHTMLEditor->GetBlockNodeParent(startNode); - } - - nsCOMPtr rightParent; - if (IsBlockNode(GetAsDOMNode(endNode))) { - rightParent = endNode->AsElement(); - } else { - NS_ENSURE_STATE(mHTMLEditor); - rightParent = mHTMLEditor->GetBlockNodeParent(endNode); - } + NS_ENSURE_STATE(mHTMLEditor); + nsCOMPtr leftParent = mHTMLEditor->GetBlock(*startNode); + nsCOMPtr rightParent = mHTMLEditor->GetBlock(*endNode); // Are endpoint block parents the same? Use default deletion if (leftParent && leftParent == rightParent) { @@ -3459,13 +3441,8 @@ nsHTMLEditRules::WillMakeBasicBlock(Selection* aSelection, if (tString.EqualsLiteral("normal") || tString.IsEmpty() ) // we are removing blocks (going to "body text") { - nsCOMPtr curBlock; - if (IsBlockNode(GetAsDOMNode(parent))) { - curBlock = parent->AsElement(); - } else { - NS_ENSURE_STATE(mHTMLEditor); - curBlock = mHTMLEditor->GetBlockNodeParent(parent); - } + NS_ENSURE_STATE(mHTMLEditor); + nsCOMPtr curBlock = mHTMLEditor->GetBlock(*parent); NS_ENSURE_TRUE(curBlock, NS_ERROR_NULL_POINTER); nsCOMPtr curBlockPar = GetAsDOMNode(curBlock->GetParentNode()); @@ -3617,17 +3594,11 @@ nsHTMLEditRules::WillCSSIndent(Selection* aSelection, nsCOMPtr liNode; if (aSelection->Collapsed()) { nsCOMPtr node; - nsCOMPtr block; int32_t offset; NS_ENSURE_STATE(mHTMLEditor); nsresult res = mHTMLEditor->GetStartNodeAndOffset(aSelection, getter_AddRefs(node), &offset); NS_ENSURE_SUCCESS(res, res); - if (IsBlockNode(GetAsDOMNode(node))) { - block = node->AsElement(); - } else { - NS_ENSURE_STATE(mHTMLEditor); - block = mHTMLEditor->GetBlockNodeParent(node); - } + nsCOMPtr block = mHTMLEditor->GetBlock(*node); if (block && nsHTMLEditUtils::IsListItem(block)) liNode = block; } @@ -4978,27 +4949,23 @@ nsHTMLEditRules::CheckForEmptyBlock(nsINode* aStartNode, if (IsInlineNode(GetAsDOMNode(aBodyNode))) { return NS_OK; } + NS_ENSURE_STATE(mHTMLEditor); + nsCOMPtr kungFuDeathGrip(mHTMLEditor); + // If we are inside an empty block, delete it. Note: do NOT delete table // elements this way. - nsCOMPtr block; - if (IsBlockNode(GetAsDOMNode(aStartNode))) { - block = aStartNode->AsElement(); - } else { - block = mHTMLEditor->GetBlockNodeParent(aStartNode); - } + nsCOMPtr block = mHTMLEditor->GetBlock(*aStartNode); bool bIsEmptyNode; nsCOMPtr emptyBlock; nsresult res; if (block && block != aBodyNode) { // Efficiency hack, avoiding IsEmptyNode() call when in body - NS_ENSURE_STATE(mHTMLEditor); res = mHTMLEditor->IsEmptyNode(block, &bIsEmptyNode, true, false); NS_ENSURE_SUCCESS(res, res); while (block && bIsEmptyNode && !nsHTMLEditUtils::IsTableElement(block) && block != aBodyNode) { emptyBlock = block; block = mHTMLEditor->GetBlockNodeParent(emptyBlock); - NS_ENSURE_STATE(mHTMLEditor); if (block) { res = mHTMLEditor->IsEmptyNode(block, &bIsEmptyNode, true, false); NS_ENSURE_SUCCESS(res, res); @@ -5719,12 +5686,7 @@ nsHTMLEditRules::PromoteRange(nsRange& aRange, EditAction aOperationType) // inside block elements that contain nothing but a solo
. It's easier // to put a workaround here than to revamp GetPromotedPoint. :-( if (startNode == endNode && startOffset == endOffset) { - nsCOMPtr block; - if (IsBlockNode(GetAsDOMNode(startNode))) { - block = startNode->AsElement(); - } else { - block = mHTMLEditor->GetBlockNodeParent(startNode); - } + nsCOMPtr block = mHTMLEditor->GetBlock(*startNode); if (block) { bool bIsEmptyNode = false; nsCOMPtr root = mHTMLEditor->GetActiveEditingHost(); @@ -7549,14 +7511,9 @@ nsHTMLEditRules::AdjustSelection(Selection* aSelection, // make sure we aren't in an empty block - user will see no cursor. If this // is happening, put a
in the block if allowed. - nsCOMPtr theblock; - if (IsBlockNode(GetAsDOMNode(selNode))) { - theblock = selNode; - } else { - NS_ENSURE_STATE(mHTMLEditor); - theblock = mHTMLEditor->GetBlockNodeParent(selNode); - } NS_ENSURE_STATE(mHTMLEditor); + nsCOMPtr theblock = mHTMLEditor->GetBlock(*selNode); + if (theblock && mHTMLEditor->IsEditable(theblock)) { bool bIsEmptyNode; NS_ENSURE_STATE(mHTMLEditor); @@ -7597,15 +7554,9 @@ nsHTMLEditRules::AdjustSelection(Selection* aSelection, if (nearNode) { // is nearNode also a descendant of same block? - nsCOMPtr block, nearBlock; - if (IsBlockNode(GetAsDOMNode(selNode))) { - block = selNode; - } else { - NS_ENSURE_STATE(mHTMLEditor); - block = mHTMLEditor->GetBlockNodeParent(selNode); - } NS_ENSURE_STATE(mHTMLEditor); - nearBlock = mHTMLEditor->GetBlockNodeParent(nearNode); + nsCOMPtr block = mHTMLEditor->GetBlock(*selNode); + nsCOMPtr nearBlock = mHTMLEditor->GetBlockNodeParent(nearNode); if (block && block == nearBlock) { if (nearNode && nsTextEditUtils::IsBreak(nearNode) ) { diff --git a/editor/libeditor/nsHTMLEditor.cpp b/editor/libeditor/nsHTMLEditor.cpp index ef25637e6b11..c169c38f6cc2 100644 --- a/editor/libeditor/nsHTMLEditor.cpp +++ b/editor/libeditor/nsHTMLEditor.cpp @@ -630,12 +630,7 @@ nsHTMLEditor::HandleKeyPressEvent(nsIDOMKeyEvent* aKeyEvent) nsCOMPtr node = selection->GetRangeAt(0)->GetStartParent(); MOZ_ASSERT(node); - nsCOMPtr blockParent; - if (IsBlockNode(node)) { - blockParent = node; - } else { - blockParent = GetBlockNodeParent(node); - } + nsCOMPtr blockParent = GetBlock(*node); if (!blockParent) { break; @@ -847,6 +842,18 @@ nsHTMLEditor::GetBlockNodeParent(nsIDOMNode *aNode) return GetAsDOMNode(GetBlockNodeParent(node)); } +/** + * Returns the node if it's a block, otherwise GetBlockNodeParent + */ +Element* +nsHTMLEditor::GetBlock(nsINode& aNode) +{ + if (NodeIsBlockStatic(&aNode)) { + return aNode.AsElement(); + } + return GetBlockNodeParent(&aNode); +} + static const char16_t nbsp = 160; /////////////////////////////////////////////////////////////////////////////// @@ -1753,12 +1760,7 @@ nsHTMLEditor::GetCSSBackgroundColorState(bool *aMixed, nsAString &aOutColor, boo if (aBlockLevel) { // we are querying the block background (and not the text background), let's // climb to the block container - nsCOMPtr blockParent; - if (NodeIsBlockStatic(nodeToExamine)) { - blockParent = nodeToExamine->AsElement(); - } else { - blockParent = GetBlockNodeParent(nodeToExamine); - } + nsCOMPtr blockParent = GetBlock(*nodeToExamine); NS_ENSURE_TRUE(blockParent, NS_OK); // Make sure to not walk off onto the Document node @@ -4566,12 +4568,7 @@ nsHTMLEditor::SetCSSBackgroundColor(const nsAString& aColor) // A unique node is selected, let's also apply the background color to // the containing block, possibly the node itself nsCOMPtr selectedNode = startNode->GetChildAt(startOffset); - nsCOMPtr blockParent; - if (NodeIsBlockStatic(selectedNode)) { - blockParent = selectedNode->AsElement(); - } else { - blockParent = GetBlockNodeParent(selectedNode); - } + nsCOMPtr blockParent = GetBlock(*selectedNode); if (blockParent && cachedBlockParent != blockParent) { cachedBlockParent = blockParent; mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(blockParent, nullptr, @@ -4625,12 +4622,7 @@ nsHTMLEditor::SetCSSBackgroundColor(const nsAString& aColor) // Then loop through the list, set the property on each node for (auto& node : arrayOfNodes) { - nsCOMPtr blockParent; - if (NodeIsBlockStatic(node)) { - blockParent = node->AsElement(); - } else { - blockParent = GetBlockNodeParent(node); - } + nsCOMPtr blockParent = GetBlock(node); if (blockParent && cachedBlockParent != blockParent) { cachedBlockParent = blockParent; mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(blockParent, nullptr, diff --git a/editor/libeditor/nsHTMLEditor.h b/editor/libeditor/nsHTMLEditor.h index 6d636ecea69b..2f6b6578b557 100644 --- a/editor/libeditor/nsHTMLEditor.h +++ b/editor/libeditor/nsHTMLEditor.h @@ -233,6 +233,7 @@ public: /* ------------ Block methods moved from nsEditor -------------- */ static mozilla::dom::Element* GetBlockNodeParent(nsINode* aNode); static nsIDOMNode* GetBlockNodeParent(nsIDOMNode* aNode); + static mozilla::dom::Element* GetBlock(nsINode& aNode); void IsNextCharInNodeWhitespace(nsIContent* aContent, int32_t aOffset,