Title: [278689] trunk
Revision
278689
Author
[email protected]
Date
2021-06-09 20:17:05 -0700 (Wed, 09 Jun 2021)

Log 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:

Modified Paths

Diff

Modified: trunk/LayoutTests/imported/w3c/ChangeLog (278688 => 278689)


--- trunk/LayoutTests/imported/w3c/ChangeLog	2021-06-10 03:10:59 UTC (rev 278688)
+++ trunk/LayoutTests/imported/w3c/ChangeLog	2021-06-10 03:17:05 UTC (rev 278689)
@@ -1,3 +1,12 @@
+2021-06-09  Cathie Chen  <[email protected]>
+
+        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.
+
+        * web-platform-tests/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/img-aspect-ratio-expected.txt:
+
 2021-06-09  Alex Christensen  <[email protected]>
 
         Performance API: Implement performance.timeOrigin

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/img-aspect-ratio-expected.txt (278688 => 278689)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/img-aspect-ratio-expected.txt	2021-06-10 03:10:59 UTC (rev 278688)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/img-aspect-ratio-expected.txt	2021-06-10 03:17:05 UTC (rev 278689)
@@ -4,7 +4,7 @@
 PASS Create, append and test immediately: <img> with attributes width=250, height=100
 PASS Create, append and test immediately: <img> with attributes width=0.8, height=0.2
 PASS Create, append and test immediately: <img> with attributes width=50% height=25%
-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
+PASS Create, append and test immediately: <img> with invalid trailing attributes width=50pp height=25xx
 PASS Computed style test: img with {"width":"10","height":"20"}
 PASS Computed style test: input with {"type":"image","width":"10","height":"20"}
 PASS Computed style test: input with {"type":"submit","width":"10","height":"20"}

Modified: trunk/Source/WebCore/ChangeLog (278688 => 278689)


--- trunk/Source/WebCore/ChangeLog	2021-06-10 03:10:59 UTC (rev 278688)
+++ trunk/Source/WebCore/ChangeLog	2021-06-10 03:17:05 UTC (rev 278689)
@@ -1,3 +1,25 @@
+2021-06-09  Cathie Chen  <[email protected]>
+
+        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.
+
+        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:
+
 2021-06-09  Andres Gonzalez  <[email protected]>
 
         iOS - VoiceOver reads the old heading text when updated with heading.firstChild.data.

Modified: trunk/Source/WebCore/html/HTMLElement.cpp (278688 => 278689)


--- trunk/Source/WebCore/html/HTMLElement.cpp	2021-06-10 03:10:59 UTC (rev 278688)
+++ trunk/Source/WebCore/html/HTMLElement.cpp	2021-06-10 03:17:05 UTC (rev 278689)
@@ -630,16 +630,16 @@
     if (!document().settings().aspectRatioOfImgFromWidthAndHeightEnabled())
         return;
 
-    double width = parseValidHTMLFloatingPointNumber(attributeWithoutSynchronization(widthAttr)).value_or(-1);
-    if (width < 0)
+    auto dimensionWidth = parseHTMLDimension(attributeWithoutSynchronization(widthAttr));
+    if (!dimensionWidth || dimensionWidth->type != HTMLDimension::Type::Pixel)
         return;
-    double height = parseValidHTMLFloatingPointNumber(attributeWithoutSynchronization(heightAttr)).value_or(-1);
-    if (height < 0)
+    auto dimensionHeight = parseHTMLDimension(attributeWithoutSynchronization(heightAttr));
+    if (!dimensionHeight || dimensionHeight->type != HTMLDimension::Type::Pixel)
         return;
 
     auto ratioList = CSSValueList::createSlashSeparated();
-    ratioList->append(CSSValuePool::singleton().createValue(width, CSSUnitType::CSS_NUMBER));
-    ratioList->append(CSSValuePool::singleton().createValue(height, CSSUnitType::CSS_NUMBER));
+    ratioList->append(CSSValuePool::singleton().createValue(dimensionWidth->number, CSSUnitType::CSS_NUMBER));
+    ratioList->append(CSSValuePool::singleton().createValue(dimensionHeight->number, CSSUnitType::CSS_NUMBER));
     auto list = CSSValueList::createSpaceSeparated();
     list->append(CSSValuePool::singleton().createIdentifierValue(CSSValueAuto));
     list->append(ratioList);

Modified: trunk/Source/WebCore/html/parser/HTMLParserIdioms.cpp (278688 => 278689)


--- trunk/Source/WebCore/html/parser/HTMLParserIdioms.cpp	2021-06-10 03:10:59 UTC (rev 278688)
+++ trunk/Source/WebCore/html/parser/HTMLParserIdioms.cpp	2021-06-10 03:17:05 UTC (rev 278689)
@@ -26,6 +26,7 @@
 #include "HTMLParserIdioms.h"
 
 #include "Decimal.h"
+#include "ParsingUtilities.h"
 #include "QualifiedName.h"
 #include <limits>
 #include <wtf/MathExtras.h>
@@ -475,4 +476,59 @@
     return usemap.substring(numberSignIndex + 1).toAtomString();
 }
 
+struct HTMLDimensionParsingResult {
+    double number;
+    unsigned parsedLength;
+};
+
+template <typename CharacterType>
+static std::optional<HTMLDimensionParsingResult> parseHTMLDimensionNumber(const CharacterType* position, unsigned length)
+{
+    if (!length || !position)
+        return std::nullopt;
+
+    const auto* begin = position;
+    const auto* end = position + length;
+    skipWhile<isHTMLSpace>(position, end);
+    if (position == end)
+        return std::nullopt;
+
+    auto* start = position;
+    skipWhile<isASCIIDigit>(position, end);
+    if (start == position)
+        return std::nullopt;
+
+    if (skipExactly(position, end, '.'))
+        skipWhile<isASCIIDigit>(position, end);
+
+    size_t parsedLength = 0;
+    double number = parseDouble(start, position - start, parsedLength);
+    if (!(parsedLength && std::isfinite(number)))
+        return std::nullopt;
+
+    HTMLDimensionParsingResult result;
+    result.number = number;
+    result.parsedLength = position - begin;
+    return result;
 }
+
+std::optional<HTMLDimension> parseHTMLDimension(StringView dimensionString)
+{
+    std::optional<HTMLDimensionParsingResult> result;
+    auto length = dimensionString.length();
+    if (dimensionString.is8Bit())
+        result = parseHTMLDimensionNumber(dimensionString.characters8(), length);
+    else
+        result = parseHTMLDimensionNumber(dimensionString.characters16(), length);
+    if (!result)
+        return std::nullopt;
+
+    HTMLDimension dimension;
+    dimension.number = result->number;
+    dimension.type = HTMLDimension::Type::Pixel;
+    if (result->parsedLength < dimensionString.length() && dimensionString[result->parsedLength] == '%')
+        dimension.type = HTMLDimension::Type::Percentage;
+    return dimension;
+}
+
+}

Modified: trunk/Source/WebCore/html/parser/HTMLParserIdioms.h (278688 => 278689)


--- trunk/Source/WebCore/html/parser/HTMLParserIdioms.h	2021-06-10 03:10:59 UTC (rev 278688)
+++ trunk/Source/WebCore/html/parser/HTMLParserIdioms.h	2021-06-10 03:17:05 UTC (rev 278689)
@@ -86,6 +86,14 @@
 
 AtomString parseHTMLHashNameReference(StringView);
 
+// https://html.spec.whatwg.org/#rules-for-parsing-dimension-values
+struct HTMLDimension {
+    enum class Type : bool { Percentage, Pixel };
+    double number;
+    Type type;
+};
+std::optional<HTMLDimension> parseHTMLDimension(StringView);
+
 // Inline implementations of some of the functions declared above.
 
 template<typename CharacterType> inline bool isHTMLSpace(CharacterType character)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to