RotationStyleValue.cpp 2.0 KB

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