Photon 1.0.0
Loading...
Searching...
No Matches
css_selector.h
Go to the documentation of this file.
1#ifndef LH_CSS_SELECTOR_H
2#define LH_CSS_SELECTOR_H
3
4#include "style.h"
5#include "media_query.h"
6#include "css_tokenizer.h"
7
8namespace litehtml
9{
11
13 {
14 int a;
15 int b;
16 int c;
17 int d;
18
19 explicit selector_specificity(int va = 0, int vb = 0, int vc = 0, int vd = 0)
20 {
21 a = va;
22 b = vb;
23 c = vc;
24 d = vd;
25 }
26
28 {
29 a += val.a;
30 b += val.b;
31 c += val.c;
32 d += val.d;
33 }
34
35 bool operator==(const selector_specificity& val) const
36 {
37 if(a == val.a && b == val.b && c == val.c && d == val.d)
38 {
39 return true;
40 }
41 return false;
42 }
43
44 bool operator!=(const selector_specificity& val) const
45 {
46 if(a != val.a || b != val.b || c != val.c || d != val.d)
47 {
48 return true;
49 }
50 return false;
51 }
52
53 bool operator > (const selector_specificity& val) const
54 {
55 if(a > val.a)
56 {
57 return true;
58 } else if(a < val.a)
59 {
60 return false;
61 } else
62 {
63 if(b > val.b)
64 {
65 return true;
66 } else if(b < val.b)
67 {
68 return false;
69 } else
70 {
71 if(c > val.c)
72 {
73 return true;
74 } else if(c < val.c)
75 {
76 return false;
77 } else
78 {
79 if(d > val.d)
80 {
81 return true;
82 } else if(d < val.d)
83 {
84 return false;
85 }
86 }
87 }
88 }
89 return false;
90 }
91
92 bool operator >= (const selector_specificity& val) const
93 {
94 if((*this) == val) return true;
95 if((*this) > val) return true;
96 return false;
97 }
98
99 bool operator <= (const selector_specificity& val) const
100 {
101 if((*this) > val)
102 {
103 return false;
104 }
105 return true;
106 }
107
108 bool operator < (const selector_specificity& val) const
109 {
110 if((*this) <= val && (*this) != val)
111 {
112 return true;
113 }
114 return false;
115 }
116
117 };
118
120
129
130 // https://www.w3.org/TR/selectors-4/#attribute-selectors
141
143
144 class css_selector;
145 class html_tag;
146
147 // <subclass-selector> | <pseudo-element-selector>
148 // = <id-selector> | <class-selector> | <attribute-selector> | <pseudo-class-selector> | <pseudo-element-selector>
150 {
151 using vector = std::vector<css_attribute_selector>;
152
154 string_id prefix; // [prefix|name]
155 string_id name; // .name, #name, [name], :name
156 string value; // [name=value], :lang(value)
157
158 attr_matcher matcher; // <attr-matcher> = ~= |= ^= $= *=
159 bool caseless_match; // value is matched ASCII case-insensitively
160
161 std::vector<shared_ptr<css_selector>> selector_list; // :not(selector_list)
162 int a, b; // :nth-child(an+b of selector_list)
163
166
167 operator bool() const { return name != empty_id; }
168 };
169
171
172 class css_element_selector // compound selector: div.class:hover
173 {
174 public:
175 using ptr = shared_ptr<css_element_selector>;
176 public:
177 string_id m_prefix;
178 string_id m_tag;
179 css_attribute_selector::vector m_attrs; // subclass and pseudo-element selectors
180 };
181
183
191
193
194 class css_selector // complex selector: div + p
195 {
196 public:
198 using vector = std::vector<css_selector::ptr>;
199
200 public:
202 int m_order = 0;
208
209 public:
210 bool parse(const string& text, document_mode mode);
211 void calc_specificity();
212 bool is_media_valid() const;
213 void add_media_to_doc(document* doc) const;
214 };
215
217 {
218 if(!m_media_query)
219 {
220 return true;
221 }
222 return m_media_query->is_used();
223 }
224
225
227
228 inline bool operator > (const css_selector& v1, const css_selector& v2)
229 {
230 if(v1.m_specificity == v2.m_specificity)
231 {
232 return (v1.m_order > v2.m_order);
233 }
234 return (v1.m_specificity > v2.m_specificity);
235 }
236
237 inline bool operator < (const css_selector& v1, const css_selector& v2)
238 {
239 if(v1.m_specificity == v2.m_specificity)
240 {
241 return (v1.m_order < v2.m_order);
242 }
243 return (v1.m_specificity < v2.m_specificity);
244 }
245
246 inline bool operator > (const css_selector::ptr& v1, const css_selector::ptr& v2)
247 {
248 return (*v1 > *v2);
249 }
250
251 inline bool operator < (const css_selector::ptr& v1, const css_selector::ptr& v2)
252 {
253 return (*v1 < *v2);
254 }
255
257
259 {
260 public:
261 typedef std::unique_ptr<used_selector> ptr;
262 typedef std::vector<used_selector::ptr> vector;
263
265 bool m_used;
266
272 };
273
274
275 enum {
279 };
280
282}
283
284#endif // LH_CSS_SELECTOR_H
Definition css_selector.h:173
css_attribute_selector::vector m_attrs
Definition css_selector.h:179
string_id m_prefix
Definition css_selector.h:177
shared_ptr< css_element_selector > ptr
Definition css_selector.h:175
string_id m_tag
Definition css_selector.h:178
Definition css_selector.h:195
css_combinator m_combinator
Definition css_selector.h:205
void calc_specificity()
Definition css_selector.cpp:9
media_query_list_list::ptr m_media_query
Definition css_selector.h:206
selector_specificity m_specificity
Definition css_selector.h:201
shared_ptr< css_selector > ptr
Definition css_selector.h:197
css_element_selector m_right
Definition css_selector.h:204
bool parse(const string &text, document_mode mode)
Definition css_selector.cpp:705
style::ptr m_style
Definition css_selector.h:207
void add_media_to_doc(document *doc) const
Definition css_selector.cpp:32
css_selector::ptr m_left
Definition css_selector.h:203
std::vector< css_selector::ptr > vector
Definition css_selector.h:198
bool is_media_valid() const
Definition css_selector.h:216
int m_order
Definition css_selector.h:202
Definition document.h:45
shared_ptr< media_query_list_list > ptr
Definition media_query.h:99
std::shared_ptr< style > ptr
Definition style.h:42
Definition css_selector.h:259
std::unique_ptr< used_selector > ptr
Definition css_selector.h:261
used_selector(const css_selector::ptr &selector, bool used)
Definition css_selector.h:267
bool m_used
Definition css_selector.h:265
std::vector< used_selector::ptr > vector
Definition css_selector.h:262
css_selector::ptr m_selector
Definition css_selector.h:264
type
Definition core.h:681
Definition background.h:12
css_combinator
Definition css_selector.h:185
@ combinator_general_sibling
Definition css_selector.h:189
@ combinator_descendant
Definition css_selector.h:186
@ combinator_adjacent_sibling
Definition css_selector.h:188
@ combinator_child
Definition css_selector.h:187
vector< css_token > css_token_vector
Definition css_tokenizer.h:151
css_selector::vector parse_selector_list(const css_token_vector &tokens, int options, document_mode mode)
Definition css_selector.cpp:676
string_id _id(const string &str)
Definition string_id.cpp:38
bool operator<(const css_selector &v1, const css_selector &v2)
Definition css_selector.h:237
@ forbid_pseudo_elements
Definition css_selector.h:278
@ forgiving_mode
Definition css_selector.h:277
@ strict_mode
Definition css_selector.h:276
attr_select_type
Definition css_selector.h:122
@ select_pseudo_element
Definition css_selector.h:127
@ select_class
Definition css_selector.h:123
@ select_attr
Definition css_selector.h:125
@ select_pseudo_class
Definition css_selector.h:126
@ select_id
Definition css_selector.h:124
document_mode
Definition types.h:47
const string_id empty_id
Definition string_id.cpp:35
attr_matcher
Definition css_selector.h:132
@ attribute_contains_word
Definition css_selector.h:136
@ attribute_exists
Definition css_selector.h:133
@ attribute_starts_with_string_hyphen
Definition css_selector.h:138
@ attribute_contains_string
Definition css_selector.h:135
@ attribute_equals
Definition css_selector.h:134
@ attribute_ends_with_string
Definition css_selector.h:139
@ attribute_starts_with_string
Definition css_selector.h:137
bool operator>(const css_selector &v1, const css_selector &v2)
Definition css_selector.h:228
Definition css_selector.h:150
std::vector< css_attribute_selector > vector
Definition css_selector.h:151
string_id prefix
Definition css_selector.h:154
int b
Definition css_selector.h:162
int a
Definition css_selector.h:162
string value
Definition css_selector.h:156
attr_select_type type
Definition css_selector.h:153
std::vector< shared_ptr< css_selector > > selector_list
Definition css_selector.h:161
bool caseless_match
Definition css_selector.h:159
string_id name
Definition css_selector.h:155
attr_matcher matcher
Definition css_selector.h:158
css_attribute_selector(attr_select_type type=select_class, string name="")
Definition css_selector.h:164
Definition css_selector.h:13
bool operator!=(const selector_specificity &val) const
Definition css_selector.h:44
bool operator<=(const selector_specificity &val) const
Definition css_selector.h:99
selector_specificity(int va=0, int vb=0, int vc=0, int vd=0)
Definition css_selector.h:19
int c
Definition css_selector.h:16
int d
Definition css_selector.h:17
void operator+=(const selector_specificity &val)
Definition css_selector.h:27
bool operator==(const selector_specificity &val) const
Definition css_selector.h:35
int b
Definition css_selector.h:15
bool operator>(const selector_specificity &val) const
Definition css_selector.h:53
int a
Definition css_selector.h:14
bool operator<(const selector_specificity &val) const
Definition css_selector.h:108
bool operator>=(const selector_specificity &val) const
Definition css_selector.h:92