diff --git a/dom/html/HTMLImageElement.cpp b/dom/html/HTMLImageElement.cpp
index 421a8fc4d842..29db35d37dd9 100644
--- a/dom/html/HTMLImageElement.cpp
+++ b/dom/html/HTMLImageElement.cpp
@@ -622,9 +622,18 @@ nsIntSize HTMLImageElement::NaturalSize() {
return {};
}
- nsIntSize size;
- Unused << image->GetHeight(&size.height);
- Unused << image->GetWidth(&size.width);
+ mozilla::image::ImageIntrinsicSize intrinsicSize;
+ nsresult rv = image->GetIntrinsicSize(&intrinsicSize);
+ if (NS_FAILED(rv)) {
+ return {};
+ }
+
+ // For now, treat a missing intrinsic 'width' or 'height' as a natural size
+ // of '0' in that axis, per spec. But we'll be changing that soon for
+ // webcompat reasons per https://github.com/whatwg/html/issues/11287
+ // and https://bugzilla.mozilla.org/show_bug.cgi?id=1935269 .
+ nsIntSize size(intrinsicSize.mWidth.valueOr(0),
+ intrinsicSize.mHeight.valueOr(0));
ImageResolution resolution = image->GetResolution();
// NOTE(emilio): What we implement here matches the image-set() spec, but it's
diff --git a/image/Image.cpp b/image/Image.cpp
index 596ac54cedc0..17ea95f01698 100644
--- a/image/Image.cpp
+++ b/image/Image.cpp
@@ -78,11 +78,10 @@ ImageMemoryCounter::ImageMemoryCounter(imgRequest* aRequest, Image* aImage,
imageURL->GetSpec(mURI);
}
- int32_t width = 0;
- int32_t height = 0;
- aImage->GetWidth(&width);
- aImage->GetHeight(&height);
- mIntrinsicSize.SizeTo(width, height);
+ ImageIntrinsicSize size;
+ if (NS_SUCCEEDED(aImage->GetIntrinsicSize(&size))) {
+ mIntrinsicSize.SizeTo(size.mWidth.valueOr(0), size.mHeight.valueOr(0));
+ } // else, leave mIntrinsicSize default-initialized as IntSize(0,0).
mType = aImage->GetType();
mHasError = aImage->HasError();