if (mDocument) { result = mDocument->BeginUpdate(UPDATE_STYLE); NS_ENSURE_SUCCESS(result, result); }
mozAutoDocUpdate updateBatch(mDocument, UPDATE_STYLE, PR_TRUE);
CSSStyleSheetImpl::InsertRuleIntoGroup(const nsAString & aRule, nsICSSGroupRule* aGroup, PRUint32 aIndex, PRUint32* _retval){  nsresult result;  NS_ASSERTION(mInner && mInner->mComplete,               "No inserting into an incomplete sheet!");  // check that the group actually belongs to this sheet!  nsCOMPtr<nsIStyleSheet> groupSheet;  aGroup->GetStyleSheet(*getter_AddRefs(groupSheet));  if (this != groupSheet) {    return NS_ERROR_INVALID_ARG;  }  // get the css parser  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);    }  }  NS_ENSURE_SUCCESS(result, result);  // parse and grab the rule   if (mDocument) {    result = mDocument->BeginUpdate(UPDATE_STYLE);    NS_ENSURE_SUCCESS(result, result);  }  result = WillDirty();  NS_ENSURE_SUCCESS(result, result);  nsCOMPtr<nsISupportsArray> rules;  result = css->ParseRule(aRule, mInner->mURL, getter_AddRefs(rules));  NS_ENSURE_SUCCESS(result, result);  PRUint32 rulecount = 0;  rules->Count(&rulecount);    if (rulecount == 0 && !aRule.IsEmpty()) {    return NS_ERROR_DOM_SYNTAX_ERR;  }  PRUint32 counter;  nsCOMPtr<nsICSSRule> rule;  for (counter = 0; counter < rulecount; counter++) {    // Only rulesets are allowed in a group as of CSS2    PRInt32 type = nsICSSRule::UNKNOWN_RULE;    rule = dont_AddRef((nsICSSRule*)rules->ElementAt(counter));    rule->GetType(type);    if (type != nsICSSRule::STYLE_RULE) {      return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;    }  }    result = aGroup->InsertStyleRulesAt(aIndex, rules);  NS_ENSURE_SUCCESS(result, result);  DidDirty();  for (counter = 0; counter < rulecount; counter++) {    rule = dont_AddRef((nsICSSRule*)rules->ElementAt(counter));      if (mDocument) {      result = mDocument->StyleRuleAdded(this, rule);      NS_ENSURE_SUCCESS(result, result);    }  }  if (mDocument) {    result = mDocument->EndUpdate(UPDATE_STYLE);    NS_ENSURE_SUCCESS(result, result);  }  if (loader) {    loader->RecycleParser(css);  }  *_retval = aIndex;  return NS_OK;}