Skip to content

Truly independent web browser

LibWeb: Map img, video and input width/height to aspect-ratio

Andreas Kling5249662b3ab6
diff --git a/Libraries/LibWeb/CSS/StyleComputer.cpp b/Libraries/LibWeb/CSS/StyleComputer.cpp
index ee53429e1b3..808e36643ff 100644
--- a/Libraries/LibWeb/CSS/StyleComputer.cpp
+++ b/Libraries/LibWeb/CSS/StyleComputer.cpp
@@ -93,6 +93,7 @@
#include <LibWeb/HTML/HTMLImageElement.h>
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/HTML/HTMLSlotElement.h>
+#include <LibWeb/HTML/HTMLVideoElement.h>
#include <LibWeb/HTML/Parser/HTMLParser.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/Layout/LayoutRustBridge.h>
@@ -1098,17 +1099,39 @@ void StyleComputer::compose_installed_engine_record(Layout::BegunRead const& rea
context.publish();
}
-static void collect_dimension_attribute(Vector<StyleProperty>& properties, DOM::Element const& element, Utf16FlyString const& attribute_name, CSS::PropertyID property_id)
+static RefPtr<CSS::StyleValue const> collect_dimension_attribute(Vector<StyleProperty>& properties, DOM::Element const& element, Utf16FlyString const& attribute_name, CSS::PropertyID property_id)
{
auto attribute = element.attribute(attribute_name);
if (!attribute.has_value())
- return;
+ return {};
auto parsed_value = HTML::parse_dimension_value(*attribute);
if (!parsed_value)
+ return {};
+
+ properties.append({ .property_id = property_id, .value = *parsed_value });
+ return parsed_value;
+}
+
+// https://html.spec.whatwg.org/multipage/rendering.html#map-to-the-aspect-ratio-property-(using-dimension-rules)
+static void collect_aspect_ratio_from_dimension_values(Vector<StyleProperty>& properties, RefPtr<CSS::StyleValue const> const& width, RefPtr<CSS::StyleValue const> const& height)
+{
+ // if element has both attributes w and h, and parsing those attributes' values using the rules for parsing
+ // dimension values doesn't generate an error or return a percentage for either,
+ if (!width || !height || !width->is_length() || !height->is_length())
return;
- properties.append({ .property_id = property_id, .value = parsed_value.release_nonnull() });
+ // then the user agent is expected to use the parsed dimensions as a presentational hint for the 'aspect-ratio'
+ // property of the form auto w / h.
+ auto aspect_ratio = CSS::StyleValueList::create(
+ CSS::StyleValueVector {
+ CSS::KeywordStyleValue::create(CSS::Keyword::Auto),
+ CSS::RatioStyleValue::create(
+ CSS::NumberStyleValue::create(width->as_length().length().raw_value()),
+ CSS::NumberStyleValue::create(height->as_length().length().raw_value())),
+ },
+ CSS::StyleValueList::Separator::Space);
+ properties.append({ .property_id = CSS::PropertyID::AspectRatio, .value = aspect_ratio });
}
// The whole transition step for an element's record, run once the record is installed.
@@ -1811,8 +1834,14 @@ Vector<StyleProperty> StyleComputer::collect_presentational_hint_properties(DOM:
auto const& dimension_source = is<HTML::HTMLImageElement>(element)
? static_cast<HTML::HTMLImageElement const&>(element).dimension_attribute_source()
: element;
- collect_dimension_attribute(properties, dimension_source, HTML::AttributeNames::width, CSS::PropertyID::Width);
- collect_dimension_attribute(properties, dimension_source, HTML::AttributeNames::height, CSS::PropertyID::Height);
+ auto width = collect_dimension_attribute(properties, dimension_source, HTML::AttributeNames::width, CSS::PropertyID::Width);
+ auto height = collect_dimension_attribute(properties, dimension_source, HTML::AttributeNames::height, CSS::PropertyID::Height);
+
+ // The width and height attributes map to the aspect-ratio property (using dimension rules) on img and video
+ // elements, and input elements with a type attribute in the Image Button state.
+ // NB: An input element only supports dimension attributes in the Image Button state.
+ if (is<HTML::HTMLImageElement>(element) || is<HTML::HTMLVideoElement>(element) || is<HTML::HTMLInputElement>(element))
+ collect_aspect_ratio_from_dimension_values(properties, width, height);
}
HashTable<CSS::PropertyID> seen_properties;
for (size_t i = properties.size(); i > 0; --i) {
diff --git a/Tests/LibWeb/Text/expected/HTML/dimension-attributes-map-to-aspect-ratio.txt b/Tests/LibWeb/Text/expected/HTML/dimension-attributes-map-to-aspect-ratio.txt
new file mode 100644
index 00000000000..ae8dca077e2
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/dimension-attributes-map-to-aspect-ratio.txt
@@ -0,0 +1,20 @@
+img: auto 1524 / 876
+img-decimal: auto 16.5 / 9
+img-percentage: auto
+img-only-width: auto
+img-zero: auto 0 / 10
+video: auto 640 / 360
+input-image: auto 30 / 20
+input-text: auto
+iframe: auto
+canvas: auto 300 / 200
+img-in-picture: auto 400 / 100
+img-in-picture-with-source-dimensions: auto 300 / 100
+contained: 200x114.953125
+contained-in-picture: 200x50
+contained-zero: 200x0
+dynamic: 200x200
+dynamic-in-picture: 200x50
+dynamic: 200x100
+dynamic: 200x0
+dynamic-in-picture: 200x400
diff --git a/Tests/LibWeb/Text/input/HTML/dimension-attributes-map-to-aspect-ratio.html b/Tests/LibWeb/Text/input/HTML/dimension-attributes-map-to-aspect-ratio.html
new file mode 100644
index 00000000000..599ef304330
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/dimension-attributes-map-to-aspect-ratio.html
@@ -0,0 +1,46 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ .contained { contain: size; width: 200px; height: auto; display: block; }
+</style>
+<img id="img" width="1524" height="876">
+<img id="img-decimal" width="16.5" height="9">
+<img id="img-percentage" width="50%" height="9">
+<img id="img-only-width" width="16">
+<img id="img-zero" width="0" height="10">
+<img id="contained-zero" class="contained" width="0" height="0">
+<video id="video" width="640" height="360"></video>
+<input id="input-image" type="image" width="30" height="20">
+<input id="input-text" type="text" width="30" height="20">
+<iframe id="iframe" width="300" height="200"></iframe>
+<canvas id="canvas" width="300" height="200"></canvas>
+<picture><source id="source-without-dimensions" srcset="../../../Assets/120.png"><img id="img-in-picture" width="400" height="100"></picture>
+<picture><source srcset="../../../Assets/120.png" width="300" height="100"><img id="img-in-picture-with-source-dimensions" width="400" height="200"></picture>
+<img id="contained" class="contained" src="../../../Assets/120.png" width="1524" height="876">
+<img id="dynamic" class="contained" src="../../../Assets/120.png" width="100" height="100">
+<picture><source id="dynamic-source" srcset="../../../Assets/120.png" width="100" height="25"><img id="dynamic-in-picture" class="contained" width="100" height="100"></picture>
+<picture><source srcset="../../../Assets/120.png"><img id="contained-in-picture" class="contained" src="../../../Assets/120.png" width="400" height="100" sizes="auto" loading="lazy"></picture>
+<script>
+ asyncTest(done => {
+ window.onload = () => {
+ for (const id of ["img", "img-decimal", "img-percentage", "img-only-width", "img-zero", "video", "input-image", "input-text", "iframe", "canvas", "img-in-picture", "img-in-picture-with-source-dimensions"])
+ println(`${id}: ${getComputedStyle(document.getElementById(id)).aspectRatio}`);
+ const printSize = id => {
+ const rect = document.getElementById(id).getBoundingClientRect();
+ println(`${id}: ${rect.width}x${rect.height}`);
+ };
+ for (const id of ["contained", "contained-in-picture", "contained-zero", "dynamic", "dynamic-in-picture"])
+ printSize(id);
+
+ document.getElementById("dynamic").setAttribute("height", "50");
+ printSize("dynamic");
+ document.getElementById("dynamic").removeAttribute("height");
+ printSize("dynamic");
+
+ const source = document.getElementById("dynamic-source");
+ source.setAttribute("height", "200");
+ printSize("dynamic-in-picture");
+ done();
+ };
+ });
+</script>