ComputedValues.h 7.5 KB

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