TransformationStyleValue.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "TransformationStyleValue.h"
  10. #include <AK/StringBuilder.h>
  11. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  12. namespace Web::CSS {
  13. String TransformationStyleValue::to_string() const
  14. {
  15. StringBuilder builder;
  16. builder.append(CSS::to_string(m_properties.transform_function));
  17. builder.append('(');
  18. for (size_t i = 0; i < m_properties.values.size(); ++i) {
  19. auto const& value = m_properties.values[i];
  20. // https://www.w3.org/TR/css-transforms-2/#individual-transforms
  21. // A <percentage> is equivalent to a <number>, for example scale: 100% is equivalent to scale: 1.
  22. // Numbers are used during serialization of specified and computed values.
  23. if ((m_properties.transform_function == CSS::TransformFunction::Scale
  24. || m_properties.transform_function == CSS::TransformFunction::Scale3d
  25. || m_properties.transform_function == CSS::TransformFunction::ScaleX
  26. || m_properties.transform_function == CSS::TransformFunction::ScaleY
  27. || m_properties.transform_function == CSS::TransformFunction::ScaleZ)
  28. && value->is_percentage()) {
  29. builder.append(MUST(String::number(value->as_percentage().percentage().as_fraction())));
  30. } else {
  31. builder.append(value->to_string());
  32. }
  33. if (i != m_properties.values.size() - 1)
  34. builder.append(", "sv);
  35. }
  36. builder.append(')');
  37. return MUST(builder.to_string());
  38. }
  39. bool TransformationStyleValue::Properties::operator==(Properties const& other) const
  40. {
  41. return transform_function == other.transform_function && values.span() == other.values.span();
  42. }
  43. }