aboutsummaryrefslogtreecommitdiffstats
path: root/src/libserver/css/css_selector.hxx
blob: 5ed4d54b820c72f03472b049e45aa7f5d826e196 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*-
 * 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.
 */

#pragma once

#ifndef RSPAMD_CSS_SELECTOR_HXX
#define RSPAMD_CSS_SELECTOR_HXX

#include <variant>
#include <string>
#include <optional>
#include <vector>
#include <functional>
#include <memory>

#include "parse_error.hxx"
#include "css_parser.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, e.g. .class */
		SELECTOR_ID, /* e.g. #id */
		SELECTOR_ALL /* * selector */
	};

	selector_type type;
	std::variant<tag_id_t, std::string_view> value;

	/* Conditions for the css selector */
	/* Dependency on attributes */
	struct css_attribute_condition {
		std::string_view attribute;
		std::string_view op = "";
		std::string_view value = "";
	};

	/* General dependency chain */
	using css_selector_ptr = std::unique_ptr<css_selector>;
	using css_selector_dep = std::variant<css_attribute_condition, css_selector_ptr>;
	std::vector<css_selector_dep> dependencies;

	 auto to_tag(void) const -> std::optional<tag_id_t> {
		if (type == selector_type::SELECTOR_ELEMENT) {
			return std::get<tag_id_t>(value);
		}
		return std::nullopt;
	}

	auto to_string(void) const -> std::optional<const std::string_view> {
		if (type == selector_type::SELECTOR_ELEMENT) {
			return std::string_view(std::get<std::string_view>(value));
		}
		return std::nullopt;
	};

	explicit css_selector(selector_type t) : type(t) {}

	auto debug_str(void) const -> std::string;
};


using selectors_vec = std::vector<std::unique_ptr<css_selector>>;

/*
 * Consume selectors token and split them to the list of selectors
 */
auto process_selector_tokens(rspamd_mempool_t *pool,
							 const blocks_gen_functor &next_token_functor)
	-> selectors_vec;

}

#endif //RSPAMD_CSS_SELECTOR_HXX