*getter_AddRefs(newNameSpace));
getter_AddRefs(newNameSpace));
CSSStyleSheetImpl::InsertRule(const nsAString& aRule,                               PRUint32 aIndex,                               PRUint32* aReturn){  NS_ENSURE_TRUE(mInner, NS_ERROR_FAILURE);  // No doing this if the sheet is not complete!  PRBool complete;  GetComplete(complete);  if (!complete) {    return NS_ERROR_DOM_INVALID_ACCESS_ERR;  }    nsresult result;  result = WillDirty();  if (NS_FAILED(result))    return result;    if (! mInner->mOrderedRules) {    result = NS_NewISupportsArray(&(mInner->mOrderedRules));  }  if (NS_FAILED(result))    return result;  PRUint32 count;  mInner->mOrderedRules->Count(&count);  if (aIndex > count)    return NS_ERROR_DOM_INDEX_SIZE_ERR;    nsCOMPtr<nsICSSLoader> loader;  nsCOMPtr<nsICSSParser> css;  nsCOMPtr<nsIHTMLContentContainer> htmlContainer(do_QueryInterface(mDocument));  if (htmlContainer) {    htmlContainer->GetCSSLoader(*getter_AddRefs(loader));  }  NS_ASSERTION(loader || !mDocument, "Document with no CSS loader!");  if (loader) {    result = loader->GetParserFor(this, getter_AddRefs(css));  }  else {    result = NS_NewCSSParser(getter_AddRefs(css));    if (css) {      css->SetStyleSheet(this);    }  }  if (NS_FAILED(result))    return result;  if (mDocument) {    result = mDocument->BeginUpdate();    if (NS_FAILED(result))      return result;  }  nsCOMPtr<nsISupportsArray> rules;  result = css->ParseRule(aRule, mInner->mURL, getter_AddRefs(rules));  if (NS_FAILED(result))    return result;    PRUint32 rulecount = 0;  rules->Count(&rulecount);    if (rulecount == 0 && !aRule.IsEmpty()) {    return NS_ERROR_DOM_SYNTAX_ERR;  }    // Hierarchy checking.  Just check the first and last rule in the list.    // check that we're not inserting before a charset rule  nsCOMPtr<nsICSSRule> nextRule;  PRInt32 nextType = nsICSSRule::UNKNOWN_RULE;  nextRule = dont_AddRef((nsICSSRule*)mInner->mOrderedRules->ElementAt(aIndex));  if (nextRule) {    nextRule->GetType(nextType);    if (nextType == nsICSSRule::CHARSET_RULE) {      return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;    }    // check last rule in list    nsCOMPtr<nsICSSRule> lastRule = dont_AddRef((nsICSSRule*)rules->ElementAt(rulecount-1));    PRInt32 lastType = nsICSSRule::UNKNOWN_RULE;    lastRule->GetType(lastType);        if (nextType == nsICSSRule::IMPORT_RULE &&        lastType != nsICSSRule::CHARSET_RULE &&        lastType != nsICSSRule::IMPORT_RULE) {      return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;    }        if (nextType == nsICSSRule::NAMESPACE_RULE &&        lastType != nsICSSRule::CHARSET_RULE &&        lastType != nsICSSRule::IMPORT_RULE &&        lastType != nsICSSRule::NAMESPACE_RULE) {      return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;    }   }    // check first rule in list  nsCOMPtr<nsICSSRule> firstRule = dont_AddRef((nsICSSRule*)rules->ElementAt(0));  PRInt32 firstType = nsICSSRule::UNKNOWN_RULE;  firstRule->GetType(firstType);  if (aIndex != 0) {    if (firstType == nsICSSRule::CHARSET_RULE) { // no inserting charset at nonzero position      return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;    }      nsCOMPtr<nsICSSRule> prevRule = dont_AddRef((nsICSSRule*)mInner->mOrderedRules->ElementAt(aIndex-1));    PRInt32 prevType = nsICSSRule::UNKNOWN_RULE;    prevRule->GetType(prevType);    if (firstType == nsICSSRule::IMPORT_RULE &&        prevType != nsICSSRule::CHARSET_RULE &&        prevType != nsICSSRule::IMPORT_RULE) {      return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;    }    if (firstType == nsICSSRule::NAMESPACE_RULE &&        prevType != nsICSSRule::CHARSET_RULE &&        prevType != nsICSSRule::IMPORT_RULE &&        prevType != nsICSSRule::NAMESPACE_RULE) {      return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;    }  }    result = mInner->mOrderedRules->InsertElementsAt(rules, aIndex);  NS_ENSURE_SUCCESS(result, result);  DidDirty();  nsCOMPtr<nsICSSRule> cssRule;  PRUint32 counter;  for (counter = 0; counter < rulecount; counter++) {    cssRule = dont_AddRef((nsICSSRule*)rules->ElementAt(counter));    cssRule->SetStyleSheet(this);        PRInt32 type = nsICSSRule::UNKNOWN_RULE;    cssRule->GetType(type);    if (type == nsICSSRule::NAMESPACE_RULE) {      if (! mInner->mNameSpace) {        nsContentUtils::GetNSManagerWeakRef()->CreateRootNameSpace(*getter_AddRefs(mInner->mNameSpace));      }      NS_ENSURE_TRUE(mInner->mNameSpace, NS_ERROR_FAILURE);      nsCOMPtr<nsICSSNameSpaceRule> nameSpaceRule(do_QueryInterface(cssRule));      nsCOMPtr<nsINameSpace> newNameSpace;          nsCOMPtr<nsIAtom> prefix;      nsAutoString urlSpec;      nameSpaceRule->GetPrefix(*getter_AddRefs(prefix));      nameSpaceRule->GetURLSpec(urlSpec);      mInner->mNameSpace->CreateChildNameSpace(prefix, urlSpec,                                               *getter_AddRefs(newNameSpace));      if (newNameSpace) {        mInner->mNameSpace = newNameSpace;      }    }    // We don't notify immediately for @import rules, but rather when    // the sheet the rule is importing is loaded    PRBool notify = PR_TRUE;    if (type == nsICSSRule::IMPORT_RULE) {      nsCOMPtr<nsIDOMCSSImportRule> importRule(do_QueryInterface(cssRule));      NS_ASSERTION(importRule, "Rule which has type IMPORT_RULE and does not implement nsIDOMCSSImportRule!");      nsCOMPtr<nsIDOMCSSStyleSheet> childSheet;      importRule->GetStyleSheet(getter_AddRefs(childSheet));      if (!childSheet) {        notify = PR_FALSE;      }    }    if (mDocument && notify) {      result = mDocument->StyleRuleAdded(this, cssRule);      NS_ENSURE_SUCCESS(result, result);    }  }    if (mDocument) {    result = mDocument->EndUpdate();    NS_ENSURE_SUCCESS(result, result);  }  if (loader) {    loader->RecycleParser(css);  }    *aReturn = aIndex;  return NS_OK;}