Skip to content

Truly independent web browser

LibWeb: Preserve large dimension values in aspect-ratio hints

Andreas Kling4bd2e3f463e4
diff --git a/Libraries/LibWeb/CSS/StyleComputer.cpp b/Libraries/LibWeb/CSS/StyleComputer.cpp
index 15b62c1bfb9..cd7f205e2ee 100644
--- a/Libraries/LibWeb/CSS/StyleComputer.cpp
+++ b/Libraries/LibWeb/CSS/StyleComputer.cpp
@@ -1099,26 +1099,31 @@ void StyleComputer::compose_installed_engine_record(Layout::BegunRead const& rea
context.publish();
}
-static RefPtr<CSS::StyleValue const> collect_dimension_attribute(Vector<StyleProperty>& properties, DOM::Element const& element, Utf16FlyString const& attribute_name, CSS::PropertyID property_id)
+static Optional<double> 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 {};
- auto parsed_value = HTML::parse_dimension_value(*attribute);
+ double unclamped_value;
+ auto parsed_value = HTML::parse_dimension_value(*attribute, unclamped_value);
if (!parsed_value)
return {};
properties.append({ .property_id = property_id, .value = *parsed_value });
- return parsed_value;
+ // NB: The ratio uses the parsed number, rather than the clamped layout dimension. Values beyond the numeric
+ // representation's range cannot form a finite ratio hint, but keep their ordinary dimension mapping.
+ if (!parsed_value->is_length() || !__builtin_isfinite(unclamped_value))
+ return {};
+ return unclamped_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)
+static void collect_aspect_ratio_from_dimension_values(Vector<StyleProperty>& properties, Optional<double> width, Optional<double> 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())
+ if (!width.has_value() || !height.has_value())
return;
// then the user agent is expected to use the parsed dimensions as a presentational hint for the 'aspect-ratio'
@@ -1127,8 +1132,8 @@ static void collect_aspect_ratio_from_dimension_values(Vector<StyleProperty>& pr
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::NumberStyleValue::create(*width),
+ CSS::NumberStyleValue::create(*height)),
},
CSS::StyleValueList::Separator::Space);
properties.append({ .property_id = CSS::PropertyID::AspectRatio, .value = aspect_ratio });
diff --git a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
index b4993e81536..d528f84f17b 100644
--- a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
+++ b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
@@ -2418,6 +2418,12 @@ static RefPtr<CSS::StyleValue const> parse_current_dimension_value(double value,
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-dimension-values
RefPtr<CSS::StyleValue const> parse_dimension_value(Utf16View input)
+{
+ double unclamped_value;
+ return parse_dimension_value(input, unclamped_value);
+}
+
+RefPtr<CSS::StyleValue const> parse_dimension_value(Utf16View input, double& unclamped_value)
{
// 1. Let input be the string being parsed.
// 2. Let position be a position variable for input, initially pointing at the start of input.
@@ -2440,6 +2446,7 @@ RefPtr<CSS::StyleValue const> parse_dimension_value(Utf16View input)
++position;
}
+ unclamped_value = integer_value;
double value = min(integer_value, CSSPixels::max_dimension_value);
// 6. If position is past the end of input, then return value as a length.
@@ -2466,7 +2473,9 @@ RefPtr<CSS::StyleValue const> parse_dimension_value(Utf16View input)
// 2. Add the value of the code point at position within input,
// interpreted as a base-ten digit (0..9) and divided by divisor, to value.
- value += (code_unit_at(input, position) - '0') / divisor;
+ auto fraction = (code_unit_at(input, position) - '0') / divisor;
+ value += fraction;
+ unclamped_value += fraction;
// 3. Advance position by 1.
++position;
diff --git a/Libraries/LibWeb/HTML/Parser/HTMLParser.h b/Libraries/LibWeb/HTML/Parser/HTMLParser.h
index e30f53d58e2..35dbed1ecf7 100644
--- a/Libraries/LibWeb/HTML/Parser/HTMLParser.h
+++ b/Libraries/LibWeb/HTML/Parser/HTMLParser.h
@@ -236,6 +236,8 @@ private:
};
RefPtr<CSS::StyleValue const> parse_dimension_value(Utf16View);
+// Also expose the numeric value before layout clamping when parsing succeeds.
+RefPtr<CSS::StyleValue const> parse_dimension_value(Utf16View, double& unclamped_value);
RefPtr<CSS::StyleValue const> parse_nonzero_dimension_value(Utf16View);
Optional<Color> parse_legacy_color_value(Utf16View);
RefPtr<CSS::StyleValue const> parse_table_child_element_align_value(Utf16View);
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
index ae8dca077e2..2b3444b1c53 100644
--- 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
@@ -18,3 +18,13 @@ dynamic-in-picture: 200x50
dynamic: 200x100
dynamic: 200x0
dynamic-in-picture: 200x400
+img-large: auto 100000000 / 50000000
+img-large-decimal: auto 100000000.5 / 50000000.25
+video-large: auto 100000000 / 50000000
+input-image-large: auto 100000000 / 50000000
+img-large-percentage: auto
+img-invalid: auto
+img-trailing-text: auto 16.5 / 9
+img-author-ratio: 3 / 1
+img-large: 200x100
+img-nonfinite: auto
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
index 599ef304330..addc2e5e389 100644
--- 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
@@ -20,6 +20,14 @@
<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>
+<img id="img-large" class="contained" src="../../../Assets/120.png" width="100000000" height="50000000">
+<img id="img-large-decimal" class="contained" width="100000000.5" height="50000000.25">
+<video id="video-large" class="contained" width="100000000" height="50000000"></video>
+<input id="input-image-large" class="contained" type="image" width="100000000" height="50000000">
+<img id="img-large-percentage" class="contained" width="100000000%" height="50000000">
+<img id="img-invalid" class="contained" width="-16" height="9">
+<img id="img-trailing-text" class="contained" width=" 16.5px" height="9">
+<img id="img-author-ratio" class="contained" width="100000000" height="50000000" style="aspect-ratio: 3 / 1">
<script>
asyncTest(done => {
window.onload = () => {
@@ -40,6 +48,17 @@
const source = document.getElementById("dynamic-source");
source.setAttribute("height", "200");
printSize("dynamic-in-picture");
+
+ for (const id of ["img-large", "img-large-decimal", "video-large", "input-image-large", "img-large-percentage", "img-invalid", "img-trailing-text", "img-author-ratio"])
+ println(`${id}: ${getComputedStyle(document.getElementById(id)).aspectRatio}`);
+ printSize("img-large");
+
+ const overflow = document.createElement("img");
+ overflow.className = "contained";
+ overflow.setAttribute("width", "9".repeat(400));
+ overflow.setAttribute("height", "10");
+ document.body.append(overflow);
+ println(`img-nonfinite: ${getComputedStyle(overflow).aspectRatio}`);
done();
};
});