commit 5ea5786: [Project] CSS: Various fixes in the declarations and values parsing

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Mar 4 20:42:09 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-03-04 20:39:29 +0000
URL: https://github.com/rspamd/rspamd/commit/5ea57860c64e89d688ed9658db279a164374cbe8 (HEAD -> master)

[Project] CSS: Various fixes in the declarations and values parsing

---
 src/libserver/css/css_parser.cxx |  8 ++------
 src/libserver/css/css_parser.hxx |  4 ++--
 src/libserver/css/css_rule.cxx   | 15 +++++++++++----
 src/libserver/css/css_value.cxx  |  9 ++++++++-
 4 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/src/libserver/css/css_parser.cxx b/src/libserver/css/css_parser.cxx
index 1d2916e5e..776901ffe 100644
--- a/src/libserver/css/css_parser.cxx
+++ b/src/libserver/css/css_parser.cxx
@@ -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;
 					}
 				};
 
diff --git a/src/libserver/css/css_parser.hxx b/src/libserver/css/css_parser.hxx
index 5b2d93ae5..887d846ad 100644
--- a/src/libserver/css/css_parser.hxx
+++ b/src/libserver/css/css_parser.hxx
@@ -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);
 		}
 
diff --git a/src/libserver/css/css_rule.cxx b/src/libserver/css/css_rule.cxx
index ce35dc6b0..0e9924029 100644
--- a/src/libserver/css/css_rule.cxx
+++ b/src/libserver/css/css_rule.cxx
@@ -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;
diff --git a/src/libserver/css/css_value.cxx b/src/libserver/css/css_value.cxx
index c0ed9242c..aa842e2e2 100644
--- a/src/libserver/css/css_value.cxx
+++ b/src/libserver/css/css_value.cxx
@@ -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>) {


More information about the Commits mailing list