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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "html_tags.h"
  28. namespace rspamd::css {
  29. /*
  30. * Holds a value for css selector, internal is handled by variant
  31. */
  32. struct css_selector {
  33. enum class selector_type {
  34. SELECTOR_ELEMENT, /* e.g. tr, for this value we use tag_id_t */
  35. SELECTOR_CLASS, /* generic class, e.g. .class */
  36. SELECTOR_ID, /* e.g. #id */
  37. SELECTOR_ALL /* * selector */
  38. };
  39. selector_type type;
  40. std::variant<tag_id_t, std::string_view> value;
  41. /* Conditions for the css selector */
  42. /* Dependency on attributes */
  43. struct css_attribute_condition {
  44. std::string_view attribute;
  45. std::string_view op = "";
  46. std::string_view value = "";
  47. };
  48. /* General dependency chain */
  49. using css_selector_ptr = std::unique_ptr<css_selector>;
  50. using css_selector_dep = std::variant<css_attribute_condition, css_selector_ptr>;
  51. std::vector<css_selector_dep> dependencies;
  52. auto to_tag(void) const -> std::optional<tag_id_t> {
  53. if (type == selector_type::SELECTOR_ELEMENT) {
  54. return std::get<tag_id_t>(value);
  55. }
  56. return std::nullopt;
  57. }
  58. auto to_string(void) const -> std::optional<const std::string_view> {
  59. if (type == selector_type::SELECTOR_ELEMENT) {
  60. return std::string_view(std::get<std::string_view>(value));
  61. }
  62. return std::nullopt;
  63. };
  64. explicit css_selector(selector_type t) : type(t) {}
  65. auto debug_str(void) const -> std::string;
  66. };
  67. using selectors_vec = std::vector<std::unique_ptr<css_selector>>;
  68. /*
  69. * Consume selectors token and split them to the list of selectors
  70. */
  71. auto process_selector_tokens(rspamd_mempool_t *pool,
  72. blocks_gen_functor &&next_token_functor)
  73. -> selectors_vec;
  74. }
  75. #endif //RSPAMD_CSS_SELECTOR_HXX