From 844321d89fda5fc9424f9122f030f63ee5b29a31 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 25 Sep 2022 15:47:40 +0200 Subject: [PATCH] LibWeb: Teach CSS::StyleProperties to create CSS::Size values You can now use StyleProperties::size_value(CSS::PropertyID) to extract a CSS::Size for a given property. --- .../Libraries/LibWeb/CSS/Identifiers.json | 2 + .../Libraries/LibWeb/CSS/StyleProperties.cpp | 38 ++++++++++++++++++- .../Libraries/LibWeb/CSS/StyleProperties.h | 1 + 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibWeb/CSS/Identifiers.json b/Userland/Libraries/LibWeb/CSS/Identifiers.json index afa64db372..24c25a7f94 100644 --- a/Userland/Libraries/LibWeb/CSS/Identifiers.json +++ b/Userland/Libraries/LibWeb/CSS/Identifiers.json @@ -163,8 +163,10 @@ "lowercase", "ltr", "listbox", + "max-content", "medium", "middle", + "min-content", "minimal-ui", "monospace", "more", diff --git a/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp b/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp index 107f678f8a..ed2d2bafbd 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2022, Andreas Kling * Copyright (c) 2021-2022, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause @@ -44,6 +44,42 @@ NonnullRefPtr StyleProperties::property(CSS::PropertyID property_id) return value.release_nonnull(); } +CSS::Size StyleProperties::size_value(CSS::PropertyID id) const +{ + auto value = property(id); + if (value->is_identifier()) { + switch (value->to_identifier()) { + case ValueID::Auto: + return CSS::Size::make_auto(); + case ValueID::MinContent: + return CSS::Size::make_min_content(); + case ValueID::MaxContent: + return CSS::Size::make_max_content(); + case ValueID::None: + return CSS::Size::make_none(); + default: + VERIFY_NOT_REACHED(); + } + } + + if (value->is_calculated()) + return CSS::Size::make_length(CSS::Length::make_calculated(value->as_calculated())); + + if (value->is_percentage()) + return CSS::Size::make_percentage(value->as_percentage().percentage()); + + if (value->has_length()) { + auto length = value->to_length(); + if (length.is_auto()) + return CSS::Size::make_auto(); + return CSS::Size::make_length(value->to_length()); + } + + // FIXME: Support `fit-content()` + dbgln("FIXME: Unsupported size value: `{}`, treating as `auto`", value->to_string()); + return CSS::Size::make_auto(); +} + Length StyleProperties::length_or_fallback(CSS::PropertyID id, Length const& fallback) const { auto value = property(id); diff --git a/Userland/Libraries/LibWeb/CSS/StyleProperties.h b/Userland/Libraries/LibWeb/CSS/StyleProperties.h index d03478477e..81dbe2619b 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleProperties.h +++ b/Userland/Libraries/LibWeb/CSS/StyleProperties.h @@ -41,6 +41,7 @@ public: void set_property(CSS::PropertyID, NonnullRefPtr value); NonnullRefPtr property(CSS::PropertyID) const; + CSS::Size size_value(CSS::PropertyID) const; Length length_or_fallback(CSS::PropertyID, Length const& fallback) const; LengthPercentage length_percentage_or_fallback(CSS::PropertyID, LengthPercentage const& fallback) const; Optional length_percentage(CSS::PropertyID) const;