prettydiff2
Version:
Latest version of Pretty Diff for use in Atom Beautify to field test it in the wild before moving on to Pretty Diff 3.
895 lines (893 loc) • 123 kB
JavaScript
/*prettydiff.com topcoms:true,insize:4,inchar:" ",vertical:true */
/*jshint laxbreak: true*/
/*global ace, define, global, module*/
/***********************************************************************
csspretty is written by Austin Cheney on 7 Aug 2014.
Please see the license.txt file associated with the Pretty Diff
application for license information.
**********************************************************************/
/*
This application beautifies CSS code as well as SCSS (Sass) and LESS
variants. This application was written with extension in mind using the
same array based architecture used for the markuppretty and jspretty
libraries. The architecture focuses on separation of roles. The first
area of the application reads the code and writes an array of tokens.
The second area is the algorithm that determines what white space and
indentation should be applied. The final area is a report on the
analysis of the code.
-----------------------------------------------------------------------
*/
(function () {
"use strict";
var csspretty = function csspretty_(options) {
var token = [],
types = [],
lines = [],
depth = [],
begin = [],
uri = [],
colors = [],
output = "",
endline = false,
objsortop = false,
verticalop = false,
colorNames = {
aliceblue : 0.9288006825347457,
antiquewhite : 0.8464695170775405,
aqua : 0.7874,
aquamarine : 0.8078549208338043,
azure : 0.9726526495416643,
beige : 0.8988459998705021,
bisque : 0.8073232737297876,
black : 0,
blanchedalmond : 0.8508443960815607,
blue : 0.0722,
blueviolet : 0.12622014321946043,
brown : 0.09822428787651079,
burlywood : 0.5155984453389335,
cadetblue : 0.29424681085422044,
chartreuse : 0.7603202590262282,
chocolate : 0.23898526114557292,
coral : 0.3701793087292368,
cornflowerblue : 0.30318641994179363,
cornsilk : 0.9356211037296492,
crimson : 0.16042199953025577,
cyan : 0.7874,
darkblue : 0.018640801980939217,
darkcyan : 0.2032931783904645,
darkgoldenrod : 0.27264703559992554,
darkgray : 0.39675523072562674,
darkgreen : 0.09114342904757505,
darkgrey : 0.39675523072562674,
darkkhaki : 0.45747326349994155,
darkmagenta : 0.07353047651207048,
darkolivegreen : 0.12651920884889156,
darkorange : 0.40016167026523863,
darkorchid : 0.1341314217485677,
darkred : 0.05488967453113126,
darksalmon : 0.4054147156338075,
darkseagreen : 0.43789249325969054,
darkslateblue : 0.06579284622798763,
darkslategray : 0.06760815192804355,
darkslategrey : 0.06760815192804355,
darkturquoise : 0.4874606277449034,
darkviolet : 0.10999048339343433,
deeppink : 0.2386689582827583,
deepskyblue : 0.444816033955754,
dimgray : 0.14126329114027164,
dimgrey : 0.14126329114027164,
dodgerblue : 0.2744253699145608,
firebrick : 0.10724525535015225,
floralwhite : 0.9592248482500424,
forestgreen : 0.18920812076002244,
fuchsia : 0.2848,
gainsboro : 0.7156935005064806,
ghostwhite : 0.9431126188632283,
gold : 0.6986087742815887,
goldenrod : 0.41919977809568404,
gray : 0.21586050011389915,
green : 0.15438342968146068,
greenyellow : 0.8060947261145331,
grey : 0.21586050011389915,
honeydew : 0.9633653555478173,
hotpink : 0.3465843816971475,
indianred : 0.21406134963884,
indigo : 0.031075614863369846,
ivory : 0.9907127060061531,
khaki : 0.7701234339412052,
lavendar : 0.8031875051452125,
lavendarblush : 0.9017274863104644,
lawngreen : 0.7390589312496334,
lemonchiffon : 0.9403899224562171,
lightblue : 0.6370914128080659,
lightcoral : 0.35522120733134843,
lightcyan : 0.9458729349482863,
lightgoldenrodyellow: 0.9334835101829635,
lightgray : 0.651405637419824,
lightgreen : 0.6909197995686475,
lightgrey : 0.651405637419824,
lightpink : 0.5856615273489745,
lightsalmon : 0.47806752252059587,
lightseagreen : 0.3505014511704197,
lightskyblue : 0.5619563761833096,
lightslategray : 0.23830165007286924,
lightslategrey : 0.23830165007286924,
lightyellow : 0.9816181839288161,
lime : 0.7152,
limegreen : 0.44571042246097864,
linen : 0.8835734098437936,
magenta : 0.2848,
maroon : 0.04589194232421496,
mediumaquamarine : 0.4938970331080111,
mediumblue : 0.04407778021232784,
mediumorchid : 0.21639251153773428,
mediumpurple : 0.22905858091648004,
mediumseagreen : 0.34393112338131226,
mediumslateblue : 0.20284629471622434,
mediumspringgreen : 0.7070430819418444,
mediumturquois : 0.5133827926447991,
mediumvioletred : 0.14371899849357186,
midnightblue : 0.020717866350860484,
mintcream : 0.9783460494758793,
mistyrose : 0.8218304785918541,
moccasin : 0.8008300099156694,
navajowhite : 0.7651968234278562,
navy : 0.015585128108223519,
oldlace : 0.9190063340554899,
olive : 0.20027537200567563,
olivedrab : 0.2259315095192918,
orange : 0.48170267036309605,
orangered : 0.2551624375341641,
orchid : 0.3134880676143873,
palegoldenrod : 0.7879264788761452,
palegreen : 0.7793675900635259,
paleturquoise : 0.764360779217138,
palevioletred : 0.2875499411788909,
papayawhip : 0.8779710019983541,
peachpuff : 0.7490558987825108,
peru : 0.3011307487793569,
pink : 0.6327107070246611,
plum : 0.4573422158796909,
powderblue : 0.6825458650060524,
purple : 0.061477070432438476,
red : 0.2126,
rosyblue : 0.3231945764940708,
royalblue : 0.16663210743188323,
saddlebrown : 0.09792228502052071,
salmon : 0.3697724152759545,
sandybrown : 0.46628543696283414,
seagreen : 0.1973419970627483,
seashell : 0.927378622069223,
sienna : 0.13697631337097677,
silver : 0.527115125705813,
skyblue : 0.5529166851818412,
slateblue : 0.14784278062136097,
slategray : 0.20896704076536138,
slategrey : 0.20896704076536138,
slightsteelblue : 0.5398388828466575,
snow : 0.9653334183484877,
springgreen : 0.7305230606852947,
steelblue : 0.20562642207624846,
tan : 0.48237604163921527,
teal : 0.1699685577896842,
thistle : 0.5681840109373312,
tomato : 0.3063861271941505,
turquoise : 0.5895536427577983,
violet : 0.40315452986676303,
wheat : 0.7490970282048214,
white : 1,
whitesmoke : 0.913098651793419,
yellow : 0.9278,
yellowgreen : 0.5076295720870697
},
stats = {
braces : 0,
colon : 0,
comments : {
chars: 0,
count: 0
},
properties: {
chars: 0,
count: 0
},
selectors : {
chars: 0,
count: 0
},
semi : 0,
space : 0,
values : {
chars: 0,
count: 0
},
variables : {
chars: 0,
count: 0
}
},
lf = (options.crlf === true || options.crlf === "true")
? "\r\n"
: "\n";
(function csspretty__options() {
objsortop = (
options.objsort === true || options.objsort === "true" || options.objsort === "all" || options.objsort === "css"
);
verticalop = (
options.compressedcss === false && (options.vertical === true || options.vertical === "true" || options.vertical === "all" || options.vertical === "css")
);
options.source = (
typeof options.source === "string" && options.source.length > 0
)
? options
.source
.replace(/\r\n?/g, "\n") + " "
: "Error: no source code supplied to csspretty!";
}());
if (typeof options.source !== "string" || options.source === "" || (/^(\s+)$/).test(options.source) === true) {
if (options.nodeasync === true) {
return [options.source, "Error: no source supplied to csspretty."];
}
if (global.prettydiff.meta === undefined) {
global.prettydiff.meta = {};
}
global.prettydiff.meta.error = options.source;
return options.source;
}
(function csspretty__tokenize() {
var a = 0,
b = options
.source
.split(""),
len = options.source.length,
ltype = "",
itemsize = 0,
space = "",
endtest = false,
struct = [0],
mapper = [],
structval = "root",
nosort = [],
esctest = function csspretty__tokenize_esctest(xx) {
var yy = xx;
do {
xx = xx - 1;
} while (xx > 0 && b[xx] === "\\");
if ((yy - xx) % 2 === 0) {
return true;
}
return false;
},
// Since I am already identifying value types this is a good place to do some
// quick analysis and clean up on certain value conditions. These things are
// being corrected:
// * fractional values missing a leading 0 are provided a leading 0
// * 0 values with a dimension indicator (px, em) have the dimension
// indicator removed
// * eliminate unnecessary leading 0s
// * url values that are not quoted are wrapped in double quote characters
// * color values are set to lowercase and reduced from 6 to 3 digits if
// appropriate
value = function csspretty__tokenize_item_value(val, font) {
var x = val.split(""),
leng = x.length,
cc = 0,
dd = 0,
items = [],
block = "",
values = [],
qchar = "",
qreg = {},
colorPush = function csspretty__tokenize_item_value_colorPush(value) {
var vl = value.toLowerCase();
if ((/^(#[0-9a-f]{3,6})$/).test(vl) === true) {
colors.push(value);
} else if ((/^(rgba?\()/).test(vl) === true) {
colors.push(value);
} else if (colorNames[vl] !== undefined) {
colors.push(value);
}
return value;
};
if (options.quoteConvert === "double") {
qchar = "\"";
} else if (options.quoteConvert === "single") {
qchar = "'";
}
// this loop identifies containment so that tokens/sub-tokens are correctly
// taken
for (cc = 0; cc < leng; cc = cc + 1) {
items.push(x[cc]);
if (block === "") {
if (x[cc] === "\"") {
block = "\"";
dd = dd + 1;
} else if (x[cc] === "'") {
block = "'";
dd = dd + 1;
} else if (x[cc] === "(") {
block = ")";
dd = dd + 1;
} else if (x[cc] === "[") {
block = "]";
dd = dd + 1;
}
} else if ((x[cc] === "(" && block === ")") || (x[cc] === "[" && block === "]")) {
dd = dd + 1;
} else if (x[cc] === block) {
dd = dd - 1;
if (dd === 0) {
block = "";
}
}
if (block === "" && x[cc] === " ") {
items.pop();
values.push(colorPush(items.join("")));
items = [];
}
}
values.push(colorPush(items.join("")));
leng = values.length;
//This is where the rules mentioned above are applied
for (cc = 0; cc < leng; cc = cc + 1) {
if (options.noleadzero === false && (/^(\.\d)/).test(values[cc]) === true) {
values[cc] = "0" + values[cc];
} else if (options.noleadzero === true && (/^(0+\.)/).test(values[cc])) {
values[cc] = values[cc].replace(/^(0+\.)/, ".");
} else if ((/^(0+([a-z]{2,3}|%))$/).test(values[cc]) === true) {
values[cc] = "0";
} else if ((/^(0+)/).test(values[cc]) === true) {
values[cc] = values[cc].replace(/0+/, "0");
if ((/\d/).test(values[cc].charAt(1)) === true) {
values[cc] = values[cc].substr(1);
}
} else if ((/^url\((?!\$)/).test(values[cc]) === true && values[cc].charAt(values[cc].length - 1) === ")") {
block = values[cc].charAt(values[cc].indexOf("url(") + 4);
if (block !== "@" && block !== "{" && block !== "<") {
if (qchar === "") {
values[cc] = values[cc]
.replace(/url\(\s*('|")?/, "url(\"")
.replace(/(('|")?\s*\))$/, "\")");
} else {
values[cc] = values[cc]
.replace(/url\(\s*('|")?/, "url(" + qchar)
.replace(/(('|")?\s*\))$/, qchar + ")");
}
}
} else if (font === true) {
if (qchar === "'") {
values[cc] = values[cc].replace(/"/g, "'");
} else {
values[cc] = values[cc].replace(/'/g, "\"");
}
} else if (font === false && qchar !== "" && ((qchar === "\"" && values[cc].charAt(0) === "'" && values[cc].charAt(values[cc].length - 1) === "'") || (qchar === "'" && values[cc].charAt(0) === "\"" && values[cc].charAt(values[cc].length - 1) === "\""))) {
qreg = new RegExp(qchar, "g");
values[cc] = qchar + values[cc]
.slice(1, values[cc].length - 1)
.replace(qreg, "\\" + qchar) + qchar;
}
}
return values.join(" ");
},
//map location of empty lines for beautification
spacer = function csspretty__tokenize_space(end) {
var slen = space
.split(lf)
.length - 1,
val = 0;
if (token.length === 0 && slen > 0) {
slen = slen + 1;
}
if (slen > 0 && options.preserve > 0) {
if (slen > options.preserve) {
val = options.preserve + 1;
} else {
val = slen;
}
} else if (space.length > 1) {
val = 1;
} else if (slen === 0 && types[types.length - 1] === "comment" && types[types.length - 2] !== "comment") {
types[types.length - 1] = "comment-inline";
}
if (slen > 1 && end === true && options.preserve > 0) {
endline = true;
space = "";
return val;
}
space = "";
return val;
},
//sort parsed properties intelligently
objSort = function csspretty__tokenize_objSort() {
var cc = 0,
dd = 0,
ee = 0,
startlen = token.length - 1,
end = startlen,
keys = [],
keylen = 0,
keyend = 0,
start = 0,
sort = function csspretty__tokenize_objSort_sort(x, y) {
var xx = x[0],
yy = y[0];
if (types[xx] === "comment" || types[xx] === "comment-inline") {
do {
xx = xx + 1;
} while (
xx < startlen && (types[xx] === "comment" || types[xx] === "comment-inline")
);
}
if (types[yy] === "comment" || types[yy] === "comment-inline") {
do {
yy = yy + 1;
} while (
yy < startlen && (types[yy] === "comment" || types[yy] === "comment-inline")
);
}
if (types[xx] < types[yy]) {
return -1;
}
if (types[xx] === types[yy] && token[xx].toLowerCase() < token[yy].toLowerCase()) {
return -1;
}
return 1;
},
semiTest = true,
pairToken = [],
pairTypes = [],
pairLines = [],
pairDepth = [],
pairBegin = [];
if (types[end] === "comment" || types[end] === "comment-inline") {
do {
end = end - 1;
} while (
end > 0 && (types[end] === "comment" || types[end] === "comment-inline")
);
}
for (cc = startlen; cc > -1; cc = cc - 1) {
if (types[cc] === "end") {
dd = dd + 1;
}
if (types[cc] === "start") {
dd = dd - 1;
}
if (dd === 0) {
if ((types[cc] === "property" || types[cc] === "selector" || types[cc] === "propvar") && types[cc - 1] !== "property" && types[cc - 1] !== "selector") {
start = cc;
if (types[end + 1] === "comment-inline") {
end = end + 1;
}
if (types[start - 1] === "comment") {
do {
start = start - 1;
} while (start > -1 && types[start - 1] === "comment");
}
keys.push([
start, end + 1,
false
]);
end = start - 1;
}
}
if (dd < 0 && cc < startlen) {
if (keys.length > 1 && (types[cc - 1] === "selector" || types[cc - 1] === "propvar" || (types[cc - 2] === "propvar" && types[cc - 1] === "value") || token[cc - 1] === "=" || token[cc - 1] === ":" || token[cc - 1] === "[" || token[cc - 1] === "{" || (token[cc - 1] === "," && structval !== "map") || cc === 0)) {
if (structval === "map" && token[token.length - 1] !== ",") {
token.push(",");
types.push("semi");
lines.push(0);
depth.push(depth[depth.length - 1]);
begin.push(begin[begin.length - 1]);
keys[0][1] = keys[0][1] + 1;
}
keys.sort(sort);
keylen = keys.length;
semiTest = false;
for (dd = 0; dd < keylen; dd = dd + 1) {
keyend = keys[dd][1];
for (ee = keys[dd][0]; ee < keyend; ee = ee + 1) {
pairToken.push(token[ee]);
pairTypes.push(types[ee]);
pairLines.push(lines[ee]);
pairDepth.push(depth[ee]);
pairBegin.push(begin[ee]);
if ((token[ee] === ";" && structval === "block") || (token[ee] === "," && structval === "map") || token[ee] === "}") {
semiTest = true;
} else if ((structval === "block" && token[ee] !== ";") && (structval === "map" && token[ee] !== ",") && token[ee] !== "}" && types[ee] !== "comment" && types[ee] !== "comment-inline") {
semiTest = false;
}
}
if (semiTest === false) {
ee = pairTypes.length - 1;
if (pairTypes[ee] === "comment" || pairTypes[ee] === "comment-inline") {
do {
ee = ee - 1;
} while (
ee > 0 && (pairTypes[ee] === "comment" || pairTypes[ee] === "comment-inline")
);
}
ee = ee + 1;
if (structval === "map") {
pairToken.splice(ee, 0, ",");
} else {
pairToken.splice(ee, 0, ";");
}
pairTypes.splice(ee, 0, "semi");
pairDepth.splice(ee, 0, pairDepth[ee]);
pairBegin.splice(ee, 0, pairBegin[ee]);
if (pairLines[ee - 1] > 0) {
pairLines[ee - 1] = 0;
pairLines.splice(ee, 0, 1);
} else {
pairLines.splice(ee, 0, 0);
}
}
}
ee = pairTypes.length - 1;
if (pairTypes[ee] === "comment" || pairTypes[ee] === "comment-inline") {
do {
ee = ee - 1;
} while (
ee > 0 && (pairTypes[ee] === "comment" || pairTypes[ee] === "comment-inline")
);
}
keylen = token.length - (cc + 1);
token.splice(cc + 1, keylen);
types.splice(cc + 1, keylen);
lines.splice(cc + 1, keylen);
depth.splice(cc + 1, keylen);
begin.splice(cc + 1, keylen);
token = token.concat(pairToken);
types = types.concat(pairTypes);
lines = lines.concat(pairLines);
depth = depth.concat(pairDepth);
begin = begin.concat(pairBegin);
if (structval === "map") {
cc = token.length - 1;
if (types[cc].indexOf("comment") === 0) {
do {
cc = cc - 1;
} while (types[cc].indexOf("comment") === 0);
}
if (token[cc] === ",") {
token.splice(cc, 1);
types.splice(cc, 1);
lines.splice(cc, 1);
depth.splice(cc, 1);
begin.splice(cc, 1);
}
}
}
return;
}
}
},
//the generic token builder
buildtoken = function csspretty__tokenize_build() {
var aa = 0,
bb = 0,
out = [],
block = [],
outy = "",
mappy = 0,
comma = (
token.length > 0 && token[token.length - 1].charAt(token[token.length - 1].length - 1) === ","
),
linev = spacer(false),
spacestart = function () {
if ((/\s/).test(b[aa + 1]) === true) {
do {
aa = aa + 1;
} while ((/\s/).test(b[aa + 1]) === true);
}
};
//this loop accounts for grouping mechanisms
for (aa = a; aa < len; aa = aa + 1) {
out.push(b[aa]);
if (b[aa - 1] !== "\\" || esctest(aa) === false) {
if (b[aa] === "\"" && block[block.length - 1] !== "'") {
if (block[block.length - 1] === "\"") {
block.pop();
} else {
block.push("\"");
}
} else if (b[aa] === "'" && block[block.length - 1] !== "\"") {
if (block[block.length - 1] === "'") {
block.pop();
} else {
block.push("'");
}
} else if (block[block.length - 1] !== "\"" && block[block.length - 1] !== "'") {
if (b[aa] === "(") {
mappy = mappy + 1;
block.push(")");
spacestart();
} else if (b[aa] === "[") {
block.push("]");
spacestart();
} else if (b[aa] === "#" && b[aa + 1] === "{") {
out.push("{");
aa = aa + 1;
block.push("}");
spacestart();
} else if (b[aa] === block[block.length - 1]) {
block.pop();
if ((/\s/).test(out[out.length - 2]) === true) {
out.pop();
do {
out.pop();
} while ((/\s/).test(out[out.length - 1]) === true);
out.push(b[aa]);
}
}
}
}
if (structval === "map" && block.length === 0 && (b[aa + 1] === "," || b[aa + 1] === ")")) {
if (b[aa + 1] === ")" && token[token.length - 1] === "(") {
token.pop();
types.pop();
lines.pop();
depth.pop();
begin.pop();
struct.pop();
structval = depth[depth.length - 1];
out = ["("];
aa = a - 1;
} else {
break;
}
}
if (b[aa + 1] === ":") {
bb = aa;
if ((/\s/).test(b[bb]) === true) {
do {
bb = bb - 1;
} while ((/\s/).test(b[bb]) === true);
}
outy = b
.slice(bb - 6, bb + 1)
.join("");
if (outy.indexOf("filter") === outy.length - 6 || outy.indexOf("progid") === outy.length - 6) {
outy = "filter";
}
}
if (block.length === 0 && ((b[aa + 1] === ";" && esctest(aa + 1) === false) || (b[aa + 1] === ":" && b[aa] !== ":" && b[aa + 2] !== ":" && outy !== "filter" && outy !== "progid") || b[aa + 1] === "}" || b[aa + 1] === "{" || (b[aa + 1] === "/" && (b[aa + 2] === "*" || b[aa + 2] === "/")))) {
bb = out.length - 1;
if ((/\s/).test(out[bb]) === true) {
do {
bb = bb - 1;
aa = aa - 1;
out.pop();
} while ((/\s/).test(out[bb]) === true);
}
break;
}
if (out[0] === "@" && block.length === 0 && (b[aa + 1] === "\"" || b[aa + 1] === "'")) {
break;
}
}
a = aa;
itemsize = out.length;
if (structval === "map" && out[0] === "(") {
mapper[mapper.length - 1] = mapper[mapper.length - 1] - 1;
}
if (comma === true && structval !== "map" && types[types.length - 1] !== "comment" && types[types.length - 1] !== "comment-inline") {
token[token.length - 1] = token[token.length - 1] + out
.join("")
.replace(/\s+/g, " ")
.replace(/^\s/, "")
.replace(/\s$/, "");
return;
}
token.push(
out.join("").replace(/\s+/g, " ").replace(/^\s/, "").replace(/\s$/, "")
);
begin.push(struct[struct.length - 1]);
depth.push(structval);
lines.push(linev);
if (token[token.length - 1].indexOf("extend(") === 0) {
ltype = "pseudo";
types.push("pseudo");
} else if ("\"'".indexOf(token[token.length - 1].charAt(0)) > -1 && types[types.length - 1] === "propvar") {
ltype = "item";
types.push("value");
} else if (out[0] === "@" || out[0] === "$") {
if (types[types.length - 1] === "colon" && (types[types.length - 2] === "property" || types[types.length - 2] === "propvar")) {
ltype = "value";
types.push("value");
} else {
ltype = "propvar";
types.push("propvar");
outy = token[token.length - 1];
aa = outy.indexOf("(");
if (outy.charAt(outy.length - 1) === ")" && aa > 0) {
outy = outy.slice(aa + 1, outy.length - 1);
token[token.length - 1] = token[token.length - 1].slice(0, aa + 1) + value(
outy,
false
) + ")";
}
}
} else {
ltype = "item";
types.push("item");
}
},
// Some tokens receive a generic type named 'item' because their type is unknown
// until we know the following syntax. This function replaces the type 'item'
// with something more specific.
item = function csspretty__tokenize_item(type) {
var aa = types.length,
bb = 0,
coms = [],
tokel = (token.length > 1)
? token[token.length - 2]
: "",
toked = tokel.slice(tokel.length - 2);
//backtrack through immediately prior comments to find the correct token
if (ltype === "comment" || ltype === "comment-inline") {
do {
aa = aa - 1;
ltype = types[aa];
coms.push(token[aa]);
} while (aa > 0 && (ltype === "comment" || ltype === "comment-inline"));
} else {
aa = aa - 1;
}
//if the last non-comment type is 'item' then id it
if (ltype === "item" && types[aa].indexOf("external") < 0) {
if (type === "start") {
stats.selectors.count = stats.selectors.count + 1;
stats.selectors.chars = stats.selectors.chars + itemsize;
if (types[aa - 1] !== "comment" && types[aa - 1] !== "comment-inline" && types[aa - 1] !== "end" && types[aa - 1] !== "start" && types[aa - 1] !== "semi" && types[aa - 1] !== undefined && types[aa - 1].indexOf("external") < 0) {
(function csspretty__tokenize_item_selparts() {
var parts = [],
cc = aa,
dd = 0;
do {
parts.push(token[cc]);
if (lines[cc] > 0 && token[cc] === ":" && token[cc - 1] !== ":") {
parts.push(" ");
} else if (token[cc] !== ":") {
parts.push(" ");
}
cc = cc - 1;
} while (
cc > -1 && types[cc] !== "comment" && types[cc] !== "comment-inline" && types[cc] !== "end" && types[cc] !== "start" && types[cc] !== "semi" && types[cc] !== undefined
);
parts.reverse();
cc = cc + 1;
dd = aa - cc;
lines[aa] = lines[cc];
token.splice(cc, dd);
types.splice(cc, dd);
lines.splice(cc, dd);
depth.splice(cc, dd);
begin.splice(cc, dd);
aa = aa - dd;
token[aa] = parts
.join("")
.replace(/:\u0020/g, ":")
.replace(/(\s*,\s*)/g, ",");
}());
} else {
token[aa] = token[aa].replace(/(\s*,\s*)/g, ",");
}
if (options.compressedcss === true) {
token[aa] = token[aa]
.replace(/\s*&/, " &")
.replace(/\s*>\s*/g, ">")
.replace(/:\s+/g, ":")
.replace(/^(\s+)/, "")
.replace(/(\s+)$/, "");
} else {
token[aa] = token[aa]
.replace(/\s*&/, " &")
.replace(/\s*>\s*/g, " > ")
.replace(/:\s+/g, ": ")
.replace(/^(\s+)/, "")
.replace(/(\s+)$/, "");
}
(function csspretty__tokenize_item_selectorsort() {
var y = 0,
slen = token[aa].length,
z = "",
mark = 0,
list = [];
for (y = 0; y < slen; y = y + 1) {
if (z === "" && token[aa].charAt(y) === ",") {
list.push(token[aa].slice(mark, y));
mark = y + 1;
} else if (token[aa].charAt(y) === "\"" || token[aa].charAt(y) === "'" || token[aa].charAt(y) === "(" || token[aa].charAt(y) === "{") {
z = token[aa].charAt(y);
} else if (token[aa].charAt(y) === z && (z === "\"" || z === "''")) {
z = "";
} else if (token[aa].charAt(y) === ")" && z === "(") {
z = "";
} else if (token[aa].charAt(y) === "}" && z === "{") {
z = "";
}
}
list.push(token[aa].slice(mark, y));
list.sort();
token[aa] = list.join(",").replace(/^(\s+)/, "");
}());
types[aa] = "selector";
ltype = "selector";
} else if (type === "end") {
types[aa] = "value";
ltype = "value";
if (options.mode !== "diff") {
token[aa] = token[aa].replace(/\s*!\s+important/, " !important");
if (options.quoteConvert !== "none" && (token[aa - 2] === "font" || token[aa - 2] === "font-family")) {
token[aa] = value(token[aa], true);
} else {
token[aa] = value(token[aa], false);
}
}
//take comments out until the 'item' is found and then put the comments back
if (options.mode === "beautify" || options.mode === "parse" || (options.mode === "diff" && options.diffcomments === true)) {
if (token[token.length - 2] === "{") {
types[types.length - 1] = "propvar";
stats.values.count = stats.values.count - 1;
stats.values.chars = stats.values.chars - itemsize;
stats.variables.count = stats.variables.count + 1;
stats.variables.chars = stats.variables.chars + itemsize;
} else if (structval === "block") {
if (coms.length > 0 && ltype !== "semi" && ltype !== "end" && ltype !== "start") {
aa = coms.length - 1;
do {
token.pop();
types.pop();
lines.pop();
depth.pop();
begin.pop();
aa = aa - 1;
} while (aa > 0);
if (options.mode === "diff") {
token.push("x;");
} else {
token.push(";");
}
depth.push(structval);
begin.push(struct[struct.length - 1]);
types.push("semi");
lines.push(spacer(false));
bb = coms.length - 1;
do {
token.push(coms[aa]);
if (coms[aa].indexOf("//") === 0 && lines[lines.length - 1] === 0) {
types.push("comment-inline");
} else {
types.push("comment");
}
depth.push(structval);
begin.push(struct[struct.length - 1]);
lines.push(0);
aa = aa + 1;
} while (aa < bb);
} else {
if (options.mode === "diff") {
token.push("x;");
} else {
token.push(";");
}
depth.push(structval);
begin.push(struct[struct.length - 1]);
types.push("semi");
lines.push(spacer(false));
}
}
}
stats.values.count = stats.values.count + 1;
stats.values.chars = stats.values.chars + itemsize;
} else if (type === "semi") {
if (types[aa - 1] === "colon") {
stats.values.count = stats.values.count + 1;
stats.values.chars = stats.values.chars + itemsize;
types[aa] = "value";
ltype = "value";
if (options.mode !== "diff") {
token[aa] = token[aa].replace(/\s*!\s+important/, " !important");
if (options.qu