if (NS_FAILED(rv = child->GetTag(getter_AddRefs(tag)))) continue; if (tag == nsXULAtoms::listitem || tag == nsXULAtoms::treeitem || tag == nsXULAtoms::menu || tag == nsXULAtoms::menuitem) {
nsIAtom *tag = child->Tag();  if (tag == nsXULAtoms::listitem || tag == nsXULAtoms::treeitem || tag == nsXULAtoms::menu || tag == nsXULAtoms::menuitem) {
XULSortServiceImpl::SortContainer(nsIContent *container, sortPtr sortInfo,                                  PRBool merelyInvertFlag){  PRUint32 loop, numElements = 0, currentElement;  PRUint32 numChildren = container->GetChildCount();  if (numChildren < 1)    return NS_OK;  nsIDocument *doc = container->GetDocument();  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)  nsresult rv;  nsCOMPtr<nsIAtom> tag;  currentElement = numChildren;  PRUint32 childIndex;  // childIndex is unsigned, so childIndex >= 0 would always test true  for (childIndex = numChildren; childIndex > 0; ) {    --childIndex;    nsIContent *child = container->GetChildAt(childIndex);    if (child->IsContentOfType(nsIContent::eXUL)) {      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) {      PRUint32 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);    }    // childIndex is unsigned, so childIndex >= 0 would always test true    for (childIndex = numChildren; childIndex > 0; )    {      --childIndex;      nsIContent *child = container->GetChildAt(childIndex);      if (child->IsContentOfType(nsIContent::eXUL)) {        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    nsAutoString value;    PRUint32 childPos = container->GetChildCount();    for (loop = currentElement; loop < currentElement + numElements; loop++) {      contentSortInfo * contentSortInfo = contentSortInfoArray[loop];      nsIContent *parentNode = (nsIContent *)contentSortInfo->content;      nsIContent* kid = 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.Equals(NS_LITERAL_STRING("true")))        continue;              numChildren = parentNode->GetChildCount();      for (childIndex = 0; childIndex < numChildren; childIndex++) {        nsIContent *child = parentNode->GetChildAt(childIndex);        if (!child->IsContentOfType(nsIContent::eXUL))          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;}