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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #include "css.hxx"
  17. #include "contrib/ankerl/unordered_dense.h"
  18. #include "css_parser.hxx"
  19. #include "libserver/html/html_tag.hxx"
  20. #include "libserver/html/html_block.hxx"
  21. /* Keep unit tests implementation here (it'll possibly be moved outside one day) */
  22. #define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
  23. #define DOCTEST_CONFIG_IMPLEMENT
  24. #include "doctest/doctest.h"
  25. namespace rspamd::css {
  26. INIT_LOG_MODULE_PUBLIC(css);
  27. class css_style_sheet::impl {
  28. public:
  29. using sel_shared_hash = smart_ptr_hash<css_selector>;
  30. using sel_shared_eq = smart_ptr_equal<css_selector>;
  31. using selector_ptr = std::unique_ptr<css_selector>;
  32. using selectors_hash = ankerl::unordered_dense::map<selector_ptr, css_declarations_block_ptr,
  33. sel_shared_hash, sel_shared_eq>;
  34. using universal_selector_t = std::pair<selector_ptr, css_declarations_block_ptr>;
  35. selectors_hash tags_selector;
  36. selectors_hash class_selectors;
  37. selectors_hash id_selectors;
  38. std::optional<universal_selector_t> universal_selector;
  39. };
  40. css_style_sheet::css_style_sheet(rspamd_mempool_t *pool)
  41. : pool(pool), pimpl(new impl)
  42. {
  43. }
  44. css_style_sheet::~css_style_sheet()
  45. {
  46. }
  47. auto css_style_sheet::add_selector_rule(std::unique_ptr<css_selector> &&selector,
  48. css_declarations_block_ptr decls) -> void
  49. {
  50. impl::selectors_hash *target_hash = nullptr;
  51. switch (selector->type) {
  52. case css_selector::selector_type::SELECTOR_ALL:
  53. if (pimpl->universal_selector) {
  54. /* Another universal selector */
  55. msg_debug_css("redefined universal selector, merging rules");
  56. pimpl->universal_selector->second->merge_block(*decls);
  57. }
  58. else {
  59. msg_debug_css("added universal selector");
  60. pimpl->universal_selector = std::make_pair(std::move(selector),
  61. decls);
  62. }
  63. break;
  64. case css_selector::selector_type::SELECTOR_CLASS:
  65. target_hash = &pimpl->class_selectors;
  66. break;
  67. case css_selector::selector_type::SELECTOR_ID:
  68. target_hash = &pimpl->id_selectors;
  69. break;
  70. case css_selector::selector_type::SELECTOR_TAG:
  71. target_hash = &pimpl->tags_selector;
  72. break;
  73. }
  74. if (target_hash) {
  75. auto found_it = target_hash->find(selector);
  76. if (found_it == target_hash->end()) {
  77. /* Easy case, new element */
  78. target_hash->insert({std::move(selector), decls});
  79. }
  80. else {
  81. /* The problem with merging is actually in how to handle selectors chains
  82. * For example, we have 2 selectors:
  83. * 1. class id tag -> meaning that we first match class, then we ensure that
  84. * id is also the same and finally we check the tag
  85. * 2. tag class id -> it means that we check first tag, then class and then id
  86. * So we have somehow equal path in the xpath terms.
  87. * I suppose now, that we merely check parent stuff and handle duplicates
  88. * merging when finally resolving paths.
  89. */
  90. auto sel_str = selector->to_string().value_or("unknown");
  91. msg_debug_css("found duplicate selector: %*s", (int) sel_str.size(),
  92. sel_str.data());
  93. found_it->second->merge_block(*decls);
  94. }
  95. }
  96. }
  97. auto css_style_sheet::check_tag_block(const rspamd::html::html_tag *tag) -> rspamd::html::html_block *
  98. {
  99. std::optional<std::string_view> id_comp, class_comp;
  100. rspamd::html::html_block *res = nullptr;
  101. if (!tag) {
  102. return nullptr;
  103. }
  104. /* First, find id in a tag and a class */
  105. for (const auto &param: tag->components) {
  106. if (param.type == html::html_component_type::RSPAMD_HTML_COMPONENT_ID) {
  107. id_comp = param.value;
  108. }
  109. else if (param.type == html::html_component_type::RSPAMD_HTML_COMPONENT_CLASS) {
  110. class_comp = param.value;
  111. }
  112. }
  113. /* ID part */
  114. if (id_comp && !pimpl->id_selectors.empty()) {
  115. auto found_id_sel = pimpl->id_selectors.find(css_selector{id_comp.value()});
  116. if (found_id_sel != pimpl->id_selectors.end()) {
  117. const auto &decl = *(found_id_sel->second);
  118. res = decl.compile_to_block(pool);
  119. }
  120. }
  121. /* Class part */
  122. if (class_comp && !pimpl->class_selectors.empty()) {
  123. auto sv_split = [](auto strv, std::string_view delims = " ") -> std::vector<std::string_view> {
  124. std::vector<decltype(strv)> ret;
  125. std::size_t start = 0;
  126. while (start < strv.size()) {
  127. const auto last = strv.find_first_of(delims, start);
  128. if (start != last) {
  129. ret.emplace_back(strv.substr(start, last - start));
  130. }
  131. if (last == std::string_view::npos) {
  132. break;
  133. }
  134. start = last + 1;
  135. }
  136. return ret;
  137. };
  138. auto elts = sv_split(class_comp.value());
  139. for (const auto &e: elts) {
  140. auto found_class_sel = pimpl->class_selectors.find(
  141. css_selector{e, css_selector::selector_type::SELECTOR_CLASS});
  142. if (found_class_sel != pimpl->class_selectors.end()) {
  143. const auto &decl = *(found_class_sel->second);
  144. auto *tmp = decl.compile_to_block(pool);
  145. if (res == nullptr) {
  146. res = tmp;
  147. }
  148. else {
  149. res->propagate_block(*tmp);
  150. }
  151. }
  152. }
  153. }
  154. /* Tags part */
  155. if (!pimpl->tags_selector.empty()) {
  156. auto found_tag_sel = pimpl->tags_selector.find(
  157. css_selector{static_cast<tag_id_t>(tag->id)});
  158. if (found_tag_sel != pimpl->tags_selector.end()) {
  159. const auto &decl = *(found_tag_sel->second);
  160. auto *tmp = decl.compile_to_block(pool);
  161. if (res == nullptr) {
  162. res = tmp;
  163. }
  164. else {
  165. res->propagate_block(*tmp);
  166. }
  167. }
  168. }
  169. /* Finally, universal selector */
  170. if (pimpl->universal_selector) {
  171. auto *tmp = pimpl->universal_selector->second->compile_to_block(pool);
  172. if (res == nullptr) {
  173. res = tmp;
  174. }
  175. else {
  176. res->propagate_block(*tmp);
  177. }
  178. }
  179. return res;
  180. }
  181. auto css_parse_style(rspamd_mempool_t *pool,
  182. std::string_view input,
  183. std::shared_ptr<css_style_sheet> &&existing)
  184. -> css_return_pair
  185. {
  186. auto parse_res = rspamd::css::parse_css(pool, input,
  187. std::forward<std::shared_ptr<css_style_sheet>>(existing));
  188. if (parse_res.has_value()) {
  189. return std::make_pair(parse_res.value(), css_parse_error());
  190. }
  191. return std::make_pair(nullptr, parse_res.error());
  192. }
  193. }// namespace rspamd::css