diff --git a/content/base/public/nsIImageLoadingContent.idl b/content/base/public/nsIImageLoadingContent.idl index 1abfaa68db70..001b47e19e5b 100644 --- a/content/base/public/nsIImageLoadingContent.idl +++ b/content/base/public/nsIImageLoadingContent.idl @@ -37,7 +37,7 @@ interface nsIFrame; * interface to mirror this interface when changing it. */ -[scriptable, builtinclass, uuid(e3968acd-b796-4ca3-aec0-e7f0880f2219)] +[scriptable, builtinclass, uuid(256a5283-ebb5-4430-8e15-5ada92156ef7)] interface nsIImageLoadingContent : imgINotificationObserver { /** @@ -160,6 +160,13 @@ interface nsIImageLoadingContent : imgINotificationObserver */ void forceImageState(in boolean aForce, in unsigned long long aState); + /** + * The intrinsic size and width of this content. May differ from actual image + * size due to things like responsive image density. + */ + readonly attribute unsigned long naturalWidth; + readonly attribute unsigned long naturalHeight; + /** * A visible count is stored, if it is non-zero then this image is considered * visible. These methods increment, decrement, or return the visible coount. diff --git a/content/base/src/nsImageLoadingContent.cpp b/content/base/src/nsImageLoadingContent.cpp index b13f31a2e1ce..a900adc10d0f 100644 --- a/content/base/src/nsImageLoadingContent.cpp +++ b/content/base/src/nsImageLoadingContent.cpp @@ -877,6 +877,46 @@ nsImageLoadingContent::ForceImageState(bool aForce, return NS_OK; } +NS_IMETHODIMP +nsImageLoadingContent::GetNaturalWidth(uint32_t* aNaturalWidth) +{ + NS_ENSURE_ARG_POINTER(aNaturalWidth); + + nsCOMPtr image; + if (mCurrentRequest) { + mCurrentRequest->GetImage(getter_AddRefs(image)); + } + + int32_t width; + if (image && NS_SUCCEEDED(image->GetWidth(&width))) { + *aNaturalWidth = width; + } else { + *aNaturalWidth = 0; + } + + return NS_OK; +} + +NS_IMETHODIMP +nsImageLoadingContent::GetNaturalHeight(uint32_t* aNaturalHeight) +{ + NS_ENSURE_ARG_POINTER(aNaturalHeight); + + nsCOMPtr image; + if (mCurrentRequest) { + mCurrentRequest->GetImage(getter_AddRefs(image)); + } + + int32_t height; + if (image && NS_SUCCEEDED(image->GetHeight(&height))) { + *aNaturalHeight = height; + } else { + *aNaturalHeight = 0; + } + + return NS_OK; +} + EventStates nsImageLoadingContent::ImageState() const { diff --git a/content/html/content/src/HTMLImageElement.cpp b/content/html/content/src/HTMLImageElement.cpp index b4d41e1db7b0..fcaf7ceae58c 100644 --- a/content/html/content/src/HTMLImageElement.cpp +++ b/content/html/content/src/HTMLImageElement.cpp @@ -622,60 +622,42 @@ HTMLImageElement::Image(const GlobalObject& aGlobal, uint32_t HTMLImageElement::NaturalHeight() { - if (!mCurrentRequest) { + uint32_t height; + nsresult rv = nsImageLoadingContent::GetNaturalHeight(&height); + + if (NS_FAILED(rv)) { + MOZ_ASSERT(false, "GetNaturalHeight should not fail"); return 0; } - nsCOMPtr image; - mCurrentRequest->GetImage(getter_AddRefs(image)); - if (!image) { - return 0; - } - - int32_t height; - if (NS_SUCCEEDED(image->GetHeight(&height))) { - return height; - } - return 0; + return height; } NS_IMETHODIMP HTMLImageElement::GetNaturalHeight(uint32_t* aNaturalHeight) { - NS_ENSURE_ARG_POINTER(aNaturalHeight); - *aNaturalHeight = NaturalHeight(); - return NS_OK; } uint32_t HTMLImageElement::NaturalWidth() { - if (!mCurrentRequest) { + uint32_t width; + nsresult rv = nsImageLoadingContent::GetNaturalWidth(&width); + + if (NS_FAILED(rv)) { + MOZ_ASSERT(false, "GetNaturalWidth should not fail"); return 0; } - nsCOMPtr image; - mCurrentRequest->GetImage(getter_AddRefs(image)); - if (!image) { - return 0; - } - - int32_t width; - if (NS_SUCCEEDED(image->GetWidth(&width))) { - return width; - } - return 0; + return width; } NS_IMETHODIMP HTMLImageElement::GetNaturalWidth(uint32_t* aNaturalWidth) { - NS_ENSURE_ARG_POINTER(aNaturalWidth); - *aNaturalWidth = NaturalWidth(); - return NS_OK; }