removeBlockParent = PR_TRUE; } } if ((NS_SUCCEEDED(res)) && *aNewParentNode) { nsCOMPtr<nsIDOMNode>childNode = do_QueryInterface(rightNode); nsCOMPtr<nsIDOMNode>previousSiblingNode; while (NS_SUCCEEDED(res) && childNode) { childNode->GetPreviousSibling(getter_AddRefs(previousSiblingNode));   res = nsEditor::DeleteNode(childNode); if (NS_SUCCEEDED(res)) { res = nsEditor::InsertNode(childNode, *aNewParentNode, 0); if (gNoisy) { printf("re-parented sibling node %p\n", childNode.get()); } } if (childNode==leftNode || rightNode==leftNode) { break; } childNode = do_QueryInterface(previousSiblingNode); } }  if (NS_SUCCEEDED(res)) {  nsCOMPtr<nsIDOMNode> brNode; res = GetPriorNode(leftNode, PR_TRUE, getter_AddRefs(brNode)); if (NS_SUCCEEDED(res) && brNode) { nsCOMPtr<nsIContent> brContent = do_QueryInterface(brNode); if (brContent) { nsCOMPtr<nsIAtom> brContentTag; brContent->GetTag(*getter_AddRefs(brContentTag)); if (nsIEditProperty::br==brContentTag.get()) { res = DeleteNode(brNode); } } }
} else { nsCOMPtr<nsIDOMNode> grandParent; res = blockParentNode->GetParentNode(getter_AddRefs(grandParent)); if ((NS_SUCCEEDED(res)) && grandParent) { nsCOMPtr<nsIDOMNode>firstChildNode, lastChildNode; blockParentNode->GetFirstChild(getter_AddRefs(firstChildNode)); blockParentNode->GetLastChild(getter_AddRefs(lastChildNode)); if (firstChildNode==leftNode && lastChildNode==rightNode) { res = GetChildOffset(blockParentNode, grandParent, offsetInParent); NS_ASSERTION((NS_SUCCEEDED(res)), "bad res from GetChildOffset"); res = nsTextEditor::CreateNode(aParentTag, grandParent, offsetInParent, aNewParentNode); if (gNoisy) { printf("created a node in grandParent at offset %d\n", offsetInParent); } } else {    if (gNoisy) { printf("splitting a node because of <BR>s\n"); } nsCOMPtr<nsIDOMNode> newLeftNode; if (firstChildNode!=leftNode) { res = GetChildOffset(leftNode, blockParentNode, offsetInParent); if (gNoisy) { printf("splitting left at %d\n", offsetInParent); } res = SplitNode(blockParentNode, offsetInParent, getter_AddRefs(newLeftNode));  } if (lastChildNode!=rightNode) { res = GetChildOffset(rightNode, blockParentNode, offsetInParent); offsetInParent++; if (gNoisy) { printf("splitting right at %d\n", offsetInParent); } res = SplitNode(blockParentNode, offsetInParent, getter_AddRefs(newLeftNode)); blockParentNode = do_QueryInterface(newLeftNode); } res = GetChildOffset(leftNode, blockParentNode, offsetInParent); NS_ASSERTION((NS_SUCCEEDED(res)), "bad res from GetChildOffset"); res = nsTextEditor::CreateNode(aParentTag, blockParentNode, offsetInParent, aNewParentNode); if (gNoisy) { printf("created a node in blockParentNode at offset %d\n", offsetInParent); }  removeBlockParent = PR_TRUE; } } } if ((NS_SUCCEEDED(res)) && *aNewParentNode) { nsCOMPtr<nsIDOMNode>childNode = do_QueryInterface(rightNode); nsCOMPtr<nsIDOMNode>previousSiblingNode; while (NS_SUCCEEDED(res) && childNode) { childNode->GetPreviousSibling(getter_AddRefs(previousSiblingNode));   res = nsEditor::DeleteNode(childNode); if (NS_SUCCEEDED(res)) { res = nsEditor::InsertNode(childNode, *aNewParentNode, 0); if (gNoisy) { printf("re-parented sibling node %p\n", childNode.get()); } } if (childNode==leftNode || rightNode==leftNode) { break; } childNode = do_QueryInterface(previousSiblingNode); } }
nsHTMLEditor::ReParentBlockContent(nsIDOMNode  *aNode,                                    nsString    &aParentTag,                                   nsIDOMNode  *aBlockParentNode,                                   nsString    &aBlockParentTag,                                   BlockTransformationType aTransformation,                                   nsIDOMNode **aNewParentNode){  if (!aNode || !aBlockParentNode || !aNewParentNode) { return NS_ERROR_NULL_POINTER; }  nsCOMPtr<nsIDOMNode> blockParentNode = do_QueryInterface(aBlockParentNode);  PRBool removeBlockParent = PR_FALSE;  PRBool removeBreakBefore = PR_FALSE;  PRBool removeBreakAfter = PR_FALSE;  nsCOMPtr<nsIDOMNode>ancestor;  nsresult res = aNode->GetParentNode(getter_AddRefs(ancestor));  if (NS_FAILED(res)) return res;  nsCOMPtr<nsIDOMNode>previousAncestor = do_QueryInterface(aNode);  while (ancestor)  {    nsCOMPtr<nsIDOMElement>ancestorElement = do_QueryInterface(ancestor);    nsAutoString ancestorTag;    ancestorElement->GetTagName(ancestorTag);    if (ancestorTag.EqualsIgnoreCase(aBlockParentTag))    {      break;  // previousAncestor will contain the node to operate on    }    previousAncestor = do_QueryInterface(ancestor);    res = ancestorElement->GetParentNode(getter_AddRefs(ancestor));    if (NS_FAILED(res)) return res;  }  // now, previousAncestor is the node we are operating on  nsCOMPtr<nsIDOMNode>leftNode, rightNode;  res = GetBlockSection(previousAncestor,                         getter_AddRefs(leftNode),                         getter_AddRefs(rightNode));  if (NS_FAILED(res)) return res;  if (!leftNode || !rightNode) return NS_ERROR_NULL_POINTER;  // determine some state for managing <BR>s around the new block  PRBool isSubordinateBlock = PR_FALSE;  // if true, the content is already in a subordinate block  PRBool isRootBlock = PR_FALSE;  // if true, the content is in a root block  nsCOMPtr<nsIDOMElement>blockParentElement = do_QueryInterface(blockParentNode);  if (blockParentElement)  {    nsAutoString blockParentTag;    blockParentElement->GetTagName(blockParentTag);    IsSubordinateBlock(blockParentTag, isSubordinateBlock);    IsRootTag(blockParentTag, isRootBlock);  }  if (PR_TRUE==isRootBlock)  { // we're creating a block element where a block element did not previously exist     removeBreakBefore = PR_TRUE;    removeBreakAfter = PR_TRUE;  }  // apply the transformation  PRInt32 offsetInParent=0;  if (eInsertParent==aTransformation || PR_TRUE==isRootBlock)  {    res = GetChildOffset(leftNode, blockParentNode, offsetInParent);    NS_ASSERTION((NS_SUCCEEDED(res)), "bad res from GetChildOffset");    res = CreateNode(aParentTag, blockParentNode, offsetInParent, aNewParentNode);    if (gNoisy) { printf("created a node in blockParentNode at offset %d\n", offsetInParent); }  }  else  {    nsCOMPtr<nsIDOMNode> grandParent;    res = blockParentNode->GetParentNode(getter_AddRefs(grandParent));    if (NS_FAILED(res)) return res;    if (!grandParent) return NS_ERROR_NULL_POINTER;    nsCOMPtr<nsIDOMNode>firstChildNode, lastChildNode;    blockParentNode->GetFirstChild(getter_AddRefs(firstChildNode));    blockParentNode->GetLastChild(getter_AddRefs(lastChildNode));    if (firstChildNode==leftNode && lastChildNode==rightNode)    {      res = GetChildOffset(blockParentNode, grandParent, offsetInParent);      NS_ASSERTION((NS_SUCCEEDED(res)), "bad res from GetChildOffset");      res = CreateNode(aParentTag, grandParent, offsetInParent, aNewParentNode);      if (gNoisy) { printf("created a node in grandParent at offset %d\n", offsetInParent); }    }    else    {      // We're in the case where the content of blockParentNode is separated by <BR>'s,       // creating multiple block content ranges.      // Split blockParentNode around the blockContent      if (gNoisy) { printf("splitting a node because of <BR>s\n"); }      nsCOMPtr<nsIDOMNode> newLeftNode;      if (firstChildNode!=leftNode)      {        res = GetChildOffset(leftNode, blockParentNode, offsetInParent);        if (gNoisy) { printf("splitting left at %d\n", offsetInParent); }        res = SplitNode(blockParentNode, offsetInParent, getter_AddRefs(newLeftNode));        // after this split, blockParentNode still contains leftNode and rightNode      }      if (lastChildNode!=rightNode)      {        res = GetChildOffset(rightNode, blockParentNode, offsetInParent);        offsetInParent++;        if (gNoisy) { printf("splitting right at %d\n", offsetInParent); }        res = SplitNode(blockParentNode, offsetInParent, getter_AddRefs(newLeftNode));        blockParentNode = do_QueryInterface(newLeftNode);      }      res = GetChildOffset(leftNode, blockParentNode, offsetInParent);      NS_ASSERTION((NS_SUCCEEDED(res)), "bad res from GetChildOffset");      res = CreateNode(aParentTag, blockParentNode, offsetInParent, aNewParentNode);      if (gNoisy) { printf("created a node in blockParentNode at offset %d\n", offsetInParent); }      // what we need to do here is remove the existing block parent when we're all done.      removeBlockParent = PR_TRUE;    }  }  if ((NS_SUCCEEDED(res)) && *aNewParentNode)  { // move all the children/contents of blockParentNode to aNewParentNode    nsCOMPtr<nsIDOMNode>childNode = do_QueryInterface(rightNode);    nsCOMPtr<nsIDOMNode>previousSiblingNode;    while (NS_SUCCEEDED(res) && childNode)    {      childNode->GetPreviousSibling(getter_AddRefs(previousSiblingNode));      // explicitly delete of childNode from it's current parent      // can't just rely on DOM semantics of InsertNode doing the delete implicitly, doesn't undo!       res = nsEditor::DeleteNode(childNode);       if (NS_SUCCEEDED(res))      {        res = nsEditor::InsertNode(childNode, *aNewParentNode, 0);        if (gNoisy)         {          printf("re-parented sibling node %p\n", childNode.get());        }      }      if (childNode==leftNode || rightNode==leftNode) {        break;      }      childNode = do_QueryInterface(previousSiblingNode);    } // end while loop   }  // clean up the surrounding content to maintain vertical whitespace  if (NS_SUCCEEDED(res))  {    // if the prior node is a <BR> and we did something to change vertical whitespacing, delete the <BR>    nsCOMPtr<nsIDOMNode> brNode;    res = GetPriorNode(leftNode, PR_TRUE, getter_AddRefs(brNode));    if (NS_SUCCEEDED(res) && brNode)    {      nsCOMPtr<nsIContent> brContent = do_QueryInterface(brNode);      if (brContent)      {        nsCOMPtr<nsIAtom> brContentTag;        brContent->GetTag(*getter_AddRefs(brContentTag));        if (nsIEditProperty::br==brContentTag.get()) {          res = DeleteNode(brNode);        }      }    }    // if the next node is a <BR> and we did something to change vertical whitespacing, delete the <BR>    if (NS_SUCCEEDED(res))    {      res = GetNextNode(rightNode, PR_TRUE, getter_AddRefs(brNode));      if (NS_SUCCEEDED(res) && brNode)      {        nsCOMPtr<nsIContent> brContent = do_QueryInterface(brNode);        if (brContent)        {          nsCOMPtr<nsIAtom> brContentTag;          brContent->GetTag(*getter_AddRefs(brContentTag));          if (nsIEditProperty::br==brContentTag.get()) {            res = DeleteNode(brNode);          }        }      }    }  }  if ((NS_SUCCEEDED(res)) && (PR_TRUE==removeBlockParent))  { // we determined we need to remove the previous block parent.  Do it!    // go through list backwards so deletes don't interfere with the iteration    nsCOMPtr<nsIDOMNodeList> childNodes;    res = blockParentNode->GetChildNodes(getter_AddRefs(childNodes));    if ((NS_SUCCEEDED(res)) && (childNodes))    {      nsCOMPtr<nsIDOMNode>grandParent;      blockParentNode->GetParentNode(getter_AddRefs(grandParent));      //PRInt32 offsetInParent;      res = GetChildOffset(blockParentNode, grandParent, offsetInParent);      PRUint32 childCount;      childNodes->GetLength(&childCount);      PRInt32 i=childCount-1;      for ( ; ((NS_SUCCEEDED(res)) && (0<=i)); i--)      {        nsCOMPtr<nsIDOMNode> childNode;        res = childNodes->Item(i, getter_AddRefs(childNode));        if ((NS_SUCCEEDED(res)) && (childNode))        {          res = DeleteNode(childNode);          if (NS_SUCCEEDED(res)) {            res = InsertNode(childNode, grandParent, offsetInParent);          }        }      }      if (gNoisy) { printf("removing the old block parent %p\n", blockParentNode.get()); }      res = DeleteNode(blockParentNode);    }  }  return res;}