]> source.dussan.org Git - rspamd.git/commitdiff
[Project] CSS: Various fixes in the declarations and values parsing
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 4 Mar 2021 20:39:29 +0000 (20:39 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 4 Mar 2021 20:39:29 +0000 (20:39 +0000)
src/libserver/css/css_parser.cxx
src/libserver/css/css_parser.hxx
src/libserver/css/css_rule.cxx
src/libserver/css/css_value.cxx

index 1d2916e5e3b476e859ab4916e927a7c94cf905f9..776901ffee9a24cd94f1d56baa7aa237275dabc6 100644 (file)
@@ -579,9 +579,7 @@ bool css_parser::consume_input(const std::string_view &sv)
 
                                                ++selector_it;
 
-                                               if (ret->get_token_or_empty().type != css_parser_token::token_type::eof_token) {
-                                                       return *ret;
-                                               }
+                                               return *ret;
                                        }
                                };
 
@@ -600,9 +598,7 @@ bool css_parser::consume_input(const std::string_view &sv)
 
                                                ++decls_it;
 
-                                               if (ret->get_token_or_empty().type != css_parser_token::token_type::eof_token) {
-                                                       return *ret;
-                                               }
+                                               return *ret;
                                        }
                                };
 
index 5b2d93ae585c58a534cdebc855b8686ed3d74b06..887d846ad35e7d58e88a2586ff0d650812bb1aeb 100644 (file)
@@ -39,7 +39,7 @@ namespace rspamd::css {
 class css_consumed_block {
 public:
        enum class parser_tag_type : std::uint8_t  {
-               css_top_block,
+               css_top_block = 0,
                css_qualified_rule,
                css_at_rule,
                css_simple_block,
@@ -133,7 +133,7 @@ public:
        }
 
        auto get_function_or_invalid() const -> const css_function_block& {
-               if (is_token()) {
+               if (is_function()) {
                        return std::get<css_function_block>(content);
                }
 
index ce35dc6b093da03275f0091238d8a3e2bbe63517..0e99240299ccbc429176a19f9d8ea8e2de3c5484 100644 (file)
@@ -30,7 +30,7 @@ allowed_property_value(const css_property &prop, const css_consumed_block &parse
                        if (tok.type == css_parser_token::token_type::hash_token) {
                                return css_value::maybe_color_from_hex(tok.get_string_or_default(""));
                        }
-                       else if (tok.type == css_parser_token::token_type::string_token) {
+                       else if (tok.type == css_parser_token::token_type::ident_token) {
                                return css_value::maybe_color_from_string(tok.get_string_or_default(""));
                        }
                }
@@ -65,6 +65,7 @@ auto process_declaration_tokens(rspamd_mempool_t *pool,
 
                switch (next_tok.tag) {
                case css_consumed_block::parser_tag_type::css_component:
+                       /* Component can be a property or a compound list of values */
                        if (state == parse_property) {
                                cur_property = css_property::from_token(next_tok.get_token_or_empty())
                                                .value_or(bad_property);
@@ -81,7 +82,6 @@ auto process_declaration_tokens(rspamd_mempool_t *pool,
                                const auto &expect_colon_block = next_block_functor();
 
                                if (expect_colon_block.tag != css_consumed_block::parser_tag_type::css_component) {
-
                                        state = ignore_value; /* Ignore up to the next rule */
                                }
                                else {
@@ -130,16 +130,23 @@ auto process_declaration_tokens(rspamd_mempool_t *pool,
                        }
                        break;
                case css_consumed_block::parser_tag_type::css_function:
-               case css_consumed_block::parser_tag_type::css_function_arg:
                        if (state == parse_value) {
-                               auto maybe_value = css_value::from_css_block(next_tok);
+                               auto maybe_value = allowed_property_value(cur_property, next_tok);
 
                                if (maybe_value) {
+                                       msg_debug_css("added value %s to the property %s",
+                                                       maybe_value.value().debug_str().c_str(),
+                                                       cur_property.to_string());
                                        cur_rule->add_value(maybe_value.value());
                                }
                        }
                        break;
                case css_consumed_block::parser_tag_type::css_eof_block:
+                       if (state == parse_value) {
+                               ret.push_back(std::move(cur_rule));
+                       }
+                       can_continue = false;
+                       break;
                default:
                        can_continue = false;
                        break;
index c0ed9242c15a8fb567d942fbf2efef1b20cd75ec..aa842e2e27942f3017466da62e33e13ffea8a63a 100644 (file)
@@ -67,6 +67,13 @@ auto css_value::maybe_color_from_hex(const std::string_view &input)
                                hexpair_decode(input[4], input[5]));
                return css_value(col);
        }
+       else if (input.length() == 3) {
+               /* Rgb as 3 hex digests */
+               css_color col(hexpair_decode(input[0], input[0]),
+                               hexpair_decode(input[1], input[1]),
+                               hexpair_decode(input[2], input[2]));
+               return css_value(col);
+       }
        else if (input.length() == 8) {
                /* RGBA */
                css_color col(hexpair_decode(input[0], input[1]),
@@ -290,7 +297,7 @@ auto css_value::debug_str() const -> std::string
 {
        std::string ret;
 
-       std::visit([&](auto& arg) {
+       std::visit([&](const auto& arg) {
                using T = std::decay_t<decltype(arg)>;
 
                if constexpr (std::is_same_v<T, css_color>) {