ShorthandStyleValue.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2023, Ali Mohammad Pur <mpfard@serenityos.org>
  3. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "ShorthandStyleValue.h"
  8. #include <LibWeb/CSS/PropertyID.h>
  9. #include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
  10. #include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
  11. #include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
  12. #include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
  13. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  14. namespace Web::CSS {
  15. ShorthandStyleValue::ShorthandStyleValue(PropertyID shorthand, Vector<PropertyID> sub_properties, Vector<ValueComparingNonnullRefPtr<StyleValue const>> values)
  16. : StyleValueWithDefaultOperators(Type::Shorthand)
  17. , m_properties { shorthand, move(sub_properties), move(values) }
  18. {
  19. if (m_properties.sub_properties.size() != m_properties.values.size()) {
  20. dbgln("ShorthandStyleValue: sub_properties and values must be the same size! {} != {}", m_properties.sub_properties.size(), m_properties.values.size());
  21. VERIFY_NOT_REACHED();
  22. }
  23. }
  24. ShorthandStyleValue::~ShorthandStyleValue() = default;
  25. ValueComparingRefPtr<StyleValue const> ShorthandStyleValue::longhand(PropertyID longhand) const
  26. {
  27. for (auto i = 0u; i < m_properties.sub_properties.size(); ++i) {
  28. if (m_properties.sub_properties[i] == longhand)
  29. return m_properties.values[i];
  30. }
  31. return nullptr;
  32. }
  33. String ShorthandStyleValue::to_string() const
  34. {
  35. // Special-cases first
  36. switch (m_properties.shorthand_property) {
  37. case PropertyID::Background: {
  38. auto color = longhand(PropertyID::BackgroundColor);
  39. auto image = longhand(PropertyID::BackgroundImage);
  40. auto position = longhand(PropertyID::BackgroundPosition);
  41. auto size = longhand(PropertyID::BackgroundSize);
  42. auto repeat = longhand(PropertyID::BackgroundRepeat);
  43. auto attachment = longhand(PropertyID::BackgroundAttachment);
  44. auto origin = longhand(PropertyID::BackgroundOrigin);
  45. auto clip = longhand(PropertyID::BackgroundClip);
  46. auto get_layer_count = [](auto style_value) -> size_t {
  47. return style_value->is_value_list() ? style_value->as_value_list().size() : 1;
  48. };
  49. auto layer_count = max(get_layer_count(image), max(get_layer_count(position), max(get_layer_count(size), max(get_layer_count(repeat), max(get_layer_count(attachment), max(get_layer_count(origin), get_layer_count(clip)))))));
  50. if (layer_count == 1) {
  51. return MUST(String::formatted("{} {} {} {} {} {} {} {}", color->to_string(), image->to_string(), position->to_string(), size->to_string(), repeat->to_string(), attachment->to_string(), origin->to_string(), clip->to_string()));
  52. }
  53. auto get_layer_value_string = [](ValueComparingRefPtr<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 < layer_count; i++) {
  60. if (i)
  61. builder.append(", "sv);
  62. if (i == layer_count - 1)
  63. builder.appendff("{} ", color->to_string());
  64. builder.appendff("{} {} {} {} {} {} {}", get_layer_value_string(image, i), get_layer_value_string(position, i), get_layer_value_string(size, i), get_layer_value_string(repeat, i), get_layer_value_string(attachment, i), get_layer_value_string(origin, i), get_layer_value_string(clip, i));
  65. }
  66. return MUST(builder.to_string());
  67. }
  68. case PropertyID::BorderRadius: {
  69. auto& top_left = longhand(PropertyID::BorderTopLeftRadius)->as_border_radius();
  70. auto& top_right = longhand(PropertyID::BorderTopRightRadius)->as_border_radius();
  71. auto& bottom_right = longhand(PropertyID::BorderBottomRightRadius)->as_border_radius();
  72. auto& bottom_left = longhand(PropertyID::BorderBottomLeftRadius)->as_border_radius();
  73. return MUST(String::formatted("{} {} {} {} / {} {} {} {}",
  74. top_left.horizontal_radius().to_string(),
  75. top_right.horizontal_radius().to_string(),
  76. bottom_right.horizontal_radius().to_string(),
  77. bottom_left.horizontal_radius().to_string(),
  78. top_left.vertical_radius().to_string(),
  79. top_right.vertical_radius().to_string(),
  80. bottom_right.vertical_radius().to_string(),
  81. bottom_left.vertical_radius().to_string()));
  82. }
  83. case PropertyID::Flex:
  84. return MUST(String::formatted("{} {} {}", longhand(PropertyID::FlexGrow)->to_string(), longhand(PropertyID::FlexShrink)->to_string(), longhand(PropertyID::FlexBasis)->to_string()));
  85. case PropertyID::FlexFlow:
  86. return MUST(String::formatted("{} {}", longhand(PropertyID::FlexDirection)->to_string(), longhand(PropertyID::FlexWrap)->to_string()));
  87. case PropertyID::GridArea: {
  88. auto& row_start = longhand(PropertyID::GridRowStart)->as_grid_track_placement();
  89. auto& column_start = longhand(PropertyID::GridColumnStart)->as_grid_track_placement();
  90. auto& row_end = longhand(PropertyID::GridRowEnd)->as_grid_track_placement();
  91. auto& column_end = longhand(PropertyID::GridColumnEnd)->as_grid_track_placement();
  92. StringBuilder builder;
  93. if (!row_start.grid_track_placement().is_auto())
  94. builder.appendff("{}", row_start.grid_track_placement().to_string());
  95. if (!column_start.grid_track_placement().is_auto())
  96. builder.appendff(" / {}", column_start.grid_track_placement().to_string());
  97. if (!row_end.grid_track_placement().is_auto())
  98. builder.appendff(" / {}", row_end.grid_track_placement().to_string());
  99. if (!column_end.grid_track_placement().is_auto())
  100. builder.appendff(" / {}", column_end.grid_track_placement().to_string());
  101. return MUST(builder.to_string());
  102. }
  103. // FIXME: Serialize Grid differently once we support it better!
  104. case PropertyID::Grid:
  105. case PropertyID::GridTemplate: {
  106. auto& areas = longhand(PropertyID::GridTemplateAreas)->as_grid_template_area();
  107. auto& rows = longhand(PropertyID::GridTemplateRows)->as_grid_track_size_list();
  108. auto& columns = longhand(PropertyID::GridTemplateColumns)->as_grid_track_size_list();
  109. auto construct_rows_string = [&]() {
  110. StringBuilder builder;
  111. size_t idx = 0;
  112. for (auto const& row : rows.grid_track_size_list().track_list()) {
  113. if (areas.grid_template_area().size() > idx) {
  114. builder.append("\""sv);
  115. for (size_t y = 0; y < areas.grid_template_area()[idx].size(); ++y) {
  116. builder.append(areas.grid_template_area()[idx][y]);
  117. if (y != areas.grid_template_area()[idx].size() - 1)
  118. builder.append(" "sv);
  119. }
  120. builder.append("\" "sv);
  121. }
  122. builder.append(row.to_string());
  123. if (idx < rows.grid_track_size_list().track_list().size() - 1)
  124. builder.append(' ');
  125. idx++;
  126. }
  127. return MUST(builder.to_string());
  128. };
  129. if (columns.grid_track_size_list().track_list().size() == 0)
  130. return MUST(String::formatted("{}", construct_rows_string()));
  131. return MUST(String::formatted("{} / {}", construct_rows_string(), columns.grid_track_size_list().to_string()));
  132. }
  133. case PropertyID::ListStyle:
  134. return MUST(String::formatted("{} {} {}", longhand(PropertyID::ListStylePosition)->to_string(), longhand(PropertyID::ListStyleImage)->to_string(), longhand(PropertyID::ListStyleType)->to_string()));
  135. default:
  136. StringBuilder builder;
  137. auto first = true;
  138. for (auto& value : m_properties.values) {
  139. if (first)
  140. first = false;
  141. else
  142. builder.append(' ');
  143. builder.append(value->to_string());
  144. }
  145. return MUST(builder.to_string());
  146. }
  147. }
  148. }