1 /*-
2 * Copyright 2021 Vsevolod Stakhov
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "css_property.hxx"
18 #include "frozen/unordered_map.h"
19 #include "frozen/string.h"
20 #include "libutil/cxx/util.hxx"
21
22 namespace rspamd::css {
23
24 constexpr const auto prop_names_map = frozen::make_unordered_map<frozen::string, css_property_type>({
25 {"font", css_property_type::PROPERTY_FONT},
26 {"font-color", css_property_type::PROPERTY_FONT_COLOR},
27 {"font-size", css_property_type::PROPERTY_FONT_SIZE},
28 {"color", css_property_type::PROPERTY_COLOR},
29 {"bgcolor", css_property_type::PROPERTY_BGCOLOR},
30 {"background-color", css_property_type::PROPERTY_BGCOLOR},
31 {"background", css_property_type::PROPERTY_BACKGROUND},
32 {"height", css_property_type::PROPERTY_HEIGHT},
33 {"width", css_property_type::PROPERTY_WIDTH},
34 {"display", css_property_type::PROPERTY_DISPLAY},
35 {"visibility", css_property_type::PROPERTY_VISIBILITY},
36 {"opacity", css_property_type::PROPERTY_OPACITY},
37 });
38
39 /* Ensure that we have all cases listed */
40 static_assert(prop_names_map.size() >= static_cast<int>(css_property_type::PROPERTY_NYI));
41
token_string_to_property(const std::string_view & inp)42 auto token_string_to_property(const std::string_view &inp)
43 -> css_property_type
44 {
45
46 css_property_type ret = css_property_type::PROPERTY_NYI;
47
48 auto known_type = find_map(prop_names_map, inp);
49
50 if (known_type) {
51 ret = known_type.value().get();
52 }
53
54 return ret;
55 }
56
from_token(const css_parser_token & tok)57 auto css_property::from_token(const css_parser_token &tok)
58 -> tl::expected<css_property,css_parse_error>
59 {
60 if (tok.type == css_parser_token::token_type::ident_token) {
61 auto sv = tok.get_string_or_default("");
62
63 return css_property{token_string_to_property(sv), css_property_flag::FLAG_NORMAL};
64 }
65
66 return tl::unexpected{css_parse_error(css_parse_error_type::PARSE_ERROR_NYI)};
67 }
68
69 }
70