2025-01-27 10:39:00 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2022-2025, Sam Atkins <sam@ladybird.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <AK/Math.h>
|
|
|
|
|
#include <LibWeb/CSS/Number.h>
|
2025-11-05 12:48:23 +13:00
|
|
|
#include <LibWeb/CSS/Serialize.h>
|
2025-01-27 10:39:00 +00:00
|
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
|
2026-03-26 21:33:07 +13:00
|
|
|
i32 round_to_nearest_integer(double value)
|
2026-03-12 22:07:56 +13:00
|
|
|
{
|
|
|
|
|
// https://drafts.csswg.org/css-values-4/#css-round-to-the-nearest-integer
|
|
|
|
|
// Unless otherwise specified, in the CSS specifications rounding to the nearest integer requires rounding in
|
|
|
|
|
// the direction of +∞ when the fractional portion is exactly 0.5.
|
|
|
|
|
if (isnan(value))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (isinf(value)) {
|
|
|
|
|
if (value > 0)
|
2026-03-26 21:33:07 +13:00
|
|
|
return AK::NumericLimits<i32>::max();
|
2026-03-12 22:07:56 +13:00
|
|
|
|
2026-03-26 21:33:07 +13:00
|
|
|
return AK::NumericLimits<i32>::min();
|
2026-03-12 22:07:56 +13:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 21:33:07 +13:00
|
|
|
return AK::clamp_to<i32>(floor(value + 0.5));
|
2026-03-12 22:07:56 +13:00
|
|
|
}
|
|
|
|
|
|
2026-01-08 14:55:48 +00:00
|
|
|
void Number::serialize(StringBuilder& builder, SerializationMode) const
|
2025-01-27 10:39:00 +00:00
|
|
|
{
|
2026-01-08 14:55:48 +00:00
|
|
|
if (m_type == Type::IntegerWithExplicitSign) {
|
|
|
|
|
builder.appendff("{:+}", m_value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (m_value == AK::Infinity<double>) {
|
|
|
|
|
builder.append("infinity"sv);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (m_value == -AK::Infinity<double>) {
|
|
|
|
|
builder.append("-infinity"sv);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (isnan(m_value)) {
|
|
|
|
|
builder.append("NaN"sv);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
serialize_a_number(builder, m_value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String Number::to_string(SerializationMode mode) const
|
|
|
|
|
{
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
serialize(builder, mode);
|
|
|
|
|
return builder.to_string_without_validation();
|
2025-01-27 10:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|