1 /*
2  * This file is part of CSSTidy.
3  *
4  * CSSTidy is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * CSSTidy is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with CSSTidy; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19 #include "csspp_globals.hpp"
20 extern map< string, vector<string> > shorthands;
21 
csstidy()22 csstidy::csstidy()
23 {
24 	properties = 0;
25 	selectors = 0;
26 	charset = "";
27 	namesp = "";
28 	line = 1;
29 	tokens = "{};:()@='\"/,\\!$%&*+.<>?[]^`|~";
30 	css_level = "CSS2.1";
31 
32 	settings["remove_bslash"] = 1;
33 	settings["compress_colors"] = 1;
34 	settings["compress_font-weight"] = 1;
35 	settings["lowercase_s"] = 0;
36 	settings["optimise_shorthands"] = 1;
37 	settings["remove_last_;"] = 0;
38 	settings["case_properties"] = 0;
39 	settings["sort_properties"] = 0;
40 	settings["sort_selectors"] = 0;
41 	settings["merge_selectors"] = 2;
42 	settings["discard_invalid_properties"] = 0;
43 	settings["allow_html_in_templates"] = 0;
44 	settings["silent"] = 0;
45 	settings["preserve_css"] = 0;
46 	settings["timestamp"] = 0;
47 
48 	csstemplate.push_back("<span class=\"at\">"); //string before @rule
49 	csstemplate.push_back("</span> <span class=\"format\">{</span>\n"); //bracket after @-rule
50 	csstemplate.push_back("<span class=\"selector\">"); //string before selector
51 	csstemplate.push_back("</span> <span class=\"format\">{</span>\n"); //bracket after selector
52 	csstemplate.push_back("<span class=\"property\">"); //string before property
53 	csstemplate.push_back("</span><span class=\"value\">"); //string after property+before value
54 	csstemplate.push_back("</span><span class=\"format\">;</span>\n"); //string after value
55 	csstemplate.push_back("<span class=\"format\">}</span>"); //closing bracket - selector
56 	csstemplate.push_back("\n\n"); //space between blocks {...}
57 	csstemplate.push_back("\n<span class=\"format\">}</span>\n\n"); //closing bracket @-rule
58 	csstemplate.push_back(""); //indent in @-rule
59 	csstemplate.push_back("<span class=\"comment\">"); // before comment
60 	csstemplate.push_back("</span>\n"); //after comment
61 	csstemplate.push_back("\n"); // after last line @-rule
62 }
63 
add_token(const token_type ttype,const string data,const bool force)64 void csstidy::add_token(const token_type ttype, const string data, const bool force)
65 {
66 	if(settings["preserve_css"] || force) {
67 		token temp;
68 		temp.type = ttype;
69 		temp.data = (ttype == COMMENT) ? data : trim(data);
70 		csstokens.push_back(temp);
71 	}
72 }
73 
copy(const string media,const string selector,const string media_new,const string selector_new)74 void csstidy::copy(const string media, const string selector, const string media_new, const string selector_new)
75 {
76 	for(int k = 0; k < css[media][selector].size(); k++)
77 	{
78 		string property = css[media][selector].at(k);
79 		string value = css[media][selector][property];
80 		add(media_new,selector_new,property,value);
81 	}
82 }
83 
add(const string & media,const string & selector,const string & property,const string & value)84 void csstidy::add(const string& media, const string& selector, const string& property, const string& value)
85 {
86 	if(settings["preserve_css"]) {
87 		return;
88 	}
89 
90 	if(css[media][selector].has(property))
91 	{
92 		if( !is_important(css[media][selector][property]) || (is_important(css[media][selector][property]) && is_important(value)) )
93 		{
94 			css[media][selector].erase(property);
95 			css[media][selector][property] = trim(value);
96 		}
97 	}
98 	else
99 	{
100 		css[media][selector][property] = trim(value);
101 	}
102 }
103 
log(const string msg,const message_type type,int iline)104 void csstidy::log(const string msg, const message_type type, int iline)
105 {
106 	message new_msg;
107 	new_msg.m = msg;
108 	new_msg.t = type;
109 	if(iline == 0)
110 	{
111 		iline = line;
112 	}
113 	if(logs.count(line) > 0)
114 	{
115 		for(int i = 0; i < logs[line].size(); ++i)
116 		{
117 			if(logs[line][i].m == new_msg.m && logs[line][i].t == new_msg.t)
118 			{
119 				return;
120 			}
121 		}
122 	}
123 	logs[line].push_back(new_msg);
124 }
125 
unicode(string & istring,int & i)126 string csstidy::unicode(string& istring,int& i)
127 {
128 	++i;
129 	string add = "";
130 	bool replaced = false;
131 
132 	while(i < istring.length() && (ctype_xdigit(istring[i]) || ctype_space(istring[i])) && add.length()< 6)
133 	{
134 		add += istring[i];
135 
136 		if(ctype_space(istring[i]))
137 		{
138 			break;
139 		}
140 		i++;
141 	}
142 
143 	if(hexdec(add) > 47 && hexdec(add) < 58 || hexdec(add) > 64 && hexdec(add) < 91 || hexdec(add) > 96 && hexdec(add) < 123)
144 	{
145 		string msg = "Replaced unicode notation: Changed \\" + rtrim(add) + " to ";
146 		add = static_cast<int>(hexdec(add));
147 		msg += add;
148 		log(msg,Information);
149 		replaced = true;
150 	}
151 	else
152 	{
153 		add = trim("\\" + add);
154 	}
155 
156 	if(ctype_xdigit(istring[i+1]) && ctype_space(istring[i]) && !replaced || !ctype_space(istring[i]))
157 	{
158 		i--;
159 	}
160 
161 	if(add != "\\" || !settings["remove_bslash"] || in_str_array(tokens,istring[i+1]))
162 	{
163 		return add;
164 	}
165 	if(add == "\\")
166 	{
167 		log("Removed unnecessary backslash",Information);
168 	}
169 	return "";
170 }
171 
is_token(string & istring,const int i)172 bool csstidy::is_token(string& istring,const int i)
173 {
174 	return (in_str_array(tokens,istring[i]) && !escaped(istring,i));
175 }
176 
merge_4value_shorthands(string media,string selector)177 void csstidy::merge_4value_shorthands(string media, string selector)
178 {
179 	for(map< string, vector<string> >::iterator i = shorthands.begin(); i != shorthands.end(); ++i )
180 	{
181 		string temp;
182 
183 		if(css[media][selector].has(i->second[0]) && css[media][selector].has(i->second[1])
184 		&& css[media][selector].has(i->second[2]) && css[media][selector].has(i->second[3]))
185 		{
186 			string important = "";
187 			for(int j = 0; j < 4; ++j)
188 			{
189 				string val = css[media][selector][i->second[j]];
190 				if(is_important(val))
191 				{
192 					important = "!important";
193 					temp += gvw_important(val)+ " ";
194 				}
195 				else
196 				{
197 					temp += val + " ";
198 				}
199 				css[media][selector].erase(i->second[j]);
200 			}
201 			add(media, selector, i->first, shorthand(trim(temp + important)));
202 		}
203 	}
204 }
205 
dissolve_4value_shorthands(string property,string value)206 map<string,string> csstidy::dissolve_4value_shorthands(string property, string value)
207 {
208 	map<string, string> ret;
209 	extern map< string, vector<string> > shorthands;
210 
211 	if(shorthands[property][0] == "0")
212 	{
213 		ret[property] = value;
214 		return ret;
215 	}
216 
217 	string important = "";
218 	if(is_important(value))
219 	{
220 		value = gvw_important(value);
221 		important = "!important";
222 	}
223 	vector<string> values = explode(" ",value);
224 
225 	if(values.size() == 4)
226 	{
227 		for(int i=0; i < 4; ++i)
228 		{
229 			ret[shorthands[property][i]] = values[i] + important;
230 		}
231 	}
232 	else if(values.size() == 3)
233 	{
234 		ret[shorthands[property][0]] = values[0] + important;
235 		ret[shorthands[property][1]] = values[1] + important;
236 		ret[shorthands[property][3]] = values[1] + important;
237 		ret[shorthands[property][2]] = values[2] + important;
238 	}
239 	else if(values.size() == 2)
240 	{
241 		for(int i = 0; i < 4; ++i)
242 		{
243 			ret[shorthands[property][i]] = ((i % 2 != 0)) ? values[1] + important : values[0] + important;
244 		}
245 	}
246 	else
247 	{
248 		for(int i = 0; i < 4; ++i)
249 		{
250 			ret[shorthands[property][i]] = values[0] + important;
251 		}
252 	}
253 
254 	return ret;
255 }
256 
explode_selectors()257 void csstidy::explode_selectors()
258 {
259 	// Explode multiple selectors
260     if (settings["merge_selectors"] == 1)
261     {
262         vector<string> new_sels;
263         int lastpos = 0;
264         sel_separate.push_back(cur_selector.length());
265 
266         for (int i = 0; i < sel_separate.size(); ++i)
267         {
268             if (i == sel_separate.size()-1) {
269                 sel_separate[i] += 1;
270             }
271 
272             new_sels.push_back(cur_selector.substr(lastpos,sel_separate[i]-lastpos-1));
273             lastpos = sel_separate[i];
274         }
275 
276         if (new_sels.size() > 1)
277         {
278             for (int i = 0; i < new_sels.size(); ++i)
279             {
280 				for (pstore::iterator j = css[cur_at][cur_selector].begin(); j != css[cur_at][cur_selector].end(); ++j)
281 				{
282             		add(cur_at, new_sels[i], j->first, j->second);
283 				}
284             }
285             css[cur_at].erase(cur_selector);
286         }
287     }
288     sel_separate = vector<int>();
289 }
290