ComputedValues.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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::Overflow overflow() { return CSS::Overflow::Visible; }
  28. };
  29. struct BorderData {
  30. public:
  31. Color color { Color::Transparent };
  32. CSS::LineStyle line_style { CSS::LineStyle::None };
  33. float width { 0 };
  34. };
  35. class ComputedValues {
  36. public:
  37. CSS::Float float_() const { return m_noninherited.float_; }
  38. CSS::Clear clear() const { return m_noninherited.clear; }
  39. CSS::Cursor cursor() const { return m_inherited.cursor; }
  40. CSS::Display display() const { return m_noninherited.display; }
  41. Optional<int> z_index() const { return m_noninherited.z_index; }
  42. CSS::TextAlign text_align() const { return m_inherited.text_align; }
  43. CSS::TextDecorationLine text_decoration_line() const { return m_noninherited.text_decoration_line; }
  44. CSS::TextTransform text_transform() const { return m_inherited.text_transform; }
  45. CSS::Position position() const { return m_noninherited.position; }
  46. CSS::WhiteSpace white_space() const { return m_inherited.white_space; }
  47. CSS::FlexDirection flex_direction() const { return m_noninherited.flex_direction; }
  48. const CSS::Length& width() const { return m_noninherited.width; }
  49. const CSS::Length& min_width() const { return m_noninherited.min_width; }
  50. const CSS::Length& max_width() const { return m_noninherited.max_width; }
  51. const CSS::Length& height() const { return m_noninherited.height; }
  52. const CSS::Length& min_height() const { return m_noninherited.min_height; }
  53. const CSS::Length& max_height() const { return m_noninherited.max_height; }
  54. const CSS::LengthBox& offset() const { return m_noninherited.offset; }
  55. const CSS::LengthBox& margin() const { return m_noninherited.margin; }
  56. const CSS::LengthBox& padding() const { return m_noninherited.padding; }
  57. const BorderData& border_left() const { return m_noninherited.border_left; }
  58. const BorderData& border_top() const { return m_noninherited.border_top; }
  59. const BorderData& border_right() const { return m_noninherited.border_right; }
  60. const BorderData& border_bottom() const { return m_noninherited.border_bottom; }
  61. const CSS::Length& border_bottom_left_radius() const { return m_noninherited.border_bottom_left_radius; }
  62. const CSS::Length& border_bottom_right_radius() const { return m_noninherited.border_bottom_right_radius; }
  63. const CSS::Length& border_top_left_radius() const { return m_noninherited.border_top_left_radius; }
  64. const CSS::Length& border_top_right_radius() const { return m_noninherited.border_top_right_radius; }
  65. CSS::Overflow overflow_x() const { return m_noninherited.overflow_x; }
  66. CSS::Overflow overflow_y() const { return m_noninherited.overflow_y; }
  67. Color color() const { return m_inherited.color; }
  68. Color background_color() const { return m_noninherited.background_color; }
  69. CSS::Repeat background_repeat_x() const { return m_noninherited.background_repeat_x; }
  70. CSS::Repeat background_repeat_y() const { return m_noninherited.background_repeat_y; }
  71. CSS::ListStyleType list_style_type() const { return m_inherited.list_style_type; }
  72. ComputedValues clone_inherited_values() const
  73. {
  74. ComputedValues clone;
  75. clone.m_inherited = m_inherited;
  76. return clone;
  77. }
  78. protected:
  79. struct {
  80. Color color { InitialValues::color() };
  81. CSS::Cursor cursor { InitialValues::cursor() };
  82. CSS::TextAlign text_align { InitialValues::text_align() };
  83. CSS::TextTransform text_transform { InitialValues::text_transform() };
  84. CSS::WhiteSpace white_space { InitialValues::white_space() };
  85. CSS::ListStyleType list_style_type { InitialValues::list_style_type() };
  86. } m_inherited;
  87. struct {
  88. CSS::Float float_ { InitialValues::float_() };
  89. CSS::Clear clear { InitialValues::clear() };
  90. CSS::Display display { InitialValues::display() };
  91. Optional<int> z_index;
  92. CSS::TextDecorationLine text_decoration_line { InitialValues::text_decoration_line() };
  93. CSS::Position position { InitialValues::position() };
  94. CSS::Length width;
  95. CSS::Length min_width;
  96. CSS::Length max_width;
  97. CSS::Length height;
  98. CSS::Length min_height;
  99. CSS::Length max_height;
  100. CSS::LengthBox offset;
  101. CSS::LengthBox margin;
  102. CSS::LengthBox padding;
  103. BorderData border_left;
  104. BorderData border_top;
  105. BorderData border_right;
  106. BorderData border_bottom;
  107. Length border_bottom_left_radius;
  108. Length border_bottom_right_radius;
  109. Length border_top_left_radius;
  110. Length border_top_right_radius;
  111. Color background_color { InitialValues::background_color() };
  112. CSS::Repeat background_repeat_x { InitialValues::background_repeat() };
  113. CSS::Repeat background_repeat_y { InitialValues::background_repeat() };
  114. CSS::FlexDirection flex_direction { InitialValues::flex_direction() };
  115. CSS::Overflow overflow_x { InitialValues::overflow() };
  116. CSS::Overflow overflow_y { InitialValues::overflow() };
  117. } m_noninherited;
  118. };
  119. class ImmutableComputedValues final : public ComputedValues {
  120. };
  121. class MutableComputedValues final : public ComputedValues {
  122. public:
  123. void set_color(const Color& color) { m_inherited.color = color; }
  124. void set_cursor(CSS::Cursor cursor) { m_inherited.cursor = cursor; }
  125. void set_background_color(const Color& color) { m_noninherited.background_color = color; }
  126. void set_background_repeat_x(CSS::Repeat repeat) { m_noninherited.background_repeat_x = repeat; }
  127. void set_background_repeat_y(CSS::Repeat repeat) { m_noninherited.background_repeat_y = repeat; }
  128. void set_float(CSS::Float value) { m_noninherited.float_ = value; }
  129. void set_clear(CSS::Clear value) { m_noninherited.clear = value; }
  130. void set_z_index(Optional<int> value) { m_noninherited.z_index = value; }
  131. void set_text_align(CSS::TextAlign text_align) { m_inherited.text_align = text_align; }
  132. void set_text_decoration_line(CSS::TextDecorationLine value) { m_noninherited.text_decoration_line = value; }
  133. void set_text_transform(CSS::TextTransform value) { m_inherited.text_transform = value; }
  134. void set_position(CSS::Position position) { m_noninherited.position = position; }
  135. void set_white_space(CSS::WhiteSpace value) { m_inherited.white_space = value; }
  136. void set_width(const CSS::Length& width) { m_noninherited.width = width; }
  137. void set_min_width(const CSS::Length& width) { m_noninherited.min_width = width; }
  138. void set_max_width(const CSS::Length& width) { m_noninherited.max_width = width; }
  139. void set_height(const CSS::Length& height) { m_noninherited.height = height; }
  140. void set_min_height(const CSS::Length& height) { m_noninherited.min_height = height; }
  141. void set_max_height(const CSS::Length& height) { m_noninherited.max_height = height; }
  142. void set_offset(const CSS::LengthBox& offset) { m_noninherited.offset = offset; }
  143. void set_margin(const CSS::LengthBox& margin) { m_noninherited.margin = margin; }
  144. void set_padding(const CSS::LengthBox& padding) { m_noninherited.padding = padding; }
  145. void set_overflow_x(CSS::Overflow value) { m_noninherited.overflow_x = value; }
  146. void set_overflow_y(CSS::Overflow value) { m_noninherited.overflow_y = value; }
  147. void set_list_style_type(CSS::ListStyleType value) { m_inherited.list_style_type = value; }
  148. void set_display(CSS::Display value) { m_noninherited.display = value; }
  149. void set_border_bottom_left_radius(CSS::Length value) { m_noninherited.border_bottom_left_radius = value; }
  150. void set_border_bottom_right_radius(CSS::Length value) { m_noninherited.border_bottom_right_radius = value; }
  151. void set_border_top_left_radius(CSS::Length value) { m_noninherited.border_top_left_radius = value; }
  152. void set_border_top_right_radius(CSS::Length value) { m_noninherited.border_top_right_radius = value; }
  153. BorderData& border_left() { return m_noninherited.border_left; }
  154. BorderData& border_top() { return m_noninherited.border_top; }
  155. BorderData& border_right() { return m_noninherited.border_right; }
  156. BorderData& border_bottom() { return m_noninherited.border_bottom; }
  157. void set_flex_direction(CSS::FlexDirection value) { m_noninherited.flex_direction = value; }
  158. };
  159. }