You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

css_value.hxx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*-
  2. * Copyright 2021 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #ifndef RSPAMD_CSS_VALUE_HXX
  18. #define RSPAMD_CSS_VALUE_HXX
  19. #include <string>
  20. #include <variant>
  21. #include <optional>
  22. #include <vector>
  23. #include <iosfwd>
  24. #include "parse_error.hxx"
  25. #include "css_parser.hxx"
  26. #include "contrib/expected/expected.hpp"
  27. namespace rspamd::css {
  28. struct alignas(int) css_color {
  29. std::uint8_t r;
  30. std::uint8_t g;
  31. std::uint8_t b;
  32. std::uint8_t alpha;
  33. css_color(std::uint8_t _r, std::uint8_t _g, std::uint8_t _b, std::uint8_t _alpha = 255)
  34. : r(_r), g(_g), b(_b), alpha(_alpha)
  35. {
  36. }
  37. css_color() = default;
  38. constexpr auto to_number() const -> std::uint32_t
  39. {
  40. return (std::uint32_t) alpha << 24 |
  41. (std::uint32_t) r << 16 |
  42. (std::uint32_t) g << 8 |
  43. (std::uint32_t) b << 0;
  44. }
  45. constexpr auto to_rgb() const -> std::uint32_t
  46. {
  47. return (std::uint32_t) r << 16 |
  48. (std::uint32_t) g << 8 |
  49. (std::uint32_t) b << 0;
  50. }
  51. friend bool operator==(const css_color &l, const css_color &r)
  52. {
  53. return (memcmp(&l, &r, sizeof(css_color)) == 0);
  54. }
  55. static auto white() -> css_color
  56. {
  57. return css_color{255, 255, 255};
  58. }
  59. static auto black() -> css_color
  60. {
  61. return css_color{0, 0, 0};
  62. }
  63. };
  64. struct css_dimension {
  65. float dim;
  66. bool is_percent;
  67. };
  68. /*
  69. * Simple enum class for display stuff
  70. */
  71. enum class css_display_value : std::uint8_t {
  72. DISPLAY_INLINE,
  73. DISPLAY_BLOCK,
  74. DISPLAY_TABLE_ROW,
  75. DISPLAY_HIDDEN
  76. };
  77. /*
  78. * Value handler, uses std::variant instead of polymorphic classes for now
  79. * for simplicity
  80. */
  81. struct css_value {
  82. std::variant<css_color,
  83. float,
  84. css_display_value,
  85. css_dimension,
  86. std::monostate>
  87. value;
  88. css_value()
  89. {
  90. }
  91. css_value(const css_color &color)
  92. : value(color)
  93. {
  94. }
  95. css_value(float num)
  96. : value(num)
  97. {
  98. }
  99. css_value(css_dimension dim)
  100. : value(dim)
  101. {
  102. }
  103. css_value(css_display_value d)
  104. : value(d)
  105. {
  106. }
  107. auto to_color(void) const -> std::optional<css_color>
  108. {
  109. return extract_value_maybe<css_color>();
  110. }
  111. auto to_number(void) const -> std::optional<float>
  112. {
  113. return extract_value_maybe<float>();
  114. }
  115. auto to_dimension(void) const -> std::optional<css_dimension>
  116. {
  117. return extract_value_maybe<css_dimension>();
  118. }
  119. auto to_display(void) const -> std::optional<css_display_value>
  120. {
  121. return extract_value_maybe<css_display_value>();
  122. }
  123. auto is_valid(void) const -> bool
  124. {
  125. return !(std::holds_alternative<std::monostate>(value));
  126. }
  127. auto debug_str() const -> std::string;
  128. static auto maybe_color_from_string(const std::string_view &input)
  129. -> std::optional<css_value>;
  130. static auto maybe_color_from_hex(const std::string_view &input)
  131. -> std::optional<css_value>;
  132. static auto maybe_color_from_function(const css_consumed_block::css_function_block &func)
  133. -> std::optional<css_value>;
  134. static auto maybe_dimension_from_number(const css_parser_token &tok)
  135. -> std::optional<css_value>;
  136. static auto maybe_display_from_string(const std::string_view &input)
  137. -> std::optional<css_value>;
  138. private:
  139. template<typename T>
  140. auto extract_value_maybe(void) const -> std::optional<T>
  141. {
  142. if (std::holds_alternative<T>(value)) {
  143. return std::get<T>(value);
  144. }
  145. return std::nullopt;
  146. }
  147. };
  148. }// namespace rspamd::css
  149. #endif//RSPAMD_CSS_VALUE_HXX