diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index b181cc8bd8e..2e57ec81143 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -116,6 +116,7 @@ set(SOURCES CSS/CSSLayerBlockRule.cpp CSS/CSSLayerStatementRule.cpp CSS/CSSMarginRule.cpp + CSS/CSSMathValue.cpp CSS/CSSMediaRule.cpp CSS/CSSNamespaceRule.cpp CSS/CSSNestedDeclarations.cpp diff --git a/Libraries/LibWeb/CSS/CSSMathValue.cpp b/Libraries/LibWeb/CSS/CSSMathValue.cpp new file mode 100644 index 00000000000..edf39f39ec5 --- /dev/null +++ b/Libraries/LibWeb/CSS/CSSMathValue.cpp @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "CSSMathValue.h" +#include + +namespace Web::CSS { + +GC_DEFINE_ALLOCATOR(CSSMathValue); + +CSSMathValue::CSSMathValue(JS::Realm& realm, Bindings::CSSMathOperator operator_, NumericType type) + : CSSNumericValue(realm, move(type)) + , m_operator(operator_) +{ +} + +void CSSMathValue::initialize(JS::Realm& realm) +{ + WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSMathValue); + Base::initialize(realm); +} + +} diff --git a/Libraries/LibWeb/CSS/CSSMathValue.h b/Libraries/LibWeb/CSS/CSSMathValue.h new file mode 100644 index 00000000000..0ea9e1da3d4 --- /dev/null +++ b/Libraries/LibWeb/CSS/CSSMathValue.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace Web::CSS { + +// https://drafts.css-houdini.org/css-typed-om-1/#cssmathvalue +class CSSMathValue : public CSSNumericValue { + WEB_PLATFORM_OBJECT(CSSMathValue, CSSNumericValue); + GC_DECLARE_ALLOCATOR(CSSMathValue); + +public: + virtual ~CSSMathValue() override = default; + + Bindings::CSSMathOperator operator_() const { return m_operator; } + + enum class Nested : u8 { + No, + Yes, + }; + enum class Parens : u8 { + With, + Without, + }; + virtual String serialize_math_value(Nested, Parens) const = 0; + +protected: + explicit CSSMathValue(JS::Realm&, Bindings::CSSMathOperator, NumericType); + + virtual void initialize(JS::Realm&) override; + + Bindings::CSSMathOperator m_operator; +}; + +} diff --git a/Libraries/LibWeb/CSS/CSSMathValue.idl b/Libraries/LibWeb/CSS/CSSMathValue.idl new file mode 100644 index 00000000000..c2fec46f20d --- /dev/null +++ b/Libraries/LibWeb/CSS/CSSMathValue.idl @@ -0,0 +1,18 @@ +#import + +// https://drafts.css-houdini.org/css-typed-om-1/#cssmathvalue +[Exposed=(Window, Worker, PaintWorklet, LayoutWorklet)] +interface CSSMathValue : CSSNumericValue { + readonly attribute CSSMathOperator operator; +}; + +// https://drafts.css-houdini.org/css-typed-om-1/#enumdef-cssmathoperator +enum CSSMathOperator { + "sum", + "product", + "negate", + "invert", + "min", + "max", + "clamp", +}; diff --git a/Libraries/LibWeb/CSS/CSSNumericValue.cpp b/Libraries/LibWeb/CSS/CSSNumericValue.cpp index 5f240527f3f..4002af5cfff 100644 --- a/Libraries/LibWeb/CSS/CSSNumericValue.cpp +++ b/Libraries/LibWeb/CSS/CSSNumericValue.cpp @@ -7,6 +7,7 @@ #include "CSSNumericValue.h" #include #include +#include #include #include #include @@ -107,9 +108,11 @@ String CSSNumericValue::to_string(SerializationParams const& params) const if (auto* unit_value = as_if(this)) { return unit_value->serialize_unit_value(params.minimum, params.maximum); } - // FIXME: 2. Otherwise, serialize a CSSMathValue from this, and return the result. - - return {}; + // 2. Otherwise, serialize a CSSMathValue from this, and return the result. + auto& math_value = as(*this); + return math_value.serialize_math_value( + params.nested ? CSSMathValue::Nested::Yes : CSSMathValue::Nested::No, + params.parenless ? CSSMathValue::Parens::Without : CSSMathValue::Parens::With); } // https://drafts.css-houdini.org/css-typed-om-1/#rectify-a-numberish-value diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h index 040d7768f06..7ae9032bdba 100644 --- a/Libraries/LibWeb/Forward.h +++ b/Libraries/LibWeb/Forward.h @@ -242,6 +242,7 @@ class CSSKeywordValue; class CSSLayerBlockRule; class CSSLayerStatementRule; class CSSMarginRule; +class CSSMathValue; class CSSMediaRule; class CSSNamespaceRule; class CSSNestedDeclarations; diff --git a/Libraries/LibWeb/idl_files.cmake b/Libraries/LibWeb/idl_files.cmake index 19c38b5e861..3c3c817b8b5 100644 --- a/Libraries/LibWeb/idl_files.cmake +++ b/Libraries/LibWeb/idl_files.cmake @@ -36,6 +36,7 @@ libweb_js_bindings(CSS/CSSKeywordValue) libweb_js_bindings(CSS/CSSLayerBlockRule) libweb_js_bindings(CSS/CSSLayerStatementRule) libweb_js_bindings(CSS/CSSMarginRule) +libweb_js_bindings(CSS/CSSMathValue) libweb_js_bindings(CSS/CSSMediaRule) libweb_js_bindings(CSS/CSS NAMESPACE) libweb_js_bindings(CSS/CSSNamespaceRule) diff --git a/Tests/LibWeb/Text/expected/all-window-properties.txt b/Tests/LibWeb/Text/expected/all-window-properties.txt index 55c7f739886..347062f2e92 100644 --- a/Tests/LibWeb/Text/expected/all-window-properties.txt +++ b/Tests/LibWeb/Text/expected/all-window-properties.txt @@ -49,6 +49,7 @@ CSSKeywordValue CSSLayerBlockRule CSSLayerStatementRule CSSMarginRule +CSSMathValue CSSMediaRule CSSNamespaceRule CSSNestedDeclarations