Bug 746515 part 2 - Clean up nsHTMLEditor::SetInlinePropertyOnNode; r=ehsan

This commit is contained in:
Aryeh Gregor 2012-04-20 09:33:21 +03:00
parent 6a970aa32b
commit 8fe467bc9f

View File

@ -354,14 +354,14 @@ nsHTMLEditor::SetInlinePropertyOnTextNode( nsIDOMCharacterData *aTextNode,
nsresult nsresult
nsHTMLEditor::SetInlinePropertyOnNode( nsIDOMNode *aNode, nsHTMLEditor::SetInlinePropertyOnNode(nsIDOMNode *aNode,
nsIAtom *aProperty, nsIAtom *aProperty,
const nsAString *aAttribute, const nsAString *aAttribute,
const nsAString *aValue) const nsAString *aValue)
{ {
NS_ENSURE_TRUE(aNode && aProperty, NS_ERROR_NULL_POINTER); NS_ENSURE_TRUE(aNode && aProperty, NS_ERROR_NULL_POINTER);
nsresult res = NS_OK; nsresult res;
nsCOMPtr<nsIDOMNode> tmp; nsCOMPtr<nsIDOMNode> tmp;
nsAutoString tag; nsAutoString tag;
aProperty->ToString(tag); aProperty->ToString(tag);
@ -379,13 +379,12 @@ nsHTMLEditor::SetInlinePropertyOnNode( nsIDOMNode *aNode,
childNodes->GetLength(&childCount); childNodes->GetLength(&childCount);
if (childCount) { if (childCount) {
nsCOMArray<nsIDOMNode> arrayOfNodes; nsCOMArray<nsIDOMNode> arrayOfNodes;
nsCOMPtr<nsIDOMNode> node;
// populate the list // populate the list
for (j = 0; j < (PRInt32)childCount; j++) { for (j = 0; j < (PRInt32)childCount; j++) {
nsCOMPtr<nsIDOMNode> childNode; nsCOMPtr<nsIDOMNode> childNode;
res = childNodes->Item(j, getter_AddRefs(childNode)); res = childNodes->Item(j, getter_AddRefs(childNode));
if ((NS_SUCCEEDED(res)) && childNode && IsEditable(childNode)) { if (NS_SUCCEEDED(res) && childNode && IsEditable(childNode)) {
arrayOfNodes.AppendObject(childNode); arrayOfNodes.AppendObject(childNode);
} }
} }
@ -393,13 +392,13 @@ nsHTMLEditor::SetInlinePropertyOnNode( nsIDOMNode *aNode,
// then loop through the list, set the property on each node // then loop through the list, set the property on each node
PRInt32 listCount = arrayOfNodes.Count(); PRInt32 listCount = arrayOfNodes.Count();
for (j = 0; j < listCount; j++) { for (j = 0; j < listCount; j++) {
node = arrayOfNodes[j]; res = SetInlinePropertyOnNode(arrayOfNodes[j], aProperty,
res = SetInlinePropertyOnNode(node, aProperty, aAttribute, aValue); aAttribute, aValue);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} }
} }
} }
return res; return NS_OK;
} }
bool useCSS = (IsCSSEnabled() && bool useCSS = (IsCSSEnabled() &&
@ -408,19 +407,13 @@ nsHTMLEditor::SetInlinePropertyOnNode( nsIDOMNode *aNode,
aAttribute->EqualsLiteral("bgcolor"); aAttribute->EqualsLiteral("bgcolor");
if (useCSS) { if (useCSS) {
nsCOMPtr<nsIDOMNode> tmp = aNode; tmp = aNode;
if (IsTextNode(tmp)) if (IsTextNode(tmp)) {
{ // we are working on a text node and need to create a span container that
// we are working on a text node and need to create a span container // will carry the styles
// that will carry the styles InsertContainerAbove(aNode, address_of(tmp), NS_LITERAL_STRING("span"),
InsertContainerAbove(aNode, nsnull, nsnull);
address_of(tmp),
NS_LITERAL_STRING("span"),
nsnull,
nsnull);
} }
nsCOMPtr<nsIDOMElement>element;
element = do_QueryInterface(tmp);
// First we have to remove occurrences of the same style hint in the // First we have to remove occurrences of the same style hint in the
// children of aNode, and any equivalent non-CSS attribute on aNode itself // children of aNode, and any equivalent non-CSS attribute on aNode itself
// (if applicable). // (if applicable).
@ -431,45 +424,49 @@ nsHTMLEditor::SetInlinePropertyOnNode( nsIDOMNode *aNode,
res = RemoveAttribute(elem, *aAttribute); res = RemoveAttribute(elem, *aAttribute);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} }
PRInt32 count;
// then we add the css styles corresponding to the HTML style request // then we add the css styles corresponding to the HTML style request
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, aProperty, aAttribute, aValue, &count, false); nsCOMPtr<nsIDOMElement> element;
element = do_QueryInterface(tmp);
PRInt32 count;
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, aProperty,
aAttribute, aValue,
&count, false);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
nsCOMPtr<nsIDOMNode> nextSibling, previousSibling; nsCOMPtr<nsIDOMNode> nextSibling, previousSibling;
GetNextHTMLSibling(tmp, address_of(nextSibling)); GetNextHTMLSibling(tmp, address_of(nextSibling));
GetPriorHTMLSibling(tmp, address_of(previousSibling)); GetPriorHTMLSibling(tmp, address_of(previousSibling));
if (nextSibling || previousSibling) if (nextSibling || previousSibling) {
{
nsCOMPtr<nsIDOMNode> mergeParent; nsCOMPtr<nsIDOMNode> mergeParent;
res = tmp->GetParentNode(getter_AddRefs(mergeParent)); res = tmp->GetParentNode(getter_AddRefs(mergeParent));
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
if (previousSibling && if (previousSibling &&
nsEditor::NodeIsType(previousSibling, nsEditProperty::span) && nsEditor::NodeIsType(previousSibling, nsEditProperty::span) &&
NodesSameType(tmp, previousSibling)) NodesSameType(tmp, previousSibling)) {
{
res = JoinNodes(previousSibling, tmp, mergeParent); res = JoinNodes(previousSibling, tmp, mergeParent);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} }
if (nextSibling && if (nextSibling &&
nsEditor::NodeIsType(nextSibling, nsEditProperty::span) && nsEditor::NodeIsType(nextSibling, nsEditProperty::span) &&
NodesSameType(tmp, nextSibling)) NodesSameType(tmp, nextSibling)) {
{
res = JoinNodes(tmp, nextSibling, mergeParent); res = JoinNodes(tmp, nextSibling, mergeParent);
NS_ENSURE_SUCCESS(res, res);
} }
} }
return res; return NS_OK;
} }
// don't need to do anything if property already set on node // don't need to do anything if property already set on node
bool bHasProp; bool bHasProp;
nsCOMPtr<nsIDOMNode> styleNode; nsCOMPtr<nsIDOMNode> styleNode;
IsTextPropertySetByContent(aNode, aProperty, aAttribute, aValue, bHasProp, getter_AddRefs(styleNode)); IsTextPropertySetByContent(aNode, aProperty, aAttribute, aValue,
if (bHasProp) return NS_OK; bHasProp, getter_AddRefs(styleNode));
if (bHasProp) {
return NS_OK;
}
// is it already the right kind of node, but with wrong attribute? // is it already the right kind of node, but with wrong attribute?
if (NodeIsType(aNode, aProperty)) if (NodeIsType(aNode, aProperty)) {
{
// Just set the attribute on it. But first remove any contrary style in // Just set the attribute on it. But first remove any contrary style in
// its children, and remove any conflicting CSS style on it. // its children, and remove any conflicting CSS style on it.
res = RemoveStyleInside(aNode, aProperty, aAttribute, true); res = RemoveStyleInside(aNode, aProperty, aAttribute, true);
@ -481,29 +478,24 @@ nsHTMLEditor::SetInlinePropertyOnNode( nsIDOMNode *aNode,
nsCOMPtr<nsIDOMElement> elem = do_QueryInterface(aNode); nsCOMPtr<nsIDOMElement> elem = do_QueryInterface(aNode);
return SetAttribute(elem, *aAttribute, *aValue); return SetAttribute(elem, *aAttribute, *aValue);
} }
// Either put it inside a neighboring node, or make a new one. // Either put it inside a neighboring node, or make a new one.
nsCOMPtr<nsIDOMNode> priorNode, nextNode; nsCOMPtr<nsIDOMNode> priorNode, nextNode;
// is either of it's neighbors the right kind of node? // is either of its neighbors the right kind of node?
GetPriorHTMLSibling(aNode, address_of(priorNode)); GetPriorHTMLSibling(aNode, address_of(priorNode));
GetNextHTMLSibling(aNode, address_of(nextNode)); GetNextHTMLSibling(aNode, address_of(nextNode));
if (priorNode && NodeIsType(priorNode, aProperty) && if (priorNode && NodeIsType(priorNode, aProperty) &&
HasAttrVal(priorNode, aAttribute, aValue) && HasAttrVal(priorNode, aAttribute, aValue) &&
IsOnlyAttribute(priorNode, aAttribute) ) IsOnlyAttribute(priorNode, aAttribute)) {
{
// previous sib is already right kind of inline node; slide this over into it // previous sib is already right kind of inline node; slide this over into it
res = MoveNode(aNode, priorNode, -1); res = MoveNode(aNode, priorNode, -1);
} } else if (nextNode && NodeIsType(nextNode, aProperty) &&
else if (nextNode && NodeIsType(nextNode, aProperty) && HasAttrVal(nextNode, aAttribute, aValue) &&
HasAttrVal(nextNode, aAttribute, aValue) && IsOnlyAttribute(priorNode, aAttribute)) {
IsOnlyAttribute(priorNode, aAttribute) )
{
// following sib is already right kind of inline node; slide this over into it // following sib is already right kind of inline node; slide this over into it
res = MoveNode(aNode, nextNode, 0); res = MoveNode(aNode, nextNode, 0);
} } else {
else
{
// ok, chuck it in its very own container // ok, chuck it in its very own container
res = InsertContainerAbove(aNode, address_of(tmp), tag, aAttribute, aValue); res = InsertContainerAbove(aNode, address_of(tmp), tag, aAttribute, aValue);
} }