Menu

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

Download this file

256 lines (230 with data), 6.3 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
/*
* 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"
css_manage::css_manage()
{
properties = 0;
selectors = 0;
charset = "";
namesp = "";
line = 1;
tokens = "{};:()@='\"/,\\!$%&*+.<>?[]^`|~";
}
void css_manage::add(const string& media,const string& selector,const string property,const string value)
{
if(css[media].has(selector))
{
css_add_property(media,selector,property,value);
}
else
{
css[media][selector][property] = value;
}
}
void css_manage::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 css_manage::set(string& media, string& selector, string& property, string& value)
{
css[media][selector][property] = value;
}
void css_manage::remove(const string media,const string selector,const string property)
{
css[media][selector].erase(property);
}
string css_manage::get(string media, string selector, string property)
{
return css[media][selector][property];
}
void css_manage::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 css_manage::remove(string media, string selector)
{
css[media].erase(selector);
}
// Adds a property-value pair to an existing property-block.
void css_manage::css_add_property(const string& media, const string& selector, const string& property, const string& value)
{
if(css[media][selector].has(property))
{
if( (is_important(css[media][selector][property]) && is_important(value)) || !is_important(css[media][selector][property]) )
{
remove(media,selector,property);
css[media][selector][property] = trim(value);
}
}
else
{
css[media][selector][property] = trim(value);
}
}
void css_manage::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 css_manage::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 css_manage::is_token(string& istring,const int i)
{
if(in_str_array(tokens,istring[i]) && !escaped(istring,i))
{
return true;
}
return false;
}
void css_manage::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 + " ";
}
remove(media, selector, i->second[j]);
}
add(media, selector, i->first, shorthand(trim(temp + important)));
}
}
}
map<string,string> css_manage::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.