Changeset 254229 in webkit


Ignore:
Timestamp:
Jan 8, 2020 3:38:47 PM (4 years ago)
Author:
commit-queue@webkit.org
Message:

<img>.naturalWidth should return the density-corrected intrinsic width
https://bugs.webkit.org/show_bug.cgi?id=150443

Patch by Noam Rosenthal <Noam Rosenthal> on 2020-01-08
Reviewed by Simon Fraser.

LayoutTests/imported/w3c:

Updated expected results.

  • web-platform-tests/html/semantics/embedded-content/the-img-element/current-pixel-density/basic-expected.txt:

All tests now pass.

  • web-platform-tests/html/semantics/embedded-content/the-img-element/intrinsicsize/intrinsicsize-with-responsive-images.tentative-expected.txt:

Still fails but failure values are different.

Source/WebCore:

Take image's density into account when requesting naturalWidth/naturalHeight, not in SVG.

This now complies with the standard (https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-naturalwidth)
It also matches the behavior on Chrome and on Firefox.

Test: imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/current-pixel-density/basic.html

Updaded expected results

  • html/HTMLImageElement.cpp:

(WebCore::HTMLImageElement::effectiveImageDevicePixelRatio const):
(WebCore::HTMLImageElement::naturalWidth const):
(WebCore::HTMLImageElement::naturalHeight const):

  • html/HTMLImageElement.h:

Use effective image devicePixelRatio for naturalWidth/height calculation

  • loader/cache/CachedImage.cpp:

(WebCore::CachedImage::unclampedImageSizeForRenderer const):
(WebCore::CachedImage::imageSizeForRenderer const):

  • loader/cache/CachedImage.h:

Don't clamp to 1 when calculating naturalWidth/naturalHeight, as this has
nothing to do with zoomed images. Zoomed images behavior remains the same.

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/imported/w3c/ChangeLog

    r254193 r254229  
     12020-01-08  Noam Rosenthal  <noam@webkit.org>
     2
     3        <img>.naturalWidth should return the density-corrected intrinsic width
     4        https://bugs.webkit.org/show_bug.cgi?id=150443
     5
     6        Reviewed by Simon Fraser.
     7
     8        Updated expected results.
     9
     10        * web-platform-tests/html/semantics/embedded-content/the-img-element/current-pixel-density/basic-expected.txt:
     11                All tests now pass.
     12
     13        * web-platform-tests/html/semantics/embedded-content/the-img-element/intrinsicsize/intrinsicsize-with-responsive-images.tentative-expected.txt:
     14                Still fails but failure values are different.
     15
    1162020-01-08  Carlos Alberto Lopez Perez  <clopez@igalia.com>
    217
  • trunk/LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/current-pixel-density/basic-expected.txt

    r206240 r254229  
    22PASS <img src="/images/green-256x256.png" data-expect="256">
    33PASS <img srcset="/images/green-256x256.png 1x" data-expect="256">
    4 FAIL <img srcset="/images/green-256x256.png 1.6x" data-expect="160"> assert_equals: naturalWidth expected 160 but got 256
    5 FAIL <img srcset="/images/green-256x256.png 2x" data-expect="128"> assert_equals: naturalWidth expected 128 but got 256
    6 FAIL <img srcset="/images/green-256x256.png 10000x" data-expect="0"> assert_equals: naturalWidth expected 0 but got 256
     4PASS <img srcset="/images/green-256x256.png 1.6x" data-expect="160">
     5PASS <img srcset="/images/green-256x256.png 2x" data-expect="128">
     6PASS <img srcset="/images/green-256x256.png 10000x" data-expect="0">
    77PASS <img srcset="/images/green-256x256.png 9e99999999999999999999999x" data-expect="0">
    88PASS <img srcset="/images/green-256x256.png 256w" sizes="256px" data-expect="256">
    9 FAIL <img srcset="/images/green-256x256.png 512w" sizes="256px" data-expect="128"> assert_equals: naturalWidth expected 128 but got 256
    10 FAIL <img srcset="/images/green-256x256.png 256w" sizes="512px" data-expect="512"> assert_equals: naturalWidth expected 512 but got 256
    11 FAIL <img srcset="/images/green-256x256.png 256w" sizes="1px" data-expect="1"> assert_equals: naturalWidth expected 1 but got 256
    12 FAIL <img srcset="/images/green-256x256.png 256w" sizes="0px" data-expect="0"> assert_equals: naturalWidth expected 0 but got 256
     9PASS <img srcset="/images/green-256x256.png 512w" sizes="256px" data-expect="128">
     10PASS <img srcset="/images/green-256x256.png 256w" sizes="512px" data-expect="512">
     11PASS <img srcset="/images/green-256x256.png 256w" sizes="1px" data-expect="1">
     12PASS <img srcset="/images/green-256x256.png 256w" sizes="0px" data-expect="0">
    1313PASS <img srcset="data:image/svg+xml,<svg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='-1%20-1%202%202'%20width='20'%20height='20'><circle%20r='1'/></svg> 2x" data-expect="10">
    1414PASS <img srcset="data:image/svg+xml,<svg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='-1%20-1%202%202'%20width='20'><circle%20r='1'/></svg> 2x" data-expect="10">
  • trunk/LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/intrinsicsize/intrinsicsize-with-responsive-images.tentative-expected.txt

    r242595 r254229  
    66FAIL Test image /images/green.png with no specified sizes, width, or height assert_equals: width expected 400 but got 33
    77FAIL Test image /images/green.png with width = 800, no specified sizes, or height assert_equals: height expected 1000 but got 400
    8 FAIL Test image /images/green.png with width = 800, height = 800, and no specified sizes assert_equals: naturalWidth expected 400 but got 100
     8FAIL Test image /images/green.png with width = 800, height = 800, and no specified sizes assert_equals: naturalWidth expected 400 but got 33
    99FAIL Test image (32 x 32) with sizes = 100 and srcset descriptor = 32w assert_equals: height expected 125 but got 100
    1010
  • trunk/Source/WebCore/ChangeLog

    r254228 r254229  
     12020-01-08  Noam Rosenthal  <noam@webkit.org>
     2
     3        <img>.naturalWidth should return the density-corrected intrinsic width
     4        https://bugs.webkit.org/show_bug.cgi?id=150443
     5
     6        Reviewed by Simon Fraser.
     7
     8        Take image's density into account when requesting naturalWidth/naturalHeight, not in SVG.
     9
     10        This now complies with the standard (https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-naturalwidth)
     11        It also matches the behavior on Chrome and on Firefox.
     12
     13        Test: imported/w3c/web-platform-tests/html/semantics/embedded-content/the-img-element/current-pixel-density/basic.html
     14                Updaded expected results
     15
     16        * html/HTMLImageElement.cpp:
     17        (WebCore::HTMLImageElement::effectiveImageDevicePixelRatio const):
     18        (WebCore::HTMLImageElement::naturalWidth const):
     19        (WebCore::HTMLImageElement::naturalHeight const):
     20        * html/HTMLImageElement.h:
     21                Use effective image devicePixelRatio for naturalWidth/height calculation
     22
     23        * loader/cache/CachedImage.cpp:
     24        (WebCore::CachedImage::unclampedImageSizeForRenderer const):
     25        (WebCore::CachedImage::imageSizeForRenderer const):
     26        * loader/cache/CachedImage.h:
     27                Don't clamp to 1 when calculating naturalWidth/naturalHeight, as this has
     28                nothing to do with zoomed images. Zoomed images behavior remains the same.
     29
    1302020-01-08  Devin Rousso  <drousso@apple.com>
    231
  • trunk/Source/WebCore/html/HTMLImageElement.cpp

    • Property svn:executable set to *
    r253975 r254229  
    532532}
    533533
     534float HTMLImageElement::effectiveImageDevicePixelRatio() const
     535{
     536    if (!m_imageLoader->image())
     537        return 1.0f;
     538
     539    auto* image = m_imageLoader->image()->image();
     540
     541    if (image && image->isSVGImage())
     542        return 1.0f;
     543
     544    return m_imageDevicePixelRatio;
     545}
     546
    534547int HTMLImageElement::naturalWidth() const
    535548{
     
    537550        return 0;
    538551
    539     return m_imageLoader->image()->imageSizeForRenderer(renderer(), 1.0f).width();
     552    return m_imageLoader->image()->unclampedImageSizeForRenderer(renderer(), effectiveImageDevicePixelRatio()).width();
    540553}
    541554
     
    545558        return 0;
    546559
    547     return m_imageLoader->image()->imageSizeForRenderer(renderer(), 1.0f).height();
     560    return m_imageLoader->image()->unclampedImageSizeForRenderer(renderer(), effectiveImageDevicePixelRatio()).height();
    548561}
    549562
  • trunk/Source/WebCore/html/HTMLImageElement.h

    r252828 r254229  
    178178
    179179    void copyNonAttributePropertiesFromElement(const Element&) final;
     180
     181    float effectiveImageDevicePixelRatio() const;
    180182
    181183#if ENABLE(SERVICE_CONTROLS)
  • trunk/Source/WebCore/loader/cache/CachedImage.cpp

    r249364 r254229  
    287287}
    288288
    289 LayoutSize CachedImage::imageSizeForRenderer(const RenderElement* renderer, float multiplier, SizeType sizeType) const
     289
     290LayoutSize CachedImage::unclampedImageSizeForRenderer(const RenderElement* renderer, float multiplier, SizeType sizeType) const
    290291{
    291292    LayoutSize imageSize = LayoutSize(imageSizeForRenderer(renderer, sizeType));
     
    293294        return imageSize;
    294295
    295     // Don't let images that have a width/height >= 1 shrink below 1 when zoomed.
    296296    float widthScale = m_image->hasRelativeWidth() ? 1.0f : multiplier;
    297297    float heightScale = m_image->hasRelativeHeight() ? 1.0f : multiplier;
     298    imageSize.scale(widthScale, heightScale);
     299    return imageSize;   
     300}
     301
     302LayoutSize CachedImage::imageSizeForRenderer(const RenderElement* renderer, float multiplier, SizeType sizeType) const
     303{
     304    auto imageSize = unclampedImageSizeForRenderer(renderer, multiplier, sizeType);
     305    if (imageSize.isEmpty() || multiplier == 1.0f)
     306        return imageSize;
     307
     308    // Don't let images that have a width/height >= 1 shrink below 1 when zoomed.
    298309    LayoutSize minimumSize(imageSize.width() > 0 ? 1 : 0, imageSize.height() > 0 ? 1 : 0);
    299     imageSize.scale(widthScale, heightScale);
    300310    imageSize.clampToMinimumSize(minimumSize);
     311
    301312    ASSERT(multiplier != 1.0f || (imageSize.width().fraction() == 0.0f && imageSize.height().fraction() == 0.0f));
    302313    return imageSize;
  • trunk/Source/WebCore/loader/cache/CachedImage.h

    r251708 r254229  
    7979    // This method takes a zoom multiplier that can be used to increase the natural size of the image by the zoom.
    8080    LayoutSize imageSizeForRenderer(const RenderElement*, float multiplier, SizeType = UsedSize) const; // returns the size of the complete image.
     81    LayoutSize unclampedImageSizeForRenderer(const RenderElement* renderer, float multiplier, SizeType = UsedSize) const;
    8182    void computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio);
    8283
Note: See TracChangeset for help on using the changeset viewer.