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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. static tl::expected<css_property,css_parse_error> from_token(
  51. const css_parser_token &tok);
  52. constexpr auto to_string(void) const -> const char * {
  53. const char *ret = "nyi";
  54. switch(type) {
  55. case css_property_type::PROPERTY_FONT:
  56. ret = "font";
  57. break;
  58. case css_property_type::PROPERTY_FONT_COLOR:
  59. ret = "font-color";
  60. break;
  61. case css_property_type::PROPERTY_FONT_SIZE:
  62. ret = "font-size";
  63. break;
  64. case css_property_type::PROPERTY_COLOR:
  65. ret = "color";
  66. break;
  67. case css_property_type::PROPERTY_BGCOLOR:
  68. ret = "bgcolor";
  69. break;
  70. case css_property_type::PROPERTY_BACKGROUND:
  71. ret = "background";
  72. break;
  73. case css_property_type::PROPERTY_HEIGHT:
  74. ret = "height";
  75. break;
  76. case css_property_type::PROPERTY_WIDTH:
  77. ret = "width";
  78. break;
  79. case css_property_type::PROPERTY_DISPLAY:
  80. ret = "display";
  81. break;
  82. case css_property_type::PROPERTY_VISIBILITY:
  83. ret = "visibility";
  84. break;
  85. case css_property_type::PROPERTY_OPACITY:
  86. ret = "opacity";
  87. break;
  88. default:
  89. break;
  90. }
  91. return ret;
  92. }
  93. /* Helpers to define which values are valid for which properties */
  94. auto is_color(void) const -> bool {
  95. return type == css_property_type::PROPERTY_COLOR ||
  96. type == css_property_type::PROPERTY_BACKGROUND ||
  97. type == css_property_type::PROPERTY_BGCOLOR ||
  98. type == css_property_type::PROPERTY_FONT_COLOR ||
  99. type == css_property_type::PROPERTY_FONT;
  100. }
  101. auto is_dimension(void) const -> bool {
  102. return type == css_property_type::PROPERTY_HEIGHT ||
  103. type == css_property_type::PROPERTY_WIDTH ||
  104. type == css_property_type::PROPERTY_FONT_SIZE ||
  105. type == css_property_type::PROPERTY_FONT;
  106. }
  107. auto is_normal_number(void) const -> bool {
  108. return type == css_property_type::PROPERTY_OPACITY;
  109. }
  110. auto is_display(void) const -> bool {
  111. return type == css_property_type::PROPERTY_DISPLAY;
  112. }
  113. auto operator==(const css_property &other) const { return type == other.type; }
  114. };
  115. }
  116. /* Make properties hashable */
  117. namespace std {
  118. template<>
  119. class hash<rspamd::css::css_property> {
  120. public:
  121. /* Mix bits to provide slightly better distribution but being constexpr */
  122. constexpr size_t operator() (const rspamd::css::css_property &prop) const {
  123. std::size_t key = 0xdeadbeef ^static_cast<std::size_t>(prop.type);
  124. key = (~key) + (key << 21);
  125. key = key ^ (key >> 24);
  126. key = (key + (key << 3)) + (key << 8);
  127. key = key ^ (key >> 14);
  128. key = (key + (key << 2)) + (key << 4);
  129. key = key ^ (key >> 28);
  130. key = key + (key << 31);
  131. return key;
  132. }
  133. };
  134. }
  135. #endif //RSPAMD_CSS_PROPERTY_HXX