|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Sam Atkins <sam@ladybird.org> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-2-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include "CSSSkewY.h" |
| 8 | +#include <LibWeb/Bindings/CSSSkewYPrototype.h> |
| 9 | +#include <LibWeb/Bindings/Intrinsics.h> |
| 10 | +#include <LibWeb/CSS/CSSNumericValue.h> |
| 11 | +#include <LibWeb/CSS/CSSUnitValue.h> |
| 12 | +#include <LibWeb/Geometry/DOMMatrix.h> |
| 13 | +#include <LibWeb/WebIDL/ExceptionOr.h> |
| 14 | + |
| 15 | +namespace Web::CSS { |
| 16 | + |
| 17 | +GC_DEFINE_ALLOCATOR(CSSSkewY); |
| 18 | + |
| 19 | +GC::Ref<CSSSkewY> CSSSkewY::create(JS::Realm& realm, GC::Ref<CSSNumericValue> ay) |
| 20 | +{ |
| 21 | + return realm.create<CSSSkewY>(realm, ay); |
| 22 | +} |
| 23 | + |
| 24 | +// https://drafts.css-houdini.org/css-typed-om-1/#dom-cssskewy-cssskewy |
| 25 | +WebIDL::ExceptionOr<GC::Ref<CSSSkewY>> CSSSkewY::construct_impl(JS::Realm& realm, GC::Ref<CSSNumericValue> ay) |
| 26 | +{ |
| 27 | + // The CSSSkewY(ay) constructor must, when invoked, perform the following steps: |
| 28 | + |
| 29 | + // 1. If ay does not match <angle>, throw a TypeError. |
| 30 | + if (!ay->type().matches_angle({})) |
| 31 | + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "CSSSkewY ay component doesn't match <angle>"sv }; |
| 32 | + |
| 33 | + // 2. Return a new CSSSkewY object with its ay internal slot set to ay, and its is2D internal slot set to true. |
| 34 | + return CSSSkewY::create(realm, ay); |
| 35 | +} |
| 36 | + |
| 37 | +CSSSkewY::CSSSkewY(JS::Realm& realm, GC::Ref<CSSNumericValue> ay) |
| 38 | + : CSSTransformComponent(realm, Is2D::Yes) |
| 39 | + , m_ay(ay) |
| 40 | +{ |
| 41 | +} |
| 42 | + |
| 43 | +CSSSkewY::~CSSSkewY() = default; |
| 44 | + |
| 45 | +void CSSSkewY::initialize(JS::Realm& realm) |
| 46 | +{ |
| 47 | + WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSSkewY); |
| 48 | + Base::initialize(realm); |
| 49 | +} |
| 50 | + |
| 51 | +void CSSSkewY::visit_edges(Visitor& visitor) |
| 52 | +{ |
| 53 | + Base::visit_edges(visitor); |
| 54 | + visitor.visit(m_ay); |
| 55 | +} |
| 56 | + |
| 57 | +// https://drafts.css-houdini.org/css-typed-om-1/#serialize-a-cssskewy |
| 58 | +WebIDL::ExceptionOr<Utf16String> CSSSkewY::to_string() const |
| 59 | +{ |
| 60 | + // 1. Let s initially be "skewY(". |
| 61 | + StringBuilder builder { StringBuilder::Mode::UTF16 }; |
| 62 | + builder.append("skewY("sv); |
| 63 | + |
| 64 | + // 2. Serialize this’s ay internal slot, and append it to s. |
| 65 | + builder.append(m_ay->to_string()); |
| 66 | + |
| 67 | + // 3. Append ")" to s, and return s. |
| 68 | + builder.append(")"sv); |
| 69 | + return builder.to_utf16_string(); |
| 70 | +} |
| 71 | + |
| 72 | +// https://drafts.css-houdini.org/css-typed-om-1/#dom-csstransformcomponent-tomatrix |
| 73 | +WebIDL::ExceptionOr<GC::Ref<Geometry::DOMMatrix>> CSSSkewY::to_matrix() const |
| 74 | +{ |
| 75 | + // 1. Let matrix be a new DOMMatrix object, initialized to this’s equivalent 4x4 transform matrix, as defined in |
| 76 | + // CSS Transforms 1 § 12. Mathematical Description of Transform Functions, and with its is2D internal slot set |
| 77 | + // to the same value as this’s is2D internal slot. |
| 78 | + // NOTE: Recall that the is2D flag affects what transform, and thus what equivalent matrix, a |
| 79 | + // CSSTransformComponent represents. |
| 80 | + // As the entries of such a matrix are defined relative to the px unit, if any <length>s in this involved in |
| 81 | + // generating the matrix are not compatible units with px (such as relative lengths or percentages), throw a |
| 82 | + // TypeError. |
| 83 | + auto matrix = Geometry::DOMMatrix::create(realm()); |
| 84 | + |
| 85 | + // NB: to() throws a TypeError if the conversion can't be done. |
| 86 | + auto ay_rad = TRY(m_ay->to("rad"_fly_string))->value(); |
| 87 | + matrix->set_m12(tanf(ay_rad)); |
| 88 | + |
| 89 | + // 2. Return matrix. |
| 90 | + return matrix; |
| 91 | +} |
| 92 | + |
| 93 | +WebIDL::ExceptionOr<void> CSSSkewY::set_ay(GC::Ref<CSSNumericValue> ay) |
| 94 | +{ |
| 95 | + // AD-HOC: Not specced. https://github.com/w3c/css-houdini-drafts/issues/1153 |
| 96 | + // WPT expects this to throw for invalid values. |
| 97 | + if (!ay->type().matches_angle({})) |
| 98 | + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "CSSSkewY ay component doesn't match <angle>"sv }; |
| 99 | + m_ay = ay; |
| 100 | + return {}; |
| 101 | +} |
| 102 | + |
| 103 | +// https://drafts.css-houdini.org/css-typed-om-1/#dom-cssskew-is2d |
| 104 | +void CSSSkewY::set_is_2d(bool) |
| 105 | +{ |
| 106 | + // The is2D attribute of a CSSSkewY, CSSSkewYX, or CSSSkewYY object must, on setting, do nothing. |
| 107 | +} |
| 108 | + |
| 109 | +} |
0 commit comments