Bug 678465 - 'document-element-inserted' doesn't fire on ImageDocument; r=bz
authorGabor Krizsanits <gkrizsanits@mozilla.com>
Thu, 15 Dec 2011 15:10:36 +0000
changeset 84297 49481d05bd7caaa6b4874263eb012e8815aff688
parent 84296 329274bcfd57f3f68b20d1ccba7b255e8e5a7229
child 84298 53e708b8faa9c543c2e4c06c30a1229f6f7b3a27
push id519
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 00:38:35 +0000
treeherdermozilla-beta@788ea1ef610b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs678465
milestone11.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 678465 - 'document-element-inserted' doesn't fire on ImageDocument; r=bz
content/base/public/nsContentUtils.h
content/html/document/src/ImageDocument.cpp
content/html/document/src/MediaDocument.cpp
content/html/document/src/MediaDocument.h
content/html/document/test/Makefile.in
content/html/document/test/test_document-element-inserted.html
parser/html/nsHtml5TreeOperation.cpp
--- a/content/base/public/nsContentUtils.h
+++ b/content/base/public/nsContentUtils.h
@@ -2156,9 +2156,26 @@ public:
     return GetParameter(nsnull, aResult);
   }
 
 private:
   NS_ConvertUTF16toUTF8 mString;
   nsIMIMEHeaderParam*   mService;
 };
 
+class nsDocElementCreatedNotificationRunner : public nsRunnable
+{
+public:
+    nsDocElementCreatedNotificationRunner(nsIDocument* aDoc)
+        : mDoc(aDoc)
+    {
+    }
+
+    NS_IMETHOD Run()
+    {
+        nsContentSink::NotifyDocElementCreated(mDoc);
+        return NS_OK;
+    }
+
+    nsCOMPtr<nsIDocument> mDoc;
+};
+
 #endif /* nsContentUtils_h___ */
--- a/content/html/document/src/ImageDocument.cpp
+++ b/content/html/document/src/ImageDocument.cpp
@@ -358,17 +358,17 @@ ImageDocument::SetScriptGlobalObject(nsI
     target = do_QueryInterface(mScriptGlobalObject);
     target->RemoveEventListener(NS_LITERAL_STRING("resize"), this, false);
     target->RemoveEventListener(NS_LITERAL_STRING("keypress"), this,
                                 false);
   }
 
   // Set the script global object on the superclass before doing
   // anything that might require it....
-  nsHTMLDocument::SetScriptGlobalObject(aScriptGlobalObject);
+  MediaDocument::SetScriptGlobalObject(aScriptGlobalObject);
 
   if (aScriptGlobalObject) {
     if (!GetRootElement()) {
       // Create synthetic document
 #ifdef DEBUG
       nsresult rv =
 #endif
         CreateSyntheticDocument();
--- a/content/html/document/src/MediaDocument.cpp
+++ b/content/html/document/src/MediaDocument.cpp
@@ -46,16 +46,17 @@
 #include "nsITextToSubURI.h"
 #include "nsIURL.h"
 #include "nsIContentViewer.h"
 #include "nsIMarkupDocumentViewer.h"
 #include "nsIDocShell.h"
 #include "nsIParser.h" // kCharsetFrom* macro definition
 #include "nsIDocumentCharsetInfo.h" 
 #include "nsNodeInfoManager.h"
+#include "nsContentUtils.h"
 
 namespace mozilla {
 namespace dom {
 
 MediaDocumentStreamListener::MediaDocumentStreamListener(MediaDocument *aDocument)
 {
   mDocument = aDocument;
 }
@@ -125,16 +126,17 @@ const char* const MediaDocument::sFormat
 {
   "MediaTitleWithNoInfo",    // eWithNoInfo
   "MediaTitleWithFile",      // eWithFile
   "",                        // eWithDim
   ""                         // eWithDimAndFile
 };
 
 MediaDocument::MediaDocument()
+    : mDocumentElementInserted(false)
 {
 }
 MediaDocument::~MediaDocument()
 {
 }
 
 nsresult
 MediaDocument::Init()
@@ -428,10 +430,21 @@ MediaDocument::UpdateTitleAndCharset(con
     const PRUnichar *formatStrings[2] = {title.get(), status.get()};
     NS_NAMED_LITERAL_STRING(fmtName, "TitleWithStatus");
     mStringBundle->FormatStringFromName(fmtName.get(), formatStrings, 2,
                                         getter_Copies(titleWithStatus));
     SetTitle(titleWithStatus);
   }
 }
 
+void 
+MediaDocument::SetScriptGlobalObject(nsIScriptGlobalObject* aGlobalObject)
+{
+    nsHTMLDocument::SetScriptGlobalObject(aGlobalObject);
+    if (!mDocumentElementInserted && aGlobalObject) {
+        mDocumentElementInserted = true;
+        nsContentUtils::AddScriptRunner(
+            new nsDocElementCreatedNotificationRunner(this));        
+    }
+}
+
 } // namespace dom
 } // namespace mozilla
--- a/content/html/document/src/MediaDocument.h
+++ b/content/html/document/src/MediaDocument.h
@@ -59,16 +59,18 @@ public:
   virtual nsresult StartDocumentLoad(const char*         aCommand,
                                      nsIChannel*         aChannel,
                                      nsILoadGroup*       aLoadGroup,
                                      nsISupports*        aContainer,
                                      nsIStreamListener** aDocListener,
                                      bool                aReset = true,
                                      nsIContentSink*     aSink = nsnull);
 
+  virtual void SetScriptGlobalObject(nsIScriptGlobalObject* aGlobalObject);
+
 protected:
   virtual nsresult CreateSyntheticDocument();
 
   friend class MediaDocumentStreamListener;
   nsresult StartLayout();
 
   void GetFileName(nsAString& aResult);
 
@@ -88,19 +90,20 @@ protected:
   void UpdateTitleAndCharset(const nsACString&  aTypeStr,
                              const char* const* aFormatNames = sFormatNames,
                              PRInt32            aWidth = 0,
                              PRInt32            aHeight = 0,
                              const nsAString&   aStatus = EmptyString());
 
   nsCOMPtr<nsIStringBundle>     mStringBundle;
   static const char* const      sFormatNames[4];
-
+  
 private:
   enum                          {eWithNoInfo, eWithFile, eWithDim, eWithDimAndFile};
+  bool                          mDocumentElementInserted;   
 };
 
 
 class MediaDocumentStreamListener: public nsIStreamListener
 {
 public:
   MediaDocumentStreamListener(MediaDocument *aDocument);
   virtual ~MediaDocumentStreamListener();
--- a/content/html/document/test/Makefile.in
+++ b/content/html/document/test/Makefile.in
@@ -72,16 +72,17 @@ include $(topsrcdir)/config/rules.mk
 		test_bug403868.xhtml \
 		test_bug404320.html \
 		$(warning test_bug435128.html disabled because it takes 60 seconds) \
 		test_bug463104.html \
 		test_bug468353.html \
 		test_form-parsing.html \
 		test_viewport.html \
 		test_documentAll.html \
+		test_document-element-inserted.html \
 		test_bug445004.html \
 		bug445004-inner.js \
 		bug445004-outer-rel.html \
 		bug445004-outer-abs.html \
 		bug445004-outer-write.html \
 		bug445004-inner.html \
 		test_bug446483.html \
 		bug446483-iframe.html \
new file mode 100644
--- /dev/null
+++ b/content/html/document/test/test_document-element-inserted.html
@@ -0,0 +1,49 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Media test: document-element-inserted</title>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<iframe id = 'media'>
+</iframe>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+SimpleTest.waitForExplicitFinish();
+var loc;
+
+var observe = function(doc){
+    if (doc == media.contentDocument) {
+        ok(media.contentDocument.location.toString().indexOf(loc) != -1,
+          "The loaded media should be " + loc);
+        next();
+    }
+}
+
+var media = document.getElementById('media');
+var tests = [
+    "../../../media/test/short-video.ogv",
+    "../../../media/test/sound.ogg",
+    "../../content/test/image.png"
+]
+
+function next() {
+    if (tests.length > 0) {
+        var t = tests.shift();
+        loc = t.substring(t.indexOf("test"));
+        media.setAttribute("src",t);
+    }
+    else {
+        SpecialPowers.removeObserver(observe, "document-element-inserted");
+        SimpleTest.finish();
+    }
+}
+
+SpecialPowers.addObserver(observe, "document-element-inserted", false)
+next();
+
+</script>
+</pre>
+</body>
+</html>
--- a/parser/html/nsHtml5TreeOperation.cpp
+++ b/parser/html/nsHtml5TreeOperation.cpp
@@ -228,33 +228,16 @@ nsHtml5TreeOperation::Append(nsIContent*
   rv = aParent->AppendChildTo(aNode, false);
   if (NS_SUCCEEDED(rv)) {
     nsNodeUtils::ContentAppended(aParent, aNode, childCount);
   }
   parentDoc->EndUpdate(UPDATE_CONTENT_MODEL);
   return rv;
 }
 
-class nsDocElementCreatedNotificationRunner : public nsRunnable
-{
-public:
-  nsDocElementCreatedNotificationRunner(nsIDocument* aDoc)
-    : mDoc(aDoc)
-  {
-  }
-
-  NS_IMETHOD Run()
-  {
-    nsContentSink::NotifyDocElementCreated(mDoc);
-    return NS_OK;
-  }
-
-  nsCOMPtr<nsIDocument> mDoc;
-};
-
 nsresult
 nsHtml5TreeOperation::AppendToDocument(nsIContent* aNode,
                                        nsHtml5TreeOpExecutor* aBuilder)
 {
   nsresult rv = NS_OK;
   aBuilder->FlushPendingAppendNotifications();
   nsIDocument* doc = aBuilder->GetDocument();
   PRUint32 childCount = doc->GetChildCount();