namespace rspamd::css {
+struct css_consumed_block;
/*
* Represents a consumed token by a parser
*/
css_simple_block,
css_function,
css_function_arg,
- css_component
+ css_component,
+ css_selector,
};
using consumed_block_ptr = std::unique_ptr<css_consumed_block>;
content = std::move(tok);
}
+ /* Empty blocks used to avoid type checks in loops */
+ const inline static std::vector<consumed_block_ptr> empty_block_vec{};
+
+ auto get_blocks_or_empty() const -> const std::vector<consumed_block_ptr>& {
+ if (content.index() == 1) {
+ return std::get<std::vector<consumed_block_ptr>>(content);
+ }
+
+ return empty_block_vec;
+ }
+
auto token_type_str(void) const -> const char *
{
const auto *ret = "";
case parser_tag_type::css_component:
ret = "component";
break;
+ case parser_tag_type::css_selector:
+ ret = "selector";
+ break;
}
return ret;
}
+ const auto &rules = consumed_blocks->get_blocks_or_empty();
+
+ for (auto &&rule : rules) {
+ /*
+ * For now, we do not need any of the at rules, so we can safely ignore them
+ */
+ auto &&children = rule->get_blocks_or_empty();
+
+ if (children.size() > 1 &&
+ children[0]->tag == css_consumed_block::parser_tag_type::css_component) {
+ auto simple_block = std::find_if(children.begin(), children.end(),
+ [](auto &bl) {
+ return bl->tag == css_consumed_block::parser_tag_type::css_simple_block;
+ });
+
+ if (simple_block != children.end()) {
+ /*
+ * We have a component and a simple block,
+ * so we can parse a declaration
+ */
+
+ /* First, tag all components as preamble */
+ for (auto it = children.begin(); it != simple_block; ++it) {
+ (*it)->tag = css_consumed_block::parser_tag_type::css_selector;
+ }
+ }
+ }
+ }
+
auto debug_str = consumed_blocks->debug_str();
msg_debug_css("consumed css: {%*s}", (int)debug_str.size(), debug_str.data());
#include <optional>
#include "contrib/expected/expected.hpp"
#include "parse_error.hxx"
+#include "css_tokeniser.hxx"
#include "html_tags.h"
namespace rspamd::css {
selector_type type;
std::variant<tag_id_t, std::string> value;
- constexpr std::optional<tag_id_t> to_tag (void) const {
+ auto to_tag(void) const -> std::optional<tag_id_t> {
if (type == selector_type::SELECTOR_ELEMENT) {
return std::get<tag_id_t>(value);
}
return std::nullopt;
}
- constexpr std::optional<const std::string_view> to_string (void) const {
+ auto to_string(void) const -> std::optional<const std::string_view> {
if (type == selector_type::SELECTOR_ELEMENT) {
return std::string_view(std::get<std::string>(value));
}
size_t inlen);
};
+
+
}
#endif //RSPAMD_CSS_SELECTOR_HXX