BackgroundStyleValue.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  11. namespace Web::CSS {
  12. BackgroundStyleValue::BackgroundStyleValue(
  13. ValueComparingNonnullRefPtr<StyleValue const> color,
  14. ValueComparingNonnullRefPtr<StyleValue const> image,
  15. ValueComparingNonnullRefPtr<StyleValue const> position,
  16. ValueComparingNonnullRefPtr<StyleValue const> size,
  17. ValueComparingNonnullRefPtr<StyleValue const> repeat,
  18. ValueComparingNonnullRefPtr<StyleValue const> attachment,
  19. ValueComparingNonnullRefPtr<StyleValue const> origin,
  20. ValueComparingNonnullRefPtr<StyleValue const> clip)
  21. : StyleValueWithDefaultOperators(Type::Background)
  22. , m_properties {
  23. .color = move(color),
  24. .image = move(image),
  25. .position = move(position),
  26. .size = move(size),
  27. .repeat = move(repeat),
  28. .attachment = move(attachment),
  29. .origin = move(origin),
  30. .clip = move(clip),
  31. .layer_count = 0
  32. }
  33. {
  34. auto layer_count = [](auto style_value) -> size_t {
  35. if (style_value->is_value_list())
  36. return style_value->as_value_list().size();
  37. else
  38. return 1;
  39. };
  40. m_properties.layer_count = max(layer_count(m_properties.image), layer_count(m_properties.position));
  41. m_properties.layer_count = max(m_properties.layer_count, layer_count(m_properties.size));
  42. m_properties.layer_count = max(m_properties.layer_count, layer_count(m_properties.repeat));
  43. m_properties.layer_count = max(m_properties.layer_count, layer_count(m_properties.attachment));
  44. m_properties.layer_count = max(m_properties.layer_count, layer_count(m_properties.origin));
  45. m_properties.layer_count = max(m_properties.layer_count, layer_count(m_properties.clip));
  46. VERIFY(!m_properties.color->is_value_list());
  47. }
  48. BackgroundStyleValue::~BackgroundStyleValue() = default;
  49. ErrorOr<String> BackgroundStyleValue::to_string() const
  50. {
  51. if (m_properties.layer_count == 1) {
  52. 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()));
  53. }
  54. auto get_layer_value_string = [](ValueComparingNonnullRefPtr<StyleValue const> const& style_value, size_t index) {
  55. if (style_value->is_value_list())
  56. return style_value->as_value_list().value_at(index, true)->to_string();
  57. return style_value->to_string();
  58. };
  59. StringBuilder builder;
  60. for (size_t i = 0; i < m_properties.layer_count; i++) {
  61. if (i)
  62. TRY(builder.try_append(", "sv));
  63. if (i == m_properties.layer_count - 1)
  64. TRY(builder.try_appendff("{} ", TRY(m_properties.color->to_string())));
  65. 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))));
  66. }
  67. return builder.to_string();
  68. }
  69. }