summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/CSS/Size.cpp
blob: fa4e8f8fe9e2b46f6c32e4b0a16d0deedb32a882 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * 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_px(CSSPixels px)
{
    return make_length(CSS::Length::make_px(px));
}

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_calculated(NonnullRefPtr<Web::CSS::CalculatedStyleValue> calculated)
{
    return Size { Type::Calculated, move(calculated) };
}

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();
    }
}

ErrorOr<String> Size::to_string() const
{
    switch (m_type) {
    case Type::Auto:
        return "auto"_string;
    case Type::Calculated:
    case Type::Length:
    case Type::Percentage:
        return m_length_percentage.to_string();
    case Type::MinContent:
        return "min-content"_string;
    case Type::MaxContent:
        return "max-content"_string;
    case Type::FitContent:
        return String::formatted("fit-content({})", TRY(m_length_percentage.to_string()));
    case Type::None:
        return "none"_string;
    }
    VERIFY_NOT_REACHED();
}

}