]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Css: Fix rules insertion
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 10 Mar 2021 20:30:32 +0000 (20:30 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 10 Mar 2021 20:30:32 +0000 (20:30 +0000)
src/libserver/css/css_property.hxx
src/libserver/css/css_rule.cxx
src/libserver/css/css_rule.hxx

index b72b655f90612a14f218aade09de4e00a37922a8..973000b39a7e0b229b946e822039a17513d21e0d 100644 (file)
@@ -102,23 +102,28 @@ struct alignas(int) css_property {
        }
 
        /* Helpers to define which values are valid for which properties */
-       constexpr auto is_color(void) const -> bool {
+       auto is_color(void) const -> bool {
                return type == css_property_type::PROPERTY_COLOR ||
                                type == css_property_type::PROPERTY_BACKGROUND ||
                                type == css_property_type::PROPERTY_BGCOLOR ||
                                type == css_property_type::PROPERTY_FONT_COLOR ||
                                type == css_property_type::PROPERTY_FONT;
        }
-       constexpr auto is_dimension(void) const -> bool {
+       auto is_dimension(void) const -> bool {
                return type == css_property_type::PROPERTY_HEIGHT ||
                                type == css_property_type::PROPERTY_WIDTH ||
                                type == css_property_type::PROPERTY_FONT_SIZE ||
                                type == css_property_type::PROPERTY_FONT;
        }
-       constexpr auto is_normal_number(void) const -> bool {
+
+       auto is_normal_number(void) const -> bool {
                return type == css_property_type::PROPERTY_OPACITY;
        }
 
+       auto is_display(void) const -> bool {
+               return type == css_property_type::PROPERTY_DISPLAY;
+       }
+
        auto operator==(const css_property &other) const { return type == other.type; }
 };
 
index a706c759cebf73e938dc7a024d7641427574a684..684e5776fec4d6b2f2f2e107619d2ebca32bf7ad 100644 (file)
@@ -47,10 +47,16 @@ void css_rule::merge_values(const css_rule &other)
        });
 }
 
-auto css_declarations_block::add_rule(rule_shared_ptr &&rule) -> void
+auto css_declarations_block::add_rule(rule_shared_ptr &&rule) -> bool
 {
        auto it = rules.find(rule);
        auto &&remote_prop = rule->get_prop();
+       auto ret = true;
+
+       if (rule->get_values().size() == 0) {
+               /* Ignore rules with no values */
+               return false;
+       }
 
        if (it != rules.end()) {
                auto &&local_rule = *it;
@@ -61,7 +67,7 @@ auto css_declarations_block::add_rule(rule_shared_ptr &&rule) -> void
                                local_rule->override_values(*rule);
                        }
                        else {
-                               /* Ignore remote not important over local important */
+                               /* Override remote not important over local important */
                                local_rule->merge_values(*rule);
                        }
                }
@@ -70,7 +76,7 @@ auto css_declarations_block::add_rule(rule_shared_ptr &&rule) -> void
                                local_rule->override_values(*rule);
                        }
                        else {
-                               /* Ignore local not important over important */
+                               /* Override local not important over important */
                                local_rule->merge_values(*rule);
                        }
                }
@@ -81,6 +87,7 @@ auto css_declarations_block::add_rule(rule_shared_ptr &&rule) -> void
                        }
                        else if (remote_prop.flag == css_property_flag::FLAG_NOT_IMPORTANT) {
                                /* Ignore remote not important over local normal */
+                               ret = false;
                        }
                        else {
                                /* Merge both */
@@ -88,6 +95,11 @@ auto css_declarations_block::add_rule(rule_shared_ptr &&rule) -> void
                        }
                }
        }
+       else {
+               rules.insert(std::move(rule));
+       }
+
+       return ret;
 }
 
 }
@@ -208,7 +220,7 @@ auto process_declaration_tokens(rspamd_mempool_t *pool,
                                if (next_tok.is_token()) {
                                        const auto &parser_tok = next_tok.get_token_or_empty();
 
-                                       if (parser_tok.type == css_parser_token::token_type::semicolon_token) {
+                                       if (parser_tok.type == css_parser_token::token_type::semicolon_token && cur_rule) {
                                                ret.add_rule(std::move(cur_rule));
                                                state = parse_property;
                                                seen_not = false;
@@ -267,7 +279,7 @@ auto process_declaration_tokens(rspamd_mempool_t *pool,
                        if (state == parse_value) {
                                auto maybe_value = allowed_property_value(cur_property, next_tok);
 
-                               if (maybe_value) {
+                               if (maybe_value && cur_rule) {
                                        msg_debug_css("added value %s to the property %s",
                                                        maybe_value.value().debug_str().c_str(),
                                                        cur_property.to_string());
index dd6158538a23a76cd9f016e31c52f0ddc3138987..fa9f2a78e7b69eeb4f02027f5099485d1d89aab8 100644 (file)
@@ -111,7 +111,7 @@ public:
        using rule_shared_hash = shared_ptr_hash<css_rule>;
        using rule_shared_eq = shared_ptr_equal<css_rule>;
        css_declarations_block() = default;
-       auto add_rule(rule_shared_ptr &&rule) -> void;
+       auto add_rule(rule_shared_ptr &&rule) -> bool;
        auto get_rules(void) const -> const auto & {
                return rules;
        }