ladybird/Libraries/LibWeb/CSS/CSSMathNegate.cpp
Andreas Kling 26b0801a78 LibWeb: Return Utf16String from CSS Typed OM serialization
Make CSS Typed OM to_string() implementations produce Utf16String
instead of building UTF-8 strings and converting at the JS binding
boundary. Use Utf16StringBuilder for numeric, math, and transform
serialization paths that already feed CSS Typed OM stringification.

Keep the older CSSOM StyleValue serializer on StringBuilder for now, and
convert only where Typed OM source values or parser re-entry still need
the existing UTF-8 representation.
2026-06-22 16:10:40 +02:00

128 lines
4.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "CSSMathNegate.h"
#include <LibWeb/Bindings/CSSMathNegate.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.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
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
void CSSMathNegate::serialize_math_value(Utf16StringBuilder& s, Nested nested, Parens parens) const
{
// NB: Only steps 1 and 4 apply here.
// 1. Let s initially be the empty string.
// 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_ascii('(');
} else {
s.append_ascii("calc("sv);
}
}
// 2. Append "-" to s.
s.append_ascii('-');
// 3. Serialize thiss value internal slot with nested set to true, and append the result to s.
m_value->serialize(s, { .nested = true });
// 4. If paren-less is false, append ")" to s,
if (parens == Parens::With)
s.append_ascii(')');
// 5. Return s.
}
}
// https://drafts.css-houdini.org/css-typed-om-1/#dom-cssmathnegate-value
GC::Ref<CSSNumericValue> CSSMathNegate::value() const
{
return m_value;
}
// https://drafts.css-houdini.org/css-typed-om-1/#equal-numeric-value
bool CSSMathNegate::is_equal_numeric_value(GC::Ref<CSSNumericValue> other) const
{
// NB: Only steps 1, 4 and 5 are relevant.
// 1. If value1 and value2 are not members of the same interface, return false.
auto* other_negate = as_if<CSSMathNegate>(*other);
if (!other_negate)
return false;
// 4. Assert: value1 and value2 are both CSSMathNegates or CSSMathInverts.
// 5. Return whether value1s value and value2s value are equal numeric values.
return m_value->is_equal_numeric_value(other_negate->m_value);
}
// https://drafts.css-houdini.org/css-typed-om-1/#create-a-sum-value
Optional<SumValue> CSSMathNegate::create_a_sum_value() const
{
// 1. Let values be the result of creating a sum value from thiss value internal slot.
auto values = m_value->create_a_sum_value();
// 2. If values is failure, return failure.
if (!values.has_value())
return {};
// 3. Negate the value of each item of values.
for (auto& value : *values)
value.value = -value.value;
// 4. Return values.
return values;
}
WebIDL::ExceptionOr<NonnullRefPtr<CalculationNode const>> CSSMathNegate::create_calculation_node(CalculationContext const& context) const
{
return NegateCalculationNode::create(TRY(m_value->create_calculation_node(context)));
}
}