BackgroundStyleValue.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "BackgroundStyleValue.h"
  10. namespace Web::CSS {
  11. BackgroundStyleValue::BackgroundStyleValue(
  12. ValueComparingNonnullRefPtr<StyleValue const> color,
  13. ValueComparingNonnullRefPtr<StyleValue const> image,
  14. ValueComparingNonnullRefPtr<StyleValue const> position,
  15. ValueComparingNonnullRefPtr<StyleValue const> size,
  16. ValueComparingNonnullRefPtr<StyleValue const> repeat,
  17. ValueComparingNonnullRefPtr<StyleValue const> attachment,
  18. ValueComparingNonnullRefPtr<StyleValue const> origin,
  19. ValueComparingNonnullRefPtr<StyleValue const> clip)
  20. : StyleValueWithDefaultOperators(Type::Background)
  21. , m_properties {
  22. .color = move(color),
  23. .image = move(image),
  24. .position = move(position),
  25. .size = move(size),
  26. .repeat = move(repeat),
  27. .attachment = move(attachment),
  28. .origin = move(origin),
  29. .clip = move(clip),
  30. .layer_count = 0
  31. }
  32. {
  33. auto layer_count = [](auto style_value) -> size_t {
  34. if (style_value->is_value_list())
  35. return style_value->as_value_list().size();
  36. else
  37. return 1;
  38. };
  39. m_properties.layer_count = max(layer_count(m_properties.image), layer_count(m_properties.position));
  40. m_properties.layer_count = max(m_properties.layer_count, layer_count(m_properties.size));
  41. m_properties.layer_count = max(m_properties.layer_count, layer_count(m_properties.repeat));
  42. m_properties.layer_count = max(m_properties.layer_count, layer_count(m_properties.attachment));
  43. m_properties.layer_count = max(m_properties.layer_count, layer_count(m_properties.origin));
  44. m_properties.layer_count = max(m_properties.layer_count, layer_count(m_properties.clip));
  45. VERIFY(!m_properties.color->is_value_list());
  46. }
  47. BackgroundStyleValue::~BackgroundStyleValue() = default;
  48. ErrorOr<String> BackgroundStyleValue::to_string() const
  49. {
  50. if (m_properties.layer_count == 1) {
  51. return String::formatted("{} {} {} {} {} {} {} {}", TRY(m_properties.color->to_string()), TRY(m_properties.image->to_string()), TRY(m_properties.position->to_string()), TRY(m_properties.size->to_string()), TRY(m_properties.repeat->to_string()), TRY(m_properties.attachment->to_string()), TRY(m_properties.origin->to_string()), TRY(m_properties.clip->to_string()));
  52. }
  53. auto get_layer_value_string = [](ValueComparingNonnullRefPtr<StyleValue const> const& style_value, size_t index) {
  54. if (style_value->is_value_list())
  55. return style_value->as_value_list().value_at(index, true)->to_string();
  56. return style_value->to_string();
  57. };
  58. StringBuilder builder;
  59. for (size_t i = 0; i < m_properties.layer_count; i++) {
  60. if (i)
  61. TRY(builder.try_append(", "sv));
  62. if (i == m_properties.layer_count - 1)
  63. TRY(builder.try_appendff("{} ", TRY(m_properties.color->to_string())));
  64. TRY(builder.try_appendff("{} {} {} {} {} {} {}", TRY(get_layer_value_string(m_properties.image, i)), TRY(get_layer_value_string(m_properties.position, i)), TRY(get_layer_value_string(m_properties.size, i)), TRY(get_layer_value_string(m_properties.repeat, i)), TRY(get_layer_value_string(m_properties.attachment, i)), TRY(get_layer_value_string(m_properties.origin, i)), TRY(get_layer_value_string(m_properties.clip, i))));
  65. }
  66. return builder.to_string();
  67. }
  68. }