⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 278689 in webkit


Ignore:
Timestamp:
Jun 9, 2021, 8:17:05 PM (5 years ago)
Author:
cathiechen
Message:

Aspect ratio from width and height attribute is not compatible to string with invalid ends
https://bugs.webkit.org/show_bug.cgi?id=226469

Reviewed by Antti Koivisto.

LayoutTests/imported/w3c:

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

Source/WebCore:

The patch follows the steps defined in [1] to parse the dimension values from the attribute values.
It adds HTMLDimension to present the dimension value which has two types, Percentage and Pixel.
And parseHTMLDimension() follows the specification steps to check validation and parse the dimension
value. Currently, it is only used by parsing aspect-ratio from width and height attributes. It will
apply to other attributes length parse in the future patch.

[1] https://html.spec.whatwg.org/#rules-for-parsing-dimension-values

  • html/HTMLElement.cpp:

(WebCore::HTMLElement::applyAspectRatioFromWidthAndHeightAttributesToStyle): Call parseHTMLDimension to get the length values.

  • html/parser/HTMLParserIdioms.cpp:

(WebCore::parseHTMLDimensionNumber):
(WebCore::parseHTMLDimension):

  • html/parser/HTMLParserIdioms.h:
Location:
trunk
Files:
6 edited

Legend:

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

    r278665 r278689  
     12021-06-09  Cathie Chen  <cathiechen@igalia.com>
     2
     3        Aspect ratio from width and height attribute is not compatible to string with invalid ends
     4        https://bugs.webkit.org/show_bug.cgi?id=226469
     5
     6        Reviewed by Antti Koivisto.
     7
     8        * web-platform-tests/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/img-aspect-ratio-expected.txt:
     9
    1102021-06-09  Alex Christensen  <achristensen@webkit.org>
    211
  • trunk/LayoutTests/imported/w3c/web-platform-tests/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/img-aspect-ratio-expected.txt

    r278299 r278689  
    55PASS Create, append and test immediately: <img> with attributes width=0.8, height=0.2
    66PASS Create, append and test immediately: <img> with attributes width=50% height=25%
    7 FAIL Create, append and test immediately: <img> with invalid trailing attributes width=50pp height=25xx assert_approx_equals: expected 2 +/- 0.001 but got Infinity
     7PASS Create, append and test immediately: <img> with invalid trailing attributes width=50pp height=25xx
    88PASS Computed style test: img with {"width":"10","height":"20"}
    99PASS Computed style test: input with {"type":"image","width":"10","height":"20"}
  • trunk/Source/WebCore/ChangeLog

    r278683 r278689  
     12021-06-09  Cathie Chen  <cathiechen@igalia.com>
     2
     3        Aspect ratio from width and height attribute is not compatible to string with invalid ends
     4        https://bugs.webkit.org/show_bug.cgi?id=226469
     5
     6        Reviewed by Antti Koivisto.
     7
     8        The patch follows the steps defined in [1] to parse the dimension values from the attribute values.
     9        It adds HTMLDimension to present the dimension value which has two types, Percentage and Pixel.
     10        And parseHTMLDimension() follows the specification steps to check validation and parse the dimension
     11        value. Currently, it is only used by parsing aspect-ratio from width and height attributes. It will
     12        apply to other attributes length parse in the future patch.
     13
     14        [1] https://html.spec.whatwg.org/#rules-for-parsing-dimension-values
     15
     16        * html/HTMLElement.cpp:
     17        (WebCore::HTMLElement::applyAspectRatioFromWidthAndHeightAttributesToStyle): Call parseHTMLDimension to get the length values.
     18        * html/parser/HTMLParserIdioms.cpp:
     19        (WebCore::parseHTMLDimensionNumber):
     20        (WebCore::parseHTMLDimension):
     21        * html/parser/HTMLParserIdioms.h:
     22
    1232021-06-09  Andres Gonzalez  <andresg_22@apple.com>
    224
  • trunk/Source/WebCore/html/HTMLElement.cpp

    r278575 r278689  
    631631        return;
    632632
    633     double width = parseValidHTMLFloatingPointNumber(attributeWithoutSynchronization(widthAttr)).value_or(-1);
    634     if (width < 0)
     633    auto dimensionWidth = parseHTMLDimension(attributeWithoutSynchronization(widthAttr));
     634    if (!dimensionWidth || dimensionWidth->type != HTMLDimension::Type::Pixel)
    635635        return;
    636     double height = parseValidHTMLFloatingPointNumber(attributeWithoutSynchronization(heightAttr)).value_or(-1);
    637     if (height < 0)
     636    auto dimensionHeight = parseHTMLDimension(attributeWithoutSynchronization(heightAttr));
     637    if (!dimensionHeight || dimensionHeight->type != HTMLDimension::Type::Pixel)
    638638        return;
    639639
    640640    auto ratioList = CSSValueList::createSlashSeparated();
    641     ratioList->append(CSSValuePool::singleton().createValue(width, CSSUnitType::CSS_NUMBER));
    642     ratioList->append(CSSValuePool::singleton().createValue(height, CSSUnitType::CSS_NUMBER));
     641    ratioList->append(CSSValuePool::singleton().createValue(dimensionWidth->number, CSSUnitType::CSS_NUMBER));
     642    ratioList->append(CSSValuePool::singleton().createValue(dimensionHeight->number, CSSUnitType::CSS_NUMBER));
    643643    auto list = CSSValueList::createSpaceSeparated();
    644644    list->append(CSSValuePool::singleton().createIdentifierValue(CSSValueAuto));
  • trunk/Source/WebCore/html/parser/HTMLParserIdioms.cpp

    r278340 r278689  
    2727
    2828#include "Decimal.h"
     29#include "ParsingUtilities.h"
    2930#include "QualifiedName.h"
    3031#include <limits>
     
    476477}
    477478
    478 }
     479struct HTMLDimensionParsingResult {
     480    double number;
     481    unsigned parsedLength;
     482};
     483
     484template <typename CharacterType>
     485static std::optional<HTMLDimensionParsingResult> parseHTMLDimensionNumber(const CharacterType* position, unsigned length)
     486{
     487    if (!length || !position)
     488        return std::nullopt;
     489
     490    const auto* begin = position;
     491    const auto* end = position + length;
     492    skipWhile<isHTMLSpace>(position, end);
     493    if (position == end)
     494        return std::nullopt;
     495
     496    auto* start = position;
     497    skipWhile<isASCIIDigit>(position, end);
     498    if (start == position)
     499        return std::nullopt;
     500
     501    if (skipExactly(position, end, '.'))
     502        skipWhile<isASCIIDigit>(position, end);
     503
     504    size_t parsedLength = 0;
     505    double number = parseDouble(start, position - start, parsedLength);
     506    if (!(parsedLength && std::isfinite(number)))
     507        return std::nullopt;
     508
     509    HTMLDimensionParsingResult result;
     510    result.number = number;
     511    result.parsedLength = position - begin;
     512    return result;
     513}
     514
     515std::optional<HTMLDimension> parseHTMLDimension(StringView dimensionString)
     516{
     517    std::optional<HTMLDimensionParsingResult> result;
     518    auto length = dimensionString.length();
     519    if (dimensionString.is8Bit())
     520        result = parseHTMLDimensionNumber(dimensionString.characters8(), length);
     521    else
     522        result = parseHTMLDimensionNumber(dimensionString.characters16(), length);
     523    if (!result)
     524        return std::nullopt;
     525
     526    HTMLDimension dimension;
     527    dimension.number = result->number;
     528    dimension.type = HTMLDimension::Type::Pixel;
     529    if (result->parsedLength < dimensionString.length() && dimensionString[result->parsedLength] == '%')
     530        dimension.type = HTMLDimension::Type::Percentage;
     531    return dimension;
     532}
     533
     534}
  • trunk/Source/WebCore/html/parser/HTMLParserIdioms.h

    r278253 r278689  
    8787AtomString parseHTMLHashNameReference(StringView);
    8888
     89// https://html.spec.whatwg.org/#rules-for-parsing-dimension-values
     90struct HTMLDimension {
     91    enum class Type : bool { Percentage, Pixel };
     92    double number;
     93    Type type;
     94};
     95std::optional<HTMLDimension> parseHTMLDimension(StringView);
     96
    8997// Inline implementations of some of the functions declared above.
    9098
Note: See TracChangeset for help on using the changeset viewer.