mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-10-04 23:29:52 +00:00
LibWeb/CSS: Implement CSSMathInvert
This is almost identical to CSSMathNegate.
This commit is contained in:
parent
f2ec04d20d
commit
8efd0639cd
Notes:
github-actions[bot]
2025-08-29 09:59:33 +00:00
Author: https://github.com/AtkinsSJ
Commit: 8efd0639cd
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5955
Reviewed-by: https://github.com/trflynn89
8 changed files with 147 additions and 3 deletions
95
Libraries/LibWeb/CSS/CSSMathInvert.cpp
Normal file
95
Libraries/LibWeb/CSS/CSSMathInvert.cpp
Normal file
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "CSSMathInvert.h"
|
||||
#include <LibWeb/Bindings/CSSMathInvertPrototype.h>
|
||||
#include <LibWeb/Bindings/Intrinsics.h>
|
||||
#include <LibWeb/WebIDL/ExceptionOr.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
GC_DEFINE_ALLOCATOR(CSSMathInvert);
|
||||
|
||||
GC::Ref<CSSMathInvert> CSSMathInvert::create(JS::Realm& realm, NumericType type, GC::Ref<CSSNumericValue> values)
|
||||
{
|
||||
return realm.create<CSSMathInvert>(realm, move(type), move(values));
|
||||
}
|
||||
|
||||
// https://drafts.css-houdini.org/css-typed-om-1/#dom-cssmathinvert-cssmathinvert
|
||||
WebIDL::ExceptionOr<GC::Ref<CSSMathInvert>> CSSMathInvert::construct_impl(JS::Realm& realm, CSSNumberish value)
|
||||
{
|
||||
// The CSSMathInvert(arg) constructor is defined identically to the above, except that in the last step it returns
|
||||
// a new CSSMathInvert object.
|
||||
// NB: So, the steps below are a modification of the CSSMathNegate 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 CSSMathInvert whose value internal slot is set to arg.
|
||||
return CSSMathInvert::create(realm, converted_value->type().inverted(), converted_value);
|
||||
}
|
||||
|
||||
CSSMathInvert::CSSMathInvert(JS::Realm& realm, NumericType type, GC::Ref<CSSNumericValue> values)
|
||||
: CSSMathValue(realm, Bindings::CSSMathOperator::Invert, move(type))
|
||||
, m_value(move(values))
|
||||
{
|
||||
}
|
||||
|
||||
CSSMathInvert::~CSSMathInvert() = default;
|
||||
|
||||
void CSSMathInvert::initialize(JS::Realm& realm)
|
||||
{
|
||||
WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSMathInvert);
|
||||
Base::initialize(realm);
|
||||
}
|
||||
|
||||
void CSSMathInvert::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 CSSMathInvert::serialize_math_value(Nested nested, Parens parens) const
|
||||
{
|
||||
// NB: Only steps 1 and 6 apply here.
|
||||
// 1. Let s initially be the empty string.
|
||||
StringBuilder s;
|
||||
|
||||
// 6. Otherwise, if this is a CSSMathInvert:
|
||||
{
|
||||
// 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 "1 / " to s.
|
||||
s.append("1 / "sv);
|
||||
|
||||
// 3. Serialize this’s 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-cssmathinvert-value
|
||||
GC::Ref<CSSNumericValue> CSSMathInvert::value() const
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue