Coverage Report

Created: 2025-08-28 06:26

/src/serenity/Userland/Libraries/LibWeb/CSS/Parser/Dimension.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#pragma once
8
9
#include <AK/Variant.h>
10
#include <LibWeb/CSS/Angle.h>
11
#include <LibWeb/CSS/Flex.h>
12
#include <LibWeb/CSS/Frequency.h>
13
#include <LibWeb/CSS/Length.h>
14
#include <LibWeb/CSS/Percentage.h>
15
#include <LibWeb/CSS/PercentageOr.h>
16
#include <LibWeb/CSS/Resolution.h>
17
#include <LibWeb/CSS/Time.h>
18
19
namespace Web::CSS::Parser {
20
21
class Dimension {
22
public:
23
    Dimension(Angle&& value)
24
0
        : m_value(move(value))
25
0
    {
26
0
    }
27
28
    Dimension(Flex&& value)
29
0
        : m_value(move(value))
30
0
    {
31
0
    }
32
33
    Dimension(Frequency&& value)
34
0
        : m_value(move(value))
35
0
    {
36
0
    }
37
38
    Dimension(Length&& value)
39
0
        : m_value(move(value))
40
0
    {
41
0
    }
42
    Dimension(Percentage&& value)
43
0
        : m_value(move(value))
44
0
    {
45
0
    }
46
47
    Dimension(Resolution&& value)
48
0
        : m_value(move(value))
49
0
    {
50
0
    }
51
52
    Dimension(Time&& value)
53
0
        : m_value(move(value))
54
0
    {
55
0
    }
56
57
0
    bool is_angle() const { return m_value.has<Angle>(); }
58
0
    Angle angle() const { return m_value.get<Angle>(); }
59
60
0
    bool is_angle_percentage() const { return is_angle() || is_percentage(); }
61
    AnglePercentage angle_percentage() const
62
0
    {
63
0
        if (is_angle())
64
0
            return angle();
65
0
        return percentage();
66
0
    }
67
68
0
    bool is_flex() const { return m_value.has<Flex>(); }
69
0
    Flex flex() const { return m_value.get<Flex>(); }
70
71
0
    bool is_frequency() const { return m_value.has<Frequency>(); }
72
0
    Frequency frequency() const { return m_value.get<Frequency>(); }
73
74
0
    bool is_frequency_percentage() const { return is_frequency() || is_percentage(); }
75
    FrequencyPercentage frequency_percentage() const
76
0
    {
77
0
        if (is_frequency())
78
0
            return frequency();
79
0
        return percentage();
80
0
    }
81
82
0
    bool is_length() const { return m_value.has<Length>(); }
83
0
    Length length() const { return m_value.get<Length>(); }
84
85
0
    bool is_length_percentage() const { return is_length() || is_percentage(); }
86
    LengthPercentage length_percentage() const
87
0
    {
88
0
        if (is_length())
89
0
            return length();
90
0
        return percentage();
91
0
    }
92
93
0
    bool is_percentage() const { return m_value.has<Percentage>(); }
94
0
    Percentage percentage() const { return m_value.get<Percentage>(); }
95
96
0
    bool is_resolution() const { return m_value.has<Resolution>(); }
97
0
    Resolution resolution() const { return m_value.get<Resolution>(); }
98
99
0
    bool is_time() const { return m_value.has<Time>(); }
100
0
    Time time() const { return m_value.get<Time>(); }
101
102
0
    bool is_time_percentage() const { return is_time() || is_percentage(); }
103
    TimePercentage time_percentage() const
104
0
    {
105
0
        if (is_time())
106
0
            return time();
107
0
        return percentage();
108
0
    }
109
110
private:
111
    Variant<Angle, Flex, Frequency, Length, Percentage, Resolution, Time> m_value;
112
};
113
114
}