ShorthandStyleValue.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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(SerializationMode mode) const
  34. {
  35. // If all the longhands are the same CSS-wide keyword, just return that once.
  36. Optional<Keyword> built_in_keyword;
  37. bool all_same_keyword = true;
  38. for (auto& value : m_properties.values) {
  39. if (!value->is_css_wide_keyword()) {
  40. all_same_keyword = false;
  41. break;
  42. }
  43. auto keyword = value->to_keyword();
  44. if (!built_in_keyword.has_value()) {
  45. built_in_keyword = keyword;
  46. continue;
  47. }
  48. if (built_in_keyword != keyword) {
  49. all_same_keyword = false;
  50. break;
  51. }
  52. }
  53. if (all_same_keyword && built_in_keyword.has_value())
  54. return m_properties.values.first()->to_string(mode);
  55. // Then special cases
  56. switch (m_properties.shorthand_property) {
  57. case PropertyID::Background: {
  58. auto color = longhand(PropertyID::BackgroundColor);
  59. auto image = longhand(PropertyID::BackgroundImage);
  60. auto position = longhand(PropertyID::BackgroundPosition);
  61. auto size = longhand(PropertyID::BackgroundSize);
  62. auto repeat = longhand(PropertyID::BackgroundRepeat);
  63. auto attachment = longhand(PropertyID::BackgroundAttachment);
  64. auto origin = longhand(PropertyID::BackgroundOrigin);
  65. auto clip = longhand(PropertyID::BackgroundClip);
  66. auto get_layer_count = [](auto style_value) -> size_t {
  67. return style_value->is_value_list() ? style_value->as_value_list().size() : 1;
  68. };
  69. 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)))))));
  70. if (layer_count == 1) {
  71. return MUST(String::formatted("{} {} {} {} {} {} {} {}", color->to_string(mode), image->to_string(mode), position->to_string(mode), size->to_string(mode), repeat->to_string(mode), attachment->to_string(mode), origin->to_string(mode), clip->to_string(mode)));
  72. }
  73. auto get_layer_value_string = [mode](ValueComparingRefPtr<CSSStyleValue const> const& style_value, size_t index) {
  74. if (style_value->is_value_list())
  75. return style_value->as_value_list().value_at(index, true)->to_string(mode);
  76. return style_value->to_string(mode);
  77. };
  78. StringBuilder builder;
  79. for (size_t i = 0; i < layer_count; i++) {
  80. if (i)
  81. builder.append(", "sv);
  82. if (i == layer_count - 1)
  83. builder.appendff("{} ", color->to_string(mode));
  84. 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));
  85. }
  86. return MUST(builder.to_string());
  87. }
  88. case PropertyID::BorderRadius: {
  89. auto& top_left = longhand(PropertyID::BorderTopLeftRadius)->as_border_radius();
  90. auto& top_right = longhand(PropertyID::BorderTopRightRadius)->as_border_radius();
  91. auto& bottom_right = longhand(PropertyID::BorderBottomRightRadius)->as_border_radius();
  92. auto& bottom_left = longhand(PropertyID::BorderBottomLeftRadius)->as_border_radius();
  93. return MUST(String::formatted("{} {} {} {} / {} {} {} {}",
  94. top_left.horizontal_radius().to_string(),
  95. top_right.horizontal_radius().to_string(),
  96. bottom_right.horizontal_radius().to_string(),
  97. bottom_left.horizontal_radius().to_string(),
  98. top_left.vertical_radius().to_string(),
  99. top_right.vertical_radius().to_string(),
  100. bottom_right.vertical_radius().to_string(),
  101. bottom_left.vertical_radius().to_string()));
  102. }
  103. case PropertyID::Columns: {
  104. auto column_width = longhand(PropertyID::ColumnWidth)->to_string(mode);
  105. auto column_count = longhand(PropertyID::ColumnCount)->to_string(mode);
  106. if (column_width == column_count)
  107. return column_width;
  108. if (column_width.equals_ignoring_ascii_case("auto"sv))
  109. return column_count;
  110. if (column_count.equals_ignoring_ascii_case("auto"sv))
  111. return column_width;
  112. return MUST(String::formatted("{} {}", column_width, column_count));
  113. }
  114. case PropertyID::Flex:
  115. return MUST(String::formatted("{} {} {}", longhand(PropertyID::FlexGrow)->to_string(mode), longhand(PropertyID::FlexShrink)->to_string(mode), longhand(PropertyID::FlexBasis)->to_string(mode)));
  116. case PropertyID::FlexFlow:
  117. return MUST(String::formatted("{} {}", longhand(PropertyID::FlexDirection)->to_string(mode), longhand(PropertyID::FlexWrap)->to_string(mode)));
  118. case PropertyID::Font:
  119. return MUST(String::formatted("{} {} {} {} {} / {} {}",
  120. longhand(PropertyID::FontStyle)->to_string(mode),
  121. longhand(PropertyID::FontVariant)->to_string(mode),
  122. longhand(PropertyID::FontWeight)->to_string(mode),
  123. longhand(PropertyID::FontWidth)->to_string(mode),
  124. longhand(PropertyID::FontSize)->to_string(mode),
  125. longhand(PropertyID::LineHeight)->to_string(mode),
  126. longhand(PropertyID::FontFamily)->to_string(mode)));
  127. case PropertyID::GridArea: {
  128. auto& row_start = longhand(PropertyID::GridRowStart)->as_grid_track_placement();
  129. auto& column_start = longhand(PropertyID::GridColumnStart)->as_grid_track_placement();
  130. auto& row_end = longhand(PropertyID::GridRowEnd)->as_grid_track_placement();
  131. auto& column_end = longhand(PropertyID::GridColumnEnd)->as_grid_track_placement();
  132. StringBuilder builder;
  133. if (!row_start.grid_track_placement().is_auto())
  134. builder.appendff("{}", row_start.grid_track_placement().to_string());
  135. if (!column_start.grid_track_placement().is_auto())
  136. builder.appendff(" / {}", column_start.grid_track_placement().to_string());
  137. if (!row_end.grid_track_placement().is_auto())
  138. builder.appendff(" / {}", row_end.grid_track_placement().to_string());
  139. if (!column_end.grid_track_placement().is_auto())
  140. builder.appendff(" / {}", column_end.grid_track_placement().to_string());
  141. return MUST(builder.to_string());
  142. }
  143. // FIXME: Serialize Grid differently once we support it better!
  144. case PropertyID::Grid:
  145. case PropertyID::GridTemplate: {
  146. auto& areas = longhand(PropertyID::GridTemplateAreas)->as_grid_template_area();
  147. auto& rows = longhand(PropertyID::GridTemplateRows)->as_grid_track_size_list();
  148. auto& columns = longhand(PropertyID::GridTemplateColumns)->as_grid_track_size_list();
  149. auto construct_rows_string = [&]() {
  150. StringBuilder builder;
  151. size_t idx = 0;
  152. for (auto const& row : rows.grid_track_size_list().track_list()) {
  153. if (areas.grid_template_area().size() > idx) {
  154. builder.append("\""sv);
  155. for (size_t y = 0; y < areas.grid_template_area()[idx].size(); ++y) {
  156. builder.append(areas.grid_template_area()[idx][y]);
  157. if (y != areas.grid_template_area()[idx].size() - 1)
  158. builder.append(" "sv);
  159. }
  160. builder.append("\" "sv);
  161. }
  162. builder.append(row.to_string());
  163. if (idx < rows.grid_track_size_list().track_list().size() - 1)
  164. builder.append(' ');
  165. idx++;
  166. }
  167. return MUST(builder.to_string());
  168. };
  169. if (columns.grid_track_size_list().track_list().size() == 0)
  170. return MUST(String::formatted("{}", construct_rows_string()));
  171. return MUST(String::formatted("{} / {}", construct_rows_string(), columns.grid_track_size_list().to_string()));
  172. }
  173. case PropertyID::GridColumn: {
  174. auto start = longhand(PropertyID::GridColumnStart);
  175. auto end = longhand(PropertyID::GridColumnEnd);
  176. if (end->as_grid_track_placement().grid_track_placement().is_auto())
  177. return start->to_string(mode);
  178. return MUST(String::formatted("{} / {}", start->to_string(mode), end->to_string(mode)));
  179. }
  180. case PropertyID::GridRow: {
  181. auto start = longhand(PropertyID::GridRowStart);
  182. auto end = longhand(PropertyID::GridRowEnd);
  183. if (end->as_grid_track_placement().grid_track_placement().is_auto())
  184. return start->to_string(mode);
  185. return MUST(String::formatted("{} / {}", start->to_string(mode), end->to_string(mode)));
  186. }
  187. case PropertyID::ListStyle:
  188. return MUST(String::formatted("{} {} {}", longhand(PropertyID::ListStylePosition)->to_string(mode), longhand(PropertyID::ListStyleImage)->to_string(mode), longhand(PropertyID::ListStyleType)->to_string(mode)));
  189. case PropertyID::Overflow: {
  190. auto overflow_x = longhand(PropertyID::OverflowX);
  191. auto overflow_y = longhand(PropertyID::OverflowY);
  192. if (overflow_x == overflow_y)
  193. return overflow_x->to_string(mode);
  194. return MUST(String::formatted("{} {}", overflow_x->to_string(mode), overflow_y->to_string(mode)));
  195. }
  196. case PropertyID::PlaceContent: {
  197. auto align_content = longhand(PropertyID::AlignContent)->to_string(mode);
  198. auto justify_content = longhand(PropertyID::JustifyContent)->to_string(mode);
  199. if (align_content == justify_content)
  200. return align_content;
  201. return MUST(String::formatted("{} {}", align_content, justify_content));
  202. }
  203. case PropertyID::PlaceItems: {
  204. auto align_items = longhand(PropertyID::AlignItems)->to_string(mode);
  205. auto justify_items = longhand(PropertyID::JustifyItems)->to_string(mode);
  206. if (align_items == justify_items)
  207. return align_items;
  208. return MUST(String::formatted("{} {}", align_items, justify_items));
  209. }
  210. case PropertyID::PlaceSelf: {
  211. auto align_self = longhand(PropertyID::AlignSelf)->to_string(mode);
  212. auto justify_self = longhand(PropertyID::JustifySelf)->to_string(mode);
  213. if (align_self == justify_self)
  214. return align_self;
  215. return MUST(String::formatted("{} {}", align_self, justify_self));
  216. }
  217. case PropertyID::TextDecoration: {
  218. // The rule here seems to be, only print what's different from the default value,
  219. // but if they're all default, print the line.
  220. StringBuilder builder;
  221. auto append_if_non_default = [&](PropertyID property_id) {
  222. auto value = longhand(property_id);
  223. if (!value->equals(property_initial_value(property_id))) {
  224. if (!builder.is_empty())
  225. builder.append(' ');
  226. builder.append(value->to_string(mode));
  227. }
  228. };
  229. append_if_non_default(PropertyID::TextDecorationLine);
  230. append_if_non_default(PropertyID::TextDecorationThickness);
  231. append_if_non_default(PropertyID::TextDecorationStyle);
  232. append_if_non_default(PropertyID::TextDecorationColor);
  233. if (builder.is_empty())
  234. return longhand(PropertyID::TextDecorationLine)->to_string(mode);
  235. return builder.to_string_without_validation();
  236. }
  237. default:
  238. StringBuilder builder;
  239. auto first = true;
  240. for (auto& value : m_properties.values) {
  241. if (first)
  242. first = false;
  243. else
  244. builder.append(' ');
  245. builder.append(value->to_string(mode));
  246. }
  247. return MUST(builder.to_string());
  248. }
  249. }
  250. }