mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-22 09:18:55 +00:00
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:
parent
b3e32445d3
commit
b3c099bd68
Notes:
github-actions[bot]
2025-09-11 16:08:10 +00:00
Author: https://github.com/AtkinsSJ
Commit: b3c099bd68
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/6071
2 changed files with 28 additions and 35 deletions
|
@ -55,41 +55,39 @@ Optional<NumericType> NumericType::create_from_unit(StringView unit)
|
||||||
return NumericType { BaseType::Percent, 1 };
|
return NumericType { BaseType::Percent, 1 };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (auto dimension = dimension_for_unit(unit); dimension.has_value()) {
|
||||||
|
switch (*dimension) {
|
||||||
// unit is a <length> unit
|
// unit is a <length> unit
|
||||||
if (string_to_length_unit(unit).has_value()) {
|
case DimensionType::Length:
|
||||||
// Return «[ "length" → 1 ]»
|
// Return «[ "length" → 1 ]»
|
||||||
return NumericType { BaseType::Length, 1 };
|
return NumericType { BaseType::Length, 1 };
|
||||||
}
|
|
||||||
|
|
||||||
// unit is an <angle> unit
|
// unit is an <angle> unit
|
||||||
if (string_to_angle_unit(unit).has_value()) {
|
case DimensionType::Angle:
|
||||||
// Return «[ "angle" → 1 ]»
|
// Return «[ "angle" → 1 ]»
|
||||||
return NumericType { BaseType::Angle, 1 };
|
return NumericType { BaseType::Angle, 1 };
|
||||||
}
|
|
||||||
|
|
||||||
// unit is a <time> unit
|
// unit is a <time> unit
|
||||||
if (string_to_time_unit(unit).has_value()) {
|
case DimensionType::Time:
|
||||||
// Return «[ "time" → 1 ]»
|
// Return «[ "time" → 1 ]»
|
||||||
return NumericType { BaseType::Time, 1 };
|
return NumericType { BaseType::Time, 1 };
|
||||||
}
|
|
||||||
|
|
||||||
// unit is a <frequency> unit
|
// unit is a <frequency> unit
|
||||||
if (string_to_frequency_unit(unit).has_value()) {
|
case DimensionType::Frequency:
|
||||||
// Return «[ "frequency" → 1 ]»
|
// Return «[ "frequency" → 1 ]»
|
||||||
return NumericType { BaseType::Frequency, 1 };
|
return NumericType { BaseType::Frequency, 1 };
|
||||||
}
|
|
||||||
|
|
||||||
// unit is a <resolution> unit
|
// unit is a <resolution> unit
|
||||||
if (string_to_resolution_unit(unit).has_value()) {
|
case DimensionType::Resolution:
|
||||||
// Return «[ "resolution" → 1 ]»
|
// Return «[ "resolution" → 1 ]»
|
||||||
return NumericType { BaseType::Resolution, 1 };
|
return NumericType { BaseType::Resolution, 1 };
|
||||||
}
|
|
||||||
|
|
||||||
// unit is a <flex> unit
|
// unit is a <flex> unit
|
||||||
if (string_to_flex_unit(unit).has_value()) {
|
case DimensionType::Flex:
|
||||||
// Return «[ "flex" → 1 ]»
|
// Return «[ "flex" → 1 ]»
|
||||||
return NumericType { BaseType::Flex, 1 };
|
return NumericType { BaseType::Flex, 1 };
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// anything else
|
// anything else
|
||||||
// Return failure.
|
// Return failure.
|
||||||
|
|
|
@ -131,12 +131,7 @@ static Vector<ComponentValue> replace_an_attr_function(DOM::AbstractElement& ele
|
||||||
first_argument_tokens.discard_a_token(); // raw-string
|
first_argument_tokens.discard_a_token(); // raw-string
|
||||||
syntax = RawString {};
|
syntax = RawString {};
|
||||||
} else if (syntax_ident == "%"sv
|
} else if (syntax_ident == "%"sv
|
||||||
|| string_to_angle_unit(syntax_ident).has_value()
|
|| dimension_for_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()) {
|
|
||||||
syntax = TypeSyntaxNode::create("number"_fly_string).release_nonnull<SyntaxNode>();
|
syntax = TypeSyntaxNode::create("number"_fly_string).release_nonnull<SyntaxNode>();
|
||||||
unit_name = first_argument_tokens.consume_a_token().token().ident();
|
unit_name = first_argument_tokens.consume_a_token().token().ident();
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue