if (NS_FAILED(rv = child->GetNameSpaceID(nameSpaceID))) continue;
if (NS_FAILED(rv = child->GetNameSpaceID(&nameSpaceID))) continue;
XULSortServiceImpl::SortContainer(nsIContent *container, sortPtr sortInfo, PRBool merelyInvertFlag){  PRInt32 childIndex = 0, loop, numChildren = 0, numElements = 0, currentElement, nameSpaceID;  nsCOMPtr<nsIContent> child;  nsresult rv;  if (NS_FAILED(rv = container->ChildCount(numChildren))) return rv;  if (numChildren < 1) return NS_OK;  nsCOMPtr<nsIDocument> doc;  container->GetDocument(*getter_AddRefs(doc));  if (!doc) return NS_ERROR_UNEXPECTED;  // Note: This is a straight allocation (not a COMPtr) so we  // can't return out of this routine until/unless we free it!  contentSortInfo ** contentSortInfoArray = new contentSortInfo*[numChildren + 1];  if(!contentSortInfoArray) return(NS_ERROR_OUT_OF_MEMORY);  // Note: walk backwards (and add nodes into the array backwards) because  // we also remove the nodes in this loop [via RemoveChildAt()] and if we  // were to do this in a forward-looking manner it would be harder  // (since we also skip over non XUL:treeitem nodes)  nsCOMPtr<nsIAtom> tag;  currentElement = numChildren;  for (childIndex = numChildren-1; childIndex >= 0; --childIndex) {    if (NS_FAILED(rv = container->ChildAt(childIndex, *getter_AddRefs(child)))) continue;    if (NS_FAILED(rv = child->GetNameSpaceID(nameSpaceID))) continue;    if (nameSpaceID == kNameSpaceID_XUL) {      if (NS_FAILED(rv = child->GetTag(*getter_AddRefs(tag)))) continue;      if (tag == nsXULAtoms::listitem || tag == nsXULAtoms::treeitem          || tag == nsXULAtoms::menu || tag == nsXULAtoms::menuitem) {        --currentElement;        nsCOMPtr<nsIRDFResource>  resource;        nsXULContentUtils::GetElementResource(child, getter_AddRefs(resource));        contentSortInfo *contentInfo = CreateContentSortInfo(child, resource);        if(contentInfo)          contentSortInfoArray[currentElement] = contentInfo;        ++numElements;      }    }  }    if (numElements > 0) {    /* smart sorting (sort within separators) on name column */    if (sortInfo->inbetweenSeparatorSort) {      PRInt32  startIndex=currentElement;      nsAutoString  type;      for (loop=currentElement; loop< currentElement + numElements; ++loop) {        if (NS_SUCCEEDED(rv = contentSortInfoArray[loop]->content->GetAttr(kNameSpaceID_RDF,            nsXULAtoms::type, type)) && (rv == NS_CONTENT_ATTR_HAS_VALUE))        {          if (type.EqualsWithConversion(kURINC_BookmarkSeparator)) {            if (loop > startIndex+1) {              if (merelyInvertFlag)                InvertSortInfo(&contentSortInfoArray[startIndex], loop-startIndex);              else                NS_QuickSort((void*)&contentSortInfoArray[startIndex], loop-startIndex,                             sizeof(contentSortInfo*), testSortCallback, (void*)sortInfo);              startIndex = loop+1;            }          }        }      }            if (loop > startIndex+1) {        if (merelyInvertFlag)          InvertSortInfo(&contentSortInfoArray[startIndex], loop-startIndex);        else          NS_QuickSort((void*)&contentSortInfoArray[startIndex], loop-startIndex,                       sizeof(contentSortInfo*), testSortCallback, (void*)sortInfo);        startIndex = loop+1;      }    } else {      if (merelyInvertFlag)        InvertSortInfo(&contentSortInfoArray[currentElement], numElements);      else        NS_QuickSort((void*)(&contentSortInfoArray[currentElement]), numElements,                     sizeof(contentSortInfo*), testSortCallback, (void*)sortInfo);    }    for (childIndex=numChildren-1; childIndex >= 0; childIndex--)    {      if (NS_FAILED(rv = container->ChildAt(childIndex, *getter_AddRefs(child)))) continue;      if (NS_FAILED(rv = child->GetNameSpaceID(nameSpaceID))) continue;      if (nameSpaceID == kNameSpaceID_XUL) {        if (NS_FAILED(rv = child->GetTag(*getter_AddRefs(tag)))) continue;        if (tag == nsXULAtoms::listitem || tag == nsXULAtoms::treeitem            || tag == nsXULAtoms::menu || tag == nsXULAtoms::menuitem) {          // immediately remove the child node, and ignore any errors          container->RemoveChildAt(childIndex, PR_FALSE);        }      }    }    // put the items back in sorted order    nsCOMPtr<nsIContent> parentNode;    nsAutoString value;    PRInt32 childPos = 0;    container->ChildCount(childPos);    for (loop=currentElement; loop < currentElement + numElements; loop++) {      contentSortInfo * contentSortInfo = contentSortInfoArray[loop];      parentNode = (nsIContent *)contentSortInfo->content;      nsIContent* kid = NS_STATIC_CAST(nsIContent*, parentNode);      container->InsertChildAt(kid, childPos++, PR_FALSE, PR_TRUE);      NS_RELEASE(contentSortInfo->content);      delete contentSortInfo;      // if it's a container, find its treechildren nodes, and sort those      if (NS_FAILED(rv = parentNode->GetAttr(kNameSpaceID_None, nsXULAtoms::container, value)) ||        (rv != NS_CONTENT_ATTR_HAS_VALUE) || (!value.EqualsIgnoreCase("true")))        continue;              if (NS_FAILED(rv = parentNode->ChildCount(numChildren))) continue;      for (childIndex=0; childIndex<numChildren; childIndex++) {        if (NS_FAILED(rv = parentNode->ChildAt(childIndex, *getter_AddRefs(child))))          continue;        if (NS_FAILED(rv = child->GetNameSpaceID(nameSpaceID))) continue;        if (nameSpaceID != kNameSpaceID_XUL) continue;        if (NS_FAILED(rv = child->GetTag(*getter_AddRefs(tag)))) continue;        if (tag != nsXULAtoms::treechildren && tag != nsXULAtoms::menupopup) continue;        sortInfo->parentContainer = parentNode;        SortContainer(child, sortInfo, merelyInvertFlag);      }    }  }    delete [] contentSortInfoArray;  contentSortInfoArray = nsnull;  return NS_OK;}