-
Notifications
You must be signed in to change notification settings - Fork 1
/
css_selector.cpp
266 lines (246 loc) · 6.09 KB
/
css_selector.cpp
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
#include "litehtml/html.h"
#include "litehtml/css_selector.h"
#include "litehtml/document.h"
void litehtml::css_element_selector::parse( const tstring& txt )
{
tstring::size_type el_end = txt.find_first_of(_t(".#[:"));
m_tag = txt.substr(0, el_end);
litehtml::lcase(m_tag);
m_attrs.clear();
while(el_end != tstring::npos)
{
if(txt[el_end] == _t('.'))
{
css_attribute_selector attribute;
tstring::size_type pos = txt.find_first_of(_t(".#[:"), el_end + 1);
attribute.val = txt.substr(el_end + 1, pos - el_end - 1);
split_string( attribute.val, attribute.class_val, _t(" ") );
attribute.condition = select_equal;
attribute.attribute = _t("class");
m_attrs.push_back(attribute);
el_end = pos;
} else if(txt[el_end] == _t(':'))
{
css_attribute_selector attribute;
if(txt[el_end + 1] == _t(':'))
{
tstring::size_type pos = txt.find_first_of(_t(".#[:"), el_end + 2);
attribute.val = txt.substr(el_end + 2, pos - el_end - 2);
attribute.condition = select_pseudo_element;
litehtml::lcase(attribute.val);
attribute.attribute = _t("pseudo-el");
m_attrs.push_back(attribute);
el_end = pos;
} else
{
tstring::size_type pos = txt.find_first_of(_t(".#[:("), el_end + 1);
if(pos != tstring::npos && txt.at(pos) == _t('('))
{
pos = find_close_bracket(txt, pos);
if(pos != tstring::npos)
{
pos++;
} else
{
int iii = 0;
iii++;
}
}
if(pos != tstring::npos)
{
attribute.val = txt.substr(el_end + 1, pos - el_end - 1);
} else
{
attribute.val = txt.substr(el_end + 1);
}
litehtml::lcase(attribute.val);
if(attribute.val == _t("after") || attribute.val == _t("before"))
{
attribute.condition = select_pseudo_element;
} else
{
attribute.condition = select_pseudo_class;
}
attribute.attribute = _t("pseudo");
m_attrs.push_back(attribute);
el_end = pos;
}
} else if(txt[el_end] == _t('#'))
{
css_attribute_selector attribute;
tstring::size_type pos = txt.find_first_of(_t(".#[:"), el_end + 1);
attribute.val = txt.substr(el_end + 1, pos - el_end - 1);
attribute.condition = select_equal;
attribute.attribute = _t("id");
m_attrs.push_back(attribute);
el_end = pos;
} else if(txt[el_end] == _t('['))
{
css_attribute_selector attribute;
tstring::size_type pos = txt.find_first_of(_t("]~=|$*^"), el_end + 1);
tstring attr = txt.substr(el_end + 1, pos - el_end - 1);
trim(attr);
litehtml::lcase(attr);
if(pos != tstring::npos)
{
if(txt[pos] == _t(']'))
{
attribute.condition = select_exists;
} else if(txt[pos] == _t('='))
{
attribute.condition = select_equal;
pos++;
} else if(txt.substr(pos, 2) == _t("~="))
{
attribute.condition = select_contain_str;
pos += 2;
} else if(txt.substr(pos, 2) == _t("|="))
{
attribute.condition = select_start_str;
pos += 2;
} else if(txt.substr(pos, 2) == _t("^="))
{
attribute.condition = select_start_str;
pos += 2;
} else if(txt.substr(pos, 2) == _t("$="))
{
attribute.condition = select_end_str;
pos += 2;
} else if(txt.substr(pos, 2) == _t("*="))
{
attribute.condition = select_contain_str;
pos += 2;
} else
{
attribute.condition = select_exists;
pos += 1;
}
pos = txt.find_first_not_of(_t(" \t"), pos);
if(pos != tstring::npos)
{
if(txt[pos] == _t('"'))
{
tstring::size_type pos2 = txt.find_first_of(_t("\""), pos + 1);
attribute.val = txt.substr(pos + 1, pos2 == tstring::npos ? pos2 : (pos2 - pos - 1));
pos = pos2 == tstring::npos ? pos2 : (pos2 + 1);
} else if(txt[pos] == _t(']'))
{
pos ++;
} else
{
tstring::size_type pos2 = txt.find_first_of(_t("]"), pos + 1);
attribute.val = txt.substr(pos, pos2 == tstring::npos ? pos2 : (pos2 - pos));
trim(attribute.val);
pos = pos2 == tstring::npos ? pos2 : (pos2 + 1);
}
}
} else
{
attribute.condition = select_exists;
}
attribute.attribute = attr;
m_attrs.push_back(attribute);
el_end = pos;
} else
{
el_end++;
}
el_end = txt.find_first_of(_t(".#[:"), el_end);
}
}
bool litehtml::css_selector::parse( const tstring& text )
{
if(text.empty())
{
return false;
}
string_vector tokens;
split_string(text, tokens, _t(""), _t(" \t>+~"), _t("(["));
if(tokens.empty())
{
return false;
}
tstring left;
tstring right = tokens.back();
tchar_t combinator = 0;
tokens.pop_back();
while(!tokens.empty() && (tokens.back() == _t(" ") || tokens.back() == _t("\t") || tokens.back() == _t("+") || tokens.back() == _t("~") || tokens.back() == _t(">")))
{
if(combinator == _t(' ') || combinator == 0)
{
combinator = tokens.back()[0];
}
tokens.pop_back();
}
for(string_vector::const_iterator i = tokens.begin(); i != tokens.end(); i++)
{
left += *i;
}
trim(left);
trim(right);
if(right.empty())
{
return false;
}
m_right.parse(right);
switch(combinator)
{
case _t('>'):
m_combinator = combinator_child;
break;
case _t('+'):
m_combinator = combinator_adjacent_sibling;
break;
case _t('~'):
m_combinator = combinator_general_sibling;
break;
default:
m_combinator = combinator_descendant;
break;
}
m_left = 0;
if(!left.empty())
{
m_left = std::make_shared<css_selector>(media_query_list::ptr(0));
if(!m_left->parse(left))
{
return false;
}
}
return true;
}
void litehtml::css_selector::calc_specificity()
{
if(!m_right.m_tag.empty() && m_right.m_tag != _t("*"))
{
m_specificity.d = 1;
}
for(css_attribute_selector::vector::iterator i = m_right.m_attrs.begin(); i != m_right.m_attrs.end(); i++)
{
if(i->attribute == _t("id"))
{
m_specificity.b++;
} else
{
if(i->attribute == _t("class"))
{
m_specificity.c += (int) i->class_val.size();
} else
{
m_specificity.c++;
}
}
}
if(m_left)
{
m_left->calc_specificity();
m_specificity += m_left->m_specificity;
}
}
void litehtml::css_selector::add_media_to_doc( document* doc ) const
{
if(m_media_query && doc)
{
doc->add_media_list(m_media_query);
}
}