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

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