Bug 750079 - Part c: Pass nsIContent to nsHTMLEditor::HasAttrVal; r=ehsan

This commit is contained in:
Ms2ger 2012-05-05 11:00:06 +02:00
parent e873c43701
commit d266e823ac
2 changed files with 54 additions and 57 deletions

View File

@ -712,7 +712,8 @@ protected:
bool NodeIsProperty(nsIDOMNode *aNode);
bool HasAttr(nsIDOMNode *aNode, const nsAString *aAttribute);
bool HasAttrVal(nsIDOMNode *aNode, const nsAString *aAttribute, const nsAString *aValue);
bool HasAttrVal(const nsIContent* aNode, const nsAString* aAttribute,
const nsAString& aValue);
bool IsAtFrontOfNode(nsIDOMNode *aNode, PRInt32 aOffset);
bool IsAtEndOfNode(nsIDOMNode *aNode, PRInt32 aOffset);
bool IsOnlyAttribute(nsIDOMNode *aElement, const nsAString *aAttribute);

View File

@ -272,6 +272,7 @@ nsHTMLEditor::SetInlinePropertyOnTextNode( nsIDOMCharacterData *aTextNode,
const nsAString *aAttribute,
const nsAString *aValue)
{
MOZ_ASSERT(aValue);
NS_ENSURE_TRUE(aTextNode, NS_ERROR_NULL_POINTER);
nsCOMPtr<nsIDOMNode> parent;
nsresult res = aTextNode->GetParentNode(getter_AddRefs(parent));
@ -284,7 +285,7 @@ nsHTMLEditor::SetInlinePropertyOnTextNode( nsIDOMCharacterData *aTextNode,
// don't need to do anything if no characters actually selected
if (aStartOffset == aEndOffset) return NS_OK;
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aTextNode);
nsCOMPtr<nsIDOMNode> node = aTextNode;
// don't need to do anything if property already set on node
bool bHasProp;
@ -293,14 +294,11 @@ nsHTMLEditor::SetInlinePropertyOnTextNode( nsIDOMCharacterData *aTextNode,
aAttribute, aValue)) {
// the HTML styles defined by aProperty/aAttribute has a CSS equivalence
// in this implementation for node; let's check if it carries those css styles
nsAutoString value;
if (aValue) value.Assign(*aValue);
nsAutoString value(*aValue);
mHTMLCSSUtils->IsCSSEquivalentToHTMLInlineStyleSet(node, aProperty, aAttribute,
bHasProp, value,
COMPUTED_STYLE_TYPE);
}
else
{
} else {
IsTextPropertySetByContent(node, aProperty, aAttribute, aValue, bHasProp);
}
@ -309,42 +307,41 @@ nsHTMLEditor::SetInlinePropertyOnTextNode( nsIDOMCharacterData *aTextNode,
// do we need to split the text node?
PRUint32 textLen;
aTextNode->GetLength(&textLen);
nsCOMPtr<nsIDOMNode> tmp;
if ( (PRUint32)aEndOffset != textLen )
{
if (PRUint32(aEndOffset) != textLen) {
// we need to split off back of text node
nsCOMPtr<nsIDOMNode> tmp;
res = SplitNode(node, aEndOffset, getter_AddRefs(tmp));
NS_ENSURE_SUCCESS(res, res);
node = tmp; // remember left node
}
if ( aStartOffset )
{
if (aStartOffset) {
// we need to split off front of text node
nsCOMPtr<nsIDOMNode> tmp;
res = SplitNode(node, aStartOffset, getter_AddRefs(tmp));
NS_ENSURE_SUCCESS(res, res);
}
// look for siblings that are correct type of node
nsCOMPtr<nsIDOMNode> sibling;
GetPriorHTMLSibling(node, address_of(sibling));
if (sibling && NodeIsType(sibling, aProperty) &&
HasAttrVal(sibling, aAttribute, aValue) &&
IsOnlyAttribute(sibling, aAttribute) )
{
// previous sib is already right kind of inline node; slide this over into it
res = MoveNode(node, sibling, -1);
return res;
}
sibling = nsnull;
GetNextHTMLSibling(node, address_of(sibling));
if (sibling && NodeIsType(sibling, aProperty) &&
HasAttrVal(sibling, aAttribute, aValue) &&
IsOnlyAttribute(sibling, aAttribute) )
{
// following sib is already right kind of inline node; slide this over into it
res = MoveNode(node, sibling, 0);
return res;
nsCOMPtr<nsIContent> content = do_QueryInterface(node);
NS_ENSURE_STATE(content);
if (aAttribute) {
// look for siblings that are correct type of node
nsIContent* sibling = GetPriorHTMLSibling(content);
if (sibling && sibling->Tag() == aProperty &&
HasAttrVal(sibling, aAttribute, *aValue) &&
IsOnlyAttribute(sibling, *aAttribute)) {
// previous sib is already right kind of inline node; slide this over into it
return MoveNode(node, sibling->AsDOMNode(), -1);
}
sibling = GetNextHTMLSibling(content);
if (sibling && sibling->Tag() == aProperty &&
HasAttrVal(sibling, aAttribute, *aValue) &&
IsOnlyAttribute(sibling, *aAttribute)) {
// following sib is already right kind of inline node; slide this over into it
return MoveNode(node, sibling->AsDOMNode(), 0);
}
}
// reparent the node inside inline node with appropriate {attribute,value}
@ -359,6 +356,7 @@ nsHTMLEditor::SetInlinePropertyOnNodeImpl(nsIDOMNode *aNode,
const nsAString *aValue)
{
MOZ_ASSERT(aNode && aProperty);
MOZ_ASSERT(aValue);
nsresult res;
nsCOMPtr<nsIDOMNode> tmp;
@ -456,23 +454,24 @@ nsHTMLEditor::SetInlinePropertyOnNodeImpl(nsIDOMNode *aNode,
}
// Either put it inside a neighboring node, or make a new one.
nsCOMPtr<nsIDOMNode> priorNode, nextNode;
// is either of its neighbors the right kind of node?
GetPriorHTMLSibling(aNode, address_of(priorNode));
GetNextHTMLSibling(aNode, address_of(nextNode));
if (priorNode && NodeIsType(priorNode, aProperty) &&
HasAttrVal(priorNode, aAttribute, aValue) &&
IsOnlyAttribute(priorNode, aAttribute)) {
// previous sib is already right kind of inline node; slide this over into it
return MoveNode(aNode, priorNode, -1);
}
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
if (node && aAttribute) {
nsIContent* priorNode = GetPriorHTMLSibling(node);
if (priorNode && priorNode->Tag() == aProperty &&
HasAttrVal(priorNode, aAttribute, *aValue) &&
IsOnlyAttribute(priorNode, *aAttribute)) {
// previous sib is already right kind of inline node; slide this over into it
return MoveNode(aNode, priorNode->AsDOMNode(), -1);
}
if (nextNode && NodeIsType(nextNode, aProperty) &&
HasAttrVal(nextNode, aAttribute, aValue) &&
IsOnlyAttribute(priorNode, aAttribute)) {
// following sib is already right kind of inline node; slide this over into it
return MoveNode(aNode, nextNode, 0);
nsIContent* nextNode = GetNextHTMLSibling(node);
if (nextNode && nextNode->Tag() == aProperty &&
HasAttrVal(nextNode, aAttribute, *aValue) &&
IsOnlyAttribute(priorNode, *aAttribute)) {
// following sib is already right kind of inline node; slide this over into it
return MoveNode(aNode, nextNode->AsDOMNode(), 0);
}
}
// ok, chuck it in its very own container
@ -816,24 +815,21 @@ bool nsHTMLEditor::HasAttr(nsIDOMNode* aNode,
}
bool nsHTMLEditor::HasAttrVal(nsIDOMNode* aNode,
bool nsHTMLEditor::HasAttrVal(const nsIContent* aNode,
const nsAString* aAttribute,
const nsAString* aValue)
const nsAString& aValue)
{
NS_ENSURE_TRUE(aNode, false);
MOZ_ASSERT(aNode);
if (!aAttribute || aAttribute->IsEmpty()) {
// everybody has the 'null' attribute
return true;
}
// get element
nsCOMPtr<dom::Element> element = do_QueryInterface(aNode);
NS_ENSURE_TRUE(element, false);
nsCOMPtr<nsIAtom> atom = do_GetAtom(*aAttribute);
NS_ENSURE_TRUE(atom, false);
return element->AttrValueIs(kNameSpaceID_None, atom, *aValue, eIgnoreCase);
return aNode->AttrValueIs(kNameSpaceID_None, atom, aValue, eIgnoreCase);
}
nsresult nsHTMLEditor::PromoteRangeIfStartsOrEndsInNamedAnchor(nsIDOMRange *inRange)