2#pragma warning(disable: 4456)
10void litehtml::css::parse_stylesheet(
const tchar_t* str,
const tchar_t* baseurl,
const std::shared_ptr<document>& doc,
const media_query_list::ptr& media)
15 tstring::size_type c_start = text.find(_t(
"/*"));
16 while(c_start != tstring::npos)
18 tstring::size_type c_end = text.find(_t(
"*/"), c_start + 2);
19 text.erase(c_start, c_end - c_start + 2);
20 c_start = text.find(_t(
"/*"));
23 tstring::size_type pos = text.find_first_not_of(_t(
" \n\r\t"));
24 while(pos != tstring::npos)
26 while(pos != tstring::npos && text[pos] == _t(
'@'))
28 tstring::size_type sPos = pos;
29 pos = text.find_first_of(_t(
"{;"), pos);
30 if(pos != tstring::npos && text[pos] == _t(
'{'))
32 pos = find_close_bracket(text, pos, _t(
'{'), _t(
'}'));
34 if(pos != tstring::npos)
36 parse_atrule(text.substr(sPos, pos - sPos + 1), baseurl, doc, media);
39 parse_atrule(text.substr(sPos), baseurl, doc, media);
42 if(pos != tstring::npos)
44 pos = text.find_first_not_of(_t(
" \n\r\t"), pos + 1);
48 if(pos == tstring::npos)
53 tstring::size_type style_start = text.find(_t(
"{"), pos);
54 tstring::size_type style_end = text.find(_t(
"}"), pos);
55 if(style_start != tstring::npos && style_end != tstring::npos)
57 style::ptr st = std::make_shared<style>();
58 st->add(text.substr(style_start + 1, style_end - style_start - 1).c_str(), baseurl);
60 parse_selectors(text.substr(pos, style_start - pos), st, media);
64 doc->add_media_list(media);
73 if(pos != tstring::npos)
75 pos = text.find_first_not_of(_t(
" \n\r\t"), pos);
80void litehtml::css::parse_css_url(
const tstring& str, tstring& url )
83 size_t pos1 = str.find(_t(
'('));
84 size_t pos2 = str.find(_t(
')'));
85 if(pos1 != tstring::npos && pos2 != tstring::npos)
87 url = str.substr(pos1 + 1, pos2 - pos1 - 1);
90 if(url[0] == _t(
'\'') || url[0] == _t(
'"'))
97 if(url[url.length() - 1] == _t(
'\'') || url[url.length() - 1] == _t(
'"'))
99 url.erase(url.length() - 1, 1);
105bool litehtml::css::parse_selectors(
const tstring& txt,
const litehtml::style::ptr& styles,
const media_query_list::ptr& media )
107 tstring selector = txt;
109 string_vector tokens;
110 split_string(selector, tokens, _t(
","));
112 bool added_something =
false;
114 for(string_vector::iterator tok = tokens.begin(); tok != tokens.end(); tok++)
116 css_selector::ptr selector = std::make_shared<css_selector>(media);
117 selector->m_style = styles;
119 if(selector->parse(*tok))
121 selector->calc_specificity();
122 add_selector(selector);
123 added_something =
true;
127 return added_something;
130void litehtml::css::sort_selectors()
132 std::sort(m_selectors.begin(), m_selectors.end(),
133 [](
const css_selector::ptr& v1,
const css_selector::ptr& v2)
135 return (*v1) < (*v2);
140void litehtml::css::parse_atrule(
const tstring& text,
const tchar_t* baseurl,
const std::shared_ptr<document>& doc,
const media_query_list::ptr& media)
142 if(text.substr(0, 7) == _t(
"@import"))
146 iStr = text.substr(sPos);
147 if(iStr[iStr.length() - 1] == _t(
';'))
149 iStr.erase(iStr.length() - 1);
152 string_vector tokens;
153 split_string(iStr, tokens, _t(
" "), _t(
""), _t(
"(\""));
157 parse_css_url(tokens.front(), url);
160 url = tokens.front();
162 tokens.erase(tokens.begin());
165 document_container* doc_cont = doc->container();
172 css_baseurl = baseurl;
174 doc_cont->import_css(css_text, url, css_baseurl);
175 if(!css_text.empty())
177 media_query_list::ptr new_media = media;
181 for(string_vector::iterator iter = tokens.begin(); iter != tokens.end(); iter++)
183 if(iter != tokens.begin())
185 media_str += _t(
" ");
187 media_str += (*iter);
189 new_media = media_query_list::create_from_string(media_str, doc);
195 parse_stylesheet(css_text.c_str(), css_baseurl.c_str(), doc, new_media);
200 }
else if(text.substr(0, 6) == _t(
"@media"))
202 tstring::size_type b1 = text.find_first_of(_t(
'{'));
203 tstring::size_type b2 = text.find_last_of(_t(
'}'));
204 if(b1 != tstring::npos)
206 tstring media_type = text.substr(6, b1 - 6);
208 media_query_list::ptr new_media = media_query_list::create_from_string(media_type, doc);
211 if(b2 != tstring::npos)
213 media_style = text.substr(b1 + 1, b2 - b1 - 1);
216 media_style = text.substr(b1 + 1);
219 parse_stylesheet(media_style.c_str(), baseurl, doc, new_media);