Changeset 276521 in webkit


Ignore:
Timestamp:
Apr 23, 2021 3:00:12 PM (15 months ago)
Author:
cathiechen
Message:

Not computing image aspect ratios from width and height attributes for lazy loaded images
https://bugs.webkit.org/show_bug.cgi?id=224197

Reviewed by Darin Adler.

LayoutTests/imported/w3c:

The test cases for error images and images without src in img-aspect-ratio.html are passed. This patch
doesn't change the behavior of the original aspect ratio case, so it's failed like before.

  • web-platform-tests/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/img-aspect-ratio-expected.txt:

Source/WebCore:

This patch supports error images and lazy loaded images (without src attribute) to compute
implicit aspect ratios from width and height attributes. Refactor the code a bit. Added
intrinsicAspectRatioFromWidthHeight() to compute aspect ratio from width and height attributes when
the object is allowed to which is decided by canMapWidthHeightToAspectRatio().
Remove !downcast<RenderImage>(*this).cachedImage() constraint, so that images without src attributes
is allowed. As to error images, compute the aspect ratio when the image shouldDisplayBrokenImageIcon().

  • rendering/RenderImage.cpp:

(WebCore::RenderImage::canMapWidthHeightToAspectRatio const): To indicate that the object is allowed
to compute aspect ratio from width and height attributes.
(WebCore::RenderImage::computeIntrinsicRatioInformation const): When shouldDisplayBrokenImageIcon(),
try to compute the aspect ratio from attributes width and height.

  • rendering/RenderImage.h:
  • rendering/RenderReplaced.cpp:

(WebCore::RenderReplaced::intrinsicAspectRatioFromWidthHeight const): Compute the aspect ratio from attributes width and height.
(WebCore::RenderReplaced::computeIntrinsicRatioInformation const):

  • rendering/RenderReplaced.h:

(WebCore::RenderReplaced::canMapWidthHeightToAspectRatio const): Ditto.

Location:
trunk
Files:
7 edited

Legend:

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

    r276498 r276521  
     12021-04-23  Cathie Chen  <cathiechen@igalia.com>
     2
     3        Not computing image aspect ratios from width and height attributes for lazy loaded images
     4        https://bugs.webkit.org/show_bug.cgi?id=224197
     5
     6        Reviewed by Darin Adler.
     7
     8        The test cases for error images and images without src in img-aspect-ratio.html are passed. This patch
     9        doesn't change the behavior of the original aspect ratio case, so it's failed like before.
     10
     11        * web-platform-tests/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/img-aspect-ratio-expected.txt:
     12
    1132021-04-23  Cathie Chen  <cathiechen@igalia.com>
    214
  • trunk/LayoutTests/imported/w3c/web-platform-tests/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/img-aspect-ratio-expected.txt

    r276498 r276521  
    3737PASS Loaded images test: <img> without width height attributes
    3838PASS Loaded images test: <img> with width and height attributes, but conflicting to the original aspect ratio
    39 FAIL Loaded images test: <img> with width, height and empty src attributes assert_approx_equals: aspect-ratio should override intrinsic size of images that don't have any src. expected 0.8 +/- 0.001 but got Infinity
    40 FAIL Loaded images test: Error image with width and height attributes assert_approx_equals: aspect-ratio should affect the size of error images. expected 0.8 +/- 0.001 but got 1
     39PASS Loaded images test: <img> with width, height and empty src attributes
     40PASS Loaded images test: Error image with width and height attributes
    4141PASS Loaded images test: Error image with width, height and alt attributes
    4242FAIL Loaded images test: <img> with width and height attributes, but not equal to the original aspect ratio assert_approx_equals: The original aspect ratio of blue.png expected 1.2547169811320755 +/- 0.001 but got 1.25
  • trunk/Source/WebCore/ChangeLog

    r276513 r276521  
     12021-04-23  Cathie Chen  <cathiechen@igalia.com>
     2
     3        Not computing image aspect ratios from width and height attributes for lazy loaded images
     4        https://bugs.webkit.org/show_bug.cgi?id=224197
     5
     6        Reviewed by Darin Adler.
     7
     8        This patch supports error images and lazy loaded images (without src attribute) to compute
     9        implicit aspect ratios from width and height attributes. Refactor the code a bit. Added
     10        intrinsicAspectRatioFromWidthHeight() to compute aspect ratio from width and height attributes when
     11        the object is allowed to which is decided by canMapWidthHeightToAspectRatio().
     12        Remove `!downcast<RenderImage>(*this).cachedImage()` constraint, so that images without src attributes
     13        is allowed. As to error images, compute the aspect ratio when the image shouldDisplayBrokenImageIcon().
     14
     15        * rendering/RenderImage.cpp:
     16        (WebCore::RenderImage::canMapWidthHeightToAspectRatio const): To indicate that the object is allowed
     17        to compute aspect ratio from width and height attributes.
     18        (WebCore::RenderImage::computeIntrinsicRatioInformation const): When shouldDisplayBrokenImageIcon(),
     19        try to compute the aspect ratio from attributes width and height.
     20        * rendering/RenderImage.h:
     21        * rendering/RenderReplaced.cpp:
     22        (WebCore::RenderReplaced::intrinsicAspectRatioFromWidthHeight const): Compute the aspect ratio from attributes width and height.
     23        (WebCore::RenderReplaced::computeIntrinsicRatioInformation const):
     24        * rendering/RenderReplaced.h:
     25        (WebCore::RenderReplaced::canMapWidthHeightToAspectRatio const): Ditto.
     26
    1272021-04-23  Michael Catanzaro  <mcatanzaro@igalia.com>
    228
  • trunk/Source/WebCore/rendering/RenderImage.cpp

    r276388 r276521  
    843843}
    844844
     845bool RenderImage::canMapWidthHeightToAspectRatio() const
     846{
     847    // The aspectRatioOfImgFromWidthAndHeight only applies to <img>.
     848    return is<HTMLImageElement>(element()) && !isShowingAltText();
     849}
     850
    845851void RenderImage::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio) const
    846852{
     
    859865    // Don't compute an intrinsic ratio to preserve historical WebKit behavior if we're painting alt text and/or a broken image.
    860866    if (shouldDisplayBrokenImageIcon()) {
     867        if (!style().hasAspectRatio()) {
     868            intrinsicRatio = intrinsicAspectRatioFromWidthHeight().valueOr(1);
     869            return;
     870        }
    861871        intrinsicRatio = 1;
    862872        return;
  • trunk/Source/WebCore/rendering/RenderImage.h

    r276331 r276521  
    104104    }
    105105
     106    bool canMapWidthHeightToAspectRatio() const override;
     107
    106108private:
    107109    const char* renderName() const override { return "RenderImage"; }
  • trunk/Source/WebCore/rendering/RenderReplaced.cpp

    r276228 r276521  
    475475}
    476476
     477Optional<double> RenderReplaced::intrinsicAspectRatioFromWidthHeight() const
     478{
     479    if (!settings().aspectRatioOfImgFromWidthAndHeightEnabled())
     480        return Optional<double>();
     481
     482    if (!canMapWidthHeightToAspectRatio())
     483        return Optional<double>();
     484
     485    ASSERT(element());
     486    double attributeWidth = parseValidHTMLFloatingPointNumber(element()->getAttribute(HTMLNames::widthAttr)).valueOr(0);
     487    double attributeHeight = parseValidHTMLFloatingPointNumber(element()->getAttribute(HTMLNames::heightAttr)).valueOr(0);
     488    if (attributeWidth > 0 && attributeHeight > 0)
     489        return attributeWidth / attributeHeight;
     490
     491    return Optional<double>();
     492}
     493
    477494void RenderReplaced::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio) const
    478495{
     
    491508
    492509    if (intrinsicSize.isEmpty()) {
    493         if (!settings().aspectRatioOfImgFromWidthAndHeightEnabled())
    494             return;
    495 
    496         auto* node = element();
    497         // The aspectRatioOfImgFromWidthAndHeight only applies to <img>.
    498         if (!node || !is<HTMLImageElement>(*node) || !node->hasAttribute(HTMLNames::widthAttr) || !node->hasAttribute(HTMLNames::heightAttr))
    499             return;
    500 
    501         // We shouldn't override the aspect-ratio when the <img> element has an empty src attribute.
    502         if (!is<RenderImage>(*this) || !downcast<RenderImage>(*this).cachedImage())
    503             return;
    504 
    505         double attributeWidth = parseValidHTMLFloatingPointNumber(node->getAttribute(HTMLNames::widthAttr)).valueOr(0);
    506         double attributeHeight = parseValidHTMLFloatingPointNumber(node->getAttribute(HTMLNames::heightAttr)).valueOr(0);
    507         if (attributeWidth > 0 && attributeHeight > 0)
    508             intrinsicRatio = attributeWidth / attributeHeight;
     510        intrinsicRatio = intrinsicAspectRatioFromWidthHeight().valueOr(0);
    509511        return;
    510512    }
  • trunk/Source/WebCore/rendering/RenderReplaced.h

    r272711 r276521  
    5959    void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const final;
    6060
     61    // This function determines if the object is allowed to compute aspect ratio from attributes width and height.
     62    virtual bool canMapWidthHeightToAspectRatio() const { return false; }
     63    Optional<double> intrinsicAspectRatioFromWidthHeight() const;
     64
    6165    virtual LayoutUnit minimumReplacedHeight() const { return 0_lu; }
    6266
Note: See TracChangeset for help on using the changeset viewer.