Bug 1616537 - Don't give images with empty alt an intrinsic ratio. r=bzbarsky
authorEmilio Cobos Álvarez <emilio@crisal.io>
Fri, 21 Feb 2020 21:03:22 +0000
changeset 2648953 76472c6d442738730a601ecf22d6cd83e570cae2
parent 2648952 4b23f08adbb295c8f6b2da5d2d0a9beab70b2492
child 2648954 e09bb8097c62c6e0f966a42e0a2c02a9ccadd1a8
push id490874
push userdvarga@mozilla.com
push dateSat, 22 Feb 2020 10:42:44 +0000
treeherdertry@64a8e247759e [default view] [failures only]
reviewersbzbarsky
bugs1616537, 1196668
milestone75.0a1
Bug 1616537 - Don't give images with empty alt an intrinsic ratio. r=bzbarsky The spec seems to want us to treat it like an inline, but that's not what other browsers do (I added tests for this in bug 1196668, and they still pass in all engines). This is an oddly specific condition to put in nsImageFrame::ShouldShowBrokenImageIcon(), but oh well, we're past all sanity here I think... Differential Revision: https://phabricator.services.mozilla.com/D63715
layout/generic/nsImageFrame.cpp
testing/web-platform/tests/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/img-empty-alt-replaced.html
--- a/layout/generic/nsImageFrame.cpp
+++ b/layout/generic/nsImageFrame.cpp
@@ -175,16 +175,25 @@ nsIFrame* NS_NewImageFrameForGeneratedCo
 bool nsImageFrame::ShouldShowBrokenImageIcon() const {
   // NOTE(emilio, https://github.com/w3c/csswg-drafts/issues/2832): WebKit and
   // Blink behave differently here for content: url(..), for now adapt to
   // Blink's behavior.
   if (mKind != Kind::ImageElement) {
     return false;
   }
 
+  // <img alt=""> is special, and it shouldn't draw the broken image icon,
+  // unlike the no-alt attribute or non-empty-alt-attribute case.
+  if (auto* image = HTMLImageElement::FromNode(mContent)) {
+    const nsAttrValue* alt = image->GetParsedAttr(nsGkAtoms::alt);
+    if (alt && alt->IsEmptyString()) {
+      return false;
+    }
+  }
+
   // check for broken images. valid null images (eg. img src="") are
   // not considered broken because they have no image requests
   if (nsCOMPtr<imgIRequest> currentRequest = GetCurrentRequest()) {
     uint32_t imageStatus;
     return NS_SUCCEEDED(currentRequest->GetImageStatus(&imageStatus)) &&
            (imageStatus & imgIRequest::STATUS_ERROR);
   }
 
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/img-empty-alt-replaced.html
@@ -0,0 +1,23 @@
+<!doctype html>
+<title>Images with an empty alt attribute have an intrinsic size of zero</title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<style>
+  img {
+    width: 50px;
+    height: auto;
+  }
+</style>
+<img src="broken">
+<img src="broken" alt="non-empty">
+<img src="broken" alt="">
+<script>
+const t = async_test("Images with an empty alt attribute have an intrinsic size of zero");
+onload = t.step_func_done(function() {
+  for (const img of document.querySelectorAll("img")) {
+    const alt = img.getAttribute("alt");
+    const shouldTakeUpSpace = alt == null || alt.length > 0;
+    (shouldTakeUpSpace ? assert_not_equals : assert_equals)(img.offsetHeight, 0, img.outerHTML);
+  }
+});
+</script>