ComputedValues.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Optional.h>
  8. #include <LibWeb/CSS/LengthBox.h>
  9. #include <LibWeb/CSS/StyleValue.h>
  10. namespace Web::CSS {
  11. class InitialValues {
  12. public:
  13. static CSS::Float float_() { return CSS::Float::None; }
  14. static CSS::Clear clear() { return CSS::Clear::None; }
  15. static CSS::Cursor cursor() { return CSS::Cursor::Auto; }
  16. static CSS::WhiteSpace white_space() { return CSS::WhiteSpace::Normal; }
  17. static CSS::TextAlign text_align() { return CSS::TextAlign::Left; }
  18. static CSS::Position position() { return CSS::Position::Static; }
  19. static CSS::TextDecorationLine text_decoration_line() { return CSS::TextDecorationLine::None; }
  20. static CSS::TextTransform text_transform() { return CSS::TextTransform::None; }
  21. static CSS::Display display() { return CSS::Display::Inline; }
  22. static Color color() { return Color::Black; }
  23. static Color background_color() { return Color::Transparent; }
  24. static CSS::Repeat background_repeat() { return CSS::Repeat::Repeat; }
  25. static CSS::ListStyleType list_style_type() { return CSS::ListStyleType::Disc; }
  26. static CSS::FlexDirection flex_direction() { return CSS::FlexDirection::Row; }
  27. static CSS::FlexWrap flex_wrap() { return CSS::FlexWrap::Nowrap; }
  28. static CSS::JustifyContent justify_content() { return CSS::JustifyContent::FlexStart; }
  29. static CSS::Overflow overflow() { return CSS::Overflow::Visible; }
  30. };
  31. struct BorderData {
  32. public:
  33. Color color { Color::Transparent };
  34. CSS::LineStyle line_style { CSS::LineStyle::None };
  35. float width { 0 };
  36. };
  37. struct FlexBasisData {
  38. CSS::FlexBasis type { CSS::FlexBasis::Content };
  39. CSS::Length length {};
  40. };
  41. class ComputedValues {
  42. public:
  43. CSS::Float float_() const { return m_noninherited.float_; }
  44. CSS::Clear clear() const { return m_noninherited.clear; }
  45. CSS::Cursor cursor() const { return m_inherited.cursor; }
  46. CSS::Display display() const { return m_noninherited.display; }
  47. Optional<int> z_index() const { return m_noninherited.z_index; }
  48. CSS::TextAlign text_align() const { return m_inherited.text_align; }
  49. CSS::TextDecorationLine text_decoration_line() const { return m_noninherited.text_decoration_line; }
  50. CSS::TextTransform text_transform() const { return m_inherited.text_transform; }
  51. CSS::Position position() const { return m_noninherited.position; }
  52. CSS::WhiteSpace white_space() const { return m_inherited.white_space; }
  53. CSS::FlexDirection flex_direction() const { return m_noninherited.flex_direction; }
  54. CSS::FlexWrap flex_wrap() const { return m_noninherited.flex_wrap; }
  55. FlexBasisData flex_basis() const { return m_noninherited.flex_basis; }
  56. Optional<float> flex_grow_factor() const { return m_noninherited.flex_grow_factor; }
  57. Optional<float> flex_shrink_factor() const { return m_noninherited.flex_shrink_factor; }
  58. CSS::JustifyContent justify_content() const { return m_noninherited.justify_content; }
  59. const CSS::Length& width() const { return m_noninherited.width; }
  60. const CSS::Length& min_width() const { return m_noninherited.min_width; }
  61. const CSS::Length& max_width() const { return m_noninherited.max_width; }
  62. const CSS::Length& height() const { return m_noninherited.height; }
  63. const CSS::Length& min_height() const { return m_noninherited.min_height; }
  64. const CSS::Length& max_height() const { return m_noninherited.max_height; }
  65. const CSS::LengthBox& offset() const { return m_noninherited.offset; }
  66. const CSS::LengthBox& margin() const { return m_noninherited.margin; }
  67. const CSS::LengthBox& padding() const { return m_noninherited.padding; }
  68. const BorderData& border_left() const { return m_noninherited.border_left; }
  69. const BorderData& border_top() const { return m_noninherited.border_top; }
  70. const BorderData& border_right() const { return m_noninherited.border_right; }
  71. const BorderData& border_bottom() const { return m_noninherited.border_bottom; }
  72. const CSS::Length& border_bottom_left_radius() const { return m_noninherited.border_bottom_left_radius; }
  73. const CSS::Length& border_bottom_right_radius() const { return m_noninherited.border_bottom_right_radius; }
  74. const CSS::Length& border_top_left_radius() const { return m_noninherited.border_top_left_radius; }
  75. const CSS::Length& border_top_right_radius() const { return m_noninherited.border_top_right_radius; }
  76. CSS::Overflow overflow_x() const { return m_noninherited.overflow_x; }
  77. CSS::Overflow overflow_y() const { return m_noninherited.overflow_y; }
  78. Color color() const { return m_inherited.color; }
  79. Color background_color() const { return m_noninherited.background_color; }
  80. CSS::Repeat background_repeat_x() const { return m_noninherited.background_repeat_x; }
  81. CSS::Repeat background_repeat_y() const { return m_noninherited.background_repeat_y; }
  82. CSS::ListStyleType list_style_type() const { return m_inherited.list_style_type; }
  83. ComputedValues clone_inherited_values() const
  84. {
  85. ComputedValues clone;
  86. clone.m_inherited = m_inherited;
  87. return clone;
  88. }
  89. protected:
  90. struct {
  91. Color color { InitialValues::color() };
  92. CSS::Cursor cursor { InitialValues::cursor() };
  93. CSS::TextAlign text_align { InitialValues::text_align() };
  94. CSS::TextTransform text_transform { InitialValues::text_transform() };
  95. CSS::WhiteSpace white_space { InitialValues::white_space() };
  96. CSS::ListStyleType list_style_type { InitialValues::list_style_type() };
  97. } m_inherited;
  98. struct {
  99. CSS::Float float_ { InitialValues::float_() };
  100. CSS::Clear clear { InitialValues::clear() };
  101. CSS::Display display { InitialValues::display() };
  102. Optional<int> z_index;
  103. CSS::TextDecorationLine text_decoration_line { InitialValues::text_decoration_line() };
  104. CSS::Position position { InitialValues::position() };
  105. CSS::Length width;
  106. CSS::Length min_width;
  107. CSS::Length max_width;
  108. CSS::Length height;
  109. CSS::Length min_height;
  110. CSS::Length max_height;
  111. CSS::LengthBox offset;
  112. CSS::LengthBox margin;
  113. CSS::LengthBox padding;
  114. BorderData border_left;
  115. BorderData border_top;
  116. BorderData border_right;
  117. BorderData border_bottom;
  118. Length border_bottom_left_radius;
  119. Length border_bottom_right_radius;
  120. Length border_top_left_radius;
  121. Length border_top_right_radius;
  122. Color background_color { InitialValues::background_color() };
  123. CSS::Repeat background_repeat_x { InitialValues::background_repeat() };
  124. CSS::Repeat background_repeat_y { InitialValues::background_repeat() };
  125. CSS::FlexDirection flex_direction { InitialValues::flex_direction() };
  126. CSS::FlexWrap flex_wrap { InitialValues::flex_wrap() };
  127. CSS::FlexBasisData flex_basis {};
  128. Optional<float> flex_grow_factor;
  129. Optional<float> flex_shrink_factor;
  130. CSS::JustifyContent justify_content { InitialValues::justify_content() };
  131. CSS::Overflow overflow_x { InitialValues::overflow() };
  132. CSS::Overflow overflow_y { InitialValues::overflow() };
  133. } m_noninherited;
  134. };
  135. class ImmutableComputedValues final : public ComputedValues {
  136. };
  137. class MutableComputedValues final : public ComputedValues {
  138. public:
  139. void set_color(const Color& color) { m_inherited.color = color; }
  140. void set_cursor(CSS::Cursor cursor) { m_inherited.cursor = cursor; }
  141. void set_background_color(const Color& color) { m_noninherited.background_color = color; }
  142. void set_background_repeat_x(CSS::Repeat repeat) { m_noninherited.background_repeat_x = repeat; }
  143. void set_background_repeat_y(CSS::Repeat repeat) { m_noninherited.background_repeat_y = repeat; }
  144. void set_float(CSS::Float value) { m_noninherited.float_ = value; }
  145. void set_clear(CSS::Clear value) { m_noninherited.clear = value; }
  146. void set_z_index(Optional<int> value) { m_noninherited.z_index = value; }
  147. void set_text_align(CSS::TextAlign text_align) { m_inherited.text_align = text_align; }
  148. void set_text_decoration_line(CSS::TextDecorationLine value) { m_noninherited.text_decoration_line = value; }
  149. void set_text_transform(CSS::TextTransform value) { m_inherited.text_transform = value; }
  150. void set_position(CSS::Position position) { m_noninherited.position = position; }
  151. void set_white_space(CSS::WhiteSpace value) { m_inherited.white_space = value; }
  152. void set_width(const CSS::Length& width) { m_noninherited.width = width; }
  153. void set_min_width(const CSS::Length& width) { m_noninherited.min_width = width; }
  154. void set_max_width(const CSS::Length& width) { m_noninherited.max_width = width; }
  155. void set_height(const CSS::Length& height) { m_noninherited.height = height; }
  156. void set_min_height(const CSS::Length& height) { m_noninherited.min_height = height; }
  157. void set_max_height(const CSS::Length& height) { m_noninherited.max_height = height; }
  158. void set_offset(const CSS::LengthBox& offset) { m_noninherited.offset = offset; }
  159. void set_margin(const CSS::LengthBox& margin) { m_noninherited.margin = margin; }
  160. void set_padding(const CSS::LengthBox& padding) { m_noninherited.padding = padding; }
  161. void set_overflow_x(CSS::Overflow value) { m_noninherited.overflow_x = value; }
  162. void set_overflow_y(CSS::Overflow value) { m_noninherited.overflow_y = value; }
  163. void set_list_style_type(CSS::ListStyleType value) { m_inherited.list_style_type = value; }
  164. void set_display(CSS::Display value) { m_noninherited.display = value; }
  165. void set_border_bottom_left_radius(CSS::Length value) { m_noninherited.border_bottom_left_radius = value; }
  166. void set_border_bottom_right_radius(CSS::Length value) { m_noninherited.border_bottom_right_radius = value; }
  167. void set_border_top_left_radius(CSS::Length value) { m_noninherited.border_top_left_radius = value; }
  168. void set_border_top_right_radius(CSS::Length value) { m_noninherited.border_top_right_radius = value; }
  169. BorderData& border_left() { return m_noninherited.border_left; }
  170. BorderData& border_top() { return m_noninherited.border_top; }
  171. BorderData& border_right() { return m_noninherited.border_right; }
  172. BorderData& border_bottom() { return m_noninherited.border_bottom; }
  173. void set_flex_direction(CSS::FlexDirection value) { m_noninherited.flex_direction = value; }
  174. void set_flex_wrap(CSS::FlexWrap value) { m_noninherited.flex_wrap = value; }
  175. void set_flex_basis(FlexBasisData value) { m_noninherited.flex_basis = value; }
  176. void set_flex_grow_factor(Optional<float> value) { m_noninherited.flex_grow_factor = value; }
  177. void set_flex_shrink_factor(Optional<float> value) { m_noninherited.flex_shrink_factor = value; }
  178. void set_justify_content(CSS::JustifyContent value) { m_noninherited.justify_content = value; }
  179. };
  180. }