ComputedValues.h 7.9 KB

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