ComputedValues.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Optional.h>
  28. #include <LibWeb/CSS/LengthBox.h>
  29. #include <LibWeb/CSS/StyleValue.h>
  30. namespace Web::CSS {
  31. class InitialValues {
  32. public:
  33. static CSS::Float float_() { return CSS::Float::None; }
  34. static CSS::Clear clear() { return CSS::Clear::None; }
  35. static CSS::Cursor cursor() { return CSS::Cursor::Auto; }
  36. static CSS::WhiteSpace white_space() { return CSS::WhiteSpace::Normal; }
  37. static CSS::TextAlign text_align() { return CSS::TextAlign::Left; }
  38. static CSS::Position position() { return CSS::Position::Static; }
  39. static CSS::TextDecorationLine text_decoration_line() { return CSS::TextDecorationLine::None; }
  40. static CSS::TextTransform text_transform() { return CSS::TextTransform::None; }
  41. static CSS::Display display() { return CSS::Display::Inline; }
  42. static Color color() { return Color::Black; }
  43. static Color background_color() { return Color::Transparent; }
  44. static CSS::Repeat background_repeat() { return CSS::Repeat::Repeat; }
  45. static CSS::ListStyleType list_style_type() { return CSS::ListStyleType::Disc; }
  46. static CSS::FlexDirection flex_direction() { return CSS::FlexDirection::Row; }
  47. static CSS::Overflow overflow() { return CSS::Overflow::Visible; }
  48. };
  49. struct BorderData {
  50. public:
  51. Color color { Color::Transparent };
  52. CSS::LineStyle line_style { CSS::LineStyle::None };
  53. float width { 0 };
  54. };
  55. class ComputedValues {
  56. public:
  57. CSS::Float float_() const { return m_noninherited.float_; }
  58. CSS::Clear clear() const { return m_noninherited.clear; }
  59. CSS::Cursor cursor() const { return m_inherited.cursor; }
  60. CSS::Display display() const { return m_noninherited.display; }
  61. Optional<int> z_index() const { return m_noninherited.z_index; }
  62. CSS::TextAlign text_align() const { return m_inherited.text_align; }
  63. CSS::TextDecorationLine text_decoration_line() const { return m_noninherited.text_decoration_line; }
  64. CSS::TextTransform text_transform() const { return m_inherited.text_transform; }
  65. CSS::Position position() const { return m_noninherited.position; }
  66. CSS::WhiteSpace white_space() const { return m_inherited.white_space; }
  67. CSS::FlexDirection flex_direction() const { return m_noninherited.flex_direction; }
  68. const CSS::Length& width() const { return m_noninherited.width; }
  69. const CSS::Length& min_width() const { return m_noninherited.min_width; }
  70. const CSS::Length& max_width() const { return m_noninherited.max_width; }
  71. const CSS::Length& height() const { return m_noninherited.height; }
  72. const CSS::Length& min_height() const { return m_noninherited.min_height; }
  73. const CSS::Length& max_height() const { return m_noninherited.max_height; }
  74. const CSS::LengthBox& offset() const { return m_noninherited.offset; }
  75. const CSS::LengthBox& margin() const { return m_noninherited.margin; }
  76. const CSS::LengthBox& padding() const { return m_noninherited.padding; }
  77. const BorderData& border_left() const { return m_noninherited.border_left; }
  78. const BorderData& border_top() const { return m_noninherited.border_top; }
  79. const BorderData& border_right() const { return m_noninherited.border_right; }
  80. const BorderData& border_bottom() const { return m_noninherited.border_bottom; }
  81. CSS::Overflow overflow_x() const { return m_noninherited.overflow_x; }
  82. CSS::Overflow overflow_y() const { return m_noninherited.overflow_y; }
  83. Color color() const { return m_inherited.color; }
  84. Color background_color() const { return m_noninherited.background_color; }
  85. CSS::Repeat background_repeat_x() const { return m_noninherited.background_repeat_x; }
  86. CSS::Repeat background_repeat_y() const { return m_noninherited.background_repeat_y; }
  87. CSS::ListStyleType list_style_type() const { return m_inherited.list_style_type; }
  88. ComputedValues clone_inherited_values() const
  89. {
  90. ComputedValues clone;
  91. clone.m_inherited = m_inherited;
  92. return clone;
  93. }
  94. protected:
  95. struct {
  96. Color color { InitialValues::color() };
  97. CSS::Cursor cursor { InitialValues::cursor() };
  98. CSS::TextAlign text_align { InitialValues::text_align() };
  99. CSS::TextTransform text_transform { InitialValues::text_transform() };
  100. CSS::WhiteSpace white_space { InitialValues::white_space() };
  101. CSS::ListStyleType list_style_type { InitialValues::list_style_type() };
  102. } m_inherited;
  103. struct {
  104. CSS::Float float_ { InitialValues::float_() };
  105. CSS::Clear clear { InitialValues::clear() };
  106. CSS::Display display { InitialValues::display() };
  107. Optional<int> z_index;
  108. CSS::TextDecorationLine text_decoration_line { InitialValues::text_decoration_line() };
  109. CSS::Position position { InitialValues::position() };
  110. CSS::Length width;
  111. CSS::Length min_width;
  112. CSS::Length max_width;
  113. CSS::Length height;
  114. CSS::Length min_height;
  115. CSS::Length max_height;
  116. CSS::LengthBox offset;
  117. CSS::LengthBox margin;
  118. CSS::LengthBox padding;
  119. BorderData border_left;
  120. BorderData border_top;
  121. BorderData border_right;
  122. BorderData border_bottom;
  123. Color background_color { InitialValues::background_color() };
  124. CSS::Repeat background_repeat_x { InitialValues::background_repeat() };
  125. CSS::Repeat background_repeat_y { InitialValues::background_repeat() };
  126. CSS::FlexDirection flex_direction { InitialValues::flex_direction() };
  127. CSS::Overflow overflow_x { InitialValues::overflow() };
  128. CSS::Overflow overflow_y { InitialValues::overflow() };
  129. } m_noninherited;
  130. };
  131. class ImmutableComputedValues final : public ComputedValues {
  132. };
  133. class MutableComputedValues final : public ComputedValues {
  134. public:
  135. void set_color(const Color& color) { m_inherited.color = color; }
  136. void set_cursor(CSS::Cursor cursor) { m_inherited.cursor = cursor; }
  137. void set_background_color(const Color& color) { m_noninherited.background_color = color; }
  138. void set_background_repeat_x(CSS::Repeat repeat) { m_noninherited.background_repeat_x = repeat; }
  139. void set_background_repeat_y(CSS::Repeat repeat) { m_noninherited.background_repeat_y = repeat; }
  140. void set_float(CSS::Float value) { m_noninherited.float_ = value; }
  141. void set_clear(CSS::Clear value) { m_noninherited.clear = value; }
  142. void set_z_index(Optional<int> value) { m_noninherited.z_index = value; }
  143. void set_text_align(CSS::TextAlign text_align) { m_inherited.text_align = text_align; }
  144. void set_text_decoration_line(CSS::TextDecorationLine value) { m_noninherited.text_decoration_line = value; }
  145. void set_text_transform(CSS::TextTransform value) { m_inherited.text_transform = value; }
  146. void set_position(CSS::Position position) { m_noninherited.position = position; }
  147. void set_white_space(CSS::WhiteSpace value) { m_inherited.white_space = value; }
  148. void set_width(const CSS::Length& width) { m_noninherited.width = width; }
  149. void set_min_width(const CSS::Length& width) { m_noninherited.min_width = width; }
  150. void set_max_width(const CSS::Length& width) { m_noninherited.max_width = width; }
  151. void set_height(const CSS::Length& height) { m_noninherited.height = height; }
  152. void set_min_height(const CSS::Length& height) { m_noninherited.min_height = height; }
  153. void set_max_height(const CSS::Length& height) { m_noninherited.max_height = height; }
  154. void set_offset(const CSS::LengthBox& offset) { m_noninherited.offset = offset; }
  155. void set_margin(const CSS::LengthBox& margin) { m_noninherited.margin = margin; }
  156. void set_padding(const CSS::LengthBox& padding) { m_noninherited.padding = padding; }
  157. void set_overflow_x(CSS::Overflow value) { m_noninherited.overflow_x = value; }
  158. void set_overflow_y(CSS::Overflow value) { m_noninherited.overflow_y = value; }
  159. void set_list_style_type(CSS::ListStyleType value) { m_inherited.list_style_type = value; }
  160. void set_display(CSS::Display value) { m_noninherited.display = value; }
  161. BorderData& border_left() { return m_noninherited.border_left; }
  162. BorderData& border_top() { return m_noninherited.border_top; }
  163. BorderData& border_right() { return m_noninherited.border_right; }
  164. BorderData& border_bottom() { return m_noninherited.border_bottom; }
  165. void set_flex_direction(CSS::FlexDirection value) { m_noninherited.flex_direction = value; }
  166. };
  167. }