add support for Ayatana indicator to Notification plugin
[claws.git] / src / plugins / litehtml_viewer / litehtml / el_before_after.cpp
blobc7c2d680ba053a8433b8ee195599cb97bf4a58b0
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;counters;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 add_text(get_counter_value(params));
159 break;
160 // counters
161 case 2:
163 string_vector tokens;
164 split_string(params, tokens, ",");
165 add_text(get_counters_value(tokens));
167 break;
168 // url
169 case 3:
171 string p_url = params;
172 trim(p_url);
173 if(!p_url.empty())
175 if(p_url.at(0) == '\'' || p_url.at(0) == '\"')
177 p_url.erase(0, 1);
180 if(!p_url.empty())
182 if(p_url.at(p_url.length() - 1) == '\'' || p_url.at(p_url.length() - 1) == '\"')
184 p_url.erase(p_url.length() - 1, 1);
187 if(!p_url.empty())
189 element::ptr el = std::make_shared<el_image>(get_document());
190 el->set_attr("src", p_url.c_str());
191 el->set_attr("style", "display:inline-block");
192 el->set_tagName("img");
193 appendChild(el);
194 el->parse_attributes();
197 break;
201 litehtml::string litehtml::el_before_after_base::convert_escape( const char* txt )
203 char* str_end;
204 wchar_t u_str[2];
205 u_str[0] = (wchar_t) strtol(txt, &str_end, 16);
206 u_str[1] = 0;
207 return litehtml::string(litehtml_from_wchar(u_str));