TranslationStyleValue.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
  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/LengthStyleValue.h>
  9. #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
  10. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  11. #include <LibWeb/CSS/StyleValues/TranslationStyleValue.h>
  12. namespace Web::CSS {
  13. // https://www.w3.org/TR/2021/WD-css-transforms-2-20211109/#individual-transform-serialization
  14. String TranslationStyleValue::to_string() const
  15. {
  16. auto resolve_to_string = [](LengthPercentage const& value) -> Optional<String> {
  17. if (value.is_length()) {
  18. if (value.length().raw_value() == 0)
  19. return {};
  20. }
  21. if (value.is_percentage()) {
  22. if (value.percentage().value() == 0)
  23. return {};
  24. }
  25. return value.to_string();
  26. };
  27. auto x_value = resolve_to_string(m_properties.x);
  28. auto y_value = resolve_to_string(m_properties.y);
  29. StringBuilder builder;
  30. builder.append(x_value.value_or("0px"_string));
  31. if (y_value.has_value()) {
  32. builder.append(" "sv);
  33. builder.append(y_value.value());
  34. }
  35. return builder.to_string_without_validation();
  36. }
  37. }