if (nsnull != aElement) { nsIDOMNode* domNode = nsnull; if (NS_SUCCEEDED(aElement->QueryInterface(NS_GET_IID(nsIDOMNode), (void**)&domNode))) { aSheet->SetOwningNode(domNode); NS_RELEASE(domNode);
nsresult rv = NS_OK; nsCOMPtr<nsIDOM3Node> linkingNode = do_QueryInterface(aLinkingContent); NS_ASSERTION(linkingNode || !aLinkingContent, "Need to implement nsIDOM3Node to get insertion order right");    PRInt32 sheetCount; aDocument->GetNumberOfStyleSheets(PR_FALSE, &sheetCount);   PRInt32 insertionPoint; for (insertionPoint = sheetCount - 1; insertionPoint >= 0; --insertionPoint) { nsCOMPtr<nsIStyleSheet> curSheet; aDocument->GetStyleSheetAt(insertionPoint, PR_FALSE, getter_AddRefs(curSheet)); NS_ASSERTION(curSheet, "There must be a sheet here!"); nsCOMPtr<nsIDOMStyleSheet> domSheet = do_QueryInterface(curSheet); NS_ASSERTION(domSheet, "All the \"normal\" sheets implement nsIDOMStyleSheet"); nsCOMPtr<nsIDOMNode> sheetOwner; domSheet->GetOwnerNode(getter_AddRefs(sheetOwner)); if (sheetOwner && !linkingNode) {   continue;
CSSLoaderImpl::InsertSheetInDoc(nsICSSStyleSheet* aSheet, PRInt32 aDocIndex,                                 nsIContent* aElement, PRBool aNotify,                                nsICSSLoaderObserver* aObserver){  if ((! mDocument) || (! aSheet)) {    return NS_ERROR_NULL_POINTER;  }  if (nsnull != aElement) {    nsIDOMNode* domNode = nsnull;    if (NS_SUCCEEDED(aElement->QueryInterface(NS_GET_IID(nsIDOMNode), (void**)&domNode))) {      aSheet->SetOwningNode(domNode);      NS_RELEASE(domNode);    }    nsIStyleSheetLinkingElement* element;    if (NS_SUCCEEDED(aElement->QueryInterface(NS_GET_IID(nsIStyleSheetLinkingElement),                                              (void**)&element))) {      element->SetStyleSheet(aSheet);      NS_RELEASE(element);    }  }  nsAutoString  title;  aSheet->GetTitle(title);  aSheet->SetEnabled(! IsAlternate(title));  nsVoidKey key(mDocument);  nsAutoVoidArray*  sheetMap = (nsAutoVoidArray*)mSheetMapTable.Get(&key);  if (! sheetMap) {    sheetMap = new nsAutoVoidArray();    if (sheetMap) {      mSheetMapTable.Put(&key, sheetMap);    }  }  if (sheetMap) {    PRInt32 insertIndex = sheetMap->Count();    PRBool insertedSheet = PR_FALSE;    while (0 <= --insertIndex) {      PRInt32 targetIndex = NS_PTR_TO_INT32(sheetMap->ElementAt(insertIndex));      if (targetIndex < aDocIndex) {        mDocument->InsertStyleSheetAt(aSheet, insertIndex + 1, aNotify);        sheetMap->InsertElementAt((void*)aDocIndex, insertIndex + 1);        insertedSheet = PR_TRUE;        break;      }    }    if (!insertedSheet) { // didn't insert yet      mDocument->InsertStyleSheetAt(aSheet, 0, aNotify);      sheetMap->InsertElementAt((void*)aDocIndex, 0);    }    if (nsnull != aObserver) {      aObserver->StyleSheetLoaded(aSheet, aNotify);    }    return NS_OK;  }  return NS_ERROR_OUT_OF_MEMORY;}