commit edf32f7: [Project] Css: Start stylesheet implementation

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Mar 18 23:00:06 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-03-18 22:57:23 +0000
URL: https://github.com/rspamd/rspamd/commit/edf32f759ce364c0bfff9c680cc9991a82c438a2 (HEAD -> master)

[Project] Css: Start stylesheet implementation

---
 src/libserver/css/css.hxx          |  2 ++
 src/libserver/css/css_parser.cxx   | 40 +++++++++++++++++++++++++-------------
 src/libserver/css/css_parser.hxx   |  1 -
 src/libserver/css/css_rule.cxx     | 10 ++++++----
 src/libserver/css/css_rule.hxx     |  4 +++-
 src/libserver/css/css_selector.cxx |  1 +
 6 files changed, 38 insertions(+), 20 deletions(-)

diff --git a/src/libserver/css/css.hxx b/src/libserver/css/css.hxx
index 1a511dcfd..9ed323ec3 100644
--- a/src/libserver/css/css.hxx
+++ b/src/libserver/css/css.hxx
@@ -22,6 +22,8 @@
 #include <memory>
 #include "logger.h"
 #include "css.h"
+#include "css_rule.hxx"
+#include "css_selector.hxx"
 
 namespace rspamd::css {
 
diff --git a/src/libserver/css/css_parser.cxx b/src/libserver/css/css_parser.cxx
index f6029739a..415039e19 100644
--- a/src/libserver/css/css_parser.cxx
+++ b/src/libserver/css/css_parser.cxx
@@ -18,6 +18,7 @@
 #include "css_tokeniser.hxx"
 #include "css_selector.hxx"
 #include "css_rule.hxx"
+#include "css.hxx"
 #include "fmt/core.h"
 
 #include <vector>
@@ -558,6 +559,8 @@ bool css_parser::consume_input(const std::string_view &sv)
 		return false;
 	}
 
+	style_object = std::make_unique<css_style_sheet>();
+
 	for (auto &&rule : rules) {
 		/*
 		 * For now, we do not need any of the at rules, so we can safely ignore them
@@ -598,25 +601,34 @@ bool css_parser::consume_input(const std::string_view &sv)
 
 				auto selectors_vec = process_selector_tokens(pool, selector_token_functor);
 
-				auto decls_it = (*simple_block)->get_blocks_or_empty().cbegin();
-				auto decls_end = (*simple_block)->get_blocks_or_empty().cend();
-				auto declaration_token_functor = [&decls_it,&decls_end](void)
-						-> const css_consumed_block & {
-					for (;;) {
-						if (decls_it == decls_end) {
-							return css_parser_eof_block;
+				if (selectors_vec.size() > 0) {
+					msg_debug_css("processed %d selectors", (int)selectors_vec.size());
+					auto decls_it = (*simple_block)->get_blocks_or_empty().cbegin();
+					auto decls_end = (*simple_block)->get_blocks_or_empty().cend();
+					auto declaration_token_functor = [&decls_it, &decls_end](void)
+							-> const css_consumed_block & {
+						for (;;) {
+							if (decls_it == decls_end) {
+								return css_parser_eof_block;
+							}
+
+							const auto &ret = (*decls_it);
+
+							++decls_it;
+
+							return *ret;
 						}
+					};
 
-						const auto &ret = (*decls_it);
+					auto declarations_vec = process_declaration_tokens(pool,
+							declaration_token_functor);
 
-						++decls_it;
+					if (declarations_vec && !declarations_vec->get_rules().empty()) {
+						msg_debug_css("processed %d rules",
+								(int)declarations_vec->get_rules().size());
 
-						return *ret;
 					}
-				};
-
-				auto declarations_vec = process_declaration_tokens(pool,
-						declaration_token_functor);
+				}
 			}
 		}
 	}
diff --git a/src/libserver/css/css_parser.hxx b/src/libserver/css/css_parser.hxx
index 4dbcd9f68..be788ea81 100644
--- a/src/libserver/css/css_parser.hxx
+++ b/src/libserver/css/css_parser.hxx
@@ -25,7 +25,6 @@
 #include <string>
 
 #include "css_tokeniser.hxx"
-#include "css.hxx"
 #include "parse_error.hxx"
 #include "contrib/expected/expected.hpp"
 #include "logger.h"
diff --git a/src/libserver/css/css_rule.cxx b/src/libserver/css/css_rule.cxx
index d16be2276..bd589da95 100644
--- a/src/libserver/css/css_rule.cxx
+++ b/src/libserver/css/css_rule.cxx
@@ -15,6 +15,7 @@
  */
 
 #include "css_rule.hxx"
+#include "css.hxx"
 #include <limits>
 
 namespace rspamd::css {
@@ -169,9 +170,9 @@ allowed_property_value(const css_property &prop, const css_consumed_block &parse
 
 auto process_declaration_tokens(rspamd_mempool_t *pool,
 								const blocks_gen_functor &next_block_functor)
-	-> css_declarations_block
+	-> css_declarations_block_ptr
 {
-	css_declarations_block ret;
+	css_declarations_block_ptr ret;
 	bool can_continue = true;
 	css_property cur_property{css_property_type::PROPERTY_NYI,
 							  css_property_flag::FLAG_NORMAL};
@@ -186,6 +187,7 @@ auto process_declaration_tokens(rspamd_mempool_t *pool,
 	} state = parse_property;
 
 	auto seen_not = false;
+	ret = std::make_shared<css_declarations_block>();
 
 	while (can_continue) {
 		const auto &next_tok = next_block_functor();
@@ -230,7 +232,7 @@ auto process_declaration_tokens(rspamd_mempool_t *pool,
 					const auto &parser_tok = next_tok.get_token_or_empty();
 
 					if (parser_tok.type == css_parser_token::token_type::semicolon_token && cur_rule) {
-						ret.add_rule(std::move(cur_rule));
+						ret->add_rule(std::move(cur_rule));
 						state = parse_property;
 						seen_not = false;
 						continue;
@@ -298,7 +300,7 @@ auto process_declaration_tokens(rspamd_mempool_t *pool,
 			break;
 		case css_consumed_block::parser_tag_type::css_eof_block:
 			if (state == parse_value) {
-				ret.add_rule(std::move(cur_rule));
+				ret->add_rule(std::move(cur_rule));
 			}
 			can_continue = false;
 			break;
diff --git a/src/libserver/css/css_rule.hxx b/src/libserver/css/css_rule.hxx
index dbd4fb1c7..05c3fd82d 100644
--- a/src/libserver/css/css_rule.hxx
+++ b/src/libserver/css/css_rule.hxx
@@ -94,9 +94,11 @@ private:
 	robin_hood::unordered_flat_set<rule_shared_ptr, rule_shared_hash, rule_shared_eq> rules;
 };
 
+using css_declarations_block_ptr = std::shared_ptr<css_declarations_block>;
+
 auto process_declaration_tokens(rspamd_mempool_t *pool,
 							 const blocks_gen_functor &next_token_functor)
-	-> css_declarations_block;
+	-> css_declarations_block_ptr;
 
 }
 
diff --git a/src/libserver/css/css_selector.cxx b/src/libserver/css/css_selector.cxx
index 48914dc1e..d4b578a08 100644
--- a/src/libserver/css/css_selector.cxx
+++ b/src/libserver/css/css_selector.cxx
@@ -15,6 +15,7 @@
  */
 
 #include "css_selector.hxx"
+#include "css.hxx"
 #include "fmt/core.h"
 #define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
 #include "doctest/doctest.h"


More information about the Commits mailing list