From 46b9497a66c01f1c685cfaef21e634abfaac8df3 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Mon, 27 Jan 2025 10:39:00 +0000 Subject: [PATCH] LibWeb/CSS: Make non-finite Numbers serialize as themselves We're required to serialize NaN, infinity, and -infinity as their keyword names, even after they've been converted to Numbers. --- Libraries/LibWeb/CMakeLists.txt | 1 + Libraries/LibWeb/CSS/Number.cpp | 25 +++++++++++++++++++ Libraries/LibWeb/CSS/Number.h | 9 ++----- .../CSS/StyleValues/CalculatedStyleValue.cpp | 5 ++-- .../Userland/Libraries/LibWeb/CSS/BUILD.gn | 1 + 5 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 Libraries/LibWeb/CSS/Number.cpp diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index 44234f843fe..582f6d4fc72 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -89,6 +89,7 @@ set(SOURCES CSS/MediaQuery.cpp CSS/MediaQueryList.cpp CSS/MediaQueryListEvent.cpp + CSS/Number.cpp CSS/Parser/ComponentValue.cpp CSS/Parser/GradientParsing.cpp CSS/Parser/Helpers.cpp diff --git a/Libraries/LibWeb/CSS/Number.cpp b/Libraries/LibWeb/CSS/Number.cpp new file mode 100644 index 00000000000..baed27419c5 --- /dev/null +++ b/Libraries/LibWeb/CSS/Number.cpp @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2022-2025, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace Web::CSS { + +String Number::to_string() const +{ + if (m_type == Type::IntegerWithExplicitSign) + return MUST(String::formatted("{:+}", m_value)); + if (m_value == AK::Infinity) + return "infinity"_string; + if (m_value == -AK::Infinity) + return "-infinity"_string; + if (isnan(m_value)) + return "NaN"_string; + return String::number(m_value); +} + +} diff --git a/Libraries/LibWeb/CSS/Number.h b/Libraries/LibWeb/CSS/Number.h index d5e728efc1f..74db9e79d68 100644 --- a/Libraries/LibWeb/CSS/Number.h +++ b/Libraries/LibWeb/CSS/Number.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023, Sam Atkins + * Copyright (c) 2022-2025, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -70,12 +70,7 @@ public: return { Type::Number, m_value / other.m_value }; } - String to_string() const - { - if (m_type == Type::IntegerWithExplicitSign) - return MUST(String::formatted("{:+}", m_value)); - return String::number(m_value); - } + String to_string() const; bool operator==(Number const& other) const { diff --git a/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp b/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp index 2ea4c99495a..e02a3e05c51 100644 --- a/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp +++ b/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp @@ -871,11 +871,10 @@ CalculatedStyleValue::CalculationResult ConstantCalculationNode::resolve(Calcula return { AK::E, CSSNumericType {} }; case ConstantType::Pi: return { AK::Pi, CSSNumericType {} }; - // FIXME: We need to keep track of Infinity and NaN across all nodes, since they require special handling. case ConstantType::Infinity: - return { NumericLimits::max(), CSSNumericType {} }; + return { AK::Infinity, CSSNumericType {} }; case ConstantType::MinusInfinity: - return { NumericLimits::lowest(), CSSNumericType {} }; + return { -AK::Infinity, CSSNumericType {} }; case ConstantType::NaN: return { AK::NaN, CSSNumericType {} }; } diff --git a/Meta/gn/secondary/Userland/Libraries/LibWeb/CSS/BUILD.gn b/Meta/gn/secondary/Userland/Libraries/LibWeb/CSS/BUILD.gn index 246d1e57f48..b539f388a50 100644 --- a/Meta/gn/secondary/Userland/Libraries/LibWeb/CSS/BUILD.gn +++ b/Meta/gn/secondary/Userland/Libraries/LibWeb/CSS/BUILD.gn @@ -50,6 +50,7 @@ source_set("CSS") { "MediaQuery.cpp", "MediaQueryList.cpp", "MediaQueryListEvent.cpp", + "Number.cpp", "ParsedFontFace.cpp", "PreferredColorScheme.cpp", "PreferredContrast.cpp",