ComputedValues.h 9.0 KB

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