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_rule.hxx 3.8KB

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_RULE_HXX
  18. #define RSPAMD_CSS_RULE_HXX
  19. #include "css_value.hxx"
  20. #include "css_property.hxx"
  21. #include "css_parser.hxx"
  22. #include "contrib/ankerl/unordered_dense.h"
  23. #include "libutil/cxx/util.hxx"
  24. #include "libutil/cxx/hash_util.hxx"
  25. #include <vector>
  26. #include <memory>
  27. namespace rspamd::html {
  28. /* Forward declaration */
  29. struct html_block;
  30. }// namespace rspamd::html
  31. namespace rspamd::css {
  32. class css_rule {
  33. css_property prop;
  34. using css_values_vec = std::vector<css_value>;
  35. css_values_vec values;
  36. public:
  37. /* We must create css rule explicitly from a property and values */
  38. css_rule() = delete;
  39. css_rule(const css_rule &other) = delete;
  40. /* Constructors */
  41. css_rule(css_rule &&other) noexcept = default;
  42. explicit css_rule(css_property &&prop, css_values_vec &&values) noexcept
  43. : prop(prop), values(std::forward<css_values_vec>(values))
  44. {
  45. }
  46. explicit css_rule(const css_property &prop) noexcept
  47. : prop(prop), values{}
  48. {
  49. }
  50. /* Methods */
  51. /* Comparison is special, as we care merely about property, not the values */
  52. auto operator==(const css_rule &other) const
  53. {
  54. return prop == other.prop;
  55. }
  56. constexpr const css_values_vec &get_values(void) const
  57. {
  58. return values;
  59. }
  60. constexpr const css_property &get_prop(void) const
  61. {
  62. return prop;
  63. }
  64. /* Import values from another rules according to the importance */
  65. void override_values(const css_rule &other);
  66. void merge_values(const css_rule &other);
  67. void add_value(const css_value &value);
  68. };
  69. }// namespace rspamd::css
  70. /* Make rules hashable by property */
  71. namespace std {
  72. template<>
  73. class hash<rspamd::css::css_rule> {
  74. public:
  75. using is_avalanching = void;
  76. constexpr auto operator()(const rspamd::css::css_rule &rule) const -> auto
  77. {
  78. return hash<rspamd::css::css_property>()(rule.get_prop());
  79. }
  80. };
  81. }// namespace std
  82. namespace rspamd::css {
  83. /**
  84. * Class that is designed to hold css declaration (a set of rules)
  85. */
  86. class css_declarations_block {
  87. public:
  88. using rule_shared_ptr = std::shared_ptr<css_rule>;
  89. using rule_shared_hash = smart_ptr_hash<css_rule>;
  90. using rule_shared_eq = smart_ptr_equal<css_rule>;
  91. enum class merge_type {
  92. merge_duplicate,
  93. merge_parent,
  94. merge_override
  95. };
  96. css_declarations_block() = default;
  97. auto add_rule(rule_shared_ptr rule) -> bool;
  98. auto merge_block(const css_declarations_block &other,
  99. merge_type how = merge_type::merge_duplicate) -> void;
  100. auto get_rules(void) const -> const auto &
  101. {
  102. return rules;
  103. }
  104. /**
  105. * Returns if a declaration block has some property
  106. * @param prop
  107. * @return
  108. */
  109. auto has_property(const css_property &prop) const -> bool
  110. {
  111. return (rules.find(css_rule{prop}) != rules.end());
  112. }
  113. /**
  114. * Compile CSS declaration to the html block
  115. * @param pool used to carry memory required for html_block
  116. * @return html block structure
  117. */
  118. auto compile_to_block(rspamd_mempool_t *pool) const -> rspamd::html::html_block *;
  119. private:
  120. ankerl::unordered_dense::set<rule_shared_ptr, rule_shared_hash, rule_shared_eq> rules;
  121. };
  122. using css_declarations_block_ptr = std::shared_ptr<css_declarations_block>;
  123. auto process_declaration_tokens(rspamd_mempool_t *pool,
  124. blocks_gen_functor &&next_token_functor)
  125. -> css_declarations_block_ptr;
  126. }// namespace rspamd::css
  127. #endif//RSPAMD_CSS_RULE_HXX