mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 06:27:45 +00:00
LibWeb: Add CSS::Size to represent the full range of CSS size values
Until now, we've been using CSS::LengthPercentage, sometimes wrapped in Optional, to represent CSS sizes. This meant we could not support modern values like `min-content`, `max-content`, `fit-content(<length>)`. We were also conflating `none` and `auto` which made the `min-*` and `max-*` properties confusing. The new CSS::Size class covers all possible size values as individual substates. It'll be quite a bit of work to make all layout code aware of the additional features, this patch merely makes the new type available.
This commit is contained in:
parent
04b1d32b70
commit
ba78fe008f
4 changed files with 146 additions and 0 deletions
|
@ -57,6 +57,7 @@ set(SOURCES
|
||||||
CSS/Selector.cpp
|
CSS/Selector.cpp
|
||||||
CSS/SelectorEngine.cpp
|
CSS/SelectorEngine.cpp
|
||||||
CSS/Serialize.cpp
|
CSS/Serialize.cpp
|
||||||
|
CSS/Size.cpp
|
||||||
CSS/StyleComputer.cpp
|
CSS/StyleComputer.cpp
|
||||||
CSS/StyleProperties.cpp
|
CSS/StyleProperties.cpp
|
||||||
CSS/StyleSheet.cpp
|
CSS/StyleSheet.cpp
|
||||||
|
|
72
Userland/Libraries/LibWeb/CSS/Size.cpp
Normal file
72
Userland/Libraries/LibWeb/CSS/Size.cpp
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <LibWeb/CSS/Length.h>
|
||||||
|
#include <LibWeb/CSS/Size.h>
|
||||||
|
#include <LibWeb/CSS/StyleValue.h>
|
||||||
|
|
||||||
|
namespace Web::CSS {
|
||||||
|
|
||||||
|
Size::Size(Type type, LengthPercentage length_percentage)
|
||||||
|
: m_type(type)
|
||||||
|
, m_length_percentage(move(length_percentage))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
CSS::Length Size::resolved(Layout::Node const& node, Length const& reference_value) const
|
||||||
|
{
|
||||||
|
return m_length_percentage.resolved(node, reference_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
Size Size::make_auto()
|
||||||
|
{
|
||||||
|
return Size { Type::Auto, Length::make_auto() };
|
||||||
|
}
|
||||||
|
|
||||||
|
Size Size::make_length(Length length)
|
||||||
|
{
|
||||||
|
return Size { Type::Length, move(length) };
|
||||||
|
}
|
||||||
|
|
||||||
|
Size Size::make_percentage(Percentage percentage)
|
||||||
|
{
|
||||||
|
return Size { Type::Percentage, move(percentage) };
|
||||||
|
}
|
||||||
|
|
||||||
|
Size Size::make_min_content()
|
||||||
|
{
|
||||||
|
return Size { Type::MinContent, Length::make_auto() };
|
||||||
|
}
|
||||||
|
|
||||||
|
Size Size::make_max_content()
|
||||||
|
{
|
||||||
|
return Size { Type::MaxContent, Length::make_auto() };
|
||||||
|
}
|
||||||
|
|
||||||
|
Size Size::make_fit_content(Length available_space)
|
||||||
|
{
|
||||||
|
return Size { Type::FitContent, move(available_space) };
|
||||||
|
}
|
||||||
|
|
||||||
|
Size Size::make_none()
|
||||||
|
{
|
||||||
|
return Size { Type::None, Length::make_auto() };
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Size::contains_percentage() const
|
||||||
|
{
|
||||||
|
switch (m_type) {
|
||||||
|
case Type::Auto:
|
||||||
|
case Type::MinContent:
|
||||||
|
case Type::MaxContent:
|
||||||
|
case Type::None:
|
||||||
|
return false;
|
||||||
|
default:
|
||||||
|
return m_length_percentage.contains_percentage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
72
Userland/Libraries/LibWeb/CSS/Size.h
Normal file
72
Userland/Libraries/LibWeb/CSS/Size.h
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <LibWeb/CSS/Length.h>
|
||||||
|
#include <LibWeb/CSS/Percentage.h>
|
||||||
|
|
||||||
|
namespace Web::CSS {
|
||||||
|
|
||||||
|
class Size {
|
||||||
|
public:
|
||||||
|
enum class Type {
|
||||||
|
Auto,
|
||||||
|
Length,
|
||||||
|
Percentage,
|
||||||
|
MinContent,
|
||||||
|
MaxContent,
|
||||||
|
FitContent,
|
||||||
|
None, // NOTE: This is only valid for max-width and max-height.
|
||||||
|
};
|
||||||
|
|
||||||
|
static Size make_auto();
|
||||||
|
static Size make_length(Length);
|
||||||
|
static Size make_percentage(Percentage);
|
||||||
|
static Size make_min_content();
|
||||||
|
static Size make_max_content();
|
||||||
|
static Size make_fit_content(Length available_space);
|
||||||
|
static Size make_none();
|
||||||
|
|
||||||
|
bool is_auto() const { return m_type == Type::Auto; }
|
||||||
|
bool is_length() const { return m_type == Type::Length; }
|
||||||
|
bool is_percentage() const { return m_type == Type::Percentage; }
|
||||||
|
bool is_min_content() const { return m_type == Type::MinContent; }
|
||||||
|
bool is_max_content() const { return m_type == Type::MaxContent; }
|
||||||
|
bool is_fit_content() const { return m_type == Type::FitContent; }
|
||||||
|
bool is_none() const { return m_type == Type::None; }
|
||||||
|
|
||||||
|
// FIXME: This is a stopgap API that will go away once all layout code is aware of CSS::Size.
|
||||||
|
CSS::Length resolved(Layout::Node const&, Length const& reference_value) const;
|
||||||
|
|
||||||
|
bool contains_percentage() const;
|
||||||
|
|
||||||
|
CSS::Length const& length() const
|
||||||
|
{
|
||||||
|
VERIFY(is_length());
|
||||||
|
return m_length_percentage.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
CSS::Percentage const& percentage() const
|
||||||
|
{
|
||||||
|
VERIFY(is_percentage());
|
||||||
|
return m_length_percentage.percentage();
|
||||||
|
}
|
||||||
|
|
||||||
|
CSS::Length const& fit_content_available_space() const
|
||||||
|
{
|
||||||
|
VERIFY(is_fit_content());
|
||||||
|
return m_length_percentage.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Size(Type type, LengthPercentage);
|
||||||
|
|
||||||
|
Type m_type {};
|
||||||
|
CSS::LengthPercentage m_length_percentage;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -91,6 +91,7 @@ class ResolutionStyleValue;
|
||||||
class Screen;
|
class Screen;
|
||||||
class Selector;
|
class Selector;
|
||||||
class ShadowStyleValue;
|
class ShadowStyleValue;
|
||||||
|
class Size;
|
||||||
class StringStyleValue;
|
class StringStyleValue;
|
||||||
class StyleComputer;
|
class StyleComputer;
|
||||||
class StyleProperties;
|
class StyleProperties;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue