LibWeb/CSS: Separate IntegerSV and NumberSV from CSSUnitValue

This inheritance exists for typed-om classes, but StyleValues aren't
typed-om.

Somehow this makes our z-index interpolation slightly more correct. 🎉
This commit is contained in:
Sam Atkins 2025-08-08 10:55:30 +01:00 committed by Tim Ledbetter
commit 7157d19f56
Notes: github-actions[bot] 2025-08-08 14:20:36 +00:00
10 changed files with 23 additions and 25 deletions

View file

@ -1494,7 +1494,7 @@ Optional<HashMap<FlyString, IntegerOrCalculated>> ComputedProperties::font_featu
auto const& feature_tag = tag_value->as_open_type_tagged(); auto const& feature_tag = tag_value->as_open_type_tagged();
if (feature_tag.value()->is_integer()) { if (feature_tag.value()->is_integer()) {
result.set(feature_tag.tag(), feature_tag.value()->as_integer().value()); result.set(feature_tag.tag(), feature_tag.value()->as_integer().integer());
} else { } else {
VERIFY(feature_tag.value()->is_calculated()); VERIFY(feature_tag.value()->is_calculated());
result.set(feature_tag.tag(), IntegerOrCalculated { feature_tag.value()->as_calculated() }); result.set(feature_tag.tag(), IntegerOrCalculated { feature_tag.value()->as_calculated() });
@ -1521,7 +1521,7 @@ Optional<HashMap<FlyString, NumberOrCalculated>> ComputedProperties::font_variat
auto const& axis_tag = tag_value->as_open_type_tagged(); auto const& axis_tag = tag_value->as_open_type_tagged();
if (axis_tag.value()->is_number()) { if (axis_tag.value()->is_number()) {
result.set(axis_tag.tag(), axis_tag.value()->as_number().value()); result.set(axis_tag.tag(), axis_tag.value()->as_number().number());
} else { } else {
VERIFY(axis_tag.value()->is_calculated()); VERIFY(axis_tag.value()->is_calculated());
result.set(axis_tag.tag(), NumberOrCalculated { axis_tag.value()->as_calculated() }); result.set(axis_tag.tag(), NumberOrCalculated { axis_tag.value()->as_calculated() });

View file

@ -1151,8 +1151,8 @@ static RefPtr<StyleValue const> interpolate_value_impl(DOM::Element& element, Ca
// https://drafts.csswg.org/css-values/#combine-integers // https://drafts.csswg.org/css-values/#combine-integers
// Interpolation of <integer> is defined as Vresult = round((1 - p) × VA + p × VB); // Interpolation of <integer> is defined as Vresult = round((1 - p) × VA + p × VB);
// that is, interpolation happens in the real number space as for <number>s, and the result is converted to an <integer> by rounding to the nearest integer. // that is, interpolation happens in the real number space as for <number>s, and the result is converted to an <integer> by rounding to the nearest integer.
auto interpolated_value = interpolate_raw(from.as_integer().value(), to.as_integer().value(), delta); auto interpolated_value = interpolate_raw(from.as_integer().integer(), to.as_integer().integer(), delta);
return IntegerStyleValue::create(round_to<i64>(interpolated_value)); return IntegerStyleValue::create(interpolated_value);
} }
case StyleValue::Type::Length: { case StyleValue::Type::Length: {
auto const& from_length = from.as_length().length(); auto const& from_length = from.as_length().length();

View file

@ -2180,7 +2180,7 @@ RefPtr<StyleValue const> Parser::parse_stroke_dasharray_value(TokenStream<Compon
// A <dasharray> is a list of comma and/or white space separated <number> or <length-percentage> values. A <number> value represents a value in user units. // A <dasharray> is a list of comma and/or white space separated <number> or <length-percentage> values. A <number> value represents a value in user units.
auto value = parse_number_value(tokens); auto value = parse_number_value(tokens);
if (value && value->is_number() && value->as_number().value() < 0) if (value && value->is_number() && value->as_number().number() < 0)
return {}; return {};
if (value) { if (value) {
@ -2836,7 +2836,7 @@ RefPtr<StyleValue const> Parser::parse_font_feature_settings_value(TokenStream<C
RefPtr<StyleValue const> value; RefPtr<StyleValue const> value;
if (tag_tokens.has_next_token()) { if (tag_tokens.has_next_token()) {
if (auto integer = parse_integer_value(tag_tokens)) { if (auto integer = parse_integer_value(tag_tokens)) {
if (integer->is_integer() && integer->as_integer().value() < 0) if (integer->is_integer() && integer->as_integer().integer() < 0)
return nullptr; return nullptr;
value = integer; value = integer;
} else { } else {

View file

@ -365,7 +365,7 @@ Optional<Ratio> Parser::parse_ratio(TokenStream<ComponentValue>& tokens)
if (!maybe_calc) if (!maybe_calc)
return {}; return {};
if (maybe_calc->is_number()) if (maybe_calc->is_number())
return maybe_calc->as_number().value(); return maybe_calc->as_number().number();
if (!maybe_calc->is_calculated() || !maybe_calc->as_calculated().resolves_to_number()) if (!maybe_calc->is_calculated() || !maybe_calc->as_calculated().resolves_to_number())
return {}; return {};
if (auto resolved_number = maybe_calc->as_calculated().resolve_number_deprecated({}); resolved_number.has_value() && resolved_number.value() >= 0) { if (auto resolved_number = maybe_calc->as_calculated().resolve_number_deprecated({}); resolved_number.has_value() && resolved_number.value() >= 0) {

View file

@ -130,11 +130,11 @@ String ColorFunctionStyleValue::to_string(SerializationMode mode) const
bool const is_alpha_required = [&]() { bool const is_alpha_required = [&]() {
if (alpha->is_number()) if (alpha->is_number())
return alpha->as_number().value() < 1; return alpha->as_number().number() < 1;
return true; return true;
}(); }();
if (alpha->is_number() && alpha->as_number().value() < 0) if (alpha->is_number() && alpha->as_number().number() < 0)
alpha = NumberStyleValue::create(0); alpha = NumberStyleValue::create(0);
if (is_alpha_required) { if (is_alpha_required) {

View file

@ -5,6 +5,7 @@
*/ */
#include "IntegerStyleValue.h" #include "IntegerStyleValue.h"
#include <LibWeb/CSS/Parser/ComponentValue.h>
namespace Web::CSS { namespace Web::CSS {
@ -15,7 +16,7 @@ String IntegerStyleValue::to_string(SerializationMode) const
Vector<Parser::ComponentValue> IntegerStyleValue::tokenize() const Vector<Parser::ComponentValue> IntegerStyleValue::tokenize() const
{ {
return { Parser::Token::create_number(Number { Number::Type::Integer, value() }) }; return { Parser::Token::create_number(Number { Number::Type::Integer, static_cast<double>(m_value) }) };
} }
} }

View file

@ -6,11 +6,11 @@
#pragma once #pragma once
#include <LibWeb/CSS/StyleValues/CSSUnitValue.h> #include <LibWeb/CSS/StyleValues/StyleValue.h>
namespace Web::CSS { namespace Web::CSS {
class IntegerStyleValue final : public CSSUnitValue { class IntegerStyleValue final : public StyleValue {
public: public:
static ValueComparingNonnullRefPtr<IntegerStyleValue const> create(i64 value) static ValueComparingNonnullRefPtr<IntegerStyleValue const> create(i64 value)
{ {
@ -18,8 +18,6 @@ public:
} }
i64 integer() const { return m_value; } i64 integer() const { return m_value; }
virtual double value() const override { return m_value; }
virtual StringView unit() const override { return "number"sv; }
virtual String to_string(SerializationMode) const override; virtual String to_string(SerializationMode) const override;
virtual Vector<Parser::ComponentValue> tokenize() const override; virtual Vector<Parser::ComponentValue> tokenize() const override;
@ -34,7 +32,7 @@ public:
private: private:
explicit IntegerStyleValue(i64 value) explicit IntegerStyleValue(i64 value)
: CSSUnitValue(Type::Integer) : StyleValue(Type::Integer)
, m_value(value) , m_value(value)
{ {
} }

View file

@ -8,6 +8,7 @@
*/ */
#include "NumberStyleValue.h" #include "NumberStyleValue.h"
#include <LibWeb/CSS/Parser/ComponentValue.h>
namespace Web::CSS { namespace Web::CSS {

View file

@ -9,11 +9,11 @@
#pragma once #pragma once
#include <LibWeb/CSS/StyleValues/CSSUnitValue.h> #include <LibWeb/CSS/StyleValues/StyleValue.h>
namespace Web::CSS { namespace Web::CSS {
class NumberStyleValue final : public CSSUnitValue { class NumberStyleValue final : public StyleValue {
public: public:
static ValueComparingNonnullRefPtr<NumberStyleValue const> create(double value) static ValueComparingNonnullRefPtr<NumberStyleValue const> create(double value)
{ {
@ -21,8 +21,6 @@ public:
} }
double number() const { return m_value; } double number() const { return m_value; }
virtual double value() const override { return m_value; }
virtual StringView unit() const override { return "number"sv; }
virtual String to_string(SerializationMode) const override; virtual String to_string(SerializationMode) const override;
virtual Vector<Parser::ComponentValue> tokenize() const override; virtual Vector<Parser::ComponentValue> tokenize() const override;
@ -37,7 +35,7 @@ public:
private: private:
explicit NumberStyleValue(double value) explicit NumberStyleValue(double value)
: CSSUnitValue(Type::Number) : StyleValue(Type::Number)
, m_value(value) , m_value(value)
{ {
} }

View file

@ -2,14 +2,14 @@ Harness status: OK
Found 250 tests Found 250 tests
212 Pass 215 Pass
38 Fail 35 Fail
Pass CSS Transitions: property <z-index> from neutral to [5] at (-0.3) should be [-4] Pass CSS Transitions: property <z-index> from neutral to [5] at (-0.3) should be [-4]
Pass CSS Transitions: property <z-index> from neutral to [5] at (0) should be [-2] Pass CSS Transitions: property <z-index> from neutral to [5] at (0) should be [-2]
Pass CSS Transitions: property <z-index> from neutral to [5] at (0.3) should be [0] Pass CSS Transitions: property <z-index> from neutral to [5] at (0.3) should be [0]
Pass CSS Transitions: property <z-index> from neutral to [5] at (0.6) should be [2] Pass CSS Transitions: property <z-index> from neutral to [5] at (0.6) should be [2]
Pass CSS Transitions: property <z-index> from neutral to [5] at (1) should be [5] Pass CSS Transitions: property <z-index> from neutral to [5] at (1) should be [5]
Fail CSS Transitions: property <z-index> from neutral to [5] at (1.5) should be [9] Pass CSS Transitions: property <z-index> from neutral to [5] at (1.5) should be [9]
Fail CSS Transitions with transition: all: property <z-index> from neutral to [5] at (-0.3) should be [-4] Fail CSS Transitions with transition: all: property <z-index> from neutral to [5] at (-0.3) should be [-4]
Fail CSS Transitions with transition: all: property <z-index> from neutral to [5] at (0) should be [-2] Fail CSS Transitions with transition: all: property <z-index> from neutral to [5] at (0) should be [-2]
Fail CSS Transitions with transition: all: property <z-index> from neutral to [5] at (0.3) should be [0] Fail CSS Transitions with transition: all: property <z-index> from neutral to [5] at (0.3) should be [0]
@ -21,13 +21,13 @@ Pass CSS Animations: property <z-index> from neutral to [5] at (0) should be [-2
Pass CSS Animations: property <z-index> from neutral to [5] at (0.3) should be [0] Pass CSS Animations: property <z-index> from neutral to [5] at (0.3) should be [0]
Pass CSS Animations: property <z-index> from neutral to [5] at (0.6) should be [2] Pass CSS Animations: property <z-index> from neutral to [5] at (0.6) should be [2]
Pass CSS Animations: property <z-index> from neutral to [5] at (1) should be [5] Pass CSS Animations: property <z-index> from neutral to [5] at (1) should be [5]
Fail CSS Animations: property <z-index> from neutral to [5] at (1.5) should be [9] Pass CSS Animations: property <z-index> from neutral to [5] at (1.5) should be [9]
Pass Web Animations: property <z-index> from neutral to [5] at (-0.3) should be [-4] Pass Web Animations: property <z-index> from neutral to [5] at (-0.3) should be [-4]
Pass Web Animations: property <z-index> from neutral to [5] at (0) should be [-2] Pass Web Animations: property <z-index> from neutral to [5] at (0) should be [-2]
Pass Web Animations: property <z-index> from neutral to [5] at (0.3) should be [0] Pass Web Animations: property <z-index> from neutral to [5] at (0.3) should be [0]
Pass Web Animations: property <z-index> from neutral to [5] at (0.6) should be [2] Pass Web Animations: property <z-index> from neutral to [5] at (0.6) should be [2]
Pass Web Animations: property <z-index> from neutral to [5] at (1) should be [5] Pass Web Animations: property <z-index> from neutral to [5] at (1) should be [5]
Fail Web Animations: property <z-index> from neutral to [5] at (1.5) should be [9] Pass Web Animations: property <z-index> from neutral to [5] at (1.5) should be [9]
Pass CSS Transitions with transition-behavior:allow-discrete: property <z-index> from [initial] to [5] at (-0.3) should be [initial] Pass CSS Transitions with transition-behavior:allow-discrete: property <z-index> from [initial] to [5] at (-0.3) should be [initial]
Pass CSS Transitions with transition-behavior:allow-discrete: property <z-index> from [initial] to [5] at (0) should be [initial] Pass CSS Transitions with transition-behavior:allow-discrete: property <z-index> from [initial] to [5] at (0) should be [initial]
Pass CSS Transitions with transition-behavior:allow-discrete: property <z-index> from [initial] to [5] at (0.3) should be [initial] Pass CSS Transitions with transition-behavior:allow-discrete: property <z-index> from [initial] to [5] at (0.3) should be [initial]