Bug 739004. When cloning a static clone, set the mOriginalDocument of the new clone to the mOriginalDocument of the thing being cloned, not to the thing being cloned (which is not an original document at all). r=smaug, a=akeybl
authorBoris Zbarsky <bzbarsky@mit.edu>
Sat, 24 Mar 2012 20:46:03 -0400
changeset 92004 670caaf9d22bc0b98774ad391573a9626c5bc029
parent 92003 4e89e0ae835499519d46dbb3a7f74af7a36d701f
child 92005 4696c6beca3d1720e5b6977d69ceed02b7e61111
push idunknown
push userunknown
push dateunknown
reviewerssmaug, akeybl
bugs739004
milestone13.0a2
Bug 739004. When cloning a static clone, set the mOriginalDocument of the new clone to the mOriginalDocument of the thing being cloned, not to the thing being cloned (which is not an original document at all). r=smaug, a=akeybl
content/base/public/nsIDocument.h
content/base/src/nsDocument.cpp
layout/base/nsPresShell.cpp
--- a/content/base/public/nsIDocument.h
+++ b/content/base/public/nsIDocument.h
@@ -1414,17 +1414,21 @@ public:
    */
   virtual already_AddRefed<nsIDocument>
   CreateStaticClone(nsISupports* aCloneContainer);
 
   /**
    * If this document is a static clone, this returns the original
    * document.
    */
-  nsIDocument* GetOriginalDocument() { return mOriginalDocument; }
+  nsIDocument* GetOriginalDocument()
+  {
+    MOZ_ASSERT(!mOriginalDocument || !mOriginalDocument->GetOriginalDocument());
+    return mOriginalDocument;
+  }
 
   /**
    * Called by nsParser to preload images. Can be removed and code moved
    * to nsPreloadURIs::PreloadURIs() in file nsParser.cpp whenever the
    * parser-module is linked with gklayout-module.  aCrossOriginAttr should
    * be a void string if the attr is not present.
    */
   virtual void MaybePreLoadImage(nsIURI* uri,
--- a/content/base/src/nsDocument.cpp
+++ b/content/base/src/nsDocument.cpp
@@ -8080,19 +8080,22 @@ nsIDocument::CreateStaticClone(nsISuppor
   SetContainer(aCloneContainer);
   nsCOMPtr<nsIDOMNode> clonedNode;
   nsresult rv = domDoc->CloneNode(true, 1, getter_AddRefs(clonedNode));
   SetContainer(originalContainer);
 
   nsCOMPtr<nsIDocument> clonedDoc;
   if (NS_SUCCEEDED(rv)) {
     clonedDoc = do_QueryInterface(clonedNode);
-    nsCOMPtr<nsIDOMDocument> clonedDOMDoc = do_QueryInterface(clonedDoc);
-    if (clonedDOMDoc) {
-      clonedDoc->mOriginalDocument = this;
+    if (clonedDoc) {
+      if (IsStaticDocument()) {
+        clonedDoc->mOriginalDocument = mOriginalDocument;
+      } else {
+        clonedDoc->mOriginalDocument = this;
+      }
       PRInt32 sheetsCount = GetNumberOfStyleSheets();
       for (PRInt32 i = 0; i < sheetsCount; ++i) {
         nsRefPtr<nsCSSStyleSheet> sheet = do_QueryObject(GetStyleSheetAt(i));
         if (sheet) {
           if (sheet->IsApplicable()) {
             nsRefPtr<nsCSSStyleSheet> clonedSheet =
               sheet->Clone(nsnull, nsnull, clonedDoc, nsnull);
             NS_WARN_IF_FALSE(clonedSheet, "Cloning a stylesheet didn't work!");
--- a/layout/base/nsPresShell.cpp
+++ b/layout/base/nsPresShell.cpp
@@ -1409,21 +1409,18 @@ PresShell::SetPrefNoScriptRule()
 {
   NS_TIME_FUNCTION_MIN(1.0);
 
   nsresult rv = NS_OK;
 
   // also handle the case where print is done from print preview
   // see bug #342439 for more details
   nsIDocument* doc = mDocument;
-  if (mPresContext->Type() == nsPresContext::eContext_PrintPreview ||
-      mPresContext->Type() == nsPresContext::eContext_Print) {
-    while (doc->GetOriginalDocument()) {
-      doc = doc->GetOriginalDocument();
-    }
+  if (doc->IsStaticDocument()) {
+    doc = doc->GetOriginalDocument();
   }
 
   bool scriptEnabled = doc->IsScriptEnabled();
   if (scriptEnabled) {
     if (!mPrefStyleSheet) {
       rv = CreatePreferenceStyleSheet();
       NS_ENSURE_SUCCESS(rv, rv);
     }