/src/serenity/Userland/Libraries/LibWeb/CSS/Flex.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include "Flex.h" |
8 | | #include <LibWeb/CSS/Percentage.h> |
9 | | |
10 | | namespace Web::CSS { |
11 | | |
12 | | Flex::Flex(double value, Type type) |
13 | 0 | : m_type(type) |
14 | 0 | , m_value(value) |
15 | 0 | { |
16 | 0 | } |
17 | | |
18 | | Flex Flex::make_fr(double value) |
19 | 0 | { |
20 | 0 | return { value, Type::Fr }; |
21 | 0 | } |
22 | | |
23 | | Flex Flex::percentage_of(Percentage const& percentage) const |
24 | 0 | { |
25 | 0 | return Flex { percentage.as_fraction() * m_value, m_type }; |
26 | 0 | } |
27 | | |
28 | | String Flex::to_string() const |
29 | 0 | { |
30 | 0 | return MUST(String::formatted("{}fr", to_fr())); |
31 | 0 | } |
32 | | |
33 | | double Flex::to_fr() const |
34 | 0 | { |
35 | 0 | switch (m_type) { |
36 | 0 | case Type::Fr: |
37 | 0 | return m_value; |
38 | 0 | } |
39 | 0 | VERIFY_NOT_REACHED(); |
40 | 0 | } |
41 | | |
42 | | StringView Flex::unit_name() const |
43 | 0 | { |
44 | 0 | switch (m_type) { |
45 | 0 | case Type::Fr: |
46 | 0 | return "fr"sv; |
47 | 0 | } |
48 | 0 | VERIFY_NOT_REACHED(); |
49 | 0 | } |
50 | | |
51 | | Optional<Flex::Type> Flex::unit_from_name(StringView name) |
52 | 0 | { |
53 | 0 | if (name.equals_ignoring_ascii_case("fr"sv)) |
54 | 0 | return Type::Fr; |
55 | | |
56 | 0 | return {}; |
57 | 0 | } |
58 | | |
59 | | } |