From 71efee461ee4b71aeb5130075374199448b074b7 Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Wed, 22 Apr 2015 14:27:18 +0300 Subject: [PATCH] Bug 1154701 part 8 - Clean up nsHTMLEditor::SetInlinePropertyOnNode; r=ehsan --- editor/libeditor/nsHTMLEditRules.cpp | 6 ++- editor/libeditor/nsHTMLEditor.h | 10 ++-- editor/libeditor/nsHTMLEditorStyle.cpp | 69 ++++++++------------------ 3 files changed, 29 insertions(+), 56 deletions(-) diff --git a/editor/libeditor/nsHTMLEditRules.cpp b/editor/libeditor/nsHTMLEditRules.cpp index d9cc2b96f351..a70ea5fe1581 100644 --- a/editor/libeditor/nsHTMLEditRules.cpp +++ b/editor/libeditor/nsHTMLEditRules.cpp @@ -4562,8 +4562,10 @@ nsHTMLEditRules::CreateStyleForInsertText(Selection* aSelection, while (item) { NS_ENSURE_STATE(mHTMLEditor); - res = mHTMLEditor->SetInlinePropertyOnNode(node, item->tag, &item->attr, - &item->value); + nsCOMPtr content = do_QueryInterface(node); + NS_ENSURE_STATE(content || !node); + res = mHTMLEditor->SetInlinePropertyOnNode(*content, *item->tag, + &item->attr, item->value); NS_ENSURE_SUCCESS(res, res); item = mHTMLEditor->mTypeInState->TakeSetProperty(); } diff --git a/editor/libeditor/nsHTMLEditor.h b/editor/libeditor/nsHTMLEditor.h index 510e67c7e37a..06ef330c9c73 100644 --- a/editor/libeditor/nsHTMLEditor.h +++ b/editor/libeditor/nsHTMLEditor.h @@ -651,14 +651,10 @@ protected: nsIAtom& aProperty, const nsAString* aAttribute, const nsAString& aValue); - nsresult SetInlinePropertyOnNode( nsIDOMNode *aNode, - nsIAtom *aProperty, - const nsAString *aAttribute, - const nsAString *aValue); - nsresult SetInlinePropertyOnNode(nsIContent* aNode, - nsIAtom* aProperty, + nsresult SetInlinePropertyOnNode(nsIContent& aNode, + nsIAtom& aProperty, const nsAString* aAttribute, - const nsAString* aValue); + const nsAString& aValue); nsresult PromoteInlineRange(nsRange* aRange); nsresult PromoteRangeIfStartsOrEndsInNamedAnchor(nsRange* aRange); diff --git a/editor/libeditor/nsHTMLEditorStyle.cpp b/editor/libeditor/nsHTMLEditorStyle.cpp index b7a768f3f09f..7aaf054e41d2 100644 --- a/editor/libeditor/nsHTMLEditorStyle.cpp +++ b/editor/libeditor/nsHTMLEditorStyle.cpp @@ -203,7 +203,7 @@ nsHTMLEditor::SetInlineProperty(nsIAtom* aProperty, // Then loop through the list, set the property on each node for (auto& node : arrayOfNodes) { - res = SetInlinePropertyOnNode(node, aProperty, &aAttribute, &aValue); + res = SetInlinePropertyOnNode(*node, *aProperty, &aAttribute, aValue); NS_ENSURE_SUCCESS(res, res); } @@ -364,7 +364,7 @@ nsHTMLEditor::SetInlinePropertyOnTextNode(Text& aText, } // Reparent the node inside inline node with appropriate {attribute,value} - return SetInlinePropertyOnNode(text, &aProperty, aAttribute, &aValue); + return SetInlinePropertyOnNode(*text, aProperty, aAttribute, aValue); } @@ -393,8 +393,8 @@ nsHTMLEditor::SetInlinePropertyOnNodeImpl(nsIContent& aNode, // Then loop through the list, set the property on each node. for (auto& node : arrayOfNodes) { - nsresult rv = SetInlinePropertyOnNode(node, &aProperty, aAttribute, - &aValue); + nsresult rv = SetInlinePropertyOnNode(node, aProperty, aAttribute, + aValue); NS_ENSURE_SUCCESS(rv, rv); } } @@ -474,45 +474,23 @@ nsHTMLEditor::SetInlinePropertyOnNodeImpl(nsIContent& aNode, nsresult -nsHTMLEditor::SetInlinePropertyOnNode(nsIDOMNode *aNode, - nsIAtom *aProperty, - const nsAString *aAttribute, - const nsAString *aValue) -{ - // Before setting the property, we remove it if it's already set. - // RemoveStyleInside might remove the node we're looking at or some of its - // descendants, however, in which case we want to set the property on - // whatever wound up in its place. We have to save the original siblings and - // parent to figure this out. - NS_ENSURE_TRUE(aNode && aProperty, NS_ERROR_NULL_POINTER); - - nsCOMPtr node = do_QueryInterface(aNode); - NS_ENSURE_STATE(node); - - return SetInlinePropertyOnNode(node, aProperty, aAttribute, aValue); -} - -nsresult -nsHTMLEditor::SetInlinePropertyOnNode(nsIContent* aNode, - nsIAtom* aProperty, +nsHTMLEditor::SetInlinePropertyOnNode(nsIContent& aNode, + nsIAtom& aProperty, const nsAString* aAttribute, - const nsAString* aValue) + const nsAString& aValue) { - MOZ_ASSERT(aNode); - MOZ_ASSERT(aProperty); + nsCOMPtr previousSibling = aNode.GetPreviousSibling(), + nextSibling = aNode.GetNextSibling(); + NS_ENSURE_STATE(aNode.GetParentNode()); + OwningNonNull parent = *aNode.GetParentNode(); - nsCOMPtr previousSibling = aNode->GetPreviousSibling(), - nextSibling = aNode->GetNextSibling(); - nsCOMPtr parent = aNode->GetParentNode(); - NS_ENSURE_STATE(parent); - - nsresult res = RemoveStyleInside(aNode->AsDOMNode(), aProperty, aAttribute); + nsresult res = RemoveStyleInside(aNode.AsDOMNode(), &aProperty, aAttribute); NS_ENSURE_SUCCESS(res, res); - if (aNode->GetParentNode()) { + if (aNode.GetParentNode()) { // The node is still where it was - return SetInlinePropertyOnNodeImpl(*aNode, *aProperty, - aAttribute, *aValue); + return SetInlinePropertyOnNodeImpl(aNode, aProperty, + aAttribute, aValue); } // It's vanished. Use the old siblings for reference to construct a @@ -522,20 +500,17 @@ nsHTMLEditor::SetInlinePropertyOnNode(nsIContent* aNode, (nextSibling && nextSibling->GetParentNode() != parent)) { return NS_ERROR_UNEXPECTED; } - nsCOMArray nodesToSet; + nsTArray> nodesToSet; nsCOMPtr cur = previousSibling ? previousSibling->GetNextSibling() : parent->GetFirstChild(); - while (cur && cur != nextSibling) { + for (; cur && cur != nextSibling; cur = cur->GetNextSibling()) { if (IsEditable(cur)) { - nodesToSet.AppendObject(cur); + nodesToSet.AppendElement(*cur); } - cur = cur->GetNextSibling(); } - int32_t nodesToSetCount = nodesToSet.Count(); - for (int32_t k = 0; k < nodesToSetCount; k++) { - res = SetInlinePropertyOnNodeImpl(*nodesToSet[k], *aProperty, - aAttribute, *aValue); + for (auto& node : nodesToSet) { + res = SetInlinePropertyOnNodeImpl(node, aProperty, aAttribute, aValue); NS_ENSURE_SUCCESS(res, res); } @@ -1400,8 +1375,8 @@ nsHTMLEditor::RemoveInlinePropertyImpl(nsIAtom* aProperty, // "inverting" the style mHTMLCSSUtils->IsCSSInvertible(*aProperty, aAttribute)) { NS_NAMED_LITERAL_STRING(value, "-moz-editor-invert-value"); - SetInlinePropertyOnNode(node->AsContent(), aProperty, - aAttribute, &value); + SetInlinePropertyOnNode(*node->AsContent(), *aProperty, + aAttribute, value); } } }