LibWeb: Make CSSPerspective correctly clamp its input for toMatrix()

This commit is contained in:
Psychpsyo 2025-11-13 00:45:47 +01:00 committed by Jelle Raaijmakers
parent 684c543ddb
commit edccb92da7
Notes: github-actions[bot] 2025-11-13 14:48:12 +00:00
2 changed files with 10 additions and 9 deletions

View file

@ -121,7 +121,7 @@ WebIDL::ExceptionOr<GC::Ref<Geometry::DOMMatrix>> CSSPerspective::to_matrix() co
[&matrix](GC::Ref<CSSNumericValue> const& numeric_value) -> WebIDL::ExceptionOr<void> {
// NB: to() throws a TypeError if the conversion can't be done.
auto distance = TRY(numeric_value->to("px"_fly_string))->value();
matrix->set_m34(-1 / (distance <= 0 ? 1 : distance));
matrix->set_m34(-1 / max(distance, 1));
return {};
},
[](GC::Ref<CSSKeywordValue> const&) -> WebIDL::ExceptionOr<void> {

View file

@ -66,14 +66,15 @@ test(() => {
}, 'CSSSkewY.toMatrix() returns correct matrix');
test(() => {
const length = 10;
const component = new CSSPerspective(new CSSUnitValue(length, 'px'));
const expectedMatrix = new DOMMatrixReadOnly(
[1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, -1/length,
0, 0, 0, 1]);
assert_matrix_approx_equals(component.toMatrix(), expectedMatrix, gEpsilon);
for (const length of [10, 1, 0.5, 0, -10]) {
const component = new CSSPerspective(new CSSUnitValue(length, 'px'));
const expectedMatrix = new DOMMatrixReadOnly(
[1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, -1/Math.max(1, length),
0, 0, 0, 1]);
assert_matrix_approx_equals(component.toMatrix(), expectedMatrix, gEpsilon);
}
}, 'CSSPerspective.toMatrix() returns correct matrix');
test(() => {