1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-02 22:12:08 +00:00

LibWeb: Start fleshing out support for relative CSS units

This patch introduces support for more than just "absolute px" units in
our Length class. It now also supports "em" and "rem", which are units
relative to the font-size of the current layout node and the <html>
element's layout node respectively.
This commit is contained in:
Andreas Kling 2020-06-07 17:55:46 +02:00
parent d93bf78346
commit 731685468a
16 changed files with 163 additions and 92 deletions

View file

@ -253,6 +253,10 @@ static Optional<float> parse_number(const StringView& view)
// FIXME: Maybe we should have "ends_with_ignoring_case()" ?
if (view.to_string().to_lowercase().ends_with("px"))
return parse_number(view.substring_view(0, view.length() - 2));
if (view.to_string().to_lowercase().ends_with("rem"))
return parse_number(view.substring_view(0, view.length() - 3));
if (view.to_string().to_lowercase().ends_with("em"))
return parse_number(view.substring_view(0, view.length() - 2));
return try_parse_float(view);
}
@ -263,7 +267,11 @@ NonnullRefPtr<StyleValue> parse_css_value(const StringView& string)
if (number.has_value()) {
if (string.ends_with('%'))
return PercentageStyleValue::create(number.value());
return LengthStyleValue::create(Length(number.value(), Length::Type::Absolute));
if (string.ends_with("em"))
return LengthStyleValue::create(Length(number.value(), Length::Type::Em));
if (string.ends_with("rem"))
return LengthStyleValue::create(Length(number.value(), Length::Type::Rem));
return LengthStyleValue::create(Length(number.value(), Length::Type::Px));
}
if (string.equals_ignoring_case("inherit"))