LibWeb/CSS: Use dimension_for_unit() when we don't need the exact unit

A small code reduction, and means that NumericType will catch if new
dimensions are added, and attr() will just work in that case.
This commit is contained in:
Sam Atkins 2025-09-02 13:58:20 +01:00
commit b3c099bd68
Notes: github-actions[bot] 2025-09-11 16:08:10 +00:00
2 changed files with 28 additions and 35 deletions

View file

@ -55,40 +55,38 @@ Optional<NumericType> NumericType::create_from_unit(StringView unit)
return NumericType { BaseType::Percent, 1 };
}
// unit is a <length> unit
if (string_to_length_unit(unit).has_value()) {
// Return «[ "length" → 1 ]»
return NumericType { BaseType::Length, 1 };
}
if (auto dimension = dimension_for_unit(unit); dimension.has_value()) {
switch (*dimension) {
// unit is a <length> unit
case DimensionType::Length:
// Return «[ "length" → 1 ]»
return NumericType { BaseType::Length, 1 };
// unit is an <angle> unit
if (string_to_angle_unit(unit).has_value()) {
// Return «[ "angle" → 1 ]»
return NumericType { BaseType::Angle, 1 };
}
// unit is an <angle> unit
case DimensionType::Angle:
// Return «[ "angle" → 1 ]»
return NumericType { BaseType::Angle, 1 };
// unit is a <time> unit
if (string_to_time_unit(unit).has_value()) {
// Return «[ "time" → 1 ]»
return NumericType { BaseType::Time, 1 };
}
// unit is a <time> unit
case DimensionType::Time:
// Return «[ "time" → 1 ]»
return NumericType { BaseType::Time, 1 };
// unit is a <frequency> unit
if (string_to_frequency_unit(unit).has_value()) {
// Return «[ "frequency" → 1 ]»
return NumericType { BaseType::Frequency, 1 };
}
// unit is a <frequency> unit
case DimensionType::Frequency:
// Return «[ "frequency" → 1 ]»
return NumericType { BaseType::Frequency, 1 };
// unit is a <resolution> unit
if (string_to_resolution_unit(unit).has_value()) {
// Return «[ "resolution" → 1 ]»
return NumericType { BaseType::Resolution, 1 };
}
// unit is a <resolution> unit
case DimensionType::Resolution:
// Return «[ "resolution" → 1 ]»
return NumericType { BaseType::Resolution, 1 };
// unit is a <flex> unit
if (string_to_flex_unit(unit).has_value()) {
// Return «[ "flex" → 1 ]»
return NumericType { BaseType::Flex, 1 };
// unit is a <flex> unit
case DimensionType::Flex:
// Return «[ "flex" → 1 ]»
return NumericType { BaseType::Flex, 1 };
}
}
// anything else

View file

@ -131,12 +131,7 @@ static Vector<ComponentValue> replace_an_attr_function(DOM::AbstractElement& ele
first_argument_tokens.discard_a_token(); // raw-string
syntax = RawString {};
} else if (syntax_ident == "%"sv
|| string_to_angle_unit(syntax_ident).has_value()
|| string_to_flex_unit(syntax_ident).has_value()
|| string_to_frequency_unit(syntax_ident).has_value()
|| string_to_length_unit(syntax_ident).has_value()
|| string_to_resolution_unit(syntax_ident).has_value()
|| string_to_time_unit(syntax_ident).has_value()) {
|| dimension_for_unit(syntax_ident).has_value()) {
syntax = TypeSyntaxNode::create("number"_fly_string).release_nonnull<SyntaxNode>();
unit_name = first_argument_tokens.consume_a_token().token().ident();
} else {