Menu

[r29]: / trunk-cpp / csstidy.cpp  Maximize  Restore  History

Download this file

271 lines (244 with data), 7.6 kB

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