From: L. David Baron <dbaron@dbaron.org>
Date: Mon, 24 Jan 2011 12:40:49 -0800
Subject: Correctly fix up the parentage of child sheets when removing the
 primary nsCSSStyleSheet from an nsCSSStyleSheetInner. (Bug 623351)
 r=bzbarsky a1.9.1.18=dveditz

---
 layout/style/nsCSSStyleSheet.cpp |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)

diff --git a/layout/style/nsCSSStyleSheet.cpp b/layout/style/nsCSSStyleSheet.cpp
index 143b1e3..7757551 100644
--- a/layout/style/nsCSSStyleSheet.cpp
+++ b/layout/style/nsCSSStyleSheet.cpp
@@ -812,6 +812,15 @@ struct ChildSheetListBuilder {
     aSheet->mParent = parent;
     aSheet->SetOwningDocument(parent->mDocument);
   }
+
+  static void ReparentChildList(nsCSSStyleSheet* aPrimarySheet,
+                                nsCSSStyleSheet* aFirstChild)
+  {
+    for (nsCSSStyleSheet *child = aFirstChild; child; child = child->mNext) {
+      child->mParent = aPrimarySheet;
+      child->SetOwningDocument(aPrimarySheet->mDocument);
+    }
+  }
 };
   
 PRBool
@@ -908,6 +917,9 @@ nsCSSStyleSheetInner::RemoveSheet(nsICSSStyleSheet* aSheet)
     NS_ASSERTION(mSheets.Count(), "no parents");
     mOrderedRules.EnumerateForwards(SetStyleSheetReference,
                                     (nsICSSStyleSheet*)mSheets.ElementAt(0));
+
+    ChildSheetListBuilder::ReparentChildList(static_cast<nsCSSStyleSheet*>(
+      (nsICSSStyleSheet*)mSheets.ElementAt(0)), mFirstChild);
   }
   else {
     mSheets.RemoveElement(aSheet);
