From c5d1ec4dea5fd795eb96cbb8211e9116284dc2e0 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Sat, 17 Feb 2024 08:09:53 +0000 Subject: [PATCH] LibWeb/CSS: Ensure length is absolute before converting to pixels Previously, creating a DOMMatrix with a transform that contained non-absolute units would cause a crash. --- Tests/LibWeb/Text/expected/geometry/dommatrix-create.txt | 1 + Tests/LibWeb/Text/input/geometry/dommatrix-create.html | 3 +++ Userland/Libraries/LibWeb/CSS/Transformation.cpp | 6 ++++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Tests/LibWeb/Text/expected/geometry/dommatrix-create.txt b/Tests/LibWeb/Text/expected/geometry/dommatrix-create.txt index 517e542d47..f7e5a57630 100644 --- a/Tests/LibWeb/Text/expected/geometry/dommatrix-create.txt +++ b/Tests/LibWeb/Text/expected/geometry/dommatrix-create.txt @@ -16,3 +16,4 @@ 16. Exception: SyntaxError 17. Exception: SyntaxError 18. Exception: SyntaxError +19. Exception: SyntaxError diff --git a/Tests/LibWeb/Text/input/geometry/dommatrix-create.html b/Tests/LibWeb/Text/input/geometry/dommatrix-create.html index 2ea5513b1b..270a5a198f 100644 --- a/Tests/LibWeb/Text/input/geometry/dommatrix-create.html +++ b/Tests/LibWeb/Text/input/geometry/dommatrix-create.html @@ -64,5 +64,8 @@ // 18. Creating a DOMMatrix with CSS transform string with error testPart(() => new DOMMatrix('matrix(1.0, 2.0deg, 3.0, 4.0, 5.0, 6.0)')); + + // 19. Creating a DOMMatrix with CSS transform string with non-absolute units should fail + testPart(() => new DOMMatrix('translate(1em, 1em)')); }); diff --git a/Userland/Libraries/LibWeb/CSS/Transformation.cpp b/Userland/Libraries/LibWeb/CSS/Transformation.cpp index 12ca2d215f..1ca1aa957a 100644 --- a/Userland/Libraries/LibWeb/CSS/Transformation.cpp +++ b/Userland/Libraries/LibWeb/CSS/Transformation.cpp @@ -24,8 +24,10 @@ ErrorOr Transformation::to_matrix(Optional ErrorOr { if (paintable_box.has_value()) return value.resolved(paintable_box->layout_node(), reference_length).to_px(paintable_box->layout_node()).to_float(); - if (value.is_length()) - return value.length().absolute_length_to_px().to_float(); + if (value.is_length()) { + if (auto const& length = value.length(); length.is_absolute()) + return length.absolute_length_to_px().to_float(); + } return Error::from_string_literal("Transform contains non absolute units"); }, [&](CSS::AngleOrCalculated const& value) -> ErrorOr {