Bug 599975 - Fire error event for images with empty string src value; r=bz
MozReview-Commit-ID: AhXiTpgG9q3
--- 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>