diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-02-17 15:56:58 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-02-17 15:56:58 +0000 |
commit | 66f078e7f124c357eae417c01837e25761701128 (patch) | |
tree | 1417c9fecdedea01bb74ce7b977649fe2a433edf /src/libserver/css/css_parser.cxx | |
parent | 61a70903cc1a37c51fda5a80ee5bd0a1c838a1fe (diff) | |
download | rspamd-66f078e7f124c357eae417c01837e25761701128.tar.gz rspamd-66f078e7f124c357eae417c01837e25761701128.zip |
[Project] Css: Start semantic parsing for rules
Diffstat (limited to 'src/libserver/css/css_parser.cxx')
-rw-r--r-- | src/libserver/css/css_parser.cxx | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/src/libserver/css/css_parser.cxx b/src/libserver/css/css_parser.cxx index 084d1b50a..a0a9d8847 100644 --- a/src/libserver/css/css_parser.cxx +++ b/src/libserver/css/css_parser.cxx @@ -22,6 +22,7 @@ namespace rspamd::css { +struct css_consumed_block; /* * Represents a consumed token by a parser */ @@ -33,7 +34,8 @@ struct css_consumed_block { css_simple_block, css_function, css_function_arg, - css_component + css_component, + css_selector, }; using consumed_block_ptr = std::unique_ptr<css_consumed_block>; @@ -81,6 +83,17 @@ struct 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 = ""; @@ -107,6 +120,9 @@ struct css_consumed_block { case parser_tag_type::css_component: ret = "component"; break; + case parser_tag_type::css_selector: + ret = "selector"; + break; } return ret; @@ -570,6 +586,35 @@ bool css_parser::consume_input(const std::string_view &sv) } + 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()); |