aboutsummaryrefslogtreecommitdiffstats
path: root/include/litehtml/css_selector.h
blob: 57aa2846b55b42a00bbd9cbff6646a16034115e1 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#ifndef LH_CSS_SELECTOR_H
#define LH_CSS_SELECTOR_H

#include "string_id.h"
#include "style.h"
#include "media_query.h"
#include "css_tokenizer.h"

namespace litehtml
{
	//////////////////////////////////////////////////////////////////////////

	struct selector_specificity
	{
		int		a;
		int		b;
		int		c;
		int		d;

		explicit selector_specificity(int va = 0, int vb = 0, int vc = 0, int vd = 0)
		{
			a	= va;
			b	= vb;
			c	= vc;
			d	= vd;
		}

		void operator += (const selector_specificity& val)
		{
			a	+= val.a;
			b	+= val.b;
			c	+= val.c;
			d	+= val.d;
		}

		bool operator==(const selector_specificity& val) const
		{
			if(a == val.a && b == val.b && c == val.c && d == val.d)
			{
				return true;
			}
			return false;
		}

		bool operator!=(const selector_specificity& val) const
		{
			if(a != val.a || b != val.b || c != val.c || d != val.d)
			{
				return true;
			}
			return false;
		}

		bool operator > (const selector_specificity& val) const
		{
			if(a > val.a)
			{
				return true;
			} else if(a < val.a)
			{
				return false;
			} else
			{
				if(b > val.b)
				{
					return true;
				} else if(b < val.b)
				{
					return false;
				} else
				{
					if(c > val.c)
					{
						return true;
					} else if(c < val.c)
					{
						return false;
					} else
					{
						if(d > val.d)
						{
							return true;
						} else if(d < val.d)
						{
							return false;
						}
					}
				}
			}
			return false;
		}

		bool operator >= (const selector_specificity& val) const
		{
			if((*this) == val) return true;
			if((*this) > val) return true;
			return false;
		}

		bool operator <= (const selector_specificity& val) const
		{
			if((*this) > val)
			{
				return false;
			}
			return true;
		}

		bool operator < (const selector_specificity& val) const
		{
			if((*this) <= val && (*this) != val)
			{
				return true;
			}
			return false;
		}

	};

	//////////////////////////////////////////////////////////////////////////

	enum attr_select_type
	{
		select_class,
		select_id,
		select_attr,
		select_pseudo_class,
		select_pseudo_element,
	};

	// https://www.w3.org/TR/selectors-4/#attribute-selectors
	enum attr_matcher : char
	{
		attribute_exists                    = 0,
		attribute_equals                    = '=',
		attribute_contains_string           = '*', // *=
		attribute_contains_word             = '~', // ~=
		attribute_starts_with_string        = '^', // ^=
		attribute_starts_with_string_hyphen = '|', // |=
		attribute_ends_with_string          = '$', // $=
	};

	//////////////////////////////////////////////////////////////////////////

	class css_selector;
	class html_tag;

	// <subclass-selector> | <pseudo-element-selector>
	// = <id-selector> | <class-selector> | <attribute-selector> | <pseudo-class-selector> | <pseudo-element-selector>
	struct css_attribute_selector
	{
		using vector = std::vector<css_attribute_selector>;

		attr_select_type	type;
		string_id			prefix;   // [prefix|name]
		string_id			name;     // .name, #name, [name], :name
		string				value;    // [name=value], :lang(value)

		attr_matcher		matcher;         // <attr-matcher>   = ~= |= ^= $= *=
		bool				caseless_match;  // value is matched ASCII case-insensitively

		std::vector<shared_ptr<css_selector>> selector_list; // :not(selector_list)
		int a, b; // :nth-child(an+b of selector_list)

		css_attribute_selector(attr_select_type type = select_class, string name = "")
			: type(type), prefix(empty_id), name(_id(name)), matcher(), caseless_match(0), a(0), b(0) {}

		operator bool() const { return name != empty_id; }
	};

	//////////////////////////////////////////////////////////////////////////

	class css_element_selector // compound selector: div.class:hover
	{
	public:
		using ptr = shared_ptr<css_element_selector>;
	public:
		string_id						m_prefix;
		string_id						m_tag;
		css_attribute_selector::vector	m_attrs; // subclass and pseudo-element selectors
	};

	//////////////////////////////////////////////////////////////////////////

	enum css_combinator
	{
		combinator_descendant        = ' ',
		combinator_child             = '>',
		combinator_adjacent_sibling  = '+',
		combinator_general_sibling   = '~'
	};

	//////////////////////////////////////////////////////////////////////////

	class css_selector // complex selector: div + p
	{
	public:
		using ptr    = shared_ptr<css_selector>;
		using vector = std::vector<css_selector::ptr>;

	public:
		selector_specificity		m_specificity;
		int							m_order = 0;
		css_selector::ptr			m_left;
		css_element_selector		m_right;
		css_combinator				m_combinator = combinator_descendant;
		media_query_list_list::ptr	m_media_query;
		style::ptr					m_style;

	public:
		bool parse(const string& text, document_mode mode);
		void calc_specificity();
		bool is_media_valid() const;
		void add_media_to_doc(document* doc) const;
	};

	inline bool css_selector::is_media_valid() const
	{
		if(!m_media_query)
		{
			return true;
		}
		return m_media_query->is_used();
	}


	//////////////////////////////////////////////////////////////////////////

	inline bool operator > (const css_selector& v1, const css_selector& v2)
	{
		if(v1.m_specificity == v2.m_specificity)
		{
			return (v1.m_order > v2.m_order);
		}
		return (v1.m_specificity > v2.m_specificity);
	}

	inline bool operator < (const css_selector& v1, const css_selector& v2)
	{
		if(v1.m_specificity == v2.m_specificity)
		{
			return (v1.m_order < v2.m_order);
		}
		return (v1.m_specificity < v2.m_specificity);
	}

	inline bool operator > (const css_selector::ptr& v1, const css_selector::ptr& v2)
	{
		return (*v1 > *v2);
	}

	inline bool operator < (const css_selector::ptr& v1, const css_selector::ptr& v2)
	{
		return (*v1 < *v2);
	}

	//////////////////////////////////////////////////////////////////////////

	class used_selector
	{
	public:
		typedef std::unique_ptr<used_selector>	ptr;
		typedef std::vector<used_selector::ptr>	vector;

		css_selector::ptr	m_selector;
		bool				m_used;

		used_selector(const css_selector::ptr& selector, bool used)
		{
			m_used		= used;
			m_selector	= selector;
		}
	};


	enum {
		strict_mode = 0,
		forgiving_mode = 1,
		forbid_pseudo_elements = 1 << 1,
	};

	css_selector::vector parse_selector_list(const css_token_vector& tokens, int options, document_mode mode);
}

#endif  // LH_CSS_SELECTOR_H