]> source.dussan.org Git - rspamd.git/commitdiff
[Project] Css: Add display value support
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 10 Mar 2021 20:57:32 +0000 (20:57 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 10 Mar 2021 20:57:32 +0000 (20:57 +0000)
src/libserver/css/css_rule.cxx
src/libserver/css/css_value.cxx
src/libserver/css/css_value.hxx
test/lua/unit/css.lua

index 684e5776fec4d6b2f2f2e107619d2ebca32bf7ad..d16be2276d92f9a2609b155afcd097cedae8c660 100644 (file)
@@ -133,7 +133,6 @@ allowed_property_value(const css_property &prop, const css_consumed_block &parse
                        return ret;
                }
        }
-
        if (prop.is_dimension()) {
                if (parser_block.is_token()) {
                        /* A single token */
@@ -144,6 +143,16 @@ allowed_property_value(const css_property &prop, const css_consumed_block &parse
                        }
                }
        }
+       if (prop.is_display()) {
+               if (parser_block.is_token()) {
+                       /* A single token */
+                       const auto &tok = parser_block.get_token_or_empty();
+
+                       if (tok.type == css_parser_token::token_type::ident_token) {
+                               return css_value::maybe_display_from_string(tok.get_string_or_default(""));
+                       }
+               }
+       }
        if (prop.is_normal_number()) {
                if (parser_block.is_token()) {
                        /* A single token */
index 94c340ac5f5ef52e7c800559a50c5072a079e350..5470e8f30afa1232ccfe2a409e9e8a6e840dcca3 100644 (file)
 
 #include "css_value.hxx"
 #include "css_colors_list.hxx"
+#include "frozen/unordered_map.h"
+#include "frozen/string.h"
 #include "contrib/robin-hood/robin_hood.h"
 
 namespace rspamd::css {
 
-
-
-tl::expected<css_value,css_parse_error>
-css_value::from_css_block(const css_consumed_block &bl)
-{
-       return tl::unexpected{css_parse_error(css_parse_error_type::PARSE_ERROR_NYI)};
-}
-
 auto css_value::maybe_color_from_string(const std::string_view &input)
        -> std::optional<css_value>
 {
@@ -295,6 +289,44 @@ auto css_value::maybe_dimension_from_number(const css_parser_token &tok)
        return std::nullopt;
 }
 
+constexpr const auto display_names_map = frozen::make_unordered_map<frozen::string, css_display_value>({
+               {"hidden", css_display_value::DISPLAY_HIDDEN},
+               {"none", css_display_value::DISPLAY_HIDDEN},
+               {"inline", css_display_value::DISPLAY_NORMAL},
+               {"block", css_display_value::DISPLAY_NORMAL},
+               {"content", css_display_value::DISPLAY_NORMAL},
+               {"flex", css_display_value::DISPLAY_NORMAL},
+               {"grid" , css_display_value::DISPLAY_NORMAL},
+               {"inline-block", css_display_value::DISPLAY_NORMAL},
+               {"inline-flex", css_display_value::DISPLAY_NORMAL},
+               {"inline-grid", css_display_value::DISPLAY_NORMAL},
+               {"inline-table", css_display_value::DISPLAY_NORMAL},
+               {"list-item", css_display_value::DISPLAY_NORMAL},
+               {"run-in", css_display_value::DISPLAY_NORMAL},
+               {"table", css_display_value::DISPLAY_NORMAL},
+               {"table-caption", css_display_value::DISPLAY_NORMAL},
+               {"table-column-group", css_display_value::DISPLAY_NORMAL},
+               {"table-header-group", css_display_value::DISPLAY_NORMAL},
+               {"table-footer-group", css_display_value::DISPLAY_NORMAL},
+               {"table-row-group", css_display_value::DISPLAY_NORMAL},
+               {"table-cell", css_display_value::DISPLAY_NORMAL},
+               {"table-column", css_display_value::DISPLAY_NORMAL},
+               {"table-row", css_display_value::DISPLAY_NORMAL},
+               {"initial", css_display_value::DISPLAY_NORMAL},
+});
+
+auto css_value::maybe_display_from_string(const std::string_view &input)
+       -> std::optional<css_value>
+{
+       auto f = display_names_map.find(input);
+
+       if (f != display_names_map.end()) {
+               return css_value{f->second};
+       }
+
+       return std::nullopt;
+}
+
 
 auto css_value::debug_str() const -> std::string
 {
@@ -318,6 +350,10 @@ auto css_value::debug_str() const -> std::string
                                ret += "%";
                        }
                }
+               else if constexpr (std::is_same_v<T, css_display_value>) {
+                       ret += "display: ";
+                       ret += (arg == css_display_value::DISPLAY_HIDDEN ? "hidden" : "normal");
+               }
                else if constexpr (std::is_integral_v<T>) {
                        ret += "integral: " + std::to_string(static_cast<int>(arg));
                }
index d7c8f5c458390a6b1b723550013ba0bad365dfec..93324a57c38fa2d4fa7a82e528a248678c10f392 100644 (file)
@@ -82,6 +82,8 @@ struct css_value {
                        type(css_value_type::CSS_VALUE_NUMBER), value(num) {}
        css_value(css_dimension dim) :
                        type(css_value_type::CSS_VALUE_DIMENSION), value(dim) {}
+       css_value(css_display_value d) :
+                       type(css_value_type::CSS_VALUE_DISPLAY), value(d) {}
 
        auto to_color(void) const -> std::optional<css_color> {
                if (type == css_value_type::CSS_VALUE_COLOR) {
@@ -121,8 +123,6 @@ struct css_value {
 
        auto debug_str() const -> std::string;
 
-       static auto from_css_block(const css_consumed_block &bl) -> tl::expected<css_value, css_parse_error>;
-
        static auto maybe_color_from_string(const std::string_view &input)
                -> std::optional<css_value>;
        static auto maybe_color_from_hex(const std::string_view &input)
@@ -131,6 +131,8 @@ struct css_value {
                -> std::optional<css_value>;
        static auto maybe_dimension_from_number(const css_parser_token &tok)
                -> std::optional<css_value>;
+       static auto maybe_display_from_string(const std::string_view &input)
+               -> std::optional<css_value>;
 };
 
 }
index 5bb197bf24bca13b0483ffe5d4949b4dd8068f92..3078ed3bca47dd38a4f6c89a500e37f593bb1439 100644 (file)
@@ -100,8 +100,8 @@ body {
 ]],
 [[
 /* Colors */
-p { color: rgb(100%, 50%, 0%); opacity: -1; width: 1em; } /* very transparent solid orange */
-p { color: rgb(100%, 50%, 0%); opacity: 2; } /* very transparent solid orange */
+p { color: rgb(100%, 50%, 0%); opacity: -1; width: 1em; display: none; } /* very transparent solid orange */
+p { color: rgb(100%, 50%, 0%); opacity: 2; display: inline; } /* very transparent solid orange */
 p { color: rgb(100%, 50%, 0%); opacity: 0.5; } /* very transparent solid orange */
 p { color: rgb(100%, 50%, 0%); opacity: 1; width: 99%; } /* very transparent solid orange */
 p { color: rgb(100%, 50%, 0%); opacity: 10%; width: 99%; } /* very transparent solid orange */