LibWeb/CSS: Implement rectify_a_numberish_value()

This commit is contained in:
Sam Atkins 2025-08-21 12:44:57 +01:00 committed by Andreas Kling
commit 46b55f0f46
Notes: github-actions[bot] 2025-08-29 10:00:31 +00:00
2 changed files with 18 additions and 0 deletions

View file

@ -112,4 +112,20 @@ String CSSNumericValue::to_string(SerializationParams const& params) const
return {};
}
// https://drafts.css-houdini.org/css-typed-om-1/#rectify-a-numberish-value
GC::Ref<CSSNumericValue> rectify_a_numberish_value(JS::Realm& realm, CSSNumberish const& numberish, Optional<FlyString> unit)
{
// To rectify a numberish value num, optionally to a given unit unit (defaulting to "number"), perform the following steps:
return numberish.visit(
// 1. If num is a CSSNumericValue, return num.
[](GC::Root<CSSNumericValue> const& num) -> GC::Ref<CSSNumericValue> {
return GC::Ref { *num };
},
// 2. If num is a double, return a new CSSUnitValue with its value internal slot set to num and its unit
// internal slot set to unit.
[&realm, &unit](double num) -> GC::Ref<CSSNumericValue> {
return CSSUnitValue::create(realm, num, unit.value_or("number"_fly_string));
});
}
}

View file

@ -56,4 +56,6 @@ protected:
// https://drafts.css-houdini.org/css-typed-om-1/#typedefdef-cssnumberish
using CSSNumberish = Variant<double, GC::Root<CSSNumericValue>>;
GC::Ref<CSSNumericValue> rectify_a_numberish_value(JS::Realm&, CSSNumberish const&, Optional<FlyString> unit = {});
}