diff --git a/dom/html/HTMLImageElement.cpp b/dom/html/HTMLImageElement.cpp index 2a4874b19524..f65c92325a4a 100644 --- a/dom/html/HTMLImageElement.cpp +++ b/dom/html/HTMLImageElement.cpp @@ -732,7 +732,7 @@ nsIntSize HTMLImageElement::NaturalSize() { if (mResponsiveSelector) { float density = mResponsiveSelector->GetSelectedImageDensity(); MOZ_ASSERT(density >= 0.0); - resolution = {density, density}; + resolution.ScaleBy(density); } resolution.ApplyTo(size.width, size.height); diff --git a/image/Resolution.h b/image/Resolution.h index d62022f67272..b305b867b2a8 100644 --- a/image/Resolution.h +++ b/image/Resolution.h @@ -30,6 +30,13 @@ struct Resolution { float mX = 1.0f; float mY = 1.0f; + void ScaleBy(float aScale) { + if (MOZ_LIKELY(aScale != 0.0f)) { + mX *= aScale; + mY *= aScale; + } + } + void ApplyXTo(int32_t& aWidth) const { if (mX != 1.0f) { aWidth = std::round(float(aWidth) / mX); diff --git a/layout/generic/nsImageFrame.cpp b/layout/generic/nsImageFrame.cpp index 3f9609973c28..fc0f7db669b5 100644 --- a/layout/generic/nsImageFrame.cpp +++ b/layout/generic/nsImageFrame.cpp @@ -479,15 +479,13 @@ static void ScaleIntrinsicSizeForDensity(IntrinsicSize& aSize, static void ScaleIntrinsicSizeForDensity(imgIContainer* aImage, nsIContent& aContent, IntrinsicSize& aSize) { + ImageResolution resolution = aImage->GetResolution(); if (auto* image = HTMLImageElement::FromNode(aContent)) { if (auto* selector = image->GetResponsiveImageSelector()) { - float density = selector->GetSelectedImageDensity(); - MOZ_ASSERT(density >= 0.0); - ScaleIntrinsicSizeForDensity(aSize, ImageResolution{density, density}); - return; + resolution.ScaleBy(selector->GetSelectedImageDensity()); } } - ScaleIntrinsicSizeForDensity(aSize, aImage->GetResolution()); + ScaleIntrinsicSizeForDensity(aSize, resolution); } static IntrinsicSize ComputeIntrinsicSize(imgIContainer* aImage, diff --git a/layout/style/nsStyleStruct.cpp b/layout/style/nsStyleStruct.cpp index 907e2d191b21..8ac50e6b30c6 100644 --- a/layout/style/nsStyleStruct.cpp +++ b/layout/style/nsStyleStruct.cpp @@ -1631,20 +1631,20 @@ void StyleImage::ResolveImage(Document& aDoc, const StyleImage* aOld) { template <> ImageResolution StyleImage::GetResolution() const { - if (IsImageSet()) { - auto& set = AsImageSet(); - float r = set->items.AsSpan()[set->selected_index].resolution._0; - if (MOZ_LIKELY(r != 0.0f)) { - return ImageResolution(r, r); - } - } else if (imgRequestProxy* request = GetImageRequest()) { + ImageResolution resolution; + if (imgRequestProxy* request = GetImageRequest()) { RefPtr image; request->GetImage(getter_AddRefs(image)); if (image) { - return image->GetResolution(); + resolution = image->GetResolution(); } } - return {}; + if (IsImageSet()) { + auto& set = AsImageSet(); + float r = set->items.AsSpan()[set->selected_index].resolution._0; + resolution.ScaleBy(r); + } + return resolution; } template <> diff --git a/modules/libpref/init/StaticPrefList.yaml b/modules/libpref/init/StaticPrefList.yaml index 1595e592911b..979331ccd065 100644 --- a/modules/libpref/init/StaticPrefList.yaml +++ b/modules/libpref/init/StaticPrefList.yaml @@ -5308,14 +5308,9 @@ mirror: always # Whether we use EXIF metadata for image density. -# -# NOTE: Before shipping this, make sure that the issue described in the -# following comment is addressed: -# -# https://github.com/whatwg/html/pull/5574#issuecomment-826335244 - name: image.exif-density-correction.enabled type: RelaxedAtomicBool - value: @IS_NIGHTLY_BUILD@ + value: true mirror: always # The threshold for inferring that changes to an element's |src| diff --git a/testing/web-platform/tests/density-size-correction/image-set-001-ref.html b/testing/web-platform/tests/density-size-correction/image-set-001-ref.html new file mode 100644 index 000000000000..69c3dc173196 --- /dev/null +++ b/testing/web-platform/tests/density-size-correction/image-set-001-ref.html @@ -0,0 +1,11 @@ + + +
diff --git a/testing/web-platform/tests/density-size-correction/image-set-001.html b/testing/web-platform/tests/density-size-correction/image-set-001.html new file mode 100644 index 000000000000..cb155806512b --- /dev/null +++ b/testing/web-platform/tests/density-size-correction/image-set-001.html @@ -0,0 +1,15 @@ + + + + + +
diff --git a/testing/web-platform/tests/density-size-correction/image-set-002-ref.html b/testing/web-platform/tests/density-size-correction/image-set-002-ref.html new file mode 100644 index 000000000000..3fc55e8aac45 --- /dev/null +++ b/testing/web-platform/tests/density-size-correction/image-set-002-ref.html @@ -0,0 +1,11 @@ + + +
diff --git a/testing/web-platform/tests/density-size-correction/image-set-002.html b/testing/web-platform/tests/density-size-correction/image-set-002.html new file mode 100644 index 000000000000..bbb06fa34291 --- /dev/null +++ b/testing/web-platform/tests/density-size-correction/image-set-002.html @@ -0,0 +1,15 @@ + + + + + +
diff --git a/testing/web-platform/tests/density-size-correction/srcset-ref.html b/testing/web-platform/tests/density-size-correction/srcset-ref.html new file mode 100644 index 000000000000..630bf5ce146d --- /dev/null +++ b/testing/web-platform/tests/density-size-correction/srcset-ref.html @@ -0,0 +1,3 @@ + + + diff --git a/testing/web-platform/tests/density-size-correction/srcset.html b/testing/web-platform/tests/density-size-correction/srcset.html new file mode 100644 index 000000000000..4f3133ea4f27 --- /dev/null +++ b/testing/web-platform/tests/density-size-correction/srcset.html @@ -0,0 +1,6 @@ + + + + + +