ShorthandStyleValue.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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<CSSStyleValue 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<CSSStyleValue 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<CSSStyleValue 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::Columns: {
  84. auto column_width = longhand(PropertyID::ColumnWidth)->to_string();
  85. auto column_count = longhand(PropertyID::ColumnCount)->to_string();
  86. if (column_width == column_count)
  87. return column_width;
  88. if (column_width.equals_ignoring_ascii_case("auto"sv))
  89. return column_count;
  90. if (column_count.equals_ignoring_ascii_case("auto"sv))
  91. return column_width;
  92. return MUST(String::formatted("{} {}", column_width, column_count));
  93. }
  94. case PropertyID::Flex:
  95. return MUST(String::formatted("{} {} {}", longhand(PropertyID::FlexGrow)->to_string(), longhand(PropertyID::FlexShrink)->to_string(), longhand(PropertyID::FlexBasis)->to_string()));
  96. case PropertyID::FlexFlow:
  97. return MUST(String::formatted("{} {}", longhand(PropertyID::FlexDirection)->to_string(), longhand(PropertyID::FlexWrap)->to_string()));
  98. case PropertyID::Font:
  99. return MUST(String::formatted("{} {} {} {} {} / {} {}",
  100. longhand(PropertyID::FontStyle)->to_string(),
  101. longhand(PropertyID::FontVariant)->to_string(),
  102. longhand(PropertyID::FontWeight)->to_string(),
  103. longhand(PropertyID::FontWidth)->to_string(),
  104. longhand(PropertyID::FontSize)->to_string(),
  105. longhand(PropertyID::LineHeight)->to_string(),
  106. longhand(PropertyID::FontFamily)->to_string()));
  107. case PropertyID::GridArea: {
  108. auto& row_start = longhand(PropertyID::GridRowStart)->as_grid_track_placement();
  109. auto& column_start = longhand(PropertyID::GridColumnStart)->as_grid_track_placement();
  110. auto& row_end = longhand(PropertyID::GridRowEnd)->as_grid_track_placement();
  111. auto& column_end = longhand(PropertyID::GridColumnEnd)->as_grid_track_placement();
  112. StringBuilder builder;
  113. if (!row_start.grid_track_placement().is_auto())
  114. builder.appendff("{}", row_start.grid_track_placement().to_string());
  115. if (!column_start.grid_track_placement().is_auto())
  116. builder.appendff(" / {}", column_start.grid_track_placement().to_string());
  117. if (!row_end.grid_track_placement().is_auto())
  118. builder.appendff(" / {}", row_end.grid_track_placement().to_string());
  119. if (!column_end.grid_track_placement().is_auto())
  120. builder.appendff(" / {}", column_end.grid_track_placement().to_string());
  121. return MUST(builder.to_string());
  122. }
  123. // FIXME: Serialize Grid differently once we support it better!
  124. case PropertyID::Grid:
  125. case PropertyID::GridTemplate: {
  126. auto& areas = longhand(PropertyID::GridTemplateAreas)->as_grid_template_area();
  127. auto& rows = longhand(PropertyID::GridTemplateRows)->as_grid_track_size_list();
  128. auto& columns = longhand(PropertyID::GridTemplateColumns)->as_grid_track_size_list();
  129. auto construct_rows_string = [&]() {
  130. StringBuilder builder;
  131. size_t idx = 0;
  132. for (auto const& row : rows.grid_track_size_list().track_list()) {
  133. if (areas.grid_template_area().size() > idx) {
  134. builder.append("\""sv);
  135. for (size_t y = 0; y < areas.grid_template_area()[idx].size(); ++y) {
  136. builder.append(areas.grid_template_area()[idx][y]);
  137. if (y != areas.grid_template_area()[idx].size() - 1)
  138. builder.append(" "sv);
  139. }
  140. builder.append("\" "sv);
  141. }
  142. builder.append(row.to_string());
  143. if (idx < rows.grid_track_size_list().track_list().size() - 1)
  144. builder.append(' ');
  145. idx++;
  146. }
  147. return MUST(builder.to_string());
  148. };
  149. if (columns.grid_track_size_list().track_list().size() == 0)
  150. return MUST(String::formatted("{}", construct_rows_string()));
  151. return MUST(String::formatted("{} / {}", construct_rows_string(), columns.grid_track_size_list().to_string()));
  152. }
  153. case PropertyID::GridColumn: {
  154. auto start = longhand(PropertyID::GridColumnStart);
  155. auto end = longhand(PropertyID::GridColumnEnd);
  156. if (end->as_grid_track_placement().grid_track_placement().is_auto())
  157. return start->to_string();
  158. return MUST(String::formatted("{} / {}", start->to_string(), end->to_string()));
  159. }
  160. case PropertyID::GridRow: {
  161. auto start = longhand(PropertyID::GridRowStart);
  162. auto end = longhand(PropertyID::GridRowEnd);
  163. if (end->as_grid_track_placement().grid_track_placement().is_auto())
  164. return start->to_string();
  165. return MUST(String::formatted("{} / {}", start->to_string(), end->to_string()));
  166. }
  167. case PropertyID::ListStyle:
  168. return MUST(String::formatted("{} {} {}", longhand(PropertyID::ListStylePosition)->to_string(), longhand(PropertyID::ListStyleImage)->to_string(), longhand(PropertyID::ListStyleType)->to_string()));
  169. case PropertyID::Overflow:
  170. return MUST(String::formatted("{} {}", longhand(PropertyID::OverflowX)->to_string(), longhand(PropertyID::OverflowY)->to_string()));
  171. case PropertyID::PlaceContent: {
  172. auto align_content = longhand(PropertyID::AlignContent)->to_string();
  173. auto justify_content = longhand(PropertyID::JustifyContent)->to_string();
  174. if (align_content == justify_content)
  175. return align_content;
  176. return MUST(String::formatted("{} {}", align_content, justify_content));
  177. }
  178. case PropertyID::PlaceItems: {
  179. auto align_items = longhand(PropertyID::AlignItems)->to_string();
  180. auto justify_items = longhand(PropertyID::JustifyItems)->to_string();
  181. if (align_items == justify_items)
  182. return align_items;
  183. return MUST(String::formatted("{} {}", align_items, justify_items));
  184. }
  185. case PropertyID::PlaceSelf: {
  186. auto align_self = longhand(PropertyID::AlignSelf)->to_string();
  187. auto justify_self = longhand(PropertyID::JustifySelf)->to_string();
  188. if (align_self == justify_self)
  189. return align_self;
  190. return MUST(String::formatted("{} {}", align_self, justify_self));
  191. }
  192. case PropertyID::TextDecoration:
  193. return MUST(String::formatted("{} {} {} {}", longhand(PropertyID::TextDecorationLine)->to_string(), longhand(PropertyID::TextDecorationThickness)->to_string(), longhand(PropertyID::TextDecorationStyle)->to_string(), longhand(PropertyID::TextDecorationColor)->to_string()));
  194. default:
  195. StringBuilder builder;
  196. auto first = true;
  197. for (auto& value : m_properties.values) {
  198. if (first)
  199. first = false;
  200. else
  201. builder.append(' ');
  202. builder.append(value->to_string());
  203. }
  204. return MUST(builder.to_string());
  205. }
  206. }
  207. }