GridTrackSize.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2022, Martin Falisse <mfalisse@outlook.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "GridTrackSize.h"
  7. #include <AK/String.h>
  8. #include <LibWeb/CSS/Size.h>
  9. namespace Web::CSS {
  10. GridSize::GridSize(LengthPercentage length_percentage)
  11. : m_type(Type::LengthPercentage)
  12. , m_length_percentage(length_percentage) {};
  13. GridSize::GridSize(float flexible_length)
  14. : m_type(Type::FlexibleLength)
  15. , m_length_percentage { Length::make_px(0) }
  16. , m_flexible_length(flexible_length)
  17. {
  18. }
  19. GridSize::GridSize(Type type)
  20. : m_length_percentage { Length::make_auto() }
  21. {
  22. VERIFY(type == Type::MinContent || type == Type::MaxContent);
  23. m_type = type;
  24. }
  25. GridSize::GridSize()
  26. : m_type(Type::LengthPercentage)
  27. , m_length_percentage { Length::make_auto() }
  28. {
  29. }
  30. GridSize::~GridSize() = default;
  31. GridSize GridSize::make_auto()
  32. {
  33. return GridSize(CSS::Length::make_auto());
  34. }
  35. Size GridSize::css_size() const
  36. {
  37. VERIFY(m_type == Type::LengthPercentage);
  38. if (m_length_percentage.is_auto())
  39. return CSS::Size::make_auto();
  40. if (m_length_percentage.is_length())
  41. return CSS::Size::make_length(m_length_percentage.length());
  42. return CSS::Size::make_percentage(m_length_percentage.percentage());
  43. }
  44. ErrorOr<String> GridSize::to_string() const
  45. {
  46. switch (m_type) {
  47. case Type::LengthPercentage:
  48. return m_length_percentage.to_string();
  49. case Type::FlexibleLength:
  50. return String::formatted("{}fr", m_flexible_length);
  51. case Type::MaxContent:
  52. return "max-content"_string;
  53. case Type::MinContent:
  54. return "min-content"_string;
  55. }
  56. VERIFY_NOT_REACHED();
  57. }
  58. GridMinMax::GridMinMax(GridSize min_grid_size, GridSize max_grid_size)
  59. : m_min_grid_size(min_grid_size)
  60. , m_max_grid_size(max_grid_size)
  61. {
  62. }
  63. ErrorOr<String> GridMinMax::to_string() const
  64. {
  65. StringBuilder builder;
  66. builder.append("minmax("sv);
  67. builder.appendff("{}", TRY(m_min_grid_size.to_string()));
  68. builder.append(", "sv);
  69. builder.appendff("{}", TRY(m_max_grid_size.to_string()));
  70. builder.append(")"sv);
  71. return builder.to_string();
  72. }
  73. GridRepeat::GridRepeat(GridTrackSizeList grid_track_size_list, int repeat_count)
  74. : m_type(Type::Default)
  75. , m_grid_track_size_list(grid_track_size_list)
  76. , m_repeat_count(repeat_count)
  77. {
  78. }
  79. GridRepeat::GridRepeat(GridTrackSizeList grid_track_size_list, Type type)
  80. : m_type(type)
  81. , m_grid_track_size_list(grid_track_size_list)
  82. {
  83. }
  84. GridRepeat::GridRepeat()
  85. {
  86. }
  87. ErrorOr<String> GridRepeat::to_string() const
  88. {
  89. StringBuilder builder;
  90. builder.append("repeat("sv);
  91. switch (m_type) {
  92. case Type::AutoFit:
  93. builder.append("auto-fill"sv);
  94. break;
  95. case Type::AutoFill:
  96. builder.append("auto-fit"sv);
  97. break;
  98. case Type::Default:
  99. builder.appendff("{}", m_repeat_count);
  100. break;
  101. default:
  102. VERIFY_NOT_REACHED();
  103. }
  104. builder.append(", "sv);
  105. builder.appendff("{}", TRY(m_grid_track_size_list.to_string()));
  106. builder.append(")"sv);
  107. return builder.to_string();
  108. }
  109. ExplicitGridTrack::ExplicitGridTrack(CSS::GridMinMax grid_minmax)
  110. : m_type(Type::MinMax)
  111. , m_grid_minmax(grid_minmax)
  112. {
  113. }
  114. ExplicitGridTrack::ExplicitGridTrack(CSS::GridRepeat grid_repeat)
  115. : m_type(Type::Repeat)
  116. , m_grid_repeat(grid_repeat)
  117. {
  118. }
  119. ExplicitGridTrack::ExplicitGridTrack(CSS::GridSize grid_size)
  120. : m_type(Type::Default)
  121. , m_grid_size(grid_size)
  122. {
  123. }
  124. ErrorOr<String> ExplicitGridTrack::to_string() const
  125. {
  126. switch (m_type) {
  127. case Type::MinMax:
  128. return m_grid_minmax.to_string();
  129. case Type::Repeat:
  130. return m_grid_repeat.to_string();
  131. case Type::Default:
  132. return m_grid_size.to_string();
  133. default:
  134. VERIFY_NOT_REACHED();
  135. }
  136. }
  137. GridTrackSizeList::GridTrackSizeList(Vector<CSS::ExplicitGridTrack> track_list, Vector<Vector<String>> line_names)
  138. : m_track_list(track_list)
  139. , m_line_names(line_names)
  140. {
  141. }
  142. GridTrackSizeList::GridTrackSizeList()
  143. : m_track_list({})
  144. , m_line_names({})
  145. {
  146. }
  147. GridTrackSizeList GridTrackSizeList::make_auto()
  148. {
  149. return GridTrackSizeList();
  150. }
  151. ErrorOr<String> GridTrackSizeList::to_string() const
  152. {
  153. StringBuilder builder;
  154. auto print_line_names = [&](size_t index) -> void {
  155. builder.append("["sv);
  156. for (size_t y = 0; y < m_line_names[index].size(); ++y) {
  157. builder.append(m_line_names[index][y]);
  158. if (y != m_line_names[index].size() - 1)
  159. builder.append(" "sv);
  160. }
  161. builder.append("]"sv);
  162. };
  163. for (size_t i = 0; i < m_track_list.size(); ++i) {
  164. if (m_line_names[i].size() > 0) {
  165. print_line_names(i);
  166. builder.append(" "sv);
  167. }
  168. builder.append(TRY(m_track_list[i].to_string()));
  169. if (i < m_track_list.size() - 1)
  170. builder.append(" "sv);
  171. }
  172. if (m_line_names[m_track_list.size()].size() > 0) {
  173. builder.append(" "sv);
  174. print_line_names(m_track_list.size());
  175. }
  176. return builder.to_string();
  177. }
  178. }