From 39fe4e35679b0ba48380abc9256adb718583c766 Mon Sep 17 00:00:00 2001 From: Masayuki Nakano Date: Sat, 23 Apr 2016 19:59:35 +0900 Subject: [PATCH] Backout changeset 47a8205beae7 (bug 1190172 part 7) --HG-- extra : rebase_source : 660a93fad26fc7a2516c5a4f490cd28e2e42d2fc --- editor/libeditor/nsHTMLEditor.h | 6 +- editor/libeditor/nsHTMLEditorStyle.cpp | 106 ++++++++++++++----------- 2 files changed, 64 insertions(+), 48 deletions(-) diff --git a/editor/libeditor/nsHTMLEditor.h b/editor/libeditor/nsHTMLEditor.h index 710da52bd927..2f68c38bf8c4 100644 --- a/editor/libeditor/nsHTMLEditor.h +++ b/editor/libeditor/nsHTMLEditor.h @@ -659,7 +659,7 @@ protected: const nsAString* aAttribute, const nsAString& aValue); - nsresult PromoteInlineRange(nsRange& aRange); + nsresult PromoteInlineRange(nsRange* aRange); nsresult PromoteRangeIfStartsOrEndsInNamedAnchor(nsRange* aRange); nsresult SplitStyleAboveRange(nsRange* aRange, nsIAtom *aProperty, @@ -678,8 +678,8 @@ protected: const nsAString* aAttribute); bool NodeIsProperty(nsINode& aNode); - bool IsAtFrontOfNode(nsINode& aNode, int32_t aOffset); - bool IsAtEndOfNode(nsINode& aNode, int32_t aOffset); + bool IsAtFrontOfNode(nsIDOMNode *aNode, int32_t aOffset); + bool IsAtEndOfNode(nsIDOMNode *aNode, int32_t aOffset); bool IsOnlyAttribute(const nsIContent* aElement, const nsAString& aAttribute); nsresult RemoveBlockContainer(nsIDOMNode *inNode); diff --git a/editor/libeditor/nsHTMLEditorStyle.cpp b/editor/libeditor/nsHTMLEditorStyle.cpp index 9a5941775fc6..fd4b4aec376f 100644 --- a/editor/libeditor/nsHTMLEditorStyle.cpp +++ b/editor/libeditor/nsHTMLEditorStyle.cpp @@ -145,7 +145,7 @@ nsHTMLEditor::SetInlineProperty(nsIAtom* aProperty, // Adjust range to include any ancestors whose children are entirely // selected - res = PromoteInlineRange(*range); + res = PromoteInlineRange(range); NS_ENSURE_SUCCESS(res, res); // Check for easy case: both range endpoints in same text node @@ -899,74 +899,90 @@ nsHTMLEditor::PromoteRangeIfStartsOrEndsInNamedAnchor(nsRange* inRange) } nsresult -nsHTMLEditor::PromoteInlineRange(nsRange& aRange) +nsHTMLEditor::PromoteInlineRange(nsRange* inRange) { - nsCOMPtr startNode = aRange.GetStartParent(); - int32_t startOffset = aRange.StartOffset(); - nsCOMPtr endNode = aRange.GetEndParent(); - int32_t endOffset = aRange.EndOffset(); + NS_ENSURE_TRUE(inRange, NS_ERROR_NULL_POINTER); + nsresult res; + nsCOMPtr startNode, endNode, parent; + int32_t startOffset, endOffset; - while (startNode && !startNode->IsHTMLElement(nsGkAtoms::body) && - IsEditable(startNode) && IsAtFrontOfNode(*startNode, startOffset)) { - nsCOMPtr parent = startNode->GetParentNode(); - NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER); - startOffset = parent->IndexOf(startNode); + res = inRange->GetStartContainer(getter_AddRefs(startNode)); + NS_ENSURE_SUCCESS(res, res); + res = inRange->GetStartOffset(&startOffset); + NS_ENSURE_SUCCESS(res, res); + res = inRange->GetEndContainer(getter_AddRefs(endNode)); + NS_ENSURE_SUCCESS(res, res); + res = inRange->GetEndOffset(&endOffset); + NS_ENSURE_SUCCESS(res, res); + + while ( startNode && + !nsTextEditUtils::IsBody(startNode) && + IsEditable(startNode) && + IsAtFrontOfNode(startNode, startOffset) ) + { + parent = GetNodeLocation(startNode, &startOffset); startNode = parent; } + NS_ENSURE_TRUE(startNode, NS_ERROR_NULL_POINTER); - while (endNode && !endNode->IsHTMLElement(nsGkAtoms::body) && - IsEditable(endNode) && IsAtEndOfNode(*endNode, endOffset)) { - nsCOMPtr parent = endNode->GetParentNode(); - NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER); - // We are AFTER this node - endOffset = 1 + parent->IndexOf(endNode); + while ( endNode && + !nsTextEditUtils::IsBody(endNode) && + IsEditable(endNode) && + IsAtEndOfNode(endNode, endOffset) ) + { + parent = GetNodeLocation(endNode, &endOffset); endNode = parent; + endOffset++; // we are AFTER this node } + NS_ENSURE_TRUE(endNode, NS_ERROR_NULL_POINTER); - nsresult res = aRange.SetStart(startNode, startOffset); + res = inRange->SetStart(startNode, startOffset); NS_ENSURE_SUCCESS(res, res); - res = aRange.SetEnd(endNode, endOffset); - NS_ENSURE_SUCCESS(res, res); - - return NS_OK; + res = inRange->SetEnd(endNode, endOffset); + return res; } -bool -nsHTMLEditor::IsAtFrontOfNode(nsINode& aNode, int32_t aOffset) +bool nsHTMLEditor::IsAtFrontOfNode(nsIDOMNode *aNode, int32_t aOffset) { + nsCOMPtr node = do_QueryInterface(aNode); + NS_ENSURE_TRUE(node, false); if (!aOffset) { return true; } - if (IsTextNode(&aNode)) { + if (IsTextNode(aNode)) + { return false; } - - nsCOMPtr firstNode = GetFirstEditableChild(aNode); - NS_ENSURE_TRUE(firstNode, true); - if (aNode.IndexOf(firstNode) < aOffset) { - return false; + else + { + nsCOMPtr firstNode = GetFirstEditableChild(*node); + NS_ENSURE_TRUE(firstNode, true); + int32_t offset = node->IndexOf(firstNode); + if (offset < aOffset) return false; + return true; } - return true; } -bool -nsHTMLEditor::IsAtEndOfNode(nsINode& aNode, int32_t aOffset) +bool nsHTMLEditor::IsAtEndOfNode(nsIDOMNode *aNode, int32_t aOffset) { - if (aOffset == (int32_t)aNode.Length()) { - return true; - } + nsCOMPtr node = do_QueryInterface(aNode); + NS_ENSURE_TRUE(node, false); + uint32_t len = node->Length(); + if (aOffset == (int32_t)len) return true; - if (IsTextNode(&aNode)) { + if (IsTextNode(aNode)) + { return false; } - - nsCOMPtr lastNode = GetLastEditableChild(aNode); - NS_ENSURE_TRUE(lastNode, true); - if (aNode.IndexOf(lastNode) < aOffset) { - return true; + else + { + nsCOMPtr lastNode = GetLastEditableChild(*node); + NS_ENSURE_TRUE(lastNode, true); + int32_t offset = node->IndexOf(lastNode); + if (offset < aOffset) return true; + return false; } - return false; } @@ -1249,7 +1265,7 @@ nsHTMLEditor::RemoveInlinePropertyImpl(nsIAtom* aProperty, } else { // Adjust range to include any ancestors whose children are entirely // selected - res = PromoteInlineRange(*range); + res = PromoteInlineRange(range); } NS_ENSURE_SUCCESS(res, res); @@ -1386,7 +1402,7 @@ nsHTMLEditor::RelativeFontChange(FontSize aDir) RefPtr range = selection->GetRangeAt(rangeIdx); // Adjust range to include any ancestors with entirely selected children - nsresult res = PromoteInlineRange(*range); + nsresult res = PromoteInlineRange(range); NS_ENSURE_SUCCESS(res, res); // Check for easy case: both range endpoints in same text node