GridTrackSize.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. if (m_length_percentage.is_calculated()) {
  43. return CSS::Size::make_calculated(m_length_percentage.calculated());
  44. }
  45. return CSS::Size::make_percentage(m_length_percentage.percentage());
  46. }
  47. ErrorOr<String> GridSize::to_string() const
  48. {
  49. switch (m_type) {
  50. case Type::LengthPercentage:
  51. return m_length_percentage.to_string();
  52. case Type::FlexibleLength:
  53. return String::formatted("{}fr", m_flexible_length);
  54. case Type::MaxContent:
  55. return "max-content"_string;
  56. case Type::MinContent:
  57. return "min-content"_string;
  58. }
  59. VERIFY_NOT_REACHED();
  60. }
  61. GridMinMax::GridMinMax(GridSize min_grid_size, GridSize max_grid_size)
  62. : m_min_grid_size(min_grid_size)
  63. , m_max_grid_size(max_grid_size)
  64. {
  65. }
  66. ErrorOr<String> GridMinMax::to_string() const
  67. {
  68. StringBuilder builder;
  69. builder.append("minmax("sv);
  70. builder.appendff("{}", TRY(m_min_grid_size.to_string()));
  71. builder.append(", "sv);
  72. builder.appendff("{}", TRY(m_max_grid_size.to_string()));
  73. builder.append(")"sv);
  74. return builder.to_string();
  75. }
  76. GridRepeat::GridRepeat(GridTrackSizeList grid_track_size_list, int repeat_count)
  77. : m_type(Type::Default)
  78. , m_grid_track_size_list(grid_track_size_list)
  79. , m_repeat_count(repeat_count)
  80. {
  81. }
  82. GridRepeat::GridRepeat(GridTrackSizeList grid_track_size_list, Type type)
  83. : m_type(type)
  84. , m_grid_track_size_list(grid_track_size_list)
  85. {
  86. }
  87. GridRepeat::GridRepeat()
  88. {
  89. }
  90. ErrorOr<String> GridRepeat::to_string() const
  91. {
  92. StringBuilder builder;
  93. builder.append("repeat("sv);
  94. switch (m_type) {
  95. case Type::AutoFit:
  96. builder.append("auto-fill"sv);
  97. break;
  98. case Type::AutoFill:
  99. builder.append("auto-fit"sv);
  100. break;
  101. case Type::Default:
  102. builder.appendff("{}", m_repeat_count);
  103. break;
  104. default:
  105. VERIFY_NOT_REACHED();
  106. }
  107. builder.append(", "sv);
  108. builder.appendff("{}", TRY(m_grid_track_size_list.to_string()));
  109. builder.append(")"sv);
  110. return builder.to_string();
  111. }
  112. ExplicitGridTrack::ExplicitGridTrack(CSS::GridMinMax grid_minmax)
  113. : m_type(Type::MinMax)
  114. , m_grid_minmax(grid_minmax)
  115. {
  116. }
  117. ExplicitGridTrack::ExplicitGridTrack(CSS::GridRepeat grid_repeat)
  118. : m_type(Type::Repeat)
  119. , m_grid_repeat(grid_repeat)
  120. {
  121. }
  122. ExplicitGridTrack::ExplicitGridTrack(CSS::GridSize grid_size)
  123. : m_type(Type::Default)
  124. , m_grid_size(grid_size)
  125. {
  126. }
  127. ErrorOr<String> ExplicitGridTrack::to_string() const
  128. {
  129. switch (m_type) {
  130. case Type::MinMax:
  131. return m_grid_minmax.to_string();
  132. case Type::Repeat:
  133. return m_grid_repeat.to_string();
  134. case Type::Default:
  135. return m_grid_size.to_string();
  136. default:
  137. VERIFY_NOT_REACHED();
  138. }
  139. }
  140. GridTrackSizeList::GridTrackSizeList(Vector<CSS::ExplicitGridTrack> track_list, Vector<Vector<String>> line_names)
  141. : m_track_list(track_list)
  142. , m_line_names(line_names)
  143. {
  144. }
  145. GridTrackSizeList::GridTrackSizeList()
  146. : m_track_list({})
  147. , m_line_names({})
  148. {
  149. }
  150. GridTrackSizeList GridTrackSizeList::make_auto()
  151. {
  152. return GridTrackSizeList();
  153. }
  154. ErrorOr<String> GridTrackSizeList::to_string() const
  155. {
  156. StringBuilder builder;
  157. auto print_line_names = [&](size_t index) -> void {
  158. builder.append("["sv);
  159. for (size_t y = 0; y < m_line_names[index].size(); ++y) {
  160. builder.append(m_line_names[index][y]);
  161. if (y != m_line_names[index].size() - 1)
  162. builder.append(" "sv);
  163. }
  164. builder.append("]"sv);
  165. };
  166. for (size_t i = 0; i < m_track_list.size(); ++i) {
  167. if (m_line_names[i].size() > 0) {
  168. print_line_names(i);
  169. builder.append(" "sv);
  170. }
  171. builder.append(TRY(m_track_list[i].to_string()));
  172. if (i < m_track_list.size() - 1)
  173. builder.append(" "sv);
  174. }
  175. if (m_line_names[m_track_list.size()].size() > 0) {
  176. builder.append(" "sv);
  177. print_line_names(m_track_list.size());
  178. }
  179. return builder.to_string();
  180. }
  181. }