Disable stack execution on plugins
[claws.git] / src / plugins / litehtml_viewer / litehtml / el_before_after.cpp
blob8c24eec98544a70dedd448ee3e4f5808aabadd1e
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 #include "utf8_strings.h"
8 litehtml::el_before_after_base::el_before_after_base(const std::shared_ptr<document>& doc, bool before) : html_tag(doc)
10 m_tag = before ? __tag_before_ : __tag_after_;
13 void litehtml::el_before_after_base::add_style(const style& style)
15 html_tag::add_style(style);
17 auto children = m_children;
18 m_children.clear();
20 const auto& content_property = style.get_property(_content_);
21 if(content_property.m_type == prop_type_string && !content_property.m_string.empty())
23 int idx = value_index(content_property.m_string, content_property_string);
24 if(idx < 0)
26 string fnc;
27 string::size_type i = 0;
28 while(i < content_property.m_string.length() && i != string::npos)
30 if(content_property.m_string.at(i) == '"' || content_property.m_string.at(i) == '\'')
32 auto chr = content_property.m_string.at(i);
33 fnc.clear();
34 i++;
35 string::size_type pos = content_property.m_string.find(chr, i);
36 string txt;
37 if(pos == string::npos)
39 txt = content_property.m_string.substr(i);
40 i = string::npos;
41 } else
43 txt = content_property.m_string.substr(i, pos - i);
44 i = pos + 1;
46 add_text(txt);
47 } else if(content_property.m_string.at(i) == '(')
49 i++;
50 litehtml::trim(fnc);
51 litehtml::lcase(fnc);
52 string::size_type pos = content_property.m_string.find(')', i);
53 string params;
54 if(pos == string::npos)
56 params = content_property.m_string.substr(i);
57 i = string::npos;
58 } else
60 params = content_property.m_string.substr(i, pos - i);
61 i = pos + 1;
63 add_function(fnc, params);
64 fnc.clear();
65 } else
67 fnc += content_property.m_string.at(i);
68 i++;
74 if(m_children.empty())
76 m_children = children;
80 void litehtml::el_before_after_base::add_text( const string& txt )
82 string word;
83 string esc;
85 for(auto chr : txt)
87 if(chr == '\\' ||
88 !esc.empty() && esc.length() < 5 && (chr >= '0' && chr <= '9' || chr >= 'A' && chr <= 'Z' || chr >= 'a' && chr <= 'z'))
90 if(!esc.empty() && chr == '\\')
92 word += convert_escape(esc.c_str() + 1);
93 esc.clear();
95 esc += chr;
96 } else
98 if(!esc.empty())
100 word += convert_escape(esc.c_str() + 1);
101 esc.clear();
103 if(isspace(chr))
105 if(!word.empty())
107 element::ptr el = std::make_shared<el_text>(word.c_str(), get_document());
108 appendChild(el);
109 word.clear();
111 word += chr;
112 element::ptr el = std::make_shared<el_space>(word.c_str(), get_document());
113 appendChild(el);
114 word.clear();
115 } else
117 word += chr;
122 if(!esc.empty())
124 word += convert_escape(esc.c_str() + 1);
126 if(!word.empty())
128 element::ptr el = std::make_shared<el_text>(word.c_str(), get_document());
129 appendChild(el);
130 word.clear();
134 void litehtml::el_before_after_base::add_function( const string& fnc, const string& params )
136 int idx = value_index(fnc, "attr;counter;url");
137 switch(idx)
139 // attr
140 case 0:
142 string p_name = params;
143 trim(p_name);
144 lcase(p_name);
145 element::ptr el_parent = parent();
146 if (el_parent)
148 const char* attr_value = el_parent->get_attr(p_name.c_str());
149 if (attr_value)
151 add_text(attr_value);
155 break;
156 // counter
157 case 1:
158 break;
159 // url
160 case 2:
162 string p_url = params;
163 trim(p_url);
164 if(!p_url.empty())
166 if(p_url.at(0) == '\'' || p_url.at(0) == '\"')
168 p_url.erase(0, 1);
171 if(!p_url.empty())
173 if(p_url.at(p_url.length() - 1) == '\'' || p_url.at(p_url.length() - 1) == '\"')
175 p_url.erase(p_url.length() - 1, 1);
178 if(!p_url.empty())
180 element::ptr el = std::make_shared<el_image>(get_document());
181 el->set_attr("src", p_url.c_str());
182 el->set_attr("style", "display:inline-block");
183 el->set_tagName("img");
184 appendChild(el);
185 el->parse_attributes();
188 break;
192 litehtml::string litehtml::el_before_after_base::convert_escape( const char* txt )
194 char* str_end;
195 wchar_t u_str[2];
196 u_str[0] = (wchar_t) strtol(txt, &str_end, 16);
197 u_str[1] = 0;
198 return litehtml::string(litehtml_from_wchar(u_str));