]> source.dussan.org Git - rspamd.git/commitdiff
[Project] Add css_selectors
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 18 Jan 2021 14:20:16 +0000 (14:20 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 22 Jan 2021 15:58:05 +0000 (15:58 +0000)
src/libserver/css/CMakeLists.txt
src/libserver/css/css.hxx
src/libserver/css/css_property.cxx
src/libserver/css/css_property.hxx
src/libserver/css/css_rule.hxx
src/libserver/css/css_selector.cxx [new file with mode: 0644]
src/libserver/css/css_selector.hxx [new file with mode: 0644]
src/libserver/css/css_value.cxx
src/libserver/css/css_value.hxx
src/libserver/css/parse_error.hxx

index 429a7373f6e70d4e937c76231ec55f18e21447af..84d3c303873ab3c1dd8b3b29c2bbd62c70c98fbe 100644 (file)
@@ -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
index 86a8457ca3cc78c84d95234b0345463e57abd157..0b6e57e1a0aadce13207fc6455c7f8b66dd9190e 100644 (file)
@@ -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
index 483856a84c556ce6b4c7c8e48f1eabd57f56f85a..98543f75a55e18b8cef1d0afb19004e26e41d754 100644 (file)
@@ -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
index 36a3fee0779c4d80535fc70c99117e0fb33c0e59..b3f7262a236712d7a2e489b64a5bdfdfc7d33405 100644 (file)
@@ -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
index ca49ba33bccf0a839842d0403d7b5c39d3f21b9e..bcd542b0c781ba416fd0a01d846c7aa5a8f56243 100644 (file)
@@ -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_selector.cxx b/src/libserver/css/css_selector.cxx
new file mode 100644 (file)
index 0000000..2aeaaa8
--- /dev/null
@@ -0,0 +1,28 @@
+/*-
+ * 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.
+ */
+
+#include "css_selector.hxx"
+
+namespace rspamd::css {
+
+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 (file)
index 0000000..273ff60
--- /dev/null
@@ -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
index 29eb4aed2d8d16bf19627c9038790c472c9cb89d..af4691daf1b9010c3d81d29102a9bdb2472ce319 100644 (file)
 
 #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)};
 }
 
 }
-}
index 0af6a1091bd086f87a3b0b0e3d4d29f7cf69932e..e750d775e52e16927be085eb15699c2afbce6e06 100644 (file)
 #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
index d5629fa261a335fc1fe5ae25e4bdf71b93aedfe7..60b2291816e9f6e2bd2bc5e1395e7dfb03a4a9c0 100644 (file)
@@ -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