Bug 756753 - Simplify nsHTMLEditRules::LookInsideDivBQandList, nsEditor::CountEditableChildren; r=ehsan

This commit is contained in:
Ms2ger 2012-06-06 09:36:07 +02:00
parent 00749cb54a
commit fade6304fe
3 changed files with 57 additions and 74 deletions

View File

@ -3748,40 +3748,19 @@ nsEditor::IsMozEditorBogusNode(nsIContent *element)
kMOZEditorBogusNodeValue, eCaseMatters);
}
nsresult
nsEditor::CountEditableChildren(nsIDOMNode *aNode, PRUint32 &outCount)
PRUint32
nsEditor::CountEditableChildren(nsINode* aNode)
{
outCount = 0;
if (!aNode) { return NS_ERROR_NULL_POINTER; }
nsresult res=NS_OK;
bool hasChildNodes;
aNode->HasChildNodes(&hasChildNodes);
if (hasChildNodes)
{
nsCOMPtr<nsIDOMNodeList>nodeList;
res = aNode->GetChildNodes(getter_AddRefs(nodeList));
if (NS_SUCCEEDED(res) && nodeList)
{
PRUint32 i;
PRUint32 len;
nodeList->GetLength(&len);
for (i=0 ; i<len; i++)
{
nsCOMPtr<nsIDOMNode> child;
res = nodeList->Item((PRInt32)i, getter_AddRefs(child));
if ((NS_SUCCEEDED(res)) && (child))
{
if (IsEditable(child))
{
outCount++;
}
}
}
MOZ_ASSERT(aNode);
PRUint32 count = 0;
for (nsIContent* child = aNode->GetFirstChild();
child;
child = child->GetNextSibling()) {
if (IsEditable(child)) {
++count;
}
else if (!nodeList)
res = NS_ERROR_NULL_POINTER;
}
return res;
return count;
}
//END nsEditor static utility methods

View File

@ -565,7 +565,7 @@ public:
bool IsMozEditorBogusNode(nsIContent *aNode);
/** counts number of editable child nodes */
nsresult CountEditableChildren(nsIDOMNode *aNode, PRUint32 &outCount);
PRUint32 CountEditableChildren(nsINode* aNode);
/** Find the deep first and last children. */
nsINode* GetFirstEditableNode(nsINode* aRoot);

View File

@ -5875,50 +5875,54 @@ nsHTMLEditRules::LookInsideDivBQandList(nsCOMArray<nsIDOMNode>& aNodeArray)
{
// if there is only one node in the array, and it is a list, div, or blockquote,
// then look inside of it until we find inner list or content.
nsresult res = NS_OK;
PRInt32 listCount = aNodeArray.Count();
if (listCount == 1)
{
nsCOMPtr<nsIDOMNode> curNode = aNodeArray[0];
while (nsHTMLEditUtils::IsDiv(curNode)
|| nsHTMLEditUtils::IsList(curNode)
|| nsHTMLEditUtils::IsBlockquote(curNode))
{
// dive as long as there is only one child, and it is a list, div, blockquote
PRUint32 numChildren;
res = mHTMLEditor->CountEditableChildren(curNode, numChildren);
NS_ENSURE_SUCCESS(res, res);
if (numChildren == 1)
{
// keep diving
nsCOMPtr <nsIDOMNode> tmpNode = nsEditor::GetChildAt(curNode, 0);
if (nsHTMLEditUtils::IsDiv(tmpNode)
|| nsHTMLEditUtils::IsList(tmpNode)
|| nsHTMLEditUtils::IsBlockquote(tmpNode))
{
// check editablility XXX floppy moose
curNode = tmpNode;
}
else break;
}
else break;
}
// we've found innermost list/blockquote/div:
// replace the one node in the array with these nodes
aNodeArray.RemoveObjectAt(0);
if ((nsHTMLEditUtils::IsDiv(curNode) || nsHTMLEditUtils::IsBlockquote(curNode)))
{
PRInt32 j=0;
res = GetInnerContent(curNode, aNodeArray, &j, false, false);
}
else
{
aNodeArray.AppendObject(curNode);
}
if (listCount != 1) {
return NS_OK;
}
return res;
nsCOMPtr<nsINode> curNode = do_QueryInterface(aNodeArray[0]);
NS_ENSURE_STATE(curNode);
while (curNode->IsElement() &&
(curNode->AsElement()->IsHTML(nsGkAtoms::div) ||
nsHTMLEditUtils::IsList(curNode->AsElement()) ||
curNode->AsElement()->IsHTML(nsGkAtoms::blockquote))) {
// dive as long as there is only one child, and it is a list, div, blockquote
PRUint32 numChildren = mHTMLEditor->CountEditableChildren(curNode);
if (numChildren != 1) {
break;
}
// keep diving
// XXX One would expect to dive into the one editable node.
nsIContent* tmp = curNode->GetFirstChild();
if (!tmp->IsElement()) {
break;
}
dom::Element* element = tmp->AsElement();
if (!element->IsHTML(nsGkAtoms::div) &&
!nsHTMLEditUtils::IsList(element) &&
!element->IsHTML(nsGkAtoms::blockquote)) {
break;
}
// check editablility XXX floppy moose
curNode = tmp;
}
// we've found innermost list/blockquote/div:
// replace the one node in the array with these nodes
aNodeArray.RemoveObjectAt(0);
if (curNode->IsElement() &&
(curNode->AsElement()->IsHTML(nsGkAtoms::div) ||
curNode->AsElement()->IsHTML(nsGkAtoms::blockquote))) {
PRInt32 j = 0;
return GetInnerContent(curNode->AsDOMNode(), aNodeArray, &j, false, false);
}
aNodeArray.AppendObject(curNode->AsDOMNode());
return NS_OK;
}