ShorthandStyleValue.cpp 11 KB

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