/src/serenity/Userland/Libraries/LibWeb/CSS/StyleValues/RotationStyleValue.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2024, Steffen T. Larssen <dudedbz@gmail.com> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <AK/String.h> |
8 | | #include <LibWeb/CSS/StyleValues/CSSMathValue.h> |
9 | | #include <LibWeb/CSS/StyleValues/NumberStyleValue.h> |
10 | | |
11 | | #include "RotationStyleValue.h" |
12 | | |
13 | | namespace Web::CSS { |
14 | | |
15 | | // https://www.w3.org/TR/2021/WD-css-transforms-2-20211109/#individual-transform-serialization |
16 | | String RotationStyleValue::to_string() const |
17 | 0 | { |
18 | 0 | auto resolve_to_number = [](ValueComparingNonnullRefPtr<CSSStyleValue const> const& value) -> Optional<double> { |
19 | 0 | if (value->is_number()) |
20 | 0 | return value->as_number().number(); |
21 | 0 | if (value->is_math() && value->as_math().resolves_to_number()) |
22 | 0 | return value->as_math().resolve_number(); |
23 | | |
24 | 0 | VERIFY_NOT_REACHED(); |
25 | 0 | }; |
26 | |
|
27 | 0 | auto x_value = resolve_to_number(m_properties.rotation_x).value_or(0); |
28 | 0 | auto y_value = resolve_to_number(m_properties.rotation_y).value_or(0); |
29 | 0 | auto z_value = resolve_to_number(m_properties.rotation_z).value_or(0); |
30 | | |
31 | | // If the axis is parallel with the x or y axes, it must serialize as the appropriate keyword. |
32 | 0 | if (x_value > 0.0 && y_value == 0 && z_value == 0) |
33 | 0 | return MUST(String::formatted("x {}", m_properties.angle->to_string())); |
34 | | |
35 | 0 | if (x_value == 0 && y_value > 0.0 && z_value == 0) |
36 | 0 | return MUST(String::formatted("y {}", m_properties.angle->to_string())); |
37 | | |
38 | | // If a rotation about the z axis (that is, in 2D) is specified, the property must serialize as just an <angle>. |
39 | 0 | if (x_value == 0 && y_value == 0 && z_value > 0.0) |
40 | 0 | return m_properties.angle->to_string(); |
41 | | |
42 | | // It must serialize as the keyword none if and only if none was originally specified. |
43 | | // NOTE: This is handled by returning a keyword from the parser. |
44 | | |
45 | | // If any other rotation is specified, the property must serialize with an axis specified. |
46 | 0 | return MUST(String::formatted("{} {} {} {}", m_properties.rotation_x->to_string(), m_properties.rotation_y->to_string(), m_properties.rotation_z->to_string(), m_properties.angle->to_string())); |
47 | 0 | } |
48 | | |
49 | | } |