add support for Ayatana indicator to Notification plugin
[claws.git] / src / plugins / litehtml_viewer / litehtml / css_selector.h
blob4fe7db9e50b8d4aad79a86992bde1f34c934fb1f
1 #ifndef LH_CSS_SELECTOR_H
2 #define LH_CSS_SELECTOR_H
4 #include "style.h"
5 #include "media_query.h"
7 namespace litehtml
9 //////////////////////////////////////////////////////////////////////////
11 struct selector_specificity
13 int a;
14 int b;
15 int c;
16 int d;
18 explicit selector_specificity(int va = 0, int vb = 0, int vc = 0, int vd = 0)
20 a = va;
21 b = vb;
22 c = vc;
23 d = vd;
26 void operator += (const selector_specificity& val)
28 a += val.a;
29 b += val.b;
30 c += val.c;
31 d += val.d;
34 bool operator==(const selector_specificity& val) const
36 if(a == val.a && b == val.b && c == val.c && d == val.d)
38 return true;
40 return false;
43 bool operator!=(const selector_specificity& val) const
45 if(a != val.a || b != val.b || c != val.c || d != val.d)
47 return true;
49 return false;
52 bool operator > (const selector_specificity& val) const
54 if(a > val.a)
56 return true;
57 } else if(a < val.a)
59 return false;
60 } else
62 if(b > val.b)
64 return true;
65 } else if(b < val.b)
67 return false;
68 } else
70 if(c > val.c)
72 return true;
73 } else if(c < val.c)
75 return false;
76 } else
78 if(d > val.d)
80 return true;
81 } else if(d < val.d)
83 return false;
88 return false;
91 bool operator >= (const selector_specificity& val) const
93 if((*this) == val) return true;
94 if((*this) > val) return true;
95 return false;
98 bool operator <= (const selector_specificity& val) const
100 if((*this) > val)
102 return false;
104 return true;
107 bool operator < (const selector_specificity& val) const
109 if((*this) <= val && (*this) != val)
111 return true;
113 return false;
118 //////////////////////////////////////////////////////////////////////////
120 enum attr_select_type
122 select_class,
123 select_id,
125 select_exists,
126 select_equal,
127 select_contain_str,
128 select_start_str,
129 select_end_str,
131 select_pseudo_class,
132 select_pseudo_element,
135 //////////////////////////////////////////////////////////////////////////
137 class css_element_selector;
139 struct css_attribute_selector
141 typedef std::vector<css_attribute_selector> vector;
143 attr_select_type type;
144 string_id name; // .name, #name, [name], :name
145 string val; // [name=val], :lang(val)
147 std::shared_ptr<css_element_selector> sel; // :not(sel)
148 int a, b; // :nth-child(an+b)
150 css_attribute_selector()
152 type = select_class;
153 name = empty_id;
154 a = b = 0;
158 //////////////////////////////////////////////////////////////////////////
160 class css_element_selector
162 public:
163 string_id m_tag;
164 css_attribute_selector::vector m_attrs;
165 public:
167 void parse(const string& txt);
168 static void parse_nth_child_params(const string& param, int& num, int& off);
171 //////////////////////////////////////////////////////////////////////////
173 enum css_combinator
175 combinator_descendant,
176 combinator_child,
177 combinator_adjacent_sibling,
178 combinator_general_sibling
181 //////////////////////////////////////////////////////////////////////////
183 class css_selector
185 public:
186 typedef std::shared_ptr<css_selector> ptr;
187 typedef std::vector<css_selector::ptr> vector;
188 public:
189 selector_specificity m_specificity;
190 css_element_selector m_right;
191 css_selector::ptr m_left;
192 css_combinator m_combinator;
193 style::ptr m_style;
194 int m_order;
195 media_query_list::ptr m_media_query;
196 public:
197 explicit css_selector(const media_query_list::ptr& media = nullptr)
199 m_media_query = media;
200 m_combinator = combinator_descendant;
201 m_order = 0;
204 ~css_selector() = default;
206 css_selector(const css_selector& val)
208 m_right = val.m_right;
209 if(val.m_left)
211 m_left = std::make_shared<css_selector>(*val.m_left);
212 } else
214 m_left = nullptr;
216 m_combinator = val.m_combinator;
217 m_specificity = val.m_specificity;
218 m_order = val.m_order;
219 m_media_query = val.m_media_query;
222 bool parse(const string& text);
223 void calc_specificity();
224 bool is_media_valid() const;
225 void add_media_to_doc(document* doc) const;
228 inline bool css_selector::is_media_valid() const
230 if(!m_media_query)
232 return true;
234 return m_media_query->is_used();
238 //////////////////////////////////////////////////////////////////////////
240 inline bool operator > (const css_selector& v1, const css_selector& v2)
242 if(v1.m_specificity == v2.m_specificity)
244 return (v1.m_order > v2.m_order);
246 return (v1.m_specificity > v2.m_specificity);
249 inline bool operator < (const css_selector& v1, const css_selector& v2)
251 if(v1.m_specificity == v2.m_specificity)
253 return (v1.m_order < v2.m_order);
255 return (v1.m_specificity < v2.m_specificity);
258 inline bool operator > (const css_selector::ptr& v1, const css_selector::ptr& v2)
260 return (*v1 > *v2);
263 inline bool operator < (const css_selector::ptr& v1, const css_selector::ptr& v2)
265 return (*v1 < *v2);
268 //////////////////////////////////////////////////////////////////////////
270 class used_selector
272 public:
273 typedef std::unique_ptr<used_selector> ptr;
274 typedef std::vector<used_selector::ptr> vector;
276 css_selector::ptr m_selector;
277 bool m_used;
279 used_selector(const css_selector::ptr& selector, bool used)
281 m_used = used;
282 m_selector = selector;
285 used_selector(const used_selector& val)
287 m_used = val.m_used;
288 m_selector = val.m_selector;
291 used_selector& operator=(const used_selector& val)
293 m_used = val.m_used;
294 m_selector = val.m_selector;
295 return *this;
300 #endif // LH_CSS_SELECTOR_H