Skip to content

Truly independent web browser

LibWeb: Resolve default dimensions for unrendered image inputs

Andreas Kling0b35ec9b5af4
diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
index 5cc5eeae6b5..9ae16aece86 100644
--- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
@@ -28,6 +28,7 @@
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/CSS/PropertyID.h>
#include <LibWeb/CSS/SelectorMatching.h>
+#include <LibWeb/CSS/Sizing.h>
#include <LibWeb/CSS/StyleEngineInput.h>
#include <LibWeb/CSS/StyleValues/ColorFunctionStyleValue.h>
#include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
@@ -2713,11 +2714,13 @@ WebIDL::UnsignedLong HTMLInputElement::height() const
return *height;
}
- // ...or else the natural height and height of the image, in CSS pixels, if an image is available but not being rendered
- if (auto height = intrinsic_height(); height.has_value() && *height > 0)
- return height->to_int();
+ // https://html.spec.whatwg.org/multipage/input.html#input-img-dimensions
+ // If an image is available, then set naturalDimensions to the result of applying the default sizing algorithm
+ // with that image's natural width, natural height, and natural aspect ratio, if any, using a default object size
+ // of 300 by 150.
+ if (is_image_available())
+ return CSS::run_default_sizing_algorithm({}, {}, { intrinsic_width(), intrinsic_height(), intrinsic_aspect_ratio() }, { 300, 150 }).height().to_int();
- // ...or else 0, if the image is not available or does not have intrinsic dimensions.
return 0;
}
@@ -2749,11 +2752,13 @@ WebIDL::UnsignedLong HTMLInputElement::width() const
return *width;
}
- // ...or else the natural width and height of the image, in CSS pixels, if an image is available but not being rendered
- if (auto width = intrinsic_width(); width.has_value() && *width > 0)
- return width->to_int();
+ // https://html.spec.whatwg.org/multipage/input.html#input-img-dimensions
+ // If an image is available, then set naturalDimensions to the result of applying the default sizing algorithm
+ // with that image's natural width, natural height, and natural aspect ratio, if any, using a default object size
+ // of 300 by 150.
+ if (is_image_available())
+ return CSS::run_default_sizing_algorithm({}, {}, { intrinsic_width(), intrinsic_height(), intrinsic_aspect_ratio() }, { 300, 150 }).width().to_int();
- // ...or else 0, if the image is not available or does not have intrinsic dimensions.
return 0;
}
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-input-element/image-width-height.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-input-element/image-width-height.txt
index fc8f4bc3290..964c80f42a7 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-input-element/image-width-height.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-input-element/image-width-height.txt
@@ -2,8 +2,7 @@ Harness status: OK
Found 32 tests
-27 Pass
-5 Fail
+32 Pass
Pass no width/height attributes
Pass width and height attributes
Pass width attribute only
@@ -11,11 +10,11 @@ Pass height attribute only
Pass unparseable width attribute
Pass no src, with width/height attributes
Pass not in the Image Button state
-Fail SVG image with neither a natural width nor a natural height
-Fail SVG image with a natural aspect ratio only
-Fail SVG image with a natural aspect ratio only, and a width attribute
-Fail SVG image with a natural width only
-Fail SVG image with a natural height only
+Pass SVG image with neither a natural width nor a natural height
+Pass SVG image with a natural aspect ratio only
+Pass SVG image with a natural aspect ratio only, and a width attribute
+Pass SVG image with a natural width only
+Pass SVG image with a natural height only
Pass width and height attribute value "10" when not rendered
Pass width and height attribute value "0" when not rendered
Pass width and height attribute value "010" when not rendered