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_property.hxx 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_PROPERTY_HXX
  18. #define RSPAMD_CSS_PROPERTY_HXX
  19. #include <string>
  20. #include "css_tokeniser.hxx"
  21. #include "parse_error.hxx"
  22. #include "contrib/expected/expected.hpp"
  23. namespace rspamd::css {
  24. /*
  25. * To be extended with properties that are interesting from the email
  26. * point of view
  27. */
  28. enum class css_property_type : std::uint16_t {
  29. PROPERTY_FONT = 0,
  30. PROPERTY_FONT_COLOR,
  31. PROPERTY_FONT_SIZE,
  32. PROPERTY_COLOR,
  33. PROPERTY_BGCOLOR,
  34. PROPERTY_BACKGROUND,
  35. PROPERTY_HEIGHT,
  36. PROPERTY_WIDTH,
  37. PROPERTY_DISPLAY,
  38. PROPERTY_VISIBILITY,
  39. PROPERTY_OPACITY,
  40. PROPERTY_NYI,
  41. };
  42. enum class css_property_flag : std::uint16_t {
  43. FLAG_NORMAL,
  44. FLAG_IMPORTANT,
  45. FLAG_NOT_IMPORTANT
  46. };
  47. struct alignas(int) css_property {
  48. css_property_type type;
  49. css_property_flag flag;
  50. css_property(css_property_type t, css_property_flag fl = css_property_flag::FLAG_NORMAL)
  51. : type(t), flag(fl)
  52. {
  53. }
  54. static tl::expected<css_property, css_parse_error> from_token(
  55. const css_parser_token &tok);
  56. constexpr auto to_string(void) const -> const char *
  57. {
  58. const char *ret = "nyi";
  59. switch (type) {
  60. case css_property_type::PROPERTY_FONT:
  61. ret = "font";
  62. break;
  63. case css_property_type::PROPERTY_FONT_COLOR:
  64. ret = "font-color";
  65. break;
  66. case css_property_type::PROPERTY_FONT_SIZE:
  67. ret = "font-size";
  68. break;
  69. case css_property_type::PROPERTY_COLOR:
  70. ret = "color";
  71. break;
  72. case css_property_type::PROPERTY_BGCOLOR:
  73. ret = "bgcolor";
  74. break;
  75. case css_property_type::PROPERTY_BACKGROUND:
  76. ret = "background";
  77. break;
  78. case css_property_type::PROPERTY_HEIGHT:
  79. ret = "height";
  80. break;
  81. case css_property_type::PROPERTY_WIDTH:
  82. ret = "width";
  83. break;
  84. case css_property_type::PROPERTY_DISPLAY:
  85. ret = "display";
  86. break;
  87. case css_property_type::PROPERTY_VISIBILITY:
  88. ret = "visibility";
  89. break;
  90. case css_property_type::PROPERTY_OPACITY:
  91. ret = "opacity";
  92. break;
  93. default:
  94. break;
  95. }
  96. return ret;
  97. }
  98. /* Helpers to define which values are valid for which properties */
  99. auto is_color(void) const -> bool
  100. {
  101. return type == css_property_type::PROPERTY_COLOR ||
  102. type == css_property_type::PROPERTY_BACKGROUND ||
  103. type == css_property_type::PROPERTY_BGCOLOR ||
  104. type == css_property_type::PROPERTY_FONT_COLOR ||
  105. type == css_property_type::PROPERTY_FONT;
  106. }
  107. auto is_dimension(void) const -> bool
  108. {
  109. return type == css_property_type::PROPERTY_HEIGHT ||
  110. type == css_property_type::PROPERTY_WIDTH ||
  111. type == css_property_type::PROPERTY_FONT_SIZE ||
  112. type == css_property_type::PROPERTY_FONT;
  113. }
  114. auto is_normal_number(void) const -> bool
  115. {
  116. return type == css_property_type::PROPERTY_OPACITY;
  117. }
  118. auto is_display(void) const -> bool
  119. {
  120. return type == css_property_type::PROPERTY_DISPLAY;
  121. }
  122. auto is_visibility(void) const -> bool
  123. {
  124. return type == css_property_type::PROPERTY_VISIBILITY;
  125. }
  126. auto operator==(const css_property &other) const
  127. {
  128. return type == other.type;
  129. }
  130. };
  131. }// namespace rspamd::css
  132. /* Make properties hashable */
  133. namespace std {
  134. template<>
  135. class hash<rspamd::css::css_property> {
  136. public:
  137. using is_avalanching = void;
  138. /* Mix bits to provide slightly better distribution but being constexpr */
  139. constexpr size_t operator()(const rspamd::css::css_property &prop) const
  140. {
  141. std::size_t key = 0xdeadbeef ^ static_cast<std::size_t>(prop.type);
  142. key = (~key) + (key << 21);
  143. key = key ^ (key >> 24);
  144. key = (key + (key << 3)) + (key << 8);
  145. key = key ^ (key >> 14);
  146. key = (key + (key << 2)) + (key << 4);
  147. key = key ^ (key >> 28);
  148. key = key + (key << 31);
  149. return key;
  150. }
  151. };
  152. }// namespace std
  153. #endif//RSPAMD_CSS_PROPERTY_HXX