|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Sam Atkins <sam@ladybird.org> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-2-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include "CSSPerspective.h" |
| 8 | +#include <LibWeb/Bindings/CSSPerspectivePrototype.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(CSSPerspective); |
| 18 | + |
| 19 | +static WebIDL::ExceptionOr<CSSPerspectiveValueInternal> to_internal(JS::Realm& realm, CSSPerspectiveValue const& value) |
| 20 | +{ |
| 21 | + // Steps 1 and 2 of The CSSPerspective(length) constructor: |
| 22 | + // https://drafts.css-houdini.org/css-typed-om-1/#dom-cssperspective-cssperspective |
| 23 | + return value.visit( |
| 24 | + // 1. If length is a CSSNumericValue: |
| 25 | + [](GC::Root<CSSNumericValue> const& numeric_value) -> WebIDL::ExceptionOr<CSSPerspectiveValueInternal> { |
| 26 | + // 1. If length does not match <length>, throw a TypeError. |
| 27 | + if (!numeric_value->type().matches_length({})) { |
| 28 | + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "CSSPerspective length component doesn't match <length>"sv }; |
| 29 | + } |
| 30 | + return { GC::Ref { *numeric_value } }; |
| 31 | + }, |
| 32 | + // 2. Otherwise (that is, if length is not a CSSNumericValue): |
| 33 | + [&realm](CSSKeywordish const& keywordish) -> WebIDL::ExceptionOr<CSSPerspectiveValueInternal> { |
| 34 | + // 1. Rectify a keywordish value from length, then set length to the result’s value. |
| 35 | + auto rectified_length = rectify_a_keywordish_value(realm, keywordish); |
| 36 | + |
| 37 | + // 2. If length does not represent a value that is an ASCII case-insensitive match for the keyword none, |
| 38 | + // throw a TypeError. |
| 39 | + if (!rectified_length->value().equals_ignoring_ascii_case("none"_fly_string)) { |
| 40 | + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "CSSPerspective length component is a keyword other than `none`"sv }; |
| 41 | + } |
| 42 | + |
| 43 | + return { rectified_length }; |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +GC::Ref<CSSPerspective> CSSPerspective::create(JS::Realm& realm, CSSPerspectiveValueInternal length) |
| 48 | +{ |
| 49 | + return realm.create<CSSPerspective>(realm, length); |
| 50 | +} |
| 51 | + |
| 52 | +// https://drafts.css-houdini.org/css-typed-om-1/#dom-cssperspective-cssperspective |
| 53 | +WebIDL::ExceptionOr<GC::Ref<CSSPerspective>> CSSPerspective::construct_impl(JS::Realm& realm, CSSPerspectiveValue length) |
| 54 | +{ |
| 55 | + // The CSSPerspective(length) constructor must, when invoked, perform the following steps: |
| 56 | + // NB: Steps 1 and 2 are implemented in to_internal(). |
| 57 | + auto internal_length = TRY(to_internal(realm, length)); |
| 58 | + |
| 59 | + // 3. Return a new CSSPerspective object with its length internal slot set to length, and its is2D internal slot |
| 60 | + // set to false. |
| 61 | + return CSSPerspective::create(realm, internal_length); |
| 62 | +} |
| 63 | + |
| 64 | +CSSPerspective::CSSPerspective(JS::Realm& realm, CSSPerspectiveValueInternal length) |
| 65 | + : CSSTransformComponent(realm, Is2D::No) |
| 66 | + , m_length(length) |
| 67 | +{ |
| 68 | +} |
| 69 | + |
| 70 | +CSSPerspective::~CSSPerspective() = default; |
| 71 | + |
| 72 | +void CSSPerspective::initialize(JS::Realm& realm) |
| 73 | +{ |
| 74 | + WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSPerspective); |
| 75 | + Base::initialize(realm); |
| 76 | +} |
| 77 | + |
| 78 | +void CSSPerspective::visit_edges(Visitor& visitor) |
| 79 | +{ |
| 80 | + Base::visit_edges(visitor); |
| 81 | + m_length.visit([&visitor](auto const& it) { visitor.visit(it); }); |
| 82 | +} |
| 83 | + |
| 84 | +// https://drafts.css-houdini.org/css-typed-om-1/#serialize-a-cssperspective |
| 85 | +WebIDL::ExceptionOr<Utf16String> CSSPerspective::to_string() const |
| 86 | +{ |
| 87 | + // 1. Let s initially be "perspective(". |
| 88 | + StringBuilder builder { StringBuilder::Mode::UTF16 }; |
| 89 | + builder.append("perspective("sv); |
| 90 | + |
| 91 | + // 2. Serialize this’s length internal slot, with a minimum of 0px, and append it to s. |
| 92 | + auto serialized_length = m_length.visit( |
| 93 | + [](GC::Ref<CSSNumericValue> const& numeric_value) { |
| 94 | + return numeric_value->to_string({ .minimum = 0 }); |
| 95 | + }, |
| 96 | + [](GC::Ref<CSSKeywordValue> const& keyword_value) { |
| 97 | + return keyword_value->to_string(); |
| 98 | + }); |
| 99 | + builder.append(serialized_length); |
| 100 | + |
| 101 | + // 3. Append ")" to s, and return s. |
| 102 | + builder.append(")"sv); |
| 103 | + return builder.to_utf16_string(); |
| 104 | +} |
| 105 | + |
| 106 | +WebIDL::ExceptionOr<GC::Ref<Geometry::DOMMatrix>> CSSPerspective::to_matrix() const |
| 107 | +{ |
| 108 | + // 1. Let matrix be a new DOMMatrix object, initialized to this’s equivalent 4x4 transform matrix, as defined in |
| 109 | + // CSS Transforms 1 § 12. Mathematical Description of Transform Functions, and with its is2D internal slot set |
| 110 | + // to the same value as this’s is2D internal slot. |
| 111 | + // NOTE: Recall that the is2D flag affects what transform, and thus what equivalent matrix, a |
| 112 | + // CSSTransformComponent represents. |
| 113 | + // As the entries of such a matrix are defined relative to the px unit, if any <length>s in this involved in |
| 114 | + // generating the matrix are not compatible units with px (such as relative lengths or percentages), throw a |
| 115 | + // TypeError. |
| 116 | + auto matrix = Geometry::DOMMatrix::create(realm()); |
| 117 | + |
| 118 | + TRY(m_length.visit( |
| 119 | + [&matrix](GC::Ref<CSSNumericValue> const& numeric_value) -> WebIDL::ExceptionOr<void> { |
| 120 | + // NB: to() throws a TypeError if the conversion can't be done. |
| 121 | + auto distance = TRY(numeric_value->to("px"_fly_string))->value(); |
| 122 | + matrix->set_m34(-1 / (distance <= 0 ? 1 : distance)); |
| 123 | + return {}; |
| 124 | + }, |
| 125 | + [](GC::Ref<CSSKeywordValue> const&) -> WebIDL::ExceptionOr<void> { |
| 126 | + // NB: This is `none`, so do nothing. |
| 127 | + return {}; |
| 128 | + })); |
| 129 | + |
| 130 | + // 2. Return matrix. |
| 131 | + return matrix; |
| 132 | +} |
| 133 | + |
| 134 | +CSSPerspectiveValue CSSPerspective::length() const |
| 135 | +{ |
| 136 | + return m_length.visit( |
| 137 | + [](GC::Ref<CSSNumericValue> const& numeric_value) -> CSSPerspectiveValue { |
| 138 | + return GC::Root { numeric_value }; |
| 139 | + }, |
| 140 | + [](GC::Ref<CSSKeywordValue> const& keyword_value) -> CSSPerspectiveValue { |
| 141 | + return CSSKeywordish { keyword_value }; |
| 142 | + }); |
| 143 | +} |
| 144 | + |
| 145 | +WebIDL::ExceptionOr<void> CSSPerspective::set_length(CSSPerspectiveValue value) |
| 146 | +{ |
| 147 | + // AD-HOC: Not specced. https://github.com/w3c/css-houdini-drafts/issues/1153 |
| 148 | + // WPT expects this to throw for invalid values, so just reuse the constructor code. |
| 149 | + auto length = TRY(to_internal(realm(), value)); |
| 150 | + m_length = length; |
| 151 | + return {}; |
| 152 | +} |
| 153 | + |
| 154 | +// https://drafts.css-houdini.org/css-typed-om-1/#dom-cssperspective-is2d |
| 155 | +void CSSPerspective::set_is_2d(bool) |
| 156 | +{ |
| 157 | + // The is2D attribute of a CSSPerspective object must, on setting, do nothing. |
| 158 | +} |
| 159 | + |
| 160 | +} |
0 commit comments