LibWeb: Add generic int_from_style_value method

Reduces duplication in line with `number_from_style_value`,
`string_from_style_value` etc
This commit is contained in:
Callum Law
2026-02-04 19:34:33 +13:00
committed by Sam Atkins
parent 31158ef448
commit 32b9ff21df
Notes: github-actions[bot] 2026-02-23 11:24:15 +00:00
7 changed files with 27 additions and 49 deletions

View File

@@ -595,6 +595,7 @@ float ComputedProperties::flex_shrink() const
int ComputedProperties::order() const
{
auto const& value = property(PropertyID::Order);
// FIXME: Support calc()
if (!value.is_integer())
return 0;
return value.as_integer().integer();
@@ -1633,12 +1634,7 @@ HashMap<FlyString, u8> ComputedProperties::font_feature_settings() const
for (auto const& tag_value : feature_tags) {
auto const& feature_tag = tag_value->as_open_type_tagged();
if (feature_tag.value()->is_integer()) {
result.set(feature_tag.tag(), feature_tag.value()->as_integer().integer());
} else {
VERIFY(feature_tag.value()->is_calculated());
result.set(feature_tag.tag(), feature_tag.value()->as_calculated().resolve_integer({}).value());
}
result.set(feature_tag.tag(), int_from_style_value(feature_tag.value()));
}
return result;
}
@@ -2201,17 +2197,10 @@ Vector<CounterData> ComputedProperties::counter_data(PropertyID property_id) con
.is_reversed = counter.is_reversed,
.value = {},
};
if (counter.value) {
if (counter.value->is_integer()) {
data.value = AK::clamp_to<i32>(counter.value->as_integer().integer());
} else if (counter.value->is_calculated()) {
auto maybe_int = counter.value->as_calculated().resolve_integer({});
if (maybe_int.has_value())
data.value = AK::clamp_to<i32>(*maybe_int);
} else {
dbgln("Unimplemented type for {} integer value: '{}'", string_from_property_id(property_id), counter.value->to_string(SerializationMode::Normal));
}
}
if (counter.value)
data.value = AK::clamp_to<i32>(int_from_style_value(*counter.value));
result.append(move(data));
}
return result;

View File

@@ -317,16 +317,6 @@ EasingFunction EasingFunction::from_style_value(StyleValue const& style_value)
VERIFY_NOT_REACHED();
};
auto const resolve_integer = [](StyleValue const& style_value) {
if (style_value.is_integer())
return style_value.as_integer().integer();
if (style_value.is_calculated())
return style_value.as_calculated().resolve_integer({}).value();
VERIFY_NOT_REACHED();
};
if (style_value.is_easing()) {
return style_value.as_easing().function().visit(
[&](EasingStyleValue::Linear const& linear) -> EasingFunction {
@@ -358,7 +348,7 @@ EasingFunction EasingFunction::from_style_value(StyleValue const& style_value)
return CubicBezierEasingFunction { resolved_x1, resolved_y1, resolved_x2, resolved_y2, cubic_bezier.to_string(SerializationMode::Normal) };
},
[&](EasingStyleValue::Steps const& steps) -> EasingFunction {
return StepsEasingFunction { resolve_integer(steps.number_of_intervals), steps.position, steps.to_string(SerializationMode::ResolvedValue) };
return StepsEasingFunction { int_from_style_value(steps.number_of_intervals), steps.position, steps.to_string(SerializationMode::ResolvedValue) };
});
}

View File

@@ -157,15 +157,11 @@ ParsedFontFace ParsedFontFace::from_descriptors(CSSFontFaceDescriptors const& de
auto const& feature_tags = value->as_value_list().values();
OrderedHashMap<FlyString, i64> settings;
settings.ensure_capacity(feature_tags.size());
for (auto const& feature_tag : feature_tags) {
auto const& setting_value = feature_tag->as_open_type_tagged().value();
if (setting_value->is_integer()) {
settings.set(feature_tag->as_open_type_tagged().tag(), setting_value->as_integer().integer());
} else if (setting_value->is_calculated() && setting_value->as_calculated().resolves_to_number()) {
if (auto integer = setting_value->as_calculated().resolve_integer({}); integer.has_value()) {
settings.set(feature_tag->as_open_type_tagged().tag(), *integer);
}
}
for (auto const& feature_tag_style_value : feature_tags) {
auto const& feature_tag = feature_tag_style_value->as_open_type_tagged();
// FIXME: We should absolutize feature_tag.value() in case there are relative lengths
settings.set(feature_tag.tag(), int_from_style_value(feature_tag.value()));
}
font_feature_settings = move(settings);
}

View File

@@ -2804,16 +2804,6 @@ NonnullRefPtr<StyleValue const> StyleComputer::compute_math_depth(NonnullRefPtr<
? inheritance_parent->computed_properties()->math_style()
: InitialValues::math_style();
auto resolve_integer = [&](StyleValue const& integer_value) {
if (integer_value.is_integer())
return integer_value.as_integer().integer();
if (integer_value.is_calculated())
return integer_value.as_calculated().resolve_integer({}).value();
VERIFY_NOT_REACHED();
};
// The computed value of the math-depth value is determined as follows:
// - If the specified value of math-depth is auto-add and the inherited value of math-style is compact
// then the computed value of math-depth of the element is its inherited value plus one.
@@ -2823,12 +2813,12 @@ NonnullRefPtr<StyleValue const> StyleComputer::compute_math_depth(NonnullRefPtr<
// - If the specified value of math-depth is of the form add(<integer>) then the computed value of
// math-depth of the element is its inherited value plus the specified integer.
if (absolutized_value->is_add_function())
return IntegerStyleValue::create(inherited_math_depth + resolve_integer(*absolutized_value->as_add_function().value()));
return IntegerStyleValue::create(inherited_math_depth + int_from_style_value(absolutized_value->as_add_function().value()));
// - If the specified value of math-depth is of the form <integer> then the computed value of math-depth
// of the element is the specified integer.
if (absolutized_value->is_integer() || absolutized_value->is_calculated())
return IntegerStyleValue::create(resolve_integer(*absolutized_value));
return IntegerStyleValue::create(int_from_style_value(absolutized_value));
// - Otherwise, the computed value of math-depth of the element is the inherited one.
return IntegerStyleValue::create(inherited_math_depth);

View File

@@ -186,6 +186,17 @@ StyleValueVector StyleValue::subdivide_into_iterations(PropertyNameAndID const&)
return StyleValueVector { *this };
}
i64 int_from_style_value(NonnullRefPtr<StyleValue const> const& style_value)
{
if (style_value->is_integer())
return style_value->as_integer().integer();
if (style_value->is_calculated())
return style_value->as_calculated().resolve_integer({}).value();
VERIFY_NOT_REACHED();
}
double number_from_style_value(NonnullRefPtr<StyleValue const> const& style_value, Optional<double> percentage_basis)
{
if (style_value->is_number())

View File

@@ -204,6 +204,7 @@ struct StyleValueWithDefaultOperators : public StyleValue {
}
};
i64 int_from_style_value(NonnullRefPtr<StyleValue const> const& style_value);
double number_from_style_value(NonnullRefPtr<StyleValue const> const& style_value, Optional<double> percentage_basis);
FlyString const& string_from_style_value(NonnullRefPtr<StyleValue const> const& style_value);

View File

@@ -894,6 +894,7 @@ void NodeWithStyle::apply_style(CSS::ComputedProperties const& computed_style)
computed_values.set_text_anchor(computed_style.text_anchor());
// FIXME: Support calc()
if (auto const& column_count = computed_style.property(CSS::PropertyID::ColumnCount); column_count.is_integer())
computed_values.set_column_count(CSS::ColumnCount::make_integer(column_count.as_integer().integer()));