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.
This commit is contained in:
Andreas Kling 2022-09-25 15:47:40 +02:00
parent ba78fe008f
commit 844321d89f
Notes: sideshowbarker 2024-07-17 06:38:32 +09:00
3 changed files with 40 additions and 1 deletions

View File

@ -163,8 +163,10 @@
"lowercase",
"ltr",
"listbox",
"max-content",
"medium",
"middle",
"min-content",
"minimal-ui",
"monospace",
"more",

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -44,6 +44,42 @@ NonnullRefPtr<StyleValue> 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(<length>)`
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);

View File

@ -41,6 +41,7 @@ public:
void set_property(CSS::PropertyID, NonnullRefPtr<StyleValue> value);
NonnullRefPtr<StyleValue> 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<LengthPercentage> length_percentage(CSS::PropertyID) const;