diff --git a/Libraries/LibWeb/CSS/CSSNumericValue.cpp b/Libraries/LibWeb/CSS/CSSNumericValue.cpp index 9ddbd347a57..5f240527f3f 100644 --- a/Libraries/LibWeb/CSS/CSSNumericValue.cpp +++ b/Libraries/LibWeb/CSS/CSSNumericValue.cpp @@ -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 rectify_a_numberish_value(JS::Realm& realm, CSSNumberish const& numberish, Optional 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 const& num) -> GC::Ref { + 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 { + return CSSUnitValue::create(realm, num, unit.value_or("number"_fly_string)); + }); +} + } diff --git a/Libraries/LibWeb/CSS/CSSNumericValue.h b/Libraries/LibWeb/CSS/CSSNumericValue.h index edefc8ca4fa..79c6fb2feaf 100644 --- a/Libraries/LibWeb/CSS/CSSNumericValue.h +++ b/Libraries/LibWeb/CSS/CSSNumericValue.h @@ -56,4 +56,6 @@ protected: // https://drafts.css-houdini.org/css-typed-om-1/#typedefdef-cssnumberish using CSSNumberish = Variant>; +GC::Ref rectify_a_numberish_value(JS::Realm&, CSSNumberish const&, Optional unit = {}); + }