1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 18:38:12 +00:00

LibWeb: Add basic support for CSS percentages

Many properties can now have percentage values that get resolved in
layout. The reference value (what is this a percentage *of*?) differs
per property, so I've added a helper where you provide a reference
value as an added parameter to the existing length_or_fallback().
This commit is contained in:
Andreas Kling 2020-05-11 23:04:59 +02:00
parent 8ffdcce0d0
commit 5f9d80d8bc
7 changed files with 117 additions and 31 deletions

View file

@ -125,7 +125,10 @@ static Optional<float> try_parse_float(const StringView& string)
static Optional<float> parse_number(const StringView& view)
{
if (view.length() >= 2 && view[view.length() - 2] == 'p' && view[view.length() - 1] == 'x')
if (view.ends_with('%'))
return parse_number(view.substring_view(0, view.length() - 1));
if (view.ends_with("px"))
return parse_number(view.substring_view(0, view.length() - 2));
return try_parse_float(view);
@ -134,8 +137,11 @@ static Optional<float> parse_number(const StringView& view)
NonnullRefPtr<StyleValue> parse_css_value(const StringView& string)
{
auto number = parse_number(string);
if (number.has_value())
if (number.has_value()) {
if (string.ends_with('%'))
return PercentageStyleValue::create(number.value());
return LengthStyleValue::create(Length(number.value(), Length::Type::Absolute));
}
if (string == "inherit")
return InheritStyleValue::create();
if (string == "initial")