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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/robin-hood/robin_hood.h"
  23. #include "libutil/cxx/util.hxx"
  24. #include <vector>
  25. #include <memory>
  26. namespace rspamd::css {
  27. class css_rule {
  28. css_property prop;
  29. using css_values_vec = std::vector<css_value>;
  30. css_values_vec values;
  31. public:
  32. /* We must create css rule explicitly from a property and values */
  33. css_rule() = delete;
  34. css_rule(const css_rule &other) = delete;
  35. /* Constructors */
  36. css_rule(css_rule &&other) noexcept = default;
  37. explicit css_rule(css_property &&prop, css_values_vec &&values) noexcept :
  38. prop(prop), values(std::forward<css_values_vec>(values)) {}
  39. explicit css_rule(const css_property &prop) noexcept : prop(prop), values{} {}
  40. /* Methods */
  41. /* Comparison is special, as we care merely about property, not the values */
  42. auto operator==(const css_rule &other) const { return prop == other.prop; }
  43. constexpr const css_values_vec &get_values(void) const { return values; }
  44. constexpr const css_property &get_prop(void) const { return prop; }
  45. /* Import values from another rules according to the importance */
  46. void override_values(const css_rule &other);
  47. void merge_values(const css_rule &other);
  48. void add_value(const css_value &value);
  49. };
  50. }
  51. /* Make rules hashable by property */
  52. namespace std {
  53. template<>
  54. class hash<rspamd::css::css_rule> {
  55. public:
  56. constexpr auto operator()(const rspamd::css::css_rule &rule) const -> auto {
  57. return hash<rspamd::css::css_property>()(rule.get_prop());
  58. }
  59. };
  60. }
  61. namespace rspamd::css {
  62. class css_declarations_block {
  63. public:
  64. using rule_shared_ptr = std::shared_ptr<css_rule>;
  65. using rule_shared_hash = shared_ptr_hash<css_rule>;
  66. using rule_shared_eq = shared_ptr_equal<css_rule>;
  67. enum class merge_type {
  68. merge_duplicate,
  69. merge_parent,
  70. merge_override
  71. };
  72. css_declarations_block() = default;
  73. auto add_rule(rule_shared_ptr &&rule) -> bool;
  74. auto merge_block(const css_declarations_block &other,
  75. merge_type how = merge_type::merge_duplicate) -> void;
  76. auto get_rules(void) const -> const auto & {
  77. return rules;
  78. }
  79. auto has_rule(const css_rule &rule) const -> bool {
  80. return (rules.find(rule) != rules.end());
  81. }
  82. private:
  83. robin_hood::unordered_flat_set<rule_shared_ptr, rule_shared_hash, rule_shared_eq> rules;
  84. };
  85. using css_declarations_block_ptr = std::shared_ptr<css_declarations_block>;
  86. auto process_declaration_tokens(rspamd_mempool_t *pool,
  87. blocks_gen_functor &&next_token_functor)
  88. -> css_declarations_block_ptr;
  89. }
  90. #endif //RSPAMD_CSS_RULE_HXX