Bug 1602047 - For <img src=""> (no current request), do not use mapped aspect ratio. r=tnikkel

This works in chrome because they don't event create an image box for this case,
which is totally against the spec.

The spec doesn't consider an image with a null image request, but our behavior
changes in some other places as well because of it...

Depends on D56367

Differential Revision: https://phabricator.services.mozilla.com/D56368
This commit is contained in:
Emilio Cobos Álvarez
2019-12-10 05:28:15 +00:00
parent 77a365c22a
commit 0ba9abf4dd
2 changed files with 16 additions and 7 deletions

View File

@@ -440,6 +440,7 @@ static void ScaleIntrinsicSizeForDensity(nsIContent& aContent,
} }
static IntrinsicSize ComputeIntrinsicSize(imgIContainer* aImage, static IntrinsicSize ComputeIntrinsicSize(imgIContainer* aImage,
bool aHasRequest,
nsImageFrame::Kind aKind, nsImageFrame::Kind aKind,
const nsImageFrame& aFrame) { const nsImageFrame& aFrame) {
const ComputedStyle& style = *aFrame.Style(); const ComputedStyle& style = *aFrame.Style();
@@ -464,7 +465,7 @@ static IntrinsicSize ComputeIntrinsicSize(imgIContainer* aImage,
return IntrinsicSize(edgeLengthToUse, edgeLengthToUse); return IntrinsicSize(edgeLengthToUse, edgeLengthToUse);
} }
if (style.StylePosition()->mAspectRatio != 0.0f) { if (aHasRequest && style.StylePosition()->mAspectRatio != 0.0f) {
return IntrinsicSize(); return IntrinsicSize();
} }
@@ -473,11 +474,13 @@ static IntrinsicSize ComputeIntrinsicSize(imgIContainer* aImage,
bool nsImageFrame::UpdateIntrinsicSize() { bool nsImageFrame::UpdateIntrinsicSize() {
IntrinsicSize oldIntrinsicSize = mIntrinsicSize; IntrinsicSize oldIntrinsicSize = mIntrinsicSize;
mIntrinsicSize = ComputeIntrinsicSize(mImage, mKind, *this); nsCOMPtr<imgIRequest> currentRequest = GetCurrentRequest();
mIntrinsicSize = ComputeIntrinsicSize(mImage, !!currentRequest, mKind, *this);
return mIntrinsicSize != oldIntrinsicSize; return mIntrinsicSize != oldIntrinsicSize;
} }
static AspectRatio ComputeAspectRatio(imgIContainer* aImage, static AspectRatio ComputeAspectRatio(imgIContainer* aImage,
bool aHasRequest,
const nsImageFrame& aFrame) { const nsImageFrame& aFrame) {
const ComputedStyle& style = *aFrame.Style(); const ComputedStyle& style = *aFrame.Style();
if (style.StyleDisplay()->IsContainSize()) { if (style.StyleDisplay()->IsContainSize()) {
@@ -488,7 +491,7 @@ static AspectRatio ComputeAspectRatio(imgIContainer* aImage,
return *fromImage; return *fromImage;
} }
} }
if (style.StylePosition()->mAspectRatio != 0.0f) { if (aHasRequest && style.StylePosition()->mAspectRatio != 0.0f) {
return AspectRatio(style.StylePosition()->mAspectRatio); return AspectRatio(style.StylePosition()->mAspectRatio);
} }
if (aFrame.ShouldShowBrokenImageIcon()) { if (aFrame.ShouldShowBrokenImageIcon()) {
@@ -499,7 +502,8 @@ static AspectRatio ComputeAspectRatio(imgIContainer* aImage,
bool nsImageFrame::UpdateIntrinsicRatio() { bool nsImageFrame::UpdateIntrinsicRatio() {
AspectRatio oldIntrinsicRatio = mIntrinsicRatio; AspectRatio oldIntrinsicRatio = mIntrinsicRatio;
mIntrinsicRatio = ComputeAspectRatio(mImage, *this); nsCOMPtr<imgIRequest> currentRequest = GetCurrentRequest();
mIntrinsicRatio = ComputeAspectRatio(mImage, !!currentRequest, *this);
return mIntrinsicRatio != oldIntrinsicRatio; return mIntrinsicRatio != oldIntrinsicRatio;
} }
@@ -1720,9 +1724,12 @@ static bool OldImageHasDifferentRatio(const nsImageFrame& aFrame,
} }
auto currentRatio = aFrame.GetComputedIntrinsicRatio(); auto currentRatio = aFrame.GetComputedIntrinsicRatio();
MOZ_ASSERT(currentRatio == ComputeAspectRatio(&aImage, aFrame), // If we have an image, we need to have a current request.
// Same if we had an image.
const bool hasRequest = true;
MOZ_ASSERT(currentRatio == ComputeAspectRatio(&aImage, hasRequest, aFrame),
"aspect-ratio got out of sync during paint? How?"); "aspect-ratio got out of sync during paint? How?");
auto oldRatio = ComputeAspectRatio(aPrevImage, aFrame); auto oldRatio = ComputeAspectRatio(aPrevImage, hasRequest, aFrame);
return oldRatio != currentRatio; return oldRatio != currentRatio;
} }

View File

@@ -11,6 +11,7 @@
</style> </style>
<img src="/images/green.png"> <img src="/images/green.png">
<img src="/images/green.png" width=100 height=125> <img src="/images/green.png" width=100 height=125>
<img src="" width=100 height=125>
<script> <script>
let t = async_test("Image width and height attributes are used to infer aspect-ratio"); let t = async_test("Image width and height attributes are used to infer aspect-ratio");
function assert_ratio(img, expected) { function assert_ratio(img, expected) {
@@ -46,7 +47,8 @@ t.step(function() {
onload = t.step_func_done(function() { onload = t.step_func_done(function() {
let images = document.querySelectorAll("img"); let images = document.querySelectorAll("img");
assert_ratio(images[2], 1.266); // 1.266 is the original aspect ratio of blue.png assert_ratio(images[3], 1.266); // 1.266 is the original aspect ratio of blue.png
assert_equals(getComputedStyle(images[2]).height, "0px"); // aspect-ratio doesn't override intrinsic size of images that don't have any src.
assert_ratio(images[1], 2.0); // 2.0 is the original aspect ratio of green.png assert_ratio(images[1], 2.0); // 2.0 is the original aspect ratio of green.png
assert_ratio(images[0], 2.0); // Loaded image's aspect ratio, at least by default, overrides width / height ratio. assert_ratio(images[0], 2.0); // Loaded image's aspect ratio, at least by default, overrides width / height ratio.
}); });