LibWeb/CSS: Implement CSSMathValue

This is a base class for the various math functions, so it's not used
directly anywhere.
This commit is contained in:
Sam Atkins 2025-08-19 14:24:39 +01:00 committed by Andreas Kling
commit 6c8876cdb8
Notes: github-actions[bot] 2025-08-29 10:00:11 +00:00
8 changed files with 96 additions and 3 deletions

View file

@ -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

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "CSSMathValue.h"
#include <LibWeb/Bindings/Intrinsics.h>
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);
}
}

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/Bindings/CSSMathValuePrototype.h>
#include <LibWeb/CSS/CSSNumericValue.h>
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;
};
}

View file

@ -0,0 +1,18 @@
#import <CSS/CSSNumericValue.idl>
// 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",
};

View file

@ -7,6 +7,7 @@
#include "CSSNumericValue.h"
#include <LibWeb/Bindings/CSSNumericValuePrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/CSS/CSSMathValue.h>
#include <LibWeb/CSS/CSSUnitValue.h>
#include <LibWeb/CSS/NumericType.h>
#include <LibWeb/CSS/Serialize.h>
@ -107,9 +108,11 @@ String CSSNumericValue::to_string(SerializationParams const& params) const
if (auto* unit_value = as_if<CSSUnitValue>(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<CSSMathValue>(*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

View file

@ -242,6 +242,7 @@ class CSSKeywordValue;
class CSSLayerBlockRule;
class CSSLayerStatementRule;
class CSSMarginRule;
class CSSMathValue;
class CSSMediaRule;
class CSSNamespaceRule;
class CSSNestedDeclarations;

View file

@ -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)

View file

@ -49,6 +49,7 @@ CSSKeywordValue
CSSLayerBlockRule
CSSLayerStatementRule
CSSMarginRule
CSSMathValue
CSSMediaRule
CSSNamespaceRule
CSSNestedDeclarations