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