Commit 2d3ccbfc authored by Yu Han's avatar Yu Han Committed by Commit Bot

Fixes empty alt text on broken images overriding css dimensions.

Prior to this CL, an img with CSS height, width and empty alt text
renders as nothing. This is wrong because [2] states that the img
should be rendered as a replaced element with its intrinsic dimensions.

The fix continues from the previous fix to the img title attribute by
adding an additional check to see if the alt text is empty too.

[1] https://html.spec.whatwg.org/multipage/rendering.html#images-3::represents-2

Bug: 753868
Change-Id: I9b127c54d9d5eb0f1e900dee47f24aff09f2ed8b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2238327
Commit-Queue: Yu Han <yuzhehan@chromium.org>
Reviewed-by: default avatarStephen Chenney <schenney@chromium.org>
Reviewed-by: default avatarFredrik Söderquist <fs@opera.com>
Reviewed-by: default avatarMason Freed <masonfreed@chromium.org>
Cr-Commit-Position: refs/heads/master@{#779151}
parent 31c56560
......@@ -21,14 +21,6 @@
namespace blink {
static bool NoImageSourceSpecified(const Element& element) {
return element.FastGetAttribute(html_names::kSrcAttr).IsEmpty();
}
static bool ImageHasNoAltAttribute(const Element& element) {
return element.FastGetAttribute(html_names::kAltAttr).IsNull();
}
static bool ElementRepresentsNothing(const Element& element) {
const auto& html_element = To<HTMLElement>(element);
// We source fallback content/alternative text from more than just the 'alt'
......@@ -36,7 +28,7 @@ static bool ElementRepresentsNothing(const Element& element) {
// well.
bool alt_is_set = !html_element.AltText().IsNull();
bool alt_is_empty = alt_is_set && html_element.AltText().IsEmpty();
bool src_is_set = !NoImageSourceSpecified(element);
bool src_is_set = !element.getAttribute(html_names::kSrcAttr).IsEmpty();
if (src_is_set && alt_is_empty)
return true;
return !src_is_set && (!alt_is_set || alt_is_empty);
......@@ -177,7 +169,8 @@ void HTMLImageFallbackHelper::CustomStyleForAltText(Element& element,
bool image_has_intrinsic_dimensions =
new_style.Width().IsSpecifiedOrIntrinsic() &&
new_style.Height().IsSpecifiedOrIntrinsic();
bool image_has_no_alt_attribute = ImageHasNoAltAttribute(element);
bool image_has_no_alt_attribute =
element.getAttribute(html_names::kAltAttr).IsEmpty();
bool treat_as_replaced =
image_has_intrinsic_dimensions &&
(element.GetDocument().InQuirksMode() || image_has_no_alt_attribute);
......
......@@ -3,8 +3,11 @@
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="author" href="mailto:emilio@crisal.io" title="Emilio Cobos Álvarez">
<link rel="author" href="mailto:yuzhehan@chromium.org" title="Yu Han">
<link rel="author" href="https://mozilla.org" title="Mozilla">
<link rel="help" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1196668">
<link rel="help" href="https://crbug.com/753868">
<link ref="help" href="https://html.spec.whatwg.org/multipage/rendering.html#images-3">
<style>
img {
width: 100px;
......@@ -14,6 +17,8 @@
<img>
<img src="broken">
<img alt="">
<img alt>
<img src="broken" alt="">
<script>
const t = async_test("Images without alt attribute or with an empty alt attribute render as replaced elements regardless of src");
onload = t.step_func_done(function() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment