From a90d8ba8022dad062548e97947be938ff5ebc60b Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 18 Jan 2021 14:20:16 +0000 Subject: [PATCH] [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 +-- src/libserver/css/css_selector.cxx | 28 +++++++++++++ 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, 121 insertions(+), 35 deletions(-) create mode 100644 src/libserver/css/css_selector.cxx create mode 100644 src/libserver/css/css_selector.hxx 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 -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 { @@ -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 #include -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 index 000000000..2aeaaa8c9 --- /dev/null +++ b/src/libserver/css/css_selector.cxx @@ -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::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 +#include +#include +#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 value; + + constexpr std::optional to_tag (void) const { + if (type == selector_type::SELECTOR_ELEMENT) { + return std::get(value); + } + return std::nullopt; + } + + std::optional to_string (void) const { + if (type == selector_type::SELECTOR_ELEMENT) { + return std::get(value); + } + return std::nullopt; + } + + static tl::expected 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 from_bytes (const char *input, +tl::expected 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 to_string (void) const { + if (type == css_value_type::CSS_VALUE_STRING) { + return std::get(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 #include -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 -- 2.39.5