diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index c1265a26fb..1c22c1eccd 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -40,6 +40,7 @@ set(SOURCES CSS/Display.cpp CSS/FontFace.cpp CSS/Frequency.cpp + CSS/GridTrackSize.cpp CSS/Length.cpp CSS/MediaList.cpp CSS/MediaQuery.cpp diff --git a/Userland/Libraries/LibWeb/CSS/GridTrackSize.cpp b/Userland/Libraries/LibWeb/CSS/GridTrackSize.cpp new file mode 100644 index 0000000000..5c4e0f478c --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/GridTrackSize.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022, Martin Falisse + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "GridTrackSize.h" +#include +#include +#include +#include + +namespace Web::CSS { + +GridTrackSize::GridTrackSize(Length length) + : m_type(Type::Length) + , m_length(length) +{ +} + +GridTrackSize::GridTrackSize(Percentage percentage) + : m_type(Type::Percentage) + , m_percentage(percentage) +{ +} + +GridTrackSize::GridTrackSize(int flexible_length) + : m_type(Type::FlexibleLength) + , m_flexible_length(flexible_length) +{ +} + +String GridTrackSize::to_string() const +{ + switch (m_type) { + case Type::Length: + return m_length.to_string(); + case Type::Percentage: + return m_percentage.to_string(); + case Type::FlexibleLength: + return String::formatted("{}fr", m_flexible_length); + } + VERIFY_NOT_REACHED(); +} + +} diff --git a/Userland/Libraries/LibWeb/CSS/GridTrackSize.h b/Userland/Libraries/LibWeb/CSS/GridTrackSize.h new file mode 100644 index 0000000000..89ff2361c0 --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/GridTrackSize.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2022, Martin Falisse + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace Web::CSS { + +class GridTrackSize { +public: + enum class Type { + Length, + Percentage, + FlexibleLength, + // TODO: MinMax + // TODO: Repeat + // TODO: Max-Content + }; + + GridTrackSize(Length); + GridTrackSize(Percentage); + GridTrackSize(int); + + Type type() const { return m_type; } + + bool is_length() const { return m_type == Type::Length; } + bool is_percentage() const { return m_type == Type::Percentage; } + bool is_flexible_length() const { return m_type == Type::FlexibleLength; } + + Length length() const { return m_length; } + Percentage percentage() const { return m_percentage; } + int flexible_length() const { return m_flexible_length; } + + String to_string() const; + bool operator==(GridTrackSize const& other) const + { + return m_type == other.type() + && m_length == other.length() + && m_percentage == other.percentage() + && m_flexible_length == other.flexible_length(); + } + +private: + Type m_type; + Length m_length { Length::make_px(0) }; + Percentage m_percentage { Percentage(0) }; + int m_flexible_length { 0 }; +}; + +} diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index fc136593bc..ff66233853 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -57,6 +57,7 @@ class FontStyleValue; class Frequency; class FrequencyPercentage; class FrequencyStyleValue; +class GridTrackSize; class IdentifierStyleValue; class ImageStyleValue; class InheritStyleValue;