Bug 599975 - Fire error event for images with empty string src value; r=bz
authorEdgar Chen <echen@mozilla.com>
Thu, 08 Sep 2016 16:27:03 +0800
changeset 356946 743d1b4c52dbf91559b049822c8caa02e86578d6
parent 356918 57647be72e76cb9c8b8ea6069c6d53362c9892d9
child 356947 4c66e0814004e1226362e63581d10c941961cc4c
push id1324
push usermtabara@mozilla.com
push dateMon, 16 Jan 2017 13:07:44 +0000
treeherdermozilla-release@a01c49833940 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs599975
milestone51.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 599975 - Fire error event for images with empty string src value; r=bz MozReview-Commit-ID: AhXiTpgG9q3
dom/base/nsImageLoadingContent.cpp
testing/web-platform/tests/html/semantics/embedded-content/the-img-element/invalid-src.html
--- a/dom/base/nsImageLoadingContent.cpp
+++ b/dom/base/nsImageLoadingContent.cpp
@@ -752,45 +752,34 @@ nsImageLoadingContent::LoadImage(const n
 {
   // First, get a document (needed for security checks and the like)
   nsIDocument* doc = GetOurOwnerDoc();
   if (!doc) {
     // No reason to bother, I think...
     return NS_OK;
   }
 
+  if (aNewURI.IsEmpty()) {
+    // Cancel image requests and then fire only error event per spec.
+    CancelImageRequests(aNotify);
+    FireEvent(NS_LITERAL_STRING("error"));
+    return NS_OK;
+  }
+
   // Second, parse the URI string to get image URI
   nsCOMPtr<nsIURI> imageURI;
   nsresult rv = StringToURI(aNewURI, doc, getter_AddRefs(imageURI));
   if (NS_FAILED(rv)) {
     // Cancel image requests and then fire error and loadend events per spec
     CancelImageRequests(aNotify);
     FireEvent(NS_LITERAL_STRING("error"));
     FireEvent(NS_LITERAL_STRING("loadend"));
     return NS_OK;
   }
 
-  bool equal;
-
-  if (aNewURI.IsEmpty() &&
-      doc->GetDocumentURI() &&
-      NS_SUCCEEDED(doc->GetDocumentURI()->EqualsExceptRef(imageURI, &equal)) &&
-      equal)  {
-
-    // Loading an embedded img from the same URI as the document URI will not work
-    // as a resource cannot recursively embed itself. Attempting to do so generally
-    // results in having to pre-emptively close down an in-flight HTTP transaction
-    // and then incurring the significant cost of establishing a new TCP channel.
-    // This is generally triggered from <img src="">
-    // In light of that, just skip loading it..
-    // Do make sure to drop our existing image, if any
-    CancelImageRequests(aNotify);
-    return NS_OK;
-  }
-
   NS_TryToSetImmutable(imageURI);
 
   return LoadImage(imageURI, aForce, aNotify, aImageLoadType, doc);
 }
 
 nsresult
 nsImageLoadingContent::LoadImage(nsIURI* aNewURI,
                                  bool aForce,
--- a/testing/web-platform/tests/html/semantics/embedded-content/the-img-element/invalid-src.html
+++ b/testing/web-platform/tests/html/semantics/embedded-content/the-img-element/invalid-src.html
@@ -1,21 +1,42 @@
 <!doctype html>
 <meta charset="utf-8">
 <title>Loading a non-parsing URL as an image should silently fail; triggering appropriate events</title>
 <script src="/resources/testharness.js"></script>
 <script src="/resources/testharnessreport.js"></script>
-<img id=myimg />
+<img id=brokenurl />
+<img id=emptysrc />
 <script>
 async_test(function(t) {
-    var img = document.getElementById("myimg");
+    var img = document.getElementById("brokenurl");
     img.src = "http://also a broken url";
     var errorevent = false;
 
     // The errors should be queued in the event loop, so they should only trigger
     // after this block of code finishes, not during the img.src setter itself
     img.addEventListener('error', t.step_func(function(){errorevent = true;}));
     img.addEventListener('loadend', t.step_func_done(function() {
         assert_true(errorevent, "error event fired");
     }));
-});
+}, 'src="http://also a broken url"');
+
+async_test(function(t) {
+    var img = document.getElementById("emptysrc");
+    img.src = "";
+    var loadendevent = false;
+
+    // Setting src to empty string triggers only error event.
+    // The errors should be queued in the event loop, so they should only trigger
+    // after this block of code finishes, not during the img.src setter itself
+    img.addEventListener('error', t.step_func(function() {
+        // Queue this check in the event loop to check there is no loadend event
+        // fired.
+        t.step_timeout(t.step_func_done(function() {
+            assert_false(loadendevent, "loadend event should not fired");
+        }), 0)
+    }));
+    img.addEventListener('loadend', t.step_func(function() {
+        loadendevent = true;
+    }));
+}, 'src=""');
 
 </script>