commit a90d8ba: [Project] Add css_selectors

Vsevolod Stakhov vsevolod at highsecure.ru
Fri Jan 22 16:00:16 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-01-18 14:20:16 +0000
URL: https://github.com/rspamd/rspamd/commit/a90d8ba8022dad062548e97947be938ff5ebc60b

[Project] Add css_selectors

---
 src/libserver/css/CMakeLists.txt                   |  7 +--
 src/libserver/css/css.hxx                          |  6 +--
 src/libserver/css/css_property.cxx                 |  5 +-
 src/libserver/css/css_property.hxx                 |  6 +--
 src/libserver/css/css_rule.hxx                     |  6 +--
 .../css/{css_value.cxx => css_selector.cxx}        | 12 ++---
 src/libserver/css/css_selector.hxx                 | 63 ++++++++++++++++++++++
 src/libserver/css/css_value.cxx                    |  7 +--
 src/libserver/css/css_value.hxx                    | 22 ++++++--
 src/libserver/css/parse_error.hxx                  |  6 +--
 10 files changed, 98 insertions(+), 42 deletions(-)

diff --git a/src/libserver/css/CMakeLists.txt b/src/libserver/css/CMakeLists.txt
index 429a7373f..84d3c3038 100644
--- a/src/libserver/css/CMakeLists.txt
+++ b/src/libserver/css/CMakeLists.txt
@@ -1,4 +1,5 @@
 SET(LIBCSSSRC    "${CMAKE_CURRENT_SOURCE_DIR}/css.cxx"
-                "${CMAKE_CURRENT_SOURCE_DIR}/css_property.cxx"
-                "${CMAKE_CURRENT_SOURCE_DIR}/css_value.cxx"
-                PARENT_SCOPE)
\ No newline at end of file
+                 "${CMAKE_CURRENT_SOURCE_DIR}/css_property.cxx"
+                 "${CMAKE_CURRENT_SOURCE_DIR}/css_value.cxx"
+                 "${CMAKE_CURRENT_SOURCE_DIR}/css_selector.cxx"
+                 PARENT_SCOPE)
\ No newline at end of file
diff --git a/src/libserver/css/css.hxx b/src/libserver/css/css.hxx
index 86a8457ca..0b6e57e1a 100644
--- a/src/libserver/css/css.hxx
+++ b/src/libserver/css/css.hxx
@@ -18,9 +18,7 @@
 
 #include <string>
 
-namespace rspamd {
-
-namespace css {
+namespace rspamd::css {
 
 struct css_element {
 
@@ -28,6 +26,4 @@ struct css_element {
 
 }
 
-}
-
 #endif //RSPAMD_CSS_H
\ No newline at end of file
diff --git a/src/libserver/css/css_property.cxx b/src/libserver/css/css_property.cxx
index 483856a84..98543f75a 100644
--- a/src/libserver/css/css_property.cxx
+++ b/src/libserver/css/css_property.cxx
@@ -17,9 +17,7 @@
 #include "css_property.hxx"
 
 
-namespace rspamd {
-
-namespace css {
+namespace rspamd::css {
 
 auto css_property::from_bytes (const char *input, size_t inlen) -> tl::expected<css_property,css_parse_error>
 {
@@ -27,4 +25,3 @@ auto css_property::from_bytes (const char *input, size_t inlen) -> tl::expected<
 }
 
 }
-}
\ No newline at end of file
diff --git a/src/libserver/css/css_property.hxx b/src/libserver/css/css_property.hxx
index 36a3fee07..b3f7262a2 100644
--- a/src/libserver/css/css_property.hxx
+++ b/src/libserver/css/css_property.hxx
@@ -21,9 +21,7 @@
 #include "parse_error.hxx"
 #include "contrib/expected/expected.hpp"
 
-namespace rspamd {
-
-namespace css {
+namespace rspamd::css {
 
 /*
  * To be extended with properties that are interesting from the email
@@ -48,6 +46,4 @@ struct css_property {
 
 }
 
-}
-
 #endif //RSPAMD_CSS_PROPERTY_HXX
diff --git a/src/libserver/css/css_rule.hxx b/src/libserver/css/css_rule.hxx
index ca49ba33b..bcd542b0c 100644
--- a/src/libserver/css/css_rule.hxx
+++ b/src/libserver/css/css_rule.hxx
@@ -22,9 +22,7 @@
 #include <vector>
 #include <memory>
 
-namespace rspamd {
-
-namespace css {
+namespace rspamd::css {
 
 class css_rule {
 	css_property prop;
@@ -52,6 +50,4 @@ public:
 
 }
 
-}
-
 #endif //RSPAMD_CSS_RULE_HXX
diff --git a/src/libserver/css/css_value.cxx b/src/libserver/css/css_selector.cxx
similarity index 80%
copy from src/libserver/css/css_value.cxx
copy to src/libserver/css/css_selector.cxx
index 29eb4aed2..2aeaaa8c9 100644
--- a/src/libserver/css/css_value.cxx
+++ b/src/libserver/css/css_selector.cxx
@@ -14,17 +14,15 @@
  * limitations under the License.
  */
 
-#include "css_value.hxx"
+#include "css_selector.hxx"
 
-namespace rspamd {
+namespace rspamd::css {
 
-namespace css {
-
-tl::expected<css_value,css_parse_error> from_bytes (const char *input,
-													size_t inlen)
+tl::expected<css_selector,css_parse_error> css_selector::from_bytes (const char *input,
+															   size_t inlen)
 {
 	return tl::unexpected{css_parse_error(css_parse_error_type::PARSE_ERROR_NYI)};
 }
 
 }
-}
+
diff --git a/src/libserver/css/css_selector.hxx b/src/libserver/css/css_selector.hxx
new file mode 100644
index 000000000..273ff603e
--- /dev/null
+++ b/src/libserver/css/css_selector.hxx
@@ -0,0 +1,63 @@
+/*-
+ * Copyright 2021 Vsevolod Stakhov
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#ifndef RSPAMD_CSS_SELECTOR_HXX
+#define RSPAMD_CSS_SELECTOR_HXX
+
+#include <variant>
+#include <string>
+#include <optional>
+#include "contrib/expected/expected.hpp"
+#include "parse_error.hxx"
+#include "html_tags.h"
+
+namespace rspamd::css {
+
+/*
+ * Holds a value for css selector, internal is handled by variant
+ */
+struct css_selector {
+	enum class selector_type {
+		SELECTOR_ELEMENT, /* e.g. .tr, for this value we use tag_id_t */
+		SELECTOR_CLASS, /* generic class */
+		SELECTOR_ID /* e.g. #id */
+	};
+
+	selector_type type;
+	std::variant<tag_id_t, std::string> value;
+
+	constexpr std::optional<tag_id_t> to_tag (void) const {
+		if (type == selector_type::SELECTOR_ELEMENT) {
+			return std::get<tag_id_t>(value);
+		}
+		return std::nullopt;
+	}
+
+	std::optional<const std::string> to_string (void) const {
+		if (type == selector_type::SELECTOR_ELEMENT) {
+			return std::get<std::string>(value);
+		}
+		return std::nullopt;
+	}
+
+	static tl::expected<css_selector,css_parse_error> from_bytes (const char *input,
+															   size_t inlen);
+};
+
+}
+
+#endif //RSPAMD_CSS_SELECTOR_HXX
diff --git a/src/libserver/css/css_value.cxx b/src/libserver/css/css_value.cxx
index 29eb4aed2..af4691daf 100644
--- a/src/libserver/css/css_value.cxx
+++ b/src/libserver/css/css_value.cxx
@@ -16,15 +16,12 @@
 
 #include "css_value.hxx"
 
-namespace rspamd {
+namespace rspamd::css {
 
-namespace css {
-
-tl::expected<css_value,css_parse_error> from_bytes (const char *input,
+tl::expected<css_value,css_parse_error> css_value::from_bytes (const char *input,
 													size_t inlen)
 {
 	return tl::unexpected{css_parse_error(css_parse_error_type::PARSE_ERROR_NYI)};
 }
 
 }
-}
diff --git a/src/libserver/css/css_value.hxx b/src/libserver/css/css_value.hxx
index 0af6a1091..e750d775e 100644
--- a/src/libserver/css/css_value.hxx
+++ b/src/libserver/css/css_value.hxx
@@ -24,20 +24,29 @@
 #include "parse_error.hxx"
 #include "contrib/expected/expected.hpp"
 
-namespace rspamd {
-namespace css {
+namespace rspamd::css {
 
+/*
+ * Simple enum class for display stuff
+ */
 enum class css_display_value {
 	DISPLAY_NORMAL,
 	DISPLAY_
 };
 
+/*
+ * CSS flags
+ */
 enum class css_flag_value {
 	FLAG_INHERIT,
 	FLAG_IMPORTANT,
 	FLAG_NOTIMPORTANT
 };
 
+/*
+ * Value handler, uses std::variant instead of polymorphic classes for now
+ * for simplicity
+ */
 struct css_value {
 	enum class css_value_type {
 		CSS_VALUE_COLOR,
@@ -86,6 +95,14 @@ struct css_value {
 		return std::nullopt;
 	}
 
+	std::optional<const std::string> to_string (void) const {
+		if (type == css_value_type::CSS_VALUE_STRING) {
+			return std::get<std::string>(value);
+		}
+
+		return std::nullopt;
+	}
+
 	constexpr bool is_valid (void) const {
 		return (type != css_value_type::CSS_VALUE_NYI);
 	}
@@ -94,7 +111,6 @@ struct css_value {
 																  size_t inlen);
 };
 
-}
 }
 
 #endif //RSPAMD_CSS_VALUE_HXX
diff --git a/src/libserver/css/parse_error.hxx b/src/libserver/css/parse_error.hxx
index d5629fa26..60b229181 100644
--- a/src/libserver/css/parse_error.hxx
+++ b/src/libserver/css/parse_error.hxx
@@ -21,9 +21,7 @@
 #include <string>
 #include <optional>
 
-namespace rspamd {
-
-namespace css {
+namespace rspamd::css {
 
 /*
  * Generic parser errors
@@ -45,7 +43,5 @@ struct css_parse_error {
 			type(type) {}
 };
 
-}
-
 }
 #endif //RSPAMD_PARSE_ERROR_HXX


More information about the Commits mailing list