LibWeb/CSS: Implement CSSMathNegate

This commit is contained in:
Sam Atkins 2025-08-19 16:03:09 +01:00 committed by Andreas Kling
commit f2ec04d20d
Notes: github-actions[bot] 2025-08-29 09:59:42 +00:00
8 changed files with 144 additions and 3 deletions

View file

@ -116,6 +116,7 @@ set(SOURCES
CSS/CSSLayerBlockRule.cpp CSS/CSSLayerBlockRule.cpp
CSS/CSSLayerStatementRule.cpp CSS/CSSLayerStatementRule.cpp
CSS/CSSMarginRule.cpp CSS/CSSMarginRule.cpp
CSS/CSSMathNegate.cpp
CSS/CSSMathProduct.cpp CSS/CSSMathProduct.cpp
CSS/CSSMathSum.cpp CSS/CSSMathSum.cpp
CSS/CSSMathValue.cpp CSS/CSSMathValue.cpp

View file

@ -0,0 +1,92 @@
/*
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "CSSMathNegate.h"
#include <LibWeb/Bindings/CSSMathNegatePrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
namespace Web::CSS {
GC_DEFINE_ALLOCATOR(CSSMathNegate);
GC::Ref<CSSMathNegate> CSSMathNegate::create(JS::Realm& realm, NumericType type, GC::Ref<CSSNumericValue> values)
{
return realm.create<CSSMathNegate>(realm, move(type), move(values));
}
// https://drafts.css-houdini.org/css-typed-om-1/#dom-cssmathnegate-cssmathnegate
WebIDL::ExceptionOr<GC::Ref<CSSMathNegate>> CSSMathNegate::construct_impl(JS::Realm& realm, CSSNumberish value)
{
// The CSSMathNegate(arg) constructor must, when called, perform the following steps:
// 1. Replace arg with the result of rectifying a numberish value for arg.
auto converted_value = rectify_a_numberish_value(realm, value);
// 2. Return a new CSSMathNegate whose value internal slot is set to arg.
return CSSMathNegate::create(realm, converted_value->type(), converted_value);
}
CSSMathNegate::CSSMathNegate(JS::Realm& realm, NumericType type, GC::Ref<CSSNumericValue> values)
: CSSMathValue(realm, Bindings::CSSMathOperator::Negate, move(type))
, m_value(move(values))
{
}
CSSMathNegate::~CSSMathNegate() = default;
void CSSMathNegate::initialize(JS::Realm& realm)
{
WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSMathNegate);
Base::initialize(realm);
}
void CSSMathNegate::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_value);
}
// https://drafts.css-houdini.org/css-typed-om-1/#serialize-a-cssmathvalue
String CSSMathNegate::serialize_math_value(Nested nested, Parens parens) const
{
// NB: Only steps 1 and 4 apply here.
// 1. Let s initially be the empty string.
StringBuilder s;
// 4. Otherwise, if this is a CSSMathNegate:
{
// 1. If paren-less is true, continue to the next step; otherwise, if nested is true, append "(" to s;
// otherwise, append "calc(" to s.
if (parens == Parens::With) {
if (nested == Nested::Yes) {
s.append("("sv);
} else {
s.append("calc("sv);
}
}
// 2. Append "-" to s.
s.append("-"sv);
// 3. Serialize thiss value internal slot with nested set to true, and append the result to s.
s.append(m_value->to_string({ .nested = true }));
// 4. If paren-less is false, append ")" to s,
if (parens == Parens::With)
s.append(")"sv);
// 5. Return s.
return s.to_string_without_validation();
}
}
// https://drafts.css-houdini.org/css-typed-om-1/#dom-cssmathnegate-value
GC::Ref<CSSNumericValue> CSSMathNegate::value() const
{
return m_value;
}
}

View file

@ -0,0 +1,36 @@
/*
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/CSS/CSSMathValue.h>
namespace Web::CSS {
// https://drafts.css-houdini.org/css-typed-om-1/#cssmathnegate
class CSSMathNegate : public CSSMathValue {
WEB_PLATFORM_OBJECT(CSSMathNegate, CSSMathValue);
GC_DECLARE_ALLOCATOR(CSSMathNegate);
public:
[[nodiscard]] static GC::Ref<CSSMathNegate> create(JS::Realm&, NumericType, GC::Ref<CSSNumericValue>);
static WebIDL::ExceptionOr<GC::Ref<CSSMathNegate>> construct_impl(JS::Realm&, CSSNumberish);
virtual ~CSSMathNegate() override;
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Visitor&) override;
GC::Ref<CSSNumericValue> value() const;
virtual String serialize_math_value(Nested, Parens) const override;
private:
CSSMathNegate(JS::Realm&, NumericType, GC::Ref<CSSNumericValue>);
GC::Ref<CSSNumericValue> m_value;
};
}

View file

@ -0,0 +1,9 @@
#import <CSS/CSSMathValue.idl>
#import <CSS/CSSNumericValue.idl>
// https://drafts.css-houdini.org/css-typed-om-1/#cssmathnegate
[Exposed=(Window, Worker, PaintWorklet, LayoutWorklet)]
interface CSSMathNegate : CSSMathValue {
constructor(CSSNumberish arg);
readonly attribute CSSNumericValue value;
};

View file

@ -7,6 +7,7 @@
#include "CSSMathSum.h" #include "CSSMathSum.h"
#include <LibWeb/Bindings/CSSMathSumPrototype.h> #include <LibWeb/Bindings/CSSMathSumPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h> #include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/CSS/CSSMathNegate.h>
#include <LibWeb/CSS/CSSNumericArray.h> #include <LibWeb/CSS/CSSNumericArray.h>
#include <LibWeb/WebIDL/DOMException.h> #include <LibWeb/WebIDL/DOMException.h>
#include <LibWeb/WebIDL/ExceptionOr.h> #include <LibWeb/WebIDL/ExceptionOr.h>
@ -109,10 +110,9 @@ String CSSMathSum::serialize_math_value(Nested nested, Parens parens) const
// 1. If arg is a CSSMathNegate, append " - " to s, then serialize args value internal slot with nested // 1. If arg is a CSSMathNegate, append " - " to s, then serialize args value internal slot with nested
// set to true, and append the result to s. // set to true, and append the result to s.
// FIXME: Detect CSSMathNegate once that's implemented. if (auto* negate = as_if<CSSMathNegate>(*arg)) {
if (false) {
s.append(" - "sv); s.append(" - "sv);
s.append(arg->to_string({ .nested = true })); s.append(negate->value()->to_string({ .nested = true }));
} }
// 2. Otherwise, append " + " to s, then serialize arg with nested set to true, and append the result to s. // 2. Otherwise, append " + " to s, then serialize arg with nested set to true, and append the result to s.

View file

@ -242,6 +242,7 @@ class CSSKeywordValue;
class CSSLayerBlockRule; class CSSLayerBlockRule;
class CSSLayerStatementRule; class CSSLayerStatementRule;
class CSSMarginRule; class CSSMarginRule;
class CSSMathNegate;
class CSSMathProduct; class CSSMathProduct;
class CSSMathSum; class CSSMathSum;
class CSSMathValue; class CSSMathValue;

View file

@ -36,6 +36,7 @@ libweb_js_bindings(CSS/CSSKeywordValue)
libweb_js_bindings(CSS/CSSLayerBlockRule) libweb_js_bindings(CSS/CSSLayerBlockRule)
libweb_js_bindings(CSS/CSSLayerStatementRule) libweb_js_bindings(CSS/CSSLayerStatementRule)
libweb_js_bindings(CSS/CSSMarginRule) libweb_js_bindings(CSS/CSSMarginRule)
libweb_js_bindings(CSS/CSSMathNegate)
libweb_js_bindings(CSS/CSSMathProduct) libweb_js_bindings(CSS/CSSMathProduct)
libweb_js_bindings(CSS/CSSMathSum) libweb_js_bindings(CSS/CSSMathSum)
libweb_js_bindings(CSS/CSSMathValue) libweb_js_bindings(CSS/CSSMathValue)

View file

@ -49,6 +49,7 @@ CSSKeywordValue
CSSLayerBlockRule CSSLayerBlockRule
CSSLayerStatementRule CSSLayerStatementRule
CSSMarginRule CSSMarginRule
CSSMathNegate
CSSMathProduct CSSMathProduct
CSSMathSum CSSMathSum
CSSMathValue CSSMathValue