1 #include "html.h"
2 #include "el_before_after.h"
3 #include "el_text.h"
4 #include "el_space.h"
5 #include "el_image.h"
6 
el_before_after_base(const std::shared_ptr<litehtml::document> & doc,bool before)7 litehtml::el_before_after_base::el_before_after_base(const std::shared_ptr<litehtml::document>& doc, bool before) : html_tag(doc)
8 {
9 	if(before)
10 	{
11 		set_tagName(_t("::before"));
12 	} else
13 	{
14 		set_tagName(_t("::after"));
15 	}
16 }
17 
~el_before_after_base()18 litehtml::el_before_after_base::~el_before_after_base()
19 {
20 
21 }
22 
add_style(const litehtml::style & st)23 void litehtml::el_before_after_base::add_style(const litehtml::style& st)
24 {
25 	html_tag::add_style(st);
26 
27 	tstring content = get_style_property(_t("content"), false, _t(""));
28 	if(!content.empty())
29 	{
30 		int idx = value_index(content.c_str(), content_property_string);
31 		if(idx < 0)
32 		{
33 			tstring fnc;
34 			tstring::size_type i = 0;
35 			while(i < content.length() && i != tstring::npos)
36 			{
37 				if(content.at(i) == _t('"'))
38 				{
39 					fnc.clear();
40 					i++;
41 					tstring::size_type pos = content.find(_t('"'), i);
42 					tstring txt;
43 					if(pos == tstring::npos)
44 					{
45 						txt = content.substr(i);
46 						i = tstring::npos;
47 					} else
48 					{
49 						txt = content.substr(i, pos - i);
50 						i = pos + 1;
51 					}
52 					add_text(txt);
53 				} else if(content.at(i) == _t('('))
54 				{
55 					i++;
56 					litehtml::trim(fnc);
57 					litehtml::lcase(fnc);
58 					tstring::size_type pos = content.find(_t(')'), i);
59 					tstring params;
60 					if(pos == tstring::npos)
61 					{
62 						params = content.substr(i);
63 						i = tstring::npos;
64 					} else
65 					{
66 						params = content.substr(i, pos - i);
67 						i = pos + 1;
68 					}
69 					add_function(fnc, params);
70 					fnc.clear();
71 				} else
72 				{
73 					fnc += content.at(i);
74 					i++;
75 				}
76 			}
77 		}
78 	}
79 }
80 
add_text(const tstring & txt)81 void litehtml::el_before_after_base::add_text( const tstring& txt )
82 {
83 	tstring word;
84 	tstring esc;
85 	for(tstring::size_type i = 0; i < txt.length(); i++)
86 	{
87 		if( (txt.at(i) == _t(' ')) || (txt.at(i) == _t('\t')) || (txt.at(i) == _t('\\') && !esc.empty()) )
88 		{
89 			if(esc.empty())
90 			{
91 				if(!word.empty())
92 				{
93 					element::ptr el = std::make_shared<el_text>(word.c_str(), get_document());
94 					appendChild(el);
95 					word.clear();
96 				}
97 
98 				element::ptr el = std::make_shared<el_space>(txt.substr(i, 1).c_str(), get_document());
99 				appendChild(el);
100 			} else
101 			{
102 				word += convert_escape(esc.c_str() + 1);
103 				esc.clear();
104 				if(txt.at(i) == _t('\\'))
105 				{
106 					esc += txt.at(i);
107 				}
108 			}
109 		} else
110 		{
111 			if(!esc.empty() || txt.at(i) == _t('\\'))
112 			{
113 				esc += txt.at(i);
114 			} else
115 			{
116 				word += txt.at(i);
117 			}
118 		}
119 	}
120 
121 	if(!esc.empty())
122 	{
123 		word += convert_escape(esc.c_str() + 1);
124 	}
125 	if(!word.empty())
126 	{
127 		element::ptr el = std::make_shared<el_text>(word.c_str(), get_document());
128 		appendChild(el);
129 		word.clear();
130 	}
131 }
132 
add_function(const tstring & fnc,const tstring & params)133 void litehtml::el_before_after_base::add_function( const tstring& fnc, const tstring& params )
134 {
135 	int idx = value_index(fnc.c_str(), _t("attr;counter;url"));
136 	switch(idx)
137 	{
138 	// attr
139 	case 0:
140 		{
141 			tstring p_name = params;
142 			trim(p_name);
143 			lcase(p_name);
144 			element::ptr el_parent = parent();
145 			if (el_parent)
146 			{
147 				const tchar_t* attr_value = el_parent->get_attr(p_name.c_str());
148 				if (attr_value)
149 				{
150 					add_text(attr_value);
151 				}
152 			}
153 		}
154 		break;
155 	// counter
156 	case 1:
157 		break;
158 	// url
159 	case 2:
160 		{
161 			tstring p_url = params;
162 			trim(p_url);
163 			if(!p_url.empty())
164 			{
165 				if(p_url.at(0) == _t('\'') || p_url.at(0) == _t('\"'))
166 				{
167 					p_url.erase(0, 1);
168 				}
169 			}
170 			if(!p_url.empty())
171 			{
172 				if(p_url.at(p_url.length() - 1) == _t('\'') || p_url.at(p_url.length() - 1) == _t('\"'))
173 				{
174 					p_url.erase(p_url.length() - 1, 1);
175 				}
176 			}
177 			if(!p_url.empty())
178 			{
179 				element::ptr el = std::make_shared<el_image>(get_document());
180 				el->set_attr(_t("src"), p_url.c_str());
181 				el->set_attr(_t("style"), _t("display:inline-block"));
182 				el->set_tagName(_t("img"));
183 				appendChild(el);
184 				el->parse_attributes();
185 			}
186 		}
187 		break;
188 	}
189 }
190 
convert_escape(const tchar_t * txt)191 litehtml::tchar_t litehtml::el_before_after_base::convert_escape( const tchar_t* txt )
192 {
193 	tchar_t* sss = 0;
194 	return (tchar_t) t_strtol(txt, &sss, 16);
195 }
196 
apply_stylesheet(const litehtml::css & stylesheet)197 void litehtml::el_before_after_base::apply_stylesheet( const litehtml::css& stylesheet )
198 {
199 
200 }
201