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_selector.hxx 3.4KB

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_SELECTOR_HXX
  18. #define RSPAMD_CSS_SELECTOR_HXX
  19. #include <variant>
  20. #include <string>
  21. #include <optional>
  22. #include <vector>
  23. #include <memory>
  24. #include "function2/function2.hpp"
  25. #include "parse_error.hxx"
  26. #include "css_parser.hxx"
  27. #include "libserver/html/html_tags.h"
  28. #include "libcryptobox/cryptobox.h"
  29. namespace rspamd::css {
  30. /*
  31. * Holds a value for css selector, internal is handled by variant
  32. */
  33. struct css_selector {
  34. enum class selector_type {
  35. SELECTOR_TAG, /* e.g. tr, for this value we use tag_id_t */
  36. SELECTOR_CLASS, /* generic class, e.g. .class */
  37. SELECTOR_ID, /* e.g. #id */
  38. SELECTOR_ALL /* * selector */
  39. };
  40. selector_type type;
  41. std::variant<tag_id_t, std::string_view> value;
  42. /* Conditions for the css selector */
  43. /* Dependency on attributes */
  44. struct css_attribute_condition {
  45. std::string_view attribute;
  46. std::string_view op = "";
  47. std::string_view value = "";
  48. };
  49. /* General dependency chain */
  50. using css_selector_ptr = std::unique_ptr<css_selector>;
  51. using css_selector_dep = std::variant<css_attribute_condition, css_selector_ptr>;
  52. std::vector<css_selector_dep> dependencies;
  53. auto to_tag(void) const -> std::optional<tag_id_t>
  54. {
  55. if (type == selector_type::SELECTOR_TAG) {
  56. return std::get<tag_id_t>(value);
  57. }
  58. return std::nullopt;
  59. }
  60. auto to_string(void) const -> std::optional<const std::string_view>
  61. {
  62. if (type != selector_type::SELECTOR_TAG) {
  63. return std::string_view(std::get<std::string_view>(value));
  64. }
  65. return std::nullopt;
  66. };
  67. explicit css_selector(selector_type t)
  68. : type(t)
  69. {
  70. }
  71. explicit css_selector(tag_id_t t)
  72. : type(selector_type::SELECTOR_TAG)
  73. {
  74. value = t;
  75. }
  76. explicit css_selector(const std::string_view &st, selector_type t = selector_type::SELECTOR_ID)
  77. : type(t)
  78. {
  79. value = st;
  80. }
  81. auto operator==(const css_selector &other) const -> bool
  82. {
  83. return type == other.type && value == other.value;
  84. }
  85. auto debug_str(void) const -> std::string;
  86. };
  87. using selectors_vec = std::vector<std::unique_ptr<css_selector>>;
  88. /*
  89. * Consume selectors token and split them to the list of selectors
  90. */
  91. auto process_selector_tokens(rspamd_mempool_t *pool,
  92. blocks_gen_functor &&next_token_functor)
  93. -> selectors_vec;
  94. }// namespace rspamd::css
  95. /* Selectors hashing */
  96. namespace std {
  97. template<>
  98. class hash<rspamd::css::css_selector> {
  99. public:
  100. using is_avalanching = void;
  101. auto operator()(const rspamd::css::css_selector &sel) const -> std::size_t
  102. {
  103. if (sel.type == rspamd::css::css_selector::selector_type::SELECTOR_TAG) {
  104. return static_cast<std::size_t>(std::get<tag_id_t>(sel.value));
  105. }
  106. else {
  107. const auto &sv = std::get<std::string_view>(sel.value);
  108. return rspamd_cryptobox_fast_hash(sv.data(), sv.size(), 0xdeadbabe);
  109. }
  110. }
  111. };
  112. }// namespace std
  113. #endif//RSPAMD_CSS_SELECTOR_HXX